📄 multimediacontentimpl.java
字号:
video.getSrc() ) ); addVideo( slide, videoContent, video ); } slides.add( slide ); } } private void createFromZipFile( ZipFile zipFile ) throws IOException, MessagingException, MarshalException, ValidationException, ContentException, ZipException { ZipEntry smilFile = getSmilFileFromZip( zipFile ); if( smilFile == null ) { throw new SmilNotFoundException( "no-smil-zip" ); } Smil smil = Smil.unmarshal( new InputStreamReader( zipFile.getInputStream( smilFile ) ) ); createTemplate(smil); for( int i = 0; i < smil.getBody().getParCount(); i++ ) { Par par = smil.getBody().getPar( i ); Slide slide = SlideFactory.getInstance().newSlide(); slide.setDuration( par.getDur() ); com.openwave.mms.content.smil.Text text = par.getText(); if( text != null ) { DataHandler handler = getZipEntryDataHandler( zipFile, text.getSrc() ); Text textPart = new Text(); textPart.setDataHandler( handler ); textPart.setFileName( text.getSrc() ); addText( slide, textPart, text ); } com.openwave.mms.content.smil.Audio audio = par.getAudio(); if( audio != null ) { DataHandler handler = getZipEntryDataHandler( zipFile, audio.getSrc() ); Audio audioPart = new Audio(); audioPart.setDataHandler( handler ); audioPart.setFileName( audio.getSrc() ); addAudio( slide, audioPart, audio ); } com.openwave.mms.content.smil.Img image = par.getImg(); if( image != null ) { DataHandler handler = getZipEntryDataHandler( zipFile, image.getSrc() ); Image imagePart = new Image(); imagePart.setDataHandler( handler ); imagePart.setFileName( image.getSrc() ); addImage( slide, imagePart, image ); } com.openwave.mms.content.smil.Video video = par.getVideo(); if( video != null ) { DataHandler handler = getZipEntryDataHandler( zipFile, video.getSrc() ); Video videoPart = new Video(); videoPart.setDataHandler( handler ); videoPart.setFileName( video.getSrc() ); addVideo( slide, videoPart, video ); } slides.add( slide ); } } private String stripCid( String cid ) { if( cid.substring( 0, 4 ).equalsIgnoreCase( "cid:" ) ) { return cid.substring( 4 ); } else { return cid; } } private void addRegion( String regionName, Layout layout, RegionAttributes regionAttributes ) { Enumeration regions = layout.enumerateRegion(); while( regions.hasMoreElements() ) { Region region = ( Region ) regions.nextElement(); if( regionAttributes.equals( region ) ) { return; } } // existing region not found, create new one Region region = new Region(); region.setId( RegionTypeIdType.valueOf( regionName ) ); IntOrPercent top = regionAttributes.getTop(); if( top != null ) { region.setTop( top.toString() ); } IntOrPercent left = regionAttributes.getLeft(); if( left != null ) { region.setLeft( left.toString() ); } IntOrPercent width = regionAttributes.getWidth(); if( width != null ) { region.setWidth( width.toString() ); } IntOrPercent height = regionAttributes.getHeight(); if( height != null ) { region.setHeight( height.toString() ); } Fit fit = regionAttributes.getFit(); if( fit != null ) { region.setFit( fit.toString() ); } layout.addRegion( region ); } private ZipEntry getSmilFileFromZip( ZipFile zipFile ) { Enumeration files = zipFile.entries(); while( files.hasMoreElements() ) { ZipEntry file = ( ZipEntry ) files.nextElement(); if( file.getName().endsWith( ".smil" ) ) { return file; } } return null; } private DataHandler getZipEntryDataHandler( ZipFile zipFile, String entry ) { return new DataHandler( new ZipEntryDataSource( zipFile, zipFile.getEntry( entry ) ) ); } private static String generateContentID() { StringBuffer buffer = new StringBuffer(); Random random = new Random(); for( int i = 0; i < 16; i++ ) { int nextInt = random.nextInt( 16 ); buffer.append( Integer.toHexString( random.nextInt( 16 ) ) ); } return buffer.toString(); } private void createFromMultipart() throws ContentException, IOException { try { MimeMultipart multipart = ( MimeMultipart ) content; ContentType contentType = new ContentType( multipart.getContentType() ); String start = contentType.getParameter( "start" ); if( start != null && start.length() > 0 ) { BodyPart smilPart = multipart.getBodyPart( start ); if( smilPart == null || ! smilPart.getContentType() .startsWith( "application/smil" ) ) { smilPart = searchForSmilPart( multipart ); } if( smilPart != null ) { smilBased = true; changed = true; createSlidesFromSmil( smilPart, multipart ); } } else { // no start attribute in content type. just get all the parts and // look for smil BodyPart smilPart = searchForSmilPart( multipart ); if( smilPart != null ) { smilBased = true; changed = true; createSlidesFromSmil( smilPart, multipart ); } } } catch( ValidationException ve ) { throw new InvalidSmilException( "smil-validation-failed", ve ); } catch( MarshalException me ) { throw new InvalidSmilException( "smil-parsing-failed", me ); } catch( ParseException pe ) { throw new IllegalContentTypeException( "cannot-parse-content-type", pe ); } catch( MessagingException mme ) { throw new ContentException( "messaging-exception", mme ); } } /** * This function checks if the media object is already in the multipart * and adds it if it isn't. This check is necessary to make sure that we don't * add the same content twice when it is referenced by two slides. */ private static String addMediaToMultipart( MimeMultipart multipart, MediaObject media ) throws MessagingException { String contentId = getContentID( media ); BodyPart part = multipart.getBodyPart( "<" + contentId + ">" ); if( part != null ) { contentId = generateContentID(); media.setContentID( "<" + contentId + ">" ); } // check if this media object is forward locked if( media.getForwardLock() == true ) { multipart.addBodyPart( media.drmEncode() ); } else { multipart.addBodyPart( media ); } return contentId; } private static String getContentID( MediaObject media ) throws MessagingException { String contentID = media.getContentID(); if( contentID == null || contentID.length() == 0 ) { contentID = media.getFileName(); if( contentID == null || contentID.length() == 0 ) { contentID = generateContentID(); } media.setHeader( "Content-ID", "<" + contentID + ">" ); } else { // remove the leading and trailing angle brackets contentID = contentID.substring( 1, contentID.length() - 1 ); } return contentID; } private BodyPart getReferencedPart( String src, MimeMultipart multipart ) throws MessagingException { BodyPart part = null; if( src.startsWith( "cid:" ) ) { String contentId = stripCid( src ); try { part = multipart.getBodyPart( "<" + contentId + ">" ); } catch( MessagingException me ) { //ignore, this is most likely due to bad smil //or content being stripped because of drm //controls on the MMSC. } } else { int count = multipart.getCount(); for( int i = 0; ( i < count ) && ( part == null ); i++ ) { MimeBodyPart aPart = ( MimeBodyPart ) multipart.getBodyPart( i ); String contentLocation = aPart.getHeader( "Content-Location", null ); if( ( contentLocation != null ) && ( contentLocation.equals( src ) ) ) { part = aPart; } } } if( part == null ) { if( logger.isEnabledFor( Level.WARN ) ) { logger.warn( "media object [" + src + "] not found" ); } } return part; } private void setViewportSizeFromSmil( RootLayout rootLayout ) { int height = 0; int width = 0; String rootWidth = rootLayout.getWidth(); if( rootWidth != null ) { try { width = Integer.parseInt( rootWidth ); } catch( NumberFormatException nfe ) { //ignore } } String rootHeight = rootLayout.getHeight(); if( rootHeight != null ) { try { height = Integer.parseInt( rootHeight ); } catch( NumberFormatException nfe ) { //ignore } } viewportSize.setSize( width, height ); } private void createTemplate(Smil smil) { // set the template RegionAttributes textAttributes = null; RegionAttributes imageAttributes = null; Layout layout = smil.getHead().getLayout(); setViewportSizeFromSmil( layout.getRootLayout() ); Enumeration regions = layout.enumerateRegion(); while( regions.hasMoreElements() ) { Region region = ( Region ) regions.nextElement(); if( region.getId() == RegionTypeIdType.TEXT ) { textAttributes = new RegionAttributes( region ); } else if( region.getId() == RegionTypeIdType.IMAGE ) { imageAttributes = new RegionAttributes( region ); } } template = TemplateFactory.getInstance().newTemplate( null, textAttributes, imageAttributes ); } private void addText( Slide slide, Text textContent, com.openwave.mms.content.smil.Text text ) { textContent.setBegin( text.getBegin() ); textContent.setEnd( text.getEnd() ); textContent.setAlt( text.getAlt() ); slide.setText( textContent ); } private void addAudio( Slide slide, Audio audioContent, com.openwave.mms.content.smil.Audio audio ) throws ContentException { audioContent.setBegin( audio.getBegin() ); audioContent.setEnd( audio.getEnd() ); audioContent.setAlt( audio.getAlt() ); try { slide.setAudio( audioContent ); } catch( IllegalStateException e ) { throw new ContentException( "illegal-mms-smil", e ); } } private void addImage( Slide slide, Image imageContent, com.openwave.mms.content.smil.Img image ) throws ContentException { imageContent.setBegin( image.getBegin() ); imageContent.setEnd( image.getEnd() ); imageContent.setAlt( image.getAlt() ); try { slide.setImage( imageContent ); } catch( IllegalStateException e ) { throw new ContentException( "illegal-mms-smil", e ); } } private void addVideo( Slide slide, Video videoContent, com.openwave.mms.content.smil.Video video ) throws ContentException { videoContent.setBegin( video.getBegin() ); videoContent.setEnd( video.getEnd() ); videoContent.setAlt( video.getAlt() ); try { slide.setVideo( videoContent ); } catch( IllegalStateException e ) { throw new ContentException( "illegal-mms-smil", e ); } } private List slides; private Object content; private boolean changed; private Template template; private Dimension viewportSize; private boolean smilBased; private static final Logger logger = Logger.getLogger( MultimediaContentImpl.class );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -