KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > definition > XmlParser


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 package org.webharvest.definition;
38
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41
42 import javax.xml.parsers.*;
43
44 import org.apache.log4j.Logger;
45 import org.webharvest.utils.Stack;
46 import org.xml.sax.Attributes JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.helpers.DefaultHandler JavaDoc;
49
50
51 public class XmlParser extends DefaultHandler JavaDoc {
52
53     protected static Logger log = Logger.getLogger(XmlParser.class);
54
55     XmlNode root;
56
57     // working stack of elements
58
private transient Stack elementStack = new Stack();
59
60     public static XmlNode parse(InputStream JavaDoc in) {
61         long startTime = System.currentTimeMillis();
62
63         XmlParser handler = new XmlParser();
64         try {
65             SAXParserFactory parserFactory = SAXParserFactory.newInstance();
66             parserFactory.setValidating(false);
67             parserFactory.setNamespaceAware(false);
68
69             SAXParser parser = parserFactory.newSAXParser();
70
71             // call parsing
72
parser.parse(in, handler);
73
74             log.info("XML parsed in " + (System.currentTimeMillis() - startTime) + "ms.");
75         } catch (SAXException JavaDoc e) {
76             // handle exception
77
e.printStackTrace();
78         } catch (IOException JavaDoc e) {
79             // handle exception
80
e.printStackTrace();
81         } catch (ParserConfigurationException e) {
82             // handle exception
83
e.printStackTrace();
84         }
85
86         return handler.root;
87     }
88
89     private XmlNode getCurrentNode() {
90         return elementStack.size() > 0 ? (XmlNode) elementStack.peek() : null;
91     }
92
93     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
94         XmlNode currNode = getCurrentNode();
95         if (currNode != null) {
96             String JavaDoc value = new String JavaDoc(ch, start, length).trim();
97             String JavaDoc text = currNode.getText();
98             if (text == null) {
99                 text = value;
100             } else {
101                 text += " " + value;
102             }
103             currNode.setText(text);
104             if (!"".equals(value)) {
105                 currNode.addElement(value);
106             }
107         }
108     }
109     
110     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
111         XmlNode currNode = getCurrentNode();
112         XmlNode newNode = new XmlNode(qName, currNode);
113         elementStack.push(newNode);
114
115         if (currNode == null) {
116             root = newNode;
117         }
118
119         int attsCount = attributes.getLength();
120         for (int i = 0; i < attsCount; i++) {
121             newNode.addAttribute( attributes.getQName(i), attributes.getValue(i) );
122         }
123     }
124
125     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
126         if (elementStack.size() > 0) {
127             elementStack.pop();
128         }
129     }
130
131 }
Popular Tags