Home | History | Annotate | Download | only in parser
      1 /*
      2  * Copyright (C) 2007 Esmertec AG.
      3  * Copyright (C) 2007 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mms.dom.smil.parser;
     19 
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 
     23 import org.w3c.dom.smil.SMILDocument;
     24 import org.xml.sax.InputSource;
     25 import org.xml.sax.SAXException;
     26 import org.xml.sax.XMLReader;
     27 import org.xml.sax.helpers.XMLReaderFactory;
     28 
     29 import com.google.android.mms.MmsException;
     30 
     31 public class SmilXmlParser {
     32     private XMLReader mXmlReader;
     33     private SmilContentHandler mContentHandler;
     34 
     35     public SmilXmlParser() throws MmsException {
     36         //FIXME: Now we don't have the SAXParser wrapped inside,
     37         //       use the Driver class temporarily.
     38         System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
     39 
     40         try {
     41             mXmlReader = XMLReaderFactory.createXMLReader();
     42             mContentHandler = new SmilContentHandler();
     43             mXmlReader.setContentHandler(mContentHandler);
     44         } catch (SAXException e) {
     45             throw new MmsException(e);
     46         }
     47     }
     48 
     49     public SMILDocument parse(InputStream in) throws IOException, SAXException {
     50         mContentHandler.reset();
     51 
     52         mXmlReader.parse(new InputSource(in));
     53 
     54         SMILDocument doc = mContentHandler.getSmilDocument();
     55         validateDocument(doc);
     56 
     57         return doc;
     58     }
     59 
     60     private void validateDocument(SMILDocument doc) {
     61         /*
     62          * Calling getBody() will create "smil", "head", and "body" elements if they
     63          * are not present. It will also initialize the SequentialTimeElementContainer
     64          * member of SMILDocument, which could not be set on creation of the document.
     65          * @see com.android.mms.dom.smil.SmilDocumentImpl#getBody()
     66          */
     67         doc.getBody();
     68 
     69         /*
     70          * Calling getLayout() will create "layout" element if it is not present.
     71          * @see com.android.mms.dom.smil.SmilDocumentImpl#getLayout()
     72          */
     73         doc.getLayout();
     74     }
     75 }
     76