KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > NonVerifyingHandler


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22
23 package nu.xom;
24
25 /**
26  * @author Elliotte Rusty Harold
27  * @version 1.0
28  *
29  */

30 class NonVerifyingHandler extends XOMHandler {
31
32     NonVerifyingHandler(NodeFactory factory) {
33         super(factory);
34     }
35   
36     
37     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
38       String JavaDoc qualifiedName, org.xml.sax.Attributes JavaDoc attributes) {
39         
40         flushText();
41         Element element = Element.build(qualifiedName, namespaceURI);
42         if (parent == document) { // root
43
document.setRootElement(element);
44             inProlog = false;
45         }
46         
47         current = element;
48         // Need to push this, even if it's null
49
parents.push(element);
50         
51         if (parent != document) {
52             // a.k.a. parent not instanceof Document
53
parent.fastInsertChild(element, parent.getChildCount());
54         }
55         // This is optimized for the very common case where
56
// everything in the document has the same actual base URI.
57
// It may add redundant base URIs in cases like XInclude
58
// where different parts of the document have different
59
// base URIs.
60
if (locator != null) {
61              String JavaDoc baseURI = locator.getSystemId();
62              if (baseURI != null && !baseURI.equals(documentBaseURI)) {
63                  element.setActualBaseURI(baseURI);
64              }
65         }
66         
67         // Attach the attributes; this must be done before the
68
// namespaces are attached.
69
int length = attributes.getLength();
70         
71         // We've got a pretty good guess at how many attributes there
72
// will be here; we could ensureCapacity up to that length.
73
// However, that might waste memory because we wouldn't use
74
// the ones for namespace declarations. We could always
75
// trimToSize when we're done, but it's probably not worth
76
// the effort.
77
for (int i = 0; i < length; i++) {
78             String JavaDoc qName = attributes.getQName(i);
79             if (qName.startsWith("xmlns:") || qName.equals("xmlns")) {
80                 continue;
81             }
82             else {
83                 String JavaDoc namespace = attributes.getURI(i);
84                 String JavaDoc value = attributes.getValue(i);
85                 Attribute attribute = Attribute.build(
86                   qName,
87                   namespace,
88                   value,
89                   convertStringToType(attributes.getType(i))
90                 );
91                 element.fastAddAttribute(attribute);
92             }
93         }
94
95         // Attach the namespaces
96
for (int i = 0; i < length; i++) {
97             String JavaDoc qName = attributes.getQName(i);
98             if (qName.startsWith("xmlns:")) {
99                 String JavaDoc namespaceName = attributes.getValue(i);
100                 String JavaDoc namespacePrefix = qName.substring(6);
101                 String JavaDoc currentValue
102                    = element.getNamespaceURI(namespacePrefix);
103                 if (!namespaceName.equals(currentValue)) {
104                     element.addNamespaceDeclaration(
105                       namespacePrefix, namespaceName);
106                 }
107             }
108             else if (qName.equals("xmlns")) {
109                 String JavaDoc namespaceName = attributes.getValue(i);
110                 String JavaDoc namespacePrefix = "";
111                 String JavaDoc currentValue
112                   = element.getNamespaceURI(namespacePrefix);
113                 if (!namespaceName.equals(currentValue)) {
114                     element.addNamespaceDeclaration(namespacePrefix,
115                      namespaceName);
116                 }
117             }
118         }
119         
120         // this is the new parent
121
parent = element;
122     }
123     
124     
125     public void endElement(
126       String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qualifiedName) {
127         
128         // If we're immediately inside a skipped element
129
// we need to reset current to null, not to the parent
130
current = (ParentNode) parents.pop();
131         flushText();
132         
133         parent = current.getParent();
134         
135         if (parent.isDocument()) { // root element
136
Document doc = (Document) parent;
137             boolean beforeRoot = true;
138             doc.setRootElement((Element) current);
139             beforeRoot = false;
140         }
141         
142     }
143  
144     
145     // accumulate all text that's in the buffer into a text node
146
protected void flushText() {
147         
148         if (buffer.length() > 0) {
149             Text result;
150             if (!inCDATA) {
151                 result = Text.build(buffer.toString());
152             }
153             else {
154                 result = CDATASection.build(buffer.toString());
155             }
156             parent.fastInsertChild(result, parent.getChildCount());
157             buffer = new StringBuffer JavaDoc();
158         }
159         inCDATA = false;
160         finishedCDATA = false;
161         
162     }
163   
164     
165     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
166         
167         if (!inDTD) flushText();
168         if (inExternalSubset) return;
169         ProcessingInstruction result = ProcessingInstruction.build(target, data);
170         
171         if (!inDTD) {
172             if (inProlog) {
173                 parent.fastInsertChild(result, position);
174                 position++;
175             }
176             else {
177                 parent.fastInsertChild(result, parent.getChildCount());
178             }
179         }
180         else {
181             internalDTDSubset.append(" ");
182             internalDTDSubset.append(result.toXML());
183             internalDTDSubset.append("\n");
184         }
185
186     }
187     
188     
189     // LexicalHandler events
190
public void startDTD(String JavaDoc rootName, String JavaDoc publicID,
191       String JavaDoc systemID) {
192         
193         inDTD = true;
194         DocType doctype = DocType.build(rootName, publicID, systemID);
195         document.fastInsertChild(doctype, position);
196         position++;
197         internalDTDSubset = new StringBuffer JavaDoc();
198         this.doctype = doctype;
199         
200     }
201     
202     
203     public void comment(char[] text, int start, int length) {
204         
205         if (!inDTD) flushText();
206         if (inExternalSubset) return;
207
208         Comment result = Comment.build(new String JavaDoc(text, start, length));
209         ;
210         if (!inDTD) {
211             if (inProlog) {
212                 parent.insertChild(result, position);
213                 position++;
214             }
215             else {
216                 parent.fastInsertChild(result, parent.getChildCount());
217             }
218         }
219         else {
220             internalDTDSubset.append(" ");
221             internalDTDSubset.append(result.toXML());
222             internalDTDSubset.append("\n");
223         }
224
225     }
226
227 }
Popular Tags