• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.hotspot2.omadm;
2 
3 import org.xml.sax.Attributes;
4 import org.xml.sax.InputSource;
5 import org.xml.sax.SAXException;
6 import org.xml.sax.helpers.DefaultHandler;
7 
8 import java.io.IOException;
9 import java.io.StringReader;
10 
11 import javax.xml.parsers.ParserConfigurationException;
12 import javax.xml.parsers.SAXParser;
13 import javax.xml.parsers.SAXParserFactory;
14 
15 /**
16  * Parses an OMA-DM XML tree.
17  */
18 public class OMAParser extends DefaultHandler {
19     private XMLNode mRoot;
20     private XMLNode mCurrent;
21 
OMAParser()22     public OMAParser() {
23         mRoot = null;
24         mCurrent = null;
25     }
26 
parse(String text, String urn)27     public MOTree parse(String text, String urn) throws IOException, SAXException {
28         try {
29             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
30             parser.parse(new InputSource(new StringReader(text)), this);
31             return new MOTree(mRoot, urn);
32         } catch (ParserConfigurationException pce) {
33             throw new SAXException(pce);
34         }
35     }
36 
37     @Override
startElement(String uri, String localName, String qName, Attributes attributes)38     public void startElement(String uri, String localName, String qName, Attributes attributes)
39             throws SAXException {
40         XMLNode parent = mCurrent;
41 
42         mCurrent = new XMLNode(mCurrent, qName, attributes);
43 
44         if (mRoot == null)
45             mRoot = mCurrent;
46         else
47             parent.addChild(mCurrent);
48     }
49 
50     @Override
endElement(String uri, String localName, String qName)51     public void endElement(String uri, String localName, String qName) throws SAXException {
52         if (!qName.equals(mCurrent.getTag()))
53             throw new SAXException("End tag '" + qName + "' doesn't match current node: " +
54                     mCurrent);
55 
56         try {
57             mCurrent.close();
58         } catch (IOException ioe) {
59             throw new SAXException("Failed to close element", ioe);
60         }
61 
62         mCurrent = mCurrent.getParent();
63     }
64 
65     @Override
characters(char[] ch, int start, int length)66     public void characters(char[] ch, int start, int length) throws SAXException {
67         mCurrent.addText(ch, start, length);
68     }
69 }
70