CVS Update: xmlpull-api-v1/addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder
Aleksander Andrzej Slominski <[email protected]> Tue, 8 Jul 2003 15:55:19 -0500 (EST)
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
aslom 03/07/08 15:55:19
Modified: addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder
DOM2XmlPullBuilder.java
Log:
improved DOM2 builder addon to allow building DOM tree from only pars of XML pull even stream and still have all namespaces declared
Revision Changes Path
1.3 +147 -57 xmlpull-api-v1/addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder/DOM2XmlPullBuilder.java
Index: DOM2XmlPullBuilder.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/dom2_builder/src/org/xmlpull/v1/dom2_builder/DOM2XmlPullBuilder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -t -w -r1.2 -r1.3
--- DOM2XmlPullBuilder.java 3 May 2003 06:22:47 -0000 1.2
+++ DOM2XmlPullBuilder.java 8 Jul 2003 20:55:19 -0000 1.3
@@ -11,7 +11,6 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
-//import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
@@ -21,6 +20,7 @@
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
+import org.w3c.dom.DOMException;
/**
* <strong>Simplistic</strong> DOM2 builder that should be enough to do support most cases.
@@ -42,7 +42,7 @@
// create document
- Document doc1 = builder.parse(reader);
+ Element el1 = builder.parse(reader);
//System.out.println("doc="+doc);
@@ -68,11 +68,11 @@
// reparse
- Document doc2 = builder.parse(reader);
+ Element el2 = builder.parse(reader);
// check that what was written is OK
- Element root = doc2.getDocumentElement();
+ Element root = el2; //doc2.getDocumentElement();
//System.out.println("root="+root);
System.out.println ("root ns=" + root.getNamespaceURI() + ", localName=" +root.getLocalName());
assertEquals("uri1", root.getNamespaceURI());
@@ -111,55 +111,100 @@
}
}
- protected XmlPullParser pp;
- protected Document doc;
+ //protected XmlPullParser pp;
+ protected XmlPullParserFactory factory;
public DOM2XmlPullBuilder() throws XmlPullParserException {
- pp = XmlPullParserFactory.newInstance().newPullParser();
- pp.setFeature(pp.FEATURE_PROCESS_NAMESPACES, true);
+ factory = XmlPullParserFactory.newInstance();
}
- public DOM2XmlPullBuilder(XmlPullParser pp) throws XmlPullParserException {
- this.pp = pp;
+ //public DOM2XmlPullBuilder(XmlPullParser pp) throws XmlPullParserException {
+ public DOM2XmlPullBuilder(XmlPullParserFactory factory)throws XmlPullParserException
+ {
+ this.factory = factory;
}
- public Document parse(Reader reader) throws XmlPullParserException, IOException {
+ protected Document newDoc() throws XmlPullParserException {
try {
+ // if you see dreaded "java.lang.NoClassDefFoundError: org/w3c/dom/ranges/DocumentRange"
+ // add the newest xercesImpl and apis JAR file to JRE/lib/endorsed
+ // for more deatils see: http://java.sun.com/j2se/1.4.2/docs/guide/standards/index.html
DocumentBuilderFactory factory
= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
- doc = builder.newDocument();
+ return builder.newDocument();
} catch (FactoryConfigurationError ex) {
- new XmlPullParserException(
+ throw new XmlPullParserException(
"could not configure factory JAXP DocumentBuilderFactory: "+ex, null, ex);
} catch (ParserConfigurationException ex) {
- new XmlPullParserException(
+ throw new XmlPullParserException(
"could not configure parser JAXP DocumentBuilderFactory: "+ex, null, ex);
}
+ }
+
+ protected XmlPullParser newParser() throws XmlPullParserException {
+ return factory.newPullParser();
+ }
+
+ public Element parse(Reader reader) throws XmlPullParserException, IOException {
+ Document docFactory = newDoc();
+ return parse(reader, docFactory);
+ }
+
+ public Element parse(Reader reader, Document docFactory)
+ throws XmlPullParserException, IOException
+ {
+ Document doc = newDoc();
+ XmlPullParser pp = newParser();
+ pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
pp.setInput(reader);
pp.next();
- pp.require( pp.START_TAG, null, null);
- Element root = parseNode();
- doc.appendChild(root);
- return doc;
+ Element root = parseSubTree(pp, doc);
+ //doc.appendChild(root);
+ return root;
+ }
+
+ public Element parse(XmlPullParser pp) throws XmlPullParserException, IOException {
+ Document doc = newDoc();
+ Element root = parseSubTree(pp, doc);
+ return root;
}
- protected Element parseNode() throws XmlPullParserException, IOException {
+ public Element parseSubTree(XmlPullParser pp, Document docFactory)
+ throws XmlPullParserException, IOException
+ {
+ BuildProcess process = new BuildProcess();
+ return process.parseSubTree(pp, docFactory);
+ }
+
+ static class BuildProcess {
+ private XmlPullParser pp;
+ private Document docFactory;
+ private boolean scanNamespaces = true;
+
+ BuildProcess() {
+ }
+
+ public Element parseSubTree(XmlPullParser pp, Document docFactory)
+ throws XmlPullParserException, IOException
+ {
+ this.pp = pp;
+ this.docFactory = docFactory;
+ return parseSubTree();
+ }
+
+ private Element parseSubTree()
+ throws XmlPullParserException, IOException
+ {
pp.require( pp.START_TAG, null, null);
String name = pp.getName();
String ns = pp.getNamespace( );
String prefix = pp.getPrefix();
String qname = prefix != null ? prefix+":"+name : name;
- Element parent = doc.createElementNS(ns, qname);
+ Element parent = docFactory.createElementNS(ns, qname);
//declare namespaces - quite painful and easy to fail process in DOM2
- for (int i = pp.getNamespaceCount(pp.getDepth()-1); i < pp.getNamespaceCount(pp.getDepth()); ++i)
- {
- String xmlnsPrefix = pp.getNamespacePrefix(i);
- String xmlnsUri = pp.getNamespaceUri(i);
- String xmlnsDecl = (prefix != null) ? "xmlns:"+xmlnsPrefix : "xmlns";
- parent.setAttributeNS("http://www.w3.org/2000/xmlns/", xmlnsDecl, xmlnsUri);
- }
+ declareNamespaces(pp, parent);
// process attributes
for (int i = 0; i < pp.getAttributeCount(); i++)
@@ -179,11 +224,11 @@
// process children
while( pp.next() != pp.END_TAG ) {
if (pp.getEventType() == pp.START_TAG) {
- Element el = parseNode();
+ Element el = parseSubTree(pp, docFactory);
parent.appendChild(el);
} else if (pp.getEventType() == pp.TEXT) {
String text = pp.getText();
- Text textEl = doc.createTextNode(text);
+ Text textEl = docFactory.createTextNode(text);
parent.appendChild(textEl);
} else {
throw new XmlPullParserException(
@@ -194,5 +239,50 @@
return parent;
}
+ private void declareNamespaces(XmlPullParser pp, Element parent)
+ throws DOMException, XmlPullParserException
+
+
+ {
+ if(scanNamespaces) {
+ scanNamespaces = false;
+ int top = pp.getNamespaceCount(pp.getDepth()) - 1;
+ // this loop computes list of all in-scope prefixes
+ LOOP:
+ for (int i = top; i >= pp.getNamespaceCount(0); --i)
+ {
+ // make sure that no prefix is duplicated
+ String prefix = pp.getNamespacePrefix(i);
+ for (int j = top; j > i; --j)
+ {
+ if(prefix.equals(pp.getNamespacePrefix(j))) {
+ // prefix is already declared -- skip it
+ continue LOOP;
+ }
+ }
+ declareOneNamespace(pp, i, parent);
+ }
+ } else {
+ for (int i = pp.getNamespaceCount(pp.getDepth()-1);
+ i < pp.getNamespaceCount(pp.getDepth());
+ ++i)
+ {
+ declareOneNamespace(pp, i, parent);
+ }
+ }
+ }
+
+ private void declareOneNamespace(XmlPullParser pp, int i, Element parent)
+ throws DOMException, XmlPullParserException {
+ String xmlnsPrefix = pp.getNamespacePrefix(i);
+ String xmlnsUri = pp.getNamespaceUri(i);
+ String xmlnsDecl = (xmlnsPrefix != null) ? "xmlns:"+xmlnsPrefix : "xmlns";
+ parent.setAttributeNS("http://www.w3.org/2000/xmlns/", xmlnsDecl, xmlnsUri);
}
+
+
+ }
+
+}
+
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Inkjet cartridges up to 80% off. HP, Epson, Lexmark--we have your brand.
Free shipping on every order to the U.S. and Canada! Excellent service.
http://www.c1tracking.com/l.asp?cid=5510
http://us.click.yahoo.com/QWB0QC/.eUGAA/ySSFAA/2U_rlB/TM
---------------------------------------------------------------------~->
To unsubscribe from this group, send an email to:
[email protected]
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/