KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > builder > SAXOMBuilder


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.apache.axis2.om.impl.llom.builder;
18
19 import org.apache.axis2.om.*;
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.Locator JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.DefaultHandler JavaDoc;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 public class SAXOMBuilder extends DefaultHandler JavaDoc {
29     OMElement root = null;
30
31     OMNode lastNode = null;
32
33     OMElement nextElem = null;
34
35     OMFactory factory = OMAbstractFactory.getOMFactory();
36
37     List JavaDoc prefixMappings = new ArrayList JavaDoc();
38
39     public void setDocumentLocator(Locator JavaDoc arg0) {
40     }
41
42     public void startDocument() throws SAXException JavaDoc {
43
44     }
45
46     public void endDocument() throws SAXException JavaDoc {
47     }
48
49     protected OMElement createNextElement(String JavaDoc localName) throws OMException {
50         OMElement e;
51         if (lastNode == null) {
52             root = e = factory.createOMElement(localName, null, null, null);
53         } else if (lastNode.isComplete()) {
54             e = factory.createOMElement(localName, null, lastNode.getParent(),
55                     null);
56             lastNode.setNextSibling(e);
57             e.setPreviousSibling(lastNode);
58         } else {
59             OMElement parent = (OMElement) lastNode;
60             e = factory.createOMElement(localName, null, (OMElement) lastNode,
61                     null);
62             parent.setFirstChild(e);
63         }
64         return e;
65     }
66
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
71      * java.lang.String)
72      */

73     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
74             throws SAXException JavaDoc {
75         if (nextElem == null)
76             nextElem = createNextElement(null);
77         nextElem.declareNamespace(uri, prefix);
78     }
79
80     public void endPrefixMapping(String JavaDoc arg0) throws SAXException JavaDoc {
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
87      * java.lang.String, java.lang.String, org.xml.sax.Attributes)
88      */

89     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
90             String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
91         if (localName == null || localName.trim().equals(""))
92             localName = qName.substring(qName.indexOf(':') + 1);
93         if (nextElem == null)
94             nextElem = createNextElement(localName);
95         else
96             nextElem.setLocalName(localName);
97         nextElem
98                 .setNamespace(nextElem.findNamespace(namespaceURI, null));
99         int j = atts.getLength();
100         for (int i = 0; i < j; i++)
101             nextElem.addAttribute(atts.getLocalName(i), atts.getValue(i),
102                     nextElem.findNamespace(atts.getURI(i), null));
103         lastNode = nextElem;
104         nextElem = null;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
111      * java.lang.String, java.lang.String)
112      */

113     public void endElement(String JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2)
114             throws SAXException JavaDoc {
115         if (lastNode.isComplete()) {
116             OMContainer parent = lastNode.getParent();
117             parent.setComplete(true);
118             lastNode = (OMNode)parent;
119         } else {
120             OMElement e = (OMElement) lastNode;
121             e.setComplete(true);
122         }
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
129      */

130     public void characters(char[] ch, int start, int length)
131             throws SAXException JavaDoc {
132         if (lastNode == null) {
133             throw new SAXException JavaDoc("");
134         }
135         OMNode node;
136         if (lastNode.isComplete()) {
137             node = factory.createText((OMElement)lastNode.getParent(), new String JavaDoc(ch,
138                     start, length));
139             lastNode.setNextSibling(node);
140             node.setPreviousSibling(lastNode);
141         } else {
142             OMElement e = (OMElement) lastNode;
143             node = factory.createText(e, new String JavaDoc(ch, start, length));
144             e.setFirstChild(node);
145         }
146         lastNode = node;
147     }
148
149     public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
150             throws SAXException JavaDoc {
151     }
152
153     public void processingInstruction(String JavaDoc arg0, String JavaDoc arg1)
154             throws SAXException JavaDoc {
155     }
156
157     public void skippedEntity(String JavaDoc arg0) throws SAXException JavaDoc {
158     }
159
160     /**
161      * @return Returns the root.
162      */

163     public OMElement getRootElement() {
164         return root;
165     }
166 }
167
Popular Tags