1/*
2 * Copyright (C) 2007-2008 Esmertec AG.
3 * Copyright (C) 2007-2008 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
18package com.android.mms.dom.smil.parser;
19
20import org.w3c.dom.Element;
21import org.w3c.dom.Node;
22import org.w3c.dom.smil.SMILDocument;
23import org.xml.sax.Attributes;
24import org.xml.sax.helpers.DefaultHandler;
25
26import android.util.Log;
27
28import com.android.mms.LogTag;
29import com.android.mms.dom.smil.SmilDocumentImpl;
30
31public class SmilContentHandler extends DefaultHandler {
32    private static final String TAG = LogTag.TAG;
33    private static final boolean DEBUG = false;
34    private static final boolean LOCAL_LOGV = false;
35
36    private SMILDocument mSmilDocument;
37    private Node mCurrentNode;
38
39    /**
40     * Resets this handler.
41     *
42     */
43    public void reset() {
44        mSmilDocument = new SmilDocumentImpl();
45        mCurrentNode = mSmilDocument;
46    }
47
48    /**
49     * Returns the SMILDocument.
50     * @return The SMILDocument instance
51     */
52    public SMILDocument getSmilDocument() {
53        return mSmilDocument;
54    }
55
56    @Override
57    public void startElement(String uri, String localName, String qName, Attributes attributes) {
58        if (LOCAL_LOGV) {
59            Log.v(TAG, "SmilContentHandler.startElement. Creating element " + localName);
60        }
61        Element element = mSmilDocument.createElement(localName);
62        if (attributes != null) {
63            for (int i = 0; i < attributes.getLength(); i++) {
64                if (LOCAL_LOGV) {
65                    Log.v(TAG, "Attribute " + i +
66                        " lname = " + attributes.getLocalName(i) +
67                        " value = " + attributes.getValue(i));
68                }
69                element.setAttribute(attributes.getLocalName(i),
70                        attributes.getValue(i));
71            }
72        }
73        if (LOCAL_LOGV) {
74            Log.v(TAG, "Appending " + localName + " to " + mCurrentNode.getNodeName());
75        }
76        mCurrentNode.appendChild(element);
77
78        mCurrentNode = element;
79    }
80
81    @Override
82    public void endElement(String uri, String localName, String qName) {
83        if (LOCAL_LOGV) {
84            Log.v(TAG, "SmilContentHandler.endElement. localName " + localName);
85        }
86        mCurrentNode = mCurrentNode.getParentNode();
87    }
88
89    @Override
90    public void characters(char[] ch, int start, int length) {
91        if (LOCAL_LOGV) {
92            Log.v(TAG, "SmilContentHandler.characters. ch = " + new String(ch, start, length));
93        }
94    }
95}
96