📄 multimediacontentimpl.java
字号:
/** * Copyright (c) 2002-2003 Openwave Systems Inc. All rights reserved. * * The copyright to the computer software herein is the property of * Openwave Systems Inc. The software may be used and/or copied only * with the written permission of Openwave Systems Inc. or in accordance * with the terms and conditions stipulated in the agreement/contract * under which the software has been supplied. * * $Id: //mms/mms30/jcommon/content/src/com/openwave/mms/content/MultimediaContentImpl.java#2 $ */package com.openwave.mms.content;import java.awt.Dimension;import java.io.File;import java.io.FileNotFoundException;import java.io.FilenameFilter;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.StringWriter;import javax.mail.BodyPart;import javax.mail.Header;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.internet.ContentType;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.internet.MimeUtility;import javax.mail.internet.ParseException;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import java.util.Enumeration;import java.util.List;import java.util.ListIterator;import java.util.LinkedList;import java.util.Properties;import java.util.Random;import java.util.zip.ZipFile;import java.util.zip.ZipEntry;import java.util.zip.ZipException;import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import com.openwave.mms.content.smil.Body;import com.openwave.mms.content.smil.Head;import com.openwave.mms.content.smil.Img;import com.openwave.mms.content.smil.Layout;import com.openwave.mms.content.smil.Par;import com.openwave.mms.content.smil.Smil;import com.openwave.mms.content.smil.Region;import com.openwave.mms.content.smil.RootLayout;import com.openwave.mms.content.smil.types.RegionTypeIdType;/** * This package is a private class that implements the MultimediaContent interface. Each * object of this type consists of a list of Slide objects that corresponds * to a "par" element in SMIL. Each Slide object in turn contains the actual * multimedia objects like Video, Image, Audio and Text. A MultimediaContent * object also contains a Template object which specifies the coordinates * of regions on screen where the different multimedia objects (only Text, * Video and Image, in this case) are to be rendered. */final class MultimediaContentImpl implements MultimediaContent { /** * The default constructor that can be used to construct a MultimediaContentImpl * object from scratch. */ public MultimediaContentImpl() { changed = true; slides = new LinkedList(); viewportSize = new Dimension(); smilBased = true; } /** * This constructor tries to construct a MultimediaContentImpl object by reading * the input stream provided. It looks for the MIME part that contains the SMIL * description of the content and constructs the slides accordingly. If a SMIL * part is not found, it keeps the content as a multipart and does not * create any slides. Applications should use <code>isSmilBased</code> to check * whether the content is SMIL-based and call <code>getContent</code> if it returns * false. If <code>isSmilBased</code> returns true, you can use the methods * that allow you to access the slides. * * @param inputStream The input stream from which to read the multimedia content. * @exception IOException The input stream cannot be read. * @exception ContentException The stream does not contain a SMIL part * or there is a MessagingException while dealing with the MIME body parts. * @exception InvalidSmilException A subclass of ContentException is thrown if the * SMIL cannot be parsed. */ public MultimediaContentImpl( InputStream inputStream ) throws ContentException, IOException { viewportSize = new Dimension(); slides = new LinkedList(); smilBased = false; try { MimeMessage message = new MimeMessage( Session.getDefaultInstance( new Properties() ), inputStream ); content = message.getContent(); if( content instanceof MimeMultipart ) { createFromMultipart(); } else { content = new MimeBodyPart(); Enumeration headers = message.getAllHeaders(); while( headers.hasMoreElements() ) { Header header = ( Header ) headers.nextElement(); ( ( MimeBodyPart ) content ).addHeader( header.getName(), header.getValue() ); } ( ( MimeBodyPart ) content ).setContent( message.getContent(), message.getContentType() ); } } catch( MessagingException mme ) { throw new ContentException( "messaging-exception", mme ); } } /** * This constructor tries to construct a MultimediaContentImpl object from * the MimeMultipart provided. It looks for the MIME part that contains the SMIL * description of the content and constructs the slides accordingly. If a SMIL * part is not found, it keeps the content as a multipart and does not * create any slides. Applications should use <code>isSmilBased</code> to check * whether the content is SMIL-based and call <code>getContent</code> if it returns * false. If <code>isSmilBased</code> returns true then you can use the methods * that allow you to access the slides. * * @param multipart The object from which to construct the multimedia content. * @exception IOException The multipart cannot be read. * @exception ContentException The multipart does not contain a SMIL part * or there is a MessagingException dealing with the MIME body parts. * @exception IllegalContentTypeException A subclass of ContentException is thrown * if the content-type header in the MIME input stream is not multipart/ * related or if it cannot be parsed. * @exception InvalidSmilException A subclass of ContentException is thrown if the * SMIL cannot be parsed. */ public MultimediaContentImpl( MimeMultipart multipart ) throws ContentException, IOException { viewportSize = new Dimension(); slides = new LinkedList(); content = multipart; smilBased = false; createFromMultipart(); } /** * This constructor takes a java.io.File as input. The file could be a directory, a SMIL * file, or a zip file. If it is a directory, it lists the files in the * directory and picks the first file with an extension of .smil and contructs * the MultimediaContentImpl object from it. If it is a SMIL file, it constructs * the MultimediaContentImpl object as described by the SMIL file. If it is a zip * file, it treats it as a directory and constructs the MultimediaContentImpl * object accordingly. * * @param file The file as a File object. * @exception FileNotFoundException The file, or any other media file * referenced by the SMIL file, is not found. * @exception IOException There is an error reading any of the files. * @exception ContentException The directory/zip file does not contain * a SMIL file, there is a MessagingException dealing with the MIME * body parts, or the file is neither a directory nor a zip file nor * a SMIL file. * @exception InvalidSmilException A subclass of ContentException is thrown if the * SMIL cannot be parsed. * */ public MultimediaContentImpl( File file ) throws ContentException, IOException { viewportSize = new Dimension(); changed = true; slides = new LinkedList(); smilBased = true; try { if( file.isDirectory() ) { String[] smilFiles = file.list( new FilenameFilter() { public boolean accept( File dir, String name ) { return name.endsWith( ".smil" ); } } ); if( smilFiles == null || smilFiles.length == 0 || smilFiles[0] == null || smilFiles[0].length() == 0 ) { throw new SmilNotFoundException( "no-smil-directory" ); } createFromSmilFile( new File( file, smilFiles[0] ) ); } else if( file.getName().endsWith( ".zip" ) ) { ZipFile zipFile = new ZipFile( file ); createFromZipFile( zipFile ); } else { if( ! file.getName().endsWith( ".smil" ) ) { throw new ContentException( "file-must-be-directory-zip-smil" ); } createFromSmilFile( file ); } } catch( ValidationException ve ) { throw new InvalidSmilException( "smil-validation-failed", ve ); } catch( MarshalException me ) { throw new InvalidSmilException( "smil-parsing-failed", me ); } catch( MessagingException mme ) { throw new ContentException( "messaging-exception", mme ); } } /** * This method is used to check if the content contained in the object * has a SMIL description associated with it. When the object is constructed * using the constructors which take the input stream or a multipart, * this method tells you whether the content has a SMIL part. * * @return True if the content is smil based, false otherwise. */ public boolean isSmilBased() { return smilBased; } /** * Add this slide to the list of slides in the object. * * @param slide The slide to be added. */ public void addSlide( Slide slide ) { changed = true; slides.add( slide ); } /** * Add this slide to the list of slides in the object. * * @param slide The slide to be added. * @param index The index at which to add the slide. */ public void addSlide( Slide slide, int index ) { changed = true; slides.add( index, slide ); } /** * Remove a slide given its position in the list. * * @param slideNumber The position of the slide in the list. * @exception IndexOutOfBoundsException There are not that many slides * in this object. */ public void removeSlide( int slideNumber ) throws IndexOutOfBoundsException { slides.remove( slideNumber ); changed = true; } /** * Remove a slide. * * @param slide The slide to be removed. */ public void removeSlide( Slide slide ) { slides.remove( slide ); changed = true; } /** * Creates a new Slide object, adds it to the list of slides in the object, * and returns the slide. */ public Slide newSlide() { changed = true; Slide slide = SlideFactory.getInstance().newSlide(); slides.add( slide ); return slide; } /** * Creates a new Slide object, adds it to the list of slides in the object, * and returns the slide. * * @param index The index at which to add the slide. */ public Slide newSlide( int index ) { changed = true; Slide slide = SlideFactory.getInstance().newSlide(); slides.add( index, slide ); return slide; } /** * This method is used to clear the list of slides contained in the object. */ public void clear() { changed = true; slides.clear(); } /** * This method can be used to write the contents of the object to an output * stream. It generates the SMIL corresponding to its contents, creates a * MIME multipart message with the SMIL and the media objects, and writes it * to the output stream. * * @param outputStream The output stream to write to. * @exception IOException There is an error writing output to the stream. * @exception ContentException There is a MessagingException dealing with the MIME * body parts. * @exception InvalidSmilException A subclass of ContentException is thrown if the * SMIL cannot be synthesized. */ public void writeTo( OutputStream outputStream ) throws ContentException, IOException { try { if( smilBased && changed ) { createMultipart(); } if( smilBased ) { MimeMultipart multipart = ( MimeMultipart ) content; String contentType = multipart.getContentType() + ";\r\n\tstart=\"<mms.smil>\"" + ";\r\n\ttype=\"application/smil\""; MimeMessage message = new MimeMessage( Session.getDefaultInstance( new Properties() ) ); message.setContent( multipart, contentType ); message.writeTo( outputStream ); } else if( content instanceof MimeBodyPart ) { ( ( MimeBodyPart ) content ).writeTo( outputStream ); } else if( content instanceof MimeMultipart ) { MimeMessage message = new MimeMessage( Session.getDefaultInstance( new Properties() ) ); message.setContent( ( ( MimeMultipart ) content ) ); message.writeTo( outputStream ); } } catch( ValidationException ve ) { throw new InvalidSmilException( "smil-validation-failed", ve ); } catch( MarshalException me ) { throw new InvalidSmilException( "smil-creation-failed", me ); } catch( MessagingException mme ) { throw new ContentException( "messaging-exception", mme ); } } /** * This method returns the content of the object as a MimeBodyPart or * MimeMultipart. If the object was created from a MimeBodyPart or a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -