[jdom-interest] StAXBuilder, v3
Tatu Saloranta
cowtowncoder at yahoo.com
Sun Nov 21 16:26:59 PST 2004
Based on Bradley's suggestions, I modifed stax-based
builder a bit. It now uses UncheckecJDOMFactory (and
as diffs show, trivial change can and should be done
to sax builder as well).
I also changed Namespace to use efficient key object,
instead of rather slow StringBuffer-built Strings.
Result is about 10-15% improvement for document
building
when using StAX parsers (and most likely ditto for
sax-based ones), for namespace-heavy documents (tested
with OpenOffice xml, should be similar for Soap).
Due to this improvement, it didn't seem worth it
trying
to add additional caching like I did with previous
version (I tested it, and speed improvement wasn't
very impressive; if applied to Namespace object, would
mandata synchronized access as well).
I also attached 'cvs diff' output: in addition to one
new file, and changes to Namespace.java, there are
2 smaller changes: one-line change to SAXBuilder,
and couple of lines to build.xml to include StAX API
jar for compilation (note: that's only needed when
running, if StAX builder is actually used).
Let me know if there are other concerns about the
builder, or suggestions, or questions regarding
change(s) to Namespace class.
-+ Tatu +-
ps. I added standard license disclaimer in
StAXBuilder; not sure if that's the way to do it or
not.
__________________________________
Do you Yahoo!?
Meet the all-new My Yahoo! - Try it today!
http://my.yahoo.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: StAXBuilder.java
Type: text/x-java
Size: 14762 bytes
Desc: StAXBuilder.java
Url : http://www.jdom.org/pipermail/jdom-interest/attachments/20041121/6706cae6/StAXBuilder.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Namespace.java
Type: text/x-java
Size: 12287 bytes
Desc: Namespace.java
Url : http://www.jdom.org/pipermail/jdom-interest/attachments/20041121/6706cae6/Namespace.bin
-------------- next part --------------
? lib/stax1.0.jar
? src/java/org/jdom/input/StAXBuilder.java
Index: build.xml
===================================================================
RCS file: /home/cvspublic/jdom/build.xml,v
retrieving revision 1.52
diff -r1.52 build.xml
70a71
> <property name="stax.jar" value="${lib.dir}/stax1.0.jar"/>
75a77
> <pathelement location="${stax.jar}"/>
92a95
> <pathelement location="${stax.jar}"/>
Index: src/java/org/jdom/Namespace.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/Namespace.java,v
retrieving revision 1.41
diff -r1.41 Namespace.java
104c104
< private String prefix;
---
> private final String prefix;
107c107
< private String uri;
---
> private final String uri;
117,119c117,120
< namespaces.put("&", NO_NAMESPACE);
< namespaces.put("xml&http://www.w3.org/XML/1998/namespace",
< XML_NAMESPACE);
---
> //namespaces.put("&", NO_NAMESPACE);
> namespaces.put(NO_NAMESPACE.createKey(), NO_NAMESPACE);
> //namespaces.put("xml&http://www.w3.org/XML/1998/namespace", XML_NAMESPACE);
> namespaces.put(XML_NAMESPACE.createKey(), XML_NAMESPACE);
146,147c147,148
< String lookup = new StringBuffer(64)
< .append(prefix).append('&').append(uri).toString();
---
>
> NSKey lookup = new NSKey(prefix, uri);
148a150
>
273a276,323
> }
>
> protected NSKey createKey() {
> return new NSKey(prefix, uri);
> }
>
> private static class NSKey
> {
> final String prefix;
> final String uri;
>
> int hash = 0;
>
> public NSKey(String p, String u) {
> prefix = p;
> uri = u;
> }
>
> public boolean equals(Object o) {
> if (!(o instanceof NSKey)) {
> return false;
> }
> NSKey other = (NSKey) o;
> String otherPrefix = other.prefix;
> String otherURI = other.uri;
>
> return ((otherPrefix == prefix) || (otherPrefix.equals(prefix)))
> && ((otherURI == uri) || (otherURI.equals(uri)));
> }
>
> /**
> * Hash code should be cached, since key is only used as the
> * hash map key. Further, it should be based on both prefix
> * and URI -- it's quite possible to map same URI to different
> * prefixes (at least default [empty] prefix and explicit one).
> * Especially since prefix is probably much shorter, and costly
> * part is URI String's hash code.
> */
> public int hashCode() {
> if (hash == 0) {
> hash = prefix.hashCode() ^ uri.hashCode();
> }
> return hash;
> }
>
> public String toString() { // only for debugging purposes...
> return prefix + ":{" + uri +"}";
> }
Index: src/java/org/jdom/input/SAXBuilder.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/input/SAXBuilder.java,v
retrieving revision 1.89
diff -r1.89 SAXBuilder.java
123c123
< private JDOMFactory factory = new DefaultJDOMFactory();
---
> private JDOMFactory factory = new UncheckedJDOMFactory();
More information about the jdom-interest
mailing list