CVS Update: xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest
Aleksander Andrzej Slominski <[email protected]> Sun, 18 May 2003 08:21:16 -0500 (EST)
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
aslom 03/05/18 08:21:15
Modified: addons/java/wrapper/src/org/xmlpull/v1/wrapper
XmlPullParserWrapper.java
XmlPullWrapperFactory.java
XmlSerializerWrapper.java
addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic
StaticXmlSerializerWrapper.java
XmlSerializerDelegate.java
addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest
Driver.java
Log:
updated wrapper with usability tweaks and
new fragment() and event() methods to make
serializer wrapper more powerful
Revision Changes Path
1.4 +0 -2 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullParserWrapper.java
Index: XmlPullParserWrapper.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullParserWrapper.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -t -w -r1.3 -r1.4
--- XmlPullParserWrapper.java 5 May 2003 01:36:17 -0000 1.3
+++ XmlPullParserWrapper.java 18 May 2003 13:21:15 -0000 1.4
@@ -98,7 +98,6 @@
public void nextStartTag(String namespace, String name)
throws XmlPullParserException, IOException;
-
/**
* Call parser nextTag() and check that it is END_TAG, throw exception if not.
*/
@@ -116,7 +115,6 @@
*/
public void nextEndTag(String namespace, String name)
throws XmlPullParserException, IOException;
-
/**
* Read text content of element ith given namespace and name
1.5 +8 -5 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullWrapperFactory.java
Index: XmlPullWrapperFactory.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullWrapperFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -t -w -r1.4 -r1.5
--- XmlPullWrapperFactory.java 5 May 2003 01:36:17 -0000 1.4
+++ XmlPullWrapperFactory.java 18 May 2003 13:21:15 -0000 1.5
@@ -53,6 +53,9 @@
}
}
+ public XmlPullParserFactory getFactory() throws XmlPullParserException {
+ return f;
+ }
public void setFeature(String name,
boolean state) throws XmlPullParserException
@@ -83,7 +86,7 @@
//public void setUseDynamic(boolean enable) { useDynamic = enable; };
//public boolean getUseDynamic() { return useDynamic; };
- public XmlPullParserWrapper newPullWrapper() throws XmlPullParserException {
+ public XmlPullParserWrapper newPullParserWrapper() throws XmlPullParserException {
XmlPullParser pp = f.newPullParser();
// if(useDynamic) {
// return (XmlPullParserWrapper) DynamicXmlPullParserWrapper.newProxy(pp, classLoader);
@@ -91,17 +94,17 @@
return new StaticXmlPullParserWrapper(pp);
}
- public XmlPullParserWrapper newPullWrapper(XmlPullParser pp) throws XmlPullParserException {
+ public XmlPullParserWrapper newPullParserWrapper(XmlPullParser pp) throws XmlPullParserException {
return new StaticXmlPullParserWrapper(pp);
}
public XmlSerializerWrapper newSerializerWrapper() throws XmlPullParserException {
XmlSerializer xs = f.newSerializer();
- return new StaticXmlSerializerWrapper(xs);
+ return new StaticXmlSerializerWrapper(xs, this);
}
public XmlSerializerWrapper newSerializerWrapper(XmlSerializer xs) throws XmlPullParserException {
- return new StaticXmlSerializerWrapper(xs);
+ return new StaticXmlSerializerWrapper(xs, this);
}
}
1.5 +17 -7 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlSerializerWrapper.java
Index: XmlSerializerWrapper.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlSerializerWrapper.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -t -w -r1.4 -r1.5
--- XmlSerializerWrapper.java 5 May 2003 01:36:17 -0000 1.4
+++ XmlSerializerWrapper.java 18 May 2003 13:21:15 -0000 1.5
@@ -4,6 +4,7 @@
package org.xmlpull.v1.wrapper;
import java.io.IOException;
+import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@@ -29,6 +30,7 @@
*/
public String setCurrentNamespaceForElements(String value);
+
/**
* Write an attribute without namespace.
* Calls to attribute() MUST follow a call to
@@ -36,17 +38,15 @@
* given namespace, a prefix will be defined automatically.
* NOTE: current element namespace is not used attribute and attributre has no namespace.
*/
- public XmlSerializer attribute (String name, String value)
+ public XmlSerializerWrapper attribute (String name, String value)
throws IOException, IllegalArgumentException, IllegalStateException;
-
-
/** Write start tag in current namespace with name given as argument. */
- public XmlSerializer startTag (String name)
+ public XmlSerializerWrapper startTag(String name)
throws IOException, IllegalArgumentException, IllegalStateException;
/** Write end tag in current namespace with name given as argument. */
- public XmlSerializer endTag (String name)
+ public XmlSerializerWrapper endTag(String name)
throws IOException, IllegalArgumentException, IllegalStateException;
/**
@@ -54,12 +54,22 @@
* and elementText are allowed to be null. If elementText is null, an xsi:nil="true"
* will be added as an attribute.
*/
- public void element(String namespace, String elementName, String elementText)
+ public XmlSerializerWrapper element(String namespace, String elementName, String elementText)
throws IOException, XmlPullParserException;
/** Write simple text element in current namespace */
- public void element(String elementName, String elementText)
+ public XmlSerializerWrapper element(String elementName, String elementText)
throws IOException, XmlPullParserException;
+ /** Write XML fragment using currently set namespace prefixes */
+ public void fragment(String xmlFragment)
+ throws IOException, IllegalArgumentException, IllegalStateException, XmlPullParserException;
+
+ /** Serializer current event form pull parser */
+ public void event(XmlPullParser pp)
+ throws IOException, IllegalArgumentException, IllegalStateException, XmlPullParserException;
+
+ public String escapeText(String text);
+ public String escapeAttributeValue(String text);
}
1.5 +282 -11 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlSerializerWrapper.java
Index: StaticXmlSerializerWrapper.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlSerializerWrapper.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -t -w -r1.4 -r1.5
--- StaticXmlSerializerWrapper.java 5 May 2003 01:36:18 -0000 1.4
+++ StaticXmlSerializerWrapper.java 18 May 2003 13:21:15 -0000 1.5
@@ -4,9 +4,12 @@
package org.xmlpull.v1.wrapper.classic;
import java.io.IOException;
+import java.io.StringReader;
+import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
-import org.xmlpull.v1.util.XmlPullUtil;
+import org.xmlpull.v1.wrapper.XmlPullParserWrapper;
+import org.xmlpull.v1.wrapper.XmlPullWrapperFactory;
import org.xmlpull.v1.wrapper.XmlSerializerWrapper;
/**
@@ -19,13 +22,20 @@
public class StaticXmlSerializerWrapper extends XmlSerializerDelegate
implements XmlSerializerWrapper
{
+ private final static String PROPERTY_XMLDECL_STANDALONE =
+ "http://xmlpull.org/v1/doc/features.html#xmldecl-standalone";
+ private static final boolean TRACE_SIZING = false;
protected String currentNs;
+ protected XmlPullWrapperFactory wf;
+ protected XmlPullParserWrapper fragmentParser;
- public StaticXmlSerializerWrapper(XmlSerializer xs) {
+ public StaticXmlSerializerWrapper(XmlSerializer xs, XmlPullWrapperFactory wf) {
super(xs);
+ this.wf = wf;
}
public String getCurrentNamespaceForElements() { return currentNs; }
+
public String setCurrentNamespaceForElements(String value)
{
String old = currentNs;
@@ -33,32 +43,35 @@
return old;
}
- public XmlSerializer attribute (String name, String value)
+ public XmlSerializerWrapper attribute (String name, String value)
throws IOException, IllegalArgumentException, IllegalStateException
{
- return xs.attribute(null, name, value);
+ xs.attribute(null, name, value);
+ return this;
}
- public XmlSerializer startTag (String name)
+ public XmlSerializerWrapper startTag (String name)
throws IOException, IllegalArgumentException, IllegalStateException
{
- return xs.startTag(currentNs, name);
+ xs.startTag(currentNs, name);
+ return this;
}
- public XmlSerializer endTag (String name)
+ public XmlSerializerWrapper endTag(String name)
throws IOException, IllegalArgumentException, IllegalStateException
{
- return xs.endTag(currentNs, name);
+ endTag(currentNs, name);
+ return this;
}
/** Write simple text element in current namespace */
- public void element(String elementName, String elementText)
+ public XmlSerializerWrapper element(String elementName, String elementText)
throws IOException, XmlPullParserException
{
- element(currentNs, elementName, elementText);
+ return element(currentNs, elementName, elementText);
}
- public void element(String namespace, String elementName, String elementText)
+ public XmlSerializerWrapper element(String namespace, String elementName, String elementText)
throws IOException, XmlPullParserException
{
@@ -74,6 +87,264 @@
xs.text(elementText);
}
xs.endTag(namespace, elementName);
+ return this;
+ }
+
+
+ //namespace stack
+ //protected int elNamespaceCount[] = new int[ 2 ];
+ //protected int currentDepth = -1;
+
+ protected int namespaceEnd = 0;
+ protected String namespacePrefix[] = new String[ 8 ];
+ protected String namespaceUri[] = new String[ namespacePrefix.length ];
+ protected int namespaceDepth[] = new int[ namespacePrefix.length ];
+
+ private void ensureNamespacesCapacity() {
+ int newSize = namespaceEnd > 7 ? 2 * namespaceEnd : 8;
+ if(TRACE_SIZING) {
+ System.err.println(
+ getClass().getName()+" namespaceSize "+namespacePrefix.length+" ==> "+newSize);
+ }
+ String[] newNamespacePrefix = new String[newSize];
+ String[] newNamespaceUri = new String[newSize];
+ int[] newNamespaceDepth = new int[newSize];
+ if(namespacePrefix != null) {
+ System.arraycopy(namespacePrefix, 0, newNamespacePrefix, 0, namespaceEnd);
+ System.arraycopy(namespaceUri, 0, newNamespaceUri, 0, namespaceEnd);
+ System.arraycopy(namespaceDepth, 0, newNamespaceDepth, 0, namespaceEnd);
+ }
+ namespacePrefix = newNamespacePrefix;
+ namespaceUri = newNamespaceUri;
+ namespaceDepth = newNamespaceDepth;
+ }
+
+ // public XmlSerializer endTag(String namespace, String name) throws IOException
+ // {
+ // xs.endTag(currentNs, name);
+ // namespaceEnd = elNamespaceCount[ getDepth() ];
+ // return this;
+ // }
+
+ public void setPrefix(String prefix, String namespace)
+ throws IOException, IllegalArgumentException, IllegalStateException
+ {
+ xs.setPrefix(prefix, namespace);
+
+ int depth = getDepth();
+ for(int pos = namespaceEnd - 1; pos >= 0; --pos) {
+ if(namespaceDepth[ pos ] <= depth) {
+ break;
+ }
+ --namespaceEnd;
+ }
+
+ if(namespaceEnd >= namespacePrefix.length) {
+ ensureNamespacesCapacity();
+ }
+ namespacePrefix[ namespaceEnd ] = prefix;
+ namespaceUri[ namespaceEnd ] = namespace;
+ ++namespaceEnd;
+
+ }
+
+ public void fragment(String xmlFragment)
+ throws IOException, IllegalArgumentException, IllegalStateException, XmlPullParserException
+ {
+ StringBuffer buf = new StringBuffer(xmlFragment.length() + namespaceEnd * 30);
+ buf.append("<fragment");
+ LOOP:
+ for(int pos = namespaceEnd - 1; pos >= 0; --pos) {
+ String prefix = namespacePrefix[ pos ];
+ for(int i = namespaceEnd - 1; i > pos; --i) {
+ if(prefix.equals( namespacePrefix[i] )) {
+ continue LOOP;
+ }
+ }
+ buf.append(" xmlns");
+ if(prefix.length() > 0) {
+ buf.append(':').append(prefix);
+ }
+ buf.append("='");
+ buf.append(escapeAttributeValue(namespaceUri[ pos ]));
+ buf.append("'");
+ }
+
+ buf.append(">");
+ buf.append(xmlFragment);
+ buf.append("</fragment>");
+
+ if(fragmentParser == null) {
+ fragmentParser = wf.newPullParserWrapper();
+ }
+ String s = buf.toString();
+ //System.err.println(getClass().getName()+" fragment XML="+s);
+ fragmentParser.setInput(new StringReader( s ) );
+ fragmentParser.nextTag();
+ fragmentParser.require(XmlPullParser.START_TAG, null, "fragment");
+ while(true) {
+ fragmentParser.nextToken();
+ if(fragmentParser.getDepth() == 1
+ && fragmentParser.getEventType() == XmlPullParser.END_TAG)
+ {
+ break;
+ }
+ event(fragmentParser);
+ }
+ fragmentParser.require(XmlPullParser.END_TAG, null, "fragment");
}
+
+ public void event(XmlPullParser pp) throws XmlPullParserException, IOException {
+ int eventType = pp.getEventType();
+ switch (eventType) {
+ case XmlPullParser.START_DOCUMENT:
+ //use Boolean.TRUE to make it standalone
+ Boolean standalone = (Boolean) pp.getProperty(PROPERTY_XMLDECL_STANDALONE);
+ startDocument(pp.getInputEncoding(), standalone);
+ break;
+
+ case XmlPullParser.END_DOCUMENT:
+ endDocument();
+ break;
+
+ case XmlPullParser.START_TAG:
+ writeStartTag(pp);
+ break;
+
+ case XmlPullParser.END_TAG:
+ endTag(pp.getNamespace (), pp.getName ());
+ break;
+
+ case XmlPullParser.IGNORABLE_WHITESPACE:
+ //comment it to remove ignorable whtespaces from XML infoset
+ String s = pp.getText ();
+ ignorableWhitespace(s);
+ break;
+
+ case XmlPullParser.TEXT:
+ if(pp.getDepth() > 0) {
+ text(pp.getText ());
+ } else {
+ ignorableWhitespace(pp.getText ());
+ }
+ break;
+
+ case XmlPullParser.ENTITY_REF:
+ entityRef (pp.getName ());
+ break;
+
+ case XmlPullParser.CDSECT:
+ cdsect( pp.getText () );
+ break;
+
+ case XmlPullParser.PROCESSING_INSTRUCTION:
+ processingInstruction( pp.getText ());
+ break;
+
+ case XmlPullParser.COMMENT:
+ comment (pp.getText ());
+ break;
+
+ case XmlPullParser.DOCDECL:
+ docdecl (pp.getText ());
+ break;
+ }
+ }
+
+ private void writeStartTag(XmlPullParser pp) throws XmlPullParserException, IOException {
+ if (!pp.getFeature (XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES)) {
+ for (int i = pp.getNamespaceCount (pp.getDepth ()-1);
+ i < pp.getNamespaceCount (pp.getDepth ())-1; i++)
+ {
+ setPrefix(pp.getNamespacePrefix (i), pp.getNamespaceUri (i));
+ }
+ }
+ startTag(pp.getNamespace(), pp.getName());
+
+ for (int i = 0; i < pp.getAttributeCount(); i++) {
+ attribute
+ (pp.getAttributeNamespace (i),
+ pp.getAttributeName (i),
+ pp.getAttributeValue (i));
+ }
+ //ser.closeStartTag();
+ }
+
+
+ public String escapeAttributeValue(String value)
+ //protected void writeAttributeValue(String value, Writer out) throws IOException
+ {
+ int posLt = value.indexOf('<');
+ int posAmp = value.indexOf('&');
+ int posQuot = value.indexOf('"');
+ int posApos = value.indexOf('\'');
+ if(posLt == -1 && posAmp == -1 && posQuot == -1 && posApos == -1) {
+ return value;
+ }
+ StringBuffer buf = new StringBuffer(value.length() + 10);
+
+ // painful loop ...
+ for(int pos = 0, len = value.length(); pos < len; ++pos) {
+ char ch = value.charAt(pos);
+ switch(ch) {
+ case '<':
+ buf.append("<");
+ break;
+ case '&':
+ buf.append("&");
+ break;
+ case '\'':
+ buf.append("'");
+ break;
+ case '"':
+ buf.append(""");
+ break;
+ default:
+ buf.append(ch);
+ }
+ }
+ return buf.toString();
+ }
+
+ //protected void writeElementContent(String text, Writer out) throws IOException
+ public String escapeText(String text)
+ {
+ //<, & esccaped]
+ //out.write(text);
+ int posLt = text.indexOf('<');
+ int posAmp = text.indexOf('&');
+ if(posLt == -1 && posAmp == -1) { // this is shortcut
+ return text;
+ }
+ StringBuffer buf = new StringBuffer(text.length() + 10);
+ // painful loop ...
+ int pos = 0;
+ while(true) {
+ if(posLt == -1 && posAmp == -1) { // this is shortcut
+ buf.append(text.substring(pos));
+ break;
+ } else if(posLt == -1
+ || (posLt != -1 && posAmp != -1 && posAmp < posLt))
+ {
+ if(pos < posAmp) buf.append(text.substring(pos, posAmp));
+ buf.append("&");
+ pos = posAmp + 1;
+ posAmp = text.indexOf('&', pos);
+ } else if(posAmp == -1
+ || (posLt != -1 && posAmp != -1 && posLt < posAmp))
+ {
+ if(pos < posLt) buf.append(text.substring(pos, posLt));
+ buf.append("<");
+ pos = posLt + 1;
+ posLt = text.indexOf('<', pos);
+ } else {
+ throw new IllegalStateException("wrong state posLt="+posLt
+ +" posAmp="+posAmp+" for "+text);
+ }
+ }
+ return buf.toString();
+ }
+
+
}
1.3 +6 -2 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/XmlSerializerDelegate.java
Index: XmlSerializerDelegate.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/XmlSerializerDelegate.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -t -w -r1.2 -r1.3
--- XmlSerializerDelegate.java 5 May 2003 01:36:18 -0000 1.2
+++ XmlSerializerDelegate.java 18 May 2003 13:21:15 -0000 1.3
@@ -28,7 +28,9 @@
return xs.getName();
}
- public void setPrefix(String prefix, String namespace) throws IOException, IllegalArgumentException, IllegalStateException {
+ public void setPrefix(String prefix, String namespace)
+ throws IOException, IllegalArgumentException, IllegalStateException
+ {
xs.setPrefix(prefix, namespace);
}
@@ -116,7 +118,9 @@
return xs.getNamespace();
}
- public XmlSerializer endTag(String namespace, String name) throws IOException, IllegalArgumentException, IllegalStateException {
+ public XmlSerializer endTag(String namespace, String name)
+ throws IOException, IllegalArgumentException, IllegalStateException
+ {
return xs.endTag(namespace, name);
}
1.2 +1 -1 xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest/Driver.java
Index: Driver.java
===================================================================
RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest/Driver.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -t -w -r1.1 -r1.2
--- Driver.java 3 May 2003 05:56:47 -0000 1.1
+++ Driver.java 18 May 2003 13:21:15 -0000 1.2
@@ -52,7 +52,7 @@
System.err.println("direct test took "+(endDirect-startDirect)/1000.0+" seconds");
startStaticWrap = System.currentTimeMillis();
- XmlPullParserWrapper spw = staticWrapperFactory.newPullWrapper();
+ XmlPullParserWrapper spw = staticWrapperFactory.newPullParserWrapper();
for (int i = 0; i < REPEAT; i++)
{
StringReader sr = new StringReader("<hello>world!</hello>");
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading! Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/Lj3uPC/Me7FAA/uetFAA/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/