CVS Update: xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest

Aleksander Andrzej Slominski <[email protected]> Sat, 3 May 2003 00:56:47 -0500 (EST)
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       03/05/03 00:56:47

  Added:       addons/java/wrapper/samples WrapperSample.java
               addons/java/wrapper/src/org/xmlpull/v1/wrapper
                        XmlPullParserWrapper.java XmlPullWrapper.txt
                        XmlPullWrapperFactory.java
                        XmlSerializerWrapper.java
               addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic
                        StaticXmlPullParserWrapper.java
                        StaticXmlSerializerWrapper.java
                        XmlPullParserDelegate.java
                        XmlSerializerDelegate.java
               addons/java/wrapper/src/org/xmlpull/v1/wrapper/junit
                        TestXmlPullWrapper.java
               addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest
                        Driver.java
  Log:
  added wrapper classes that extend pull parser and serializer interfaces
  and make it very easy to use extended functionality
  (just replace regular factory XmlPullParserFactory with XmlPullWrapperFactory and use XmlPullParserWrapper instead XmlPullParser or XmlSerializerWrapper instead of XmlSerializer ...)
  NOTE: this is *not* yet very well tested
  
  Revision  Changes    Path
  1.1                  xmlpull-api-v1/addons/java/wrapper/samples/WrapperSample.java
  
  Index: WrapperSample.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  import java.io.StringReader;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.wrapper.XmlPullParserWrapper;
  import org.xmlpull.v1.wrapper.XmlPullWrapperFactory;
  
  /**
   * Example how to use wrapper addon to XmlPullApi
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class WrapperSample {
      /**
       *
       */
      public static void main(String[] args) throws Exception
      {
          StringReader sr = new StringReader("<hello>world!</hello>");
          XmlPullParserWrapper pw = XmlPullWrapperFactory.newInstance().newPullWrapper();
          pw.setInput(sr);
          pw.nextTag();
          pw.require(XmlPullParser.START_TAG, null, "hello");
          pw.next();
          pw.nextEndTag();
          pw.require(XmlPullParser.END_TAG, null, "hello");
      }
  }
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullParserWrapper.java
  
  Index: XmlPullParserWrapper.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper;
  
  import java.lang.reflect.Proxy;
  import java.lang.reflect.InvocationHandler;
  
  import java.io.IOException;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * Extensions to XmlPullParser interface
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public interface XmlPullParserWrapper extends XmlPullParser {
  
      public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
  
      /**
       * Return PITarget from Processing Instruction (PI) as defined in
       * XML 1.0 Section 2.6 Processing Instructions
       *  <code>[16] PI ::= '&lt;?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'</code>
       */
      public String getPITarget() throws IllegalStateException;
  
      /**
       * Return everything past PITarget and S from Processing Instruction (PI) as defined in
       * XML 1.0 Section 2.6 Processing Instructions
       *  <code>[16] PI ::= '&lt;?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'</code>
       *
       * <p><b>NOTE:</b> if there is no PI data it returns empty string.
       */
      public String getPIData() throws IllegalStateException;
  
      /**
       * Tests if the current event is of the given type and if the namespace and name match.
       * null will match any namespace and any name. If the test passes a true is returned
       * otherwise a false is returned.
       */
      public boolean matches(int type, String namespace, String name)
          throws XmlPullParserException;
  
  
      /**
       * Return value of attribute with given name and no namespace.
       */
      public String getAttributeValue(String name);
  
      /**
       * call parser nextTag() and check that it is START_TAG, throw exception if not.
       */
      public void nextStartTag()
          throws XmlPullParserException, IOException;
  
      /**
       * combine nextTag(); pp.require(pp.START_TAG, namespace, name);
       */
      public void nextStartTag(String namespace, String name)
          throws XmlPullParserException, IOException;
  
  
  
  
      /**
       * combine nextTag(); pp.require(pp.END_TAG, namespace, name);
       */
      public void nextEndTag(String namespace, String name)
          throws XmlPullParserException, IOException;
  
  
      /**
       * Read text content of element ith given namespace and name
       * (use null namespace do indicate that nemspace should not be checked)
       */
  
      public String nextText(String namespace, String name)
          throws IOException, XmlPullParserException;
  
      /**
       * Read attribute value and return it or throw exception if
       * current element does not have such attribute.
       */
  
      public String getRequiredAttributeValue(String namespace, String name)
          throws IOException, XmlPullParserException;
  
      /**
       * Call parser nextTag() and check that it is END_TAG, throw exception if not.
       */
      public void nextEndTag() throws XmlPullParserException, IOException;
  
  
      /**
       * Skip sub tree that is currently porser positioned on.
       * <br>NOTE: parser must be on START_TAG and when funtion returns
       * parser will be positioned on matching END_TAG
       */
      public void skipSubTree()
          throws XmlPullParserException, IOException;
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullWrapper.txt
  
  Index: XmlPullWrapper.txt
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper;
  
  import java.io.IOException;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * Handy functions that combines XMLPULL API into higher level functionality.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class XmlPullWrapper {
      protected XmlPullParser pp;
  
      public XmlPullWrapper(XmlPullParser parser) {
          pp = parser;
      }
  
      public XmlPullParser getPullParser() { return pp; }
  
      /**
       * This method bypasses all child subtrees until it reached END_TAG for current tree.
       * Parser must be on START_TAG of one of child subtrees.
       */
      public void jumpToEndOfTree()
          throws XmlPullParserException, IOException
      {
          pp.require(pp.START_TAG, null, null);
          while(true) {
              int eventType = pp.next();
              if (eventType == pp.START_TAG) {
                  skipSubTree();
                  pp.require(pp.END_TAG, null, null);
                  pp.next(); //skip end tag
              } else if(eventType == pp.END_TAG) {
                  break;
              }
          }
      }
  
      /**
       * This method bypasses all child subtrees until it finds a child subtree with start tag
       * that matches the tag name (if not null) and namespsce (if not null)
       * passed in. Parser must be positioned on START_TAG.
       * <p>If succesfulpositions parser on such START_TAG and return true
       * otherwise this method returns false and parser is positioned on END_TAG
       * signaling last element in curren subtree.
       */
      public boolean jumpToSubTree(final String tagNamespace, final String tagName)
          throws XmlPullParserException, IOException
      {
          if(tagNamespace == null && tagName == null) {
              throw new IllegalArgumentException(
                  "namespace and name argument can not be bith null:"+pp.getPositionDescription());
          }
          pp.require(pp.START_TAG, null, null);
          while(true) {
              int eventType = pp.next();
  
              if (eventType == pp.START_TAG)
              {
                  String name = pp.getName();
                  String namespace = pp.getNamespace();
                  boolean matches = (tagNamespace != null && tagNamespace.equals(namespace))
                      ||(tagName != null && tagName.equals(name));
                  if(matches) {
                      return true;
                  }
                  skipSubTree();
                  pp.require(pp.END_TAG, name, namespace);
                  pp.next(); //skip end tag
              } else if(eventType == pp.END_TAG) {
                  return false;
              }
          }
      }
  
      /**
       * This method bypasses all events until it finds a start tag that has
       * passed in namesapce (if not null) and namespace (if not null).
       *
       * @return true if such START_TAG was found or false otherwise (and parser is on END_DOCUMENT).
       */
      public boolean jumpToStartTag(final String tagNamespace, final String tagName)
          throws XmlPullParserException, IOException
      {
          if(tagNamespace == null && tagName == null) {
              throw new IllegalArgumentException(
                  "namespace and name argument can not be bith null:"+pp.getPositionDescription());
          }
          while(true) {
              int eventType = pp.next();
              if(eventType == pp.START_TAG)
              {
                  String name = pp.getName();
                  String namespace = pp.getNamespace();
                  boolean matches = (tagNamespace != null && tagNamespace.equals(namespace))
                      ||(tagName != null && tagName.equals(name));
                  if(matches) {
                      return true;
                  }
              } else if(eventType == pp.END_DOCUMENT) {
                  return false;
              }
  
          }
      }
  
      /**
       * This method bypasses all events until it finds an end tag that has
       * passed in namesapce (if not null) and namespace (if not null).
       *
       * @return true if such END_TAG was found or false otherwise (and parser is on END_DOCUMENT).
       */
      public boolean jumpToEndTag(final String tagNamespace, final String tagName)
          throws XmlPullParserException, IOException
      {
          if(tagNamespace == null && tagName == null) {
              throw new IllegalArgumentException(
                  "namespace and name argument can not be bith null:"+pp.getPositionDescription());
          }
          while(true) {
              int eventType = pp.next();
              if(eventType == pp.END_TAG)
              {
                  String name = pp.getName();
                  String namespace = pp.getNamespace();
                  boolean matches = (tagNamespace != null && tagNamespace.equals(namespace))
                      ||(tagName != null && tagName.equals(name));
                  if(matches) {
                      return true;
                  }
              } else if(eventType == pp.END_DOCUMENT) {
                  return false;
              }
  
          }
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlPullWrapperFactory.java
  
  Index: XmlPullWrapperFactory.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper;
  
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  import org.xmlpull.v1.XmlPullParserFactory;
  import org.xmlpull.v1.XmlSerializer;
  import org.xmlpull.v1.wrapper.classic.StaticXmlPullParserWrapper;
  import org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper;
  
  /**
   * Handy functions that combines XmlPull API into higher level functionality.
   * <p>NOTE: returned wrapper object is <strong>not</strong> multi-thread safe
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  
  public class XmlPullWrapperFactory {
      private final static boolean DEBUG = false;
      protected ClassLoader classLoader;
      protected XmlPullParserFactory factory;
      protected boolean useDynamic;
  
      public static XmlPullWrapperFactory newInstance() throws XmlPullParserException
      {
          //TODO: make into real pluggable factory service (later ...)?
          return new XmlPullWrapperFactory(null);
      }
  
      public static XmlPullWrapperFactory newInstance(XmlPullParserFactory factory)
          throws XmlPullParserException
      {
          return new XmlPullWrapperFactory(factory);
      }
  
      // ------------ IMPLEMENTATION
  
      protected XmlPullWrapperFactory(XmlPullParserFactory factory) throws XmlPullParserException {
          if(factory != null) {
              this.factory = factory;
          } else {
              this.factory = XmlPullParserFactory.newInstance();
          }
      }
  
      //public void setUseDynamic(boolean enable) { useDynamic = enable; };
      //public boolean getUseDynamic() { return useDynamic; };
  
      public XmlPullParserWrapper newPullWrapper() throws XmlPullParserException {
          XmlPullParser pp = factory.newPullParser();
          //        if(useDynamic) {
          //            return (XmlPullParserWrapper) DynamicXmlPullParserWrapper.newProxy(pp, classLoader);
          //        } else {
          return new StaticXmlPullParserWrapper(pp);
      }
  
      public XmlPullParserWrapper newPullWrapper(XmlPullParser pp) throws XmlPullParserException {
          return new StaticXmlPullParserWrapper(pp);
      }
  
      public XmlSerializerWrapper newSerializerWrapper() throws XmlPullParserException {
          XmlSerializer xs = factory.newSerializer();
          return new StaticXmlSerializerWrapper(xs);
      }
  
      public XmlSerializerWrapper newSerializerWrapper(XmlSerializer xs) throws XmlPullParserException {
          return new StaticXmlSerializerWrapper(xs);
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/XmlSerializerWrapper.java
  
  Index: XmlSerializerWrapper.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper;
  
  import java.io.IOException;
  import org.xmlpull.v1.XmlSerializer;
  
  /**
   * Extensions to XmlSerialzier interface
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public interface XmlSerializerWrapper extends XmlSerializer {
      public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
  
      public String getDefaultNamespace();
      public void setDefaultNamespace(String value);
  
      public XmlSerializer startTag (String name)
          throws IOException, IllegalArgumentException, IllegalStateException;
  
      public XmlSerializer endTag (String name)
          throws IOException, IllegalArgumentException, IllegalStateException;
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlPullParserWrapper.java
  
  Index: StaticXmlPullParserWrapper.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper.classic;
  
  import java.io.IOException;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  import org.xmlpull.v1.wrapper.XmlPullParserWrapper;
  import org.xmlpull.v1.util.XmlPullUtil;
  
  /**
   * This class seemlesly extends exisiting parser implementation by adding new methods
   * (provided by XmlPullUtil) and delegating exisiting methods to parser implementation.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class StaticXmlPullParserWrapper extends XmlPullParserDelegate
      implements XmlPullParserWrapper
  {
      public StaticXmlPullParserWrapper(XmlPullParser pp) {
          super(pp);
      }
  
      public String getAttributeValue(String name)
      {
          return XmlPullUtil.getAttributeValue(pp, name);
      }
  
  
    /**
       * Read the text of a required element and return it or throw exception if
       * required element is not found. Useful for getting the text of simple
       * elements such as <username>johndoe</username>. Assumes that parser is
       * just before the start tag and leaves the parser at the end tag. If the
       * text is nil (e.g. <username xsi:nil="true"/>), then a null will be returned.
       */
  
      public String getRequiredElementText(String namespace, String name)
          throws IOException, XmlPullParserException {
              if (name == null) {
                  throw new XmlPullParserException("name for element can not be null");
              }
  
              String text = null;
              nextStartTag(namespace, name);
              if (isNil()) {
                  nextEndTag(namespace, name);
              }
              else {
                  text = pp.nextText();
              }
              pp.require(XmlPullParser.END_TAG, namespace, name);
              return text;
      }
  
      /**
       * Is the current tag nil? Checks for xsi:nil="true".
       */
      public boolean isNil()
          throws IOException, XmlPullParserException {
  
          boolean result = false;
          String value = pp.getAttributeValue(XSI_NS, "nil");
          if ("true".equals(value)) {
              result = true;
          }
  
          return result;
      }
  
      public String getPITarget() throws IllegalStateException {
          return XmlPullUtil.getPITarget(pp);
      }
  
      public String getPIData() throws IllegalStateException {
          return XmlPullUtil.getPIData(pp);
      }
  
      public boolean matches(int type, String namespace, String name)
          throws XmlPullParserException
      {
          return XmlPullUtil.matches(pp, type, namespace, name);
      }
  
      /**
       * call parser nextTag() and check that it is START_TAG, throw exception if not.
       */
      public void nextStartTag()
          throws XmlPullParserException, IOException
      {
          XmlPullUtil.nextStartTag(pp);
      }
  
      /**
       * combine nextTag(); pp.require(pp.START_TAG, namespace, name);
       */
      public void nextStartTag(String namespace, String name)
          throws XmlPullParserException, IOException
      {
          XmlPullUtil.nextStartTag(pp, namespace, name);
      }
  
      /**
       * combine nextTag(); pp.require(pp.END_TAG, namespace, name);
       */
      public void nextEndTag(String namespace, String name)
          throws XmlPullParserException, IOException
      {
          XmlPullUtil.nextEndTag(pp, namespace, name);
      }
  
  
      /**
       * Read text content of element with given namespace and name
       * (use null namespace do indicate that nemspace should not be checked)
       */
  
      public String nextText(String namespace, String name)
          throws IOException, XmlPullParserException
      {
          return XmlPullUtil.nextText(pp, namespace, name);
      }
  
      /**
       * Read attribute value and return it or throw exception if
       * current element does not have such attribute.
       */
  
      public String getRequiredAttributeValue(String namespace, String name)
          throws IOException, XmlPullParserException
      {
          return XmlPullUtil.getRequiredAttributeValue(pp, namespace, name);
      }
  
      public void nextEndTag() throws XmlPullParserException, IOException {
          XmlPullUtil.nextEndTag(pp);
      }
  
  
      public void skipSubTree() throws XmlPullParserException, IOException {
          XmlPullUtil.skipSubTree(pp);
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/StaticXmlSerializerWrapper.java
  
  Index: StaticXmlSerializerWrapper.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper.classic;
  
  import java.io.IOException;
  import org.xmlpull.v1.XmlPullParserException;
  import org.xmlpull.v1.XmlSerializer;
  import org.xmlpull.v1.util.XmlPullUtil;
  import org.xmlpull.v1.wrapper.XmlSerializerWrapper;
  
  /**
   * This class seemlesly extends exisiting serialzier implementation by adding new methods
   * (provided by XmlPullUtil) and delegating exisiting methods to parser implementation.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class StaticXmlSerializerWrapper extends XmlSerializerDelegate
      implements XmlSerializerWrapper
  {
      protected String defaultNs;
  
      public StaticXmlSerializerWrapper(XmlSerializer xs) {
          super(xs);
      }
  
      public String getDefaultNamespace() { return defaultNs; }
      public void setDefaultNamespace(String value) { defaultNs = value; }
  
  
      public XmlSerializer startTag (String name)
          throws IOException, IllegalArgumentException, IllegalStateException
      {
          return xs.startTag(defaultNs, name);
      }
  
      public XmlSerializer endTag (String name)
          throws IOException, IllegalArgumentException, IllegalStateException
      {
          return xs.endTag(defaultNs, name);
      }
  
  
  
      /**
       * Writes a simple element such as <username>johndoe</username>. The namespace
       * 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)
          throws IOException, XmlPullParserException
      {
  
          if (elementName == null) {
              throw new XmlPullParserException("name for element can not be null");
          }
  
          xs.startTag(namespace, elementName);
          if (elementText == null) {
              xs.attribute(XSI_NS, "nil", "true");
          }
          else {
              xs.text(elementText);
          }
          xs.endTag(namespace, elementName);
      }
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/XmlPullParserDelegate.java
  
  Index: XmlPullParserDelegate.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper.classic;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.Reader;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * This is simple class that implements parser interface by delegating
   * all calls to actual wrapped class implementation that is passed in constructor.
   * Purpose of this class is to work as a base class when extending parser interface
   * by wrapping exsiting parser implementation and allowing to add new methods.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class XmlPullParserDelegate implements XmlPullParser {
  
      protected XmlPullParser pp;
  
      public XmlPullParserDelegate(XmlPullParser pp) {
          this.pp = pp;
      }
  
      public String getText() {
          return pp.getText();
      }
  
      public void setFeature(String name, boolean state) throws XmlPullParserException {
          pp.setFeature(name, state);
      }
  
      public char[] getTextCharacters(int[] holderForStartAndLength) {
          return pp.getTextCharacters(holderForStartAndLength);
      }
  
      public int getColumnNumber() {
          return pp.getColumnNumber();
      }
  
      public int getNamespaceCount(int depth) throws XmlPullParserException {
          return pp.getNamespaceCount(depth);
      }
  
      public String getNamespacePrefix(int pos) throws XmlPullParserException {
          return pp.getNamespacePrefix(pos);
      }
  
      public String getAttributeName(int index) {
          return pp.getAttributeName(index);
      }
  
      public String getName() {
          return pp.getName();
      }
  
      public boolean getFeature(String name) {
          return pp.getFeature(name);
      }
  
      public String getInputEncoding() {
          return pp.getInputEncoding();
      }
  
      public String getAttributeValue(int index) {
          return pp.getAttributeValue(index);
      }
  
      public String getNamespace(String prefix) {
          return pp.getNamespace(prefix);
      }
  
      public void setInput(Reader in) throws XmlPullParserException {
          pp.setInput(in);
      }
  
      public int getLineNumber() {
          return pp.getLineNumber();
      }
  
      public Object getProperty(String name) {
          return pp.getProperty(name);
      }
  
      public boolean isEmptyElementTag() throws XmlPullParserException {
          return pp.isEmptyElementTag();
      }
  
      public boolean isAttributeDefault(int index) {
          return pp.isAttributeDefault(index);
      }
  
      public String getNamespaceUri(int pos) throws XmlPullParserException {
          return pp.getNamespaceUri(pos);
      }
  
      public int next() throws XmlPullParserException, IOException {
          return pp.next();
      }
  
      public int nextToken() throws XmlPullParserException, IOException {
          return pp.nextToken();
      }
  
      public void defineEntityReplacementText(String entityName,
                                              String replacementText)
          throws XmlPullParserException
      {
          pp.defineEntityReplacementText(entityName, replacementText);
      }
  
      public int getAttributeCount() {
          return pp.getAttributeCount();
      }
  
      public boolean isWhitespace() throws XmlPullParserException {
          return pp.isWhitespace();
      }
  
      public String getPrefix() {
          return pp.getPrefix();
      }
  
      public void require(int type, String namespace, String name) throws XmlPullParserException, IOException {
          pp.require(type, namespace, name);
      }
  
      public String nextText() throws XmlPullParserException, IOException {
          return pp.nextText();
      }
  
      public String getAttributeType(int index) {
          return pp.getAttributeType(index);
      }
  
      public int getDepth() {
          return pp.getDepth();
      }
  
      public int nextTag() throws XmlPullParserException, IOException {
          return pp.nextTag();
      }
  
      public int getEventType() throws XmlPullParserException {
          return pp.getEventType();
      }
  
      public String getAttributePrefix(int index) {
          return pp.getAttributePrefix(index);
      }
  
      public void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException {
          pp.setInput(inputStream, inputEncoding);
      }
  
      public String getAttributeValue(String namespace, String name) {
          return pp.getAttributeValue(namespace, name);
      }
  
      public void setProperty(String name, Object value) throws XmlPullParserException {
          pp.setProperty(name, value);
      }
  
      public String getPositionDescription() {
          return pp.getPositionDescription();
      }
  
      public String getNamespace() {
          return pp.getNamespace();
      }
  
      public String getAttributeNamespace(int index) {
          return pp.getAttributeNamespace(index);
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/classic/XmlSerializerDelegate.java
  
  Index: XmlSerializerDelegate.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.wrapper.classic;
  
  import org.xmlpull.v1.XmlSerializer;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.Writer;
  
  /**
   * This is simple class that implements serializer interface by delegating
   * all calls to actual serialzier implementation passed in constructor.
   * Purpose of this class is to work as base class to allow extending interface
   * by wrapping exsiting parser implementation and allowing ot add new methods.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class XmlSerializerDelegate implements XmlSerializer {
  
      protected XmlSerializer xs;
  
      public XmlSerializerDelegate(XmlSerializer serializer) {
          this.xs = serializer;
      }
  
      public String getName() {
          return xs.getName();
      }
  
      public void setPrefix(String prefix, String namespace) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.setPrefix(prefix, namespace);
      }
  
      public void setOutput(OutputStream os, String encoding) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.setOutput(os, encoding);
      }
  
      public void endDocument() throws IOException, IllegalArgumentException, IllegalStateException {
          xs.endDocument();
      }
  
      public void comment(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.comment(text);
      }
  
      public int getDepth() {
          return xs.getDepth();
      }
  
      public void setProperty(String name, Object value) throws IllegalArgumentException, IllegalStateException {
          xs.setProperty(name, value);
      }
  
      public void cdsect(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.cdsect(text);
      }
  
      public void setFeature(String name, boolean state) throws IllegalArgumentException, IllegalStateException {
          xs.setFeature(name, state);
      }
  
      public void entityRef(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.entityRef(text);
      }
  
      public void processingInstruction(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.processingInstruction(text);
      }
  
      public void setOutput(Writer writer) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.setOutput(writer);
      }
  
      public void docdecl(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.docdecl(text);
      }
  
      public void flush() throws IOException {
          xs.flush();
      }
  
      public Object getProperty(String name) {
          return xs.getProperty(name);
      }
  
      public XmlSerializer startTag(String namespace, String name) throws IOException, IllegalArgumentException, IllegalStateException {
          return xs.startTag(namespace, name);
      }
  
      public void ignorableWhitespace(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.ignorableWhitespace(text);
      }
  
      public XmlSerializer text(String text) throws IOException, IllegalArgumentException, IllegalStateException {
          return xs.text(text);
      }
  
      public boolean getFeature(String name) {
          return xs.getFeature(name);
      }
  
      public XmlSerializer attribute(String namespace, String name, String value) throws IOException, IllegalArgumentException, IllegalStateException {
          return attribute(namespace, name, value);
      }
  
      public void startDocument(String encoding, Boolean standalone) throws IOException, IllegalArgumentException, IllegalStateException {
          xs.startDocument(encoding, standalone);
      }
  
      public String getPrefix(String namespace, boolean generatePrefix) throws IllegalArgumentException {
          return xs.getPrefix(namespace, generatePrefix);
      }
  
      public String getNamespace() {
          return xs.getNamespace();
      }
  
      public XmlSerializer endTag(String namespace, String name) throws IOException, IllegalArgumentException, IllegalStateException {
          return xs.endTag(namespace, name);
      }
  
      public XmlSerializer text(char[] buf, int start, int len) throws IOException, IllegalArgumentException, IllegalStateException {
          return xs.text(buf, start, len);
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/junit/TestXmlPullWrapper.java
  
  Index: TestXmlPullWrapper.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
  
  package org.xmlpull.v1.wrapper.junit;
  
  //import junit.framework.Test;
  import java.io.IOException;
  import java.io.StringReader;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  import org.xmlpull.v1.XmlPullParserFactory;
  import org.xmlpull.v1.util.XmlPullWrapper;
  
  /**
   * Test some wrapper utility operations.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class TestXmlPullWrapper extends TestCase {
      private XmlPullParserFactory factory;
  
      public static void main (String[] args) {
          junit.textui.TestRunner.run (new TestSuite(TestXmlPullWrapper.class));
      }
  
  
      public TestXmlPullWrapper(String name) {
          super(name);
      }
  
      protected void setUp() throws XmlPullParserException {
          factory = factory.newInstance();
          factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
          assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
          assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
      }
  
      public void testPI() throws IOException, XmlPullParserException {
          final String PI_TARGET = "xml-stylesheet";
          final String PI_DATA = "href='test.css' type='text/css'";
          testPI(PI_TARGET, " ", PI_DATA);
          testPI(PI_TARGET, "\n", PI_DATA);
          testPI(PI_TARGET, "\r", PI_DATA);
          testPI(PI_TARGET, "\t", PI_DATA);
          testPI(PI_TARGET, "  ", PI_DATA);
          testPI(PI_TARGET, "  \n \n\r\n", PI_DATA);
          testPI(PI_TARGET, " ", null);
          testPI(PI_TARGET, "  \n \n\r", null);
          testPI(PI_TARGET, null, null); // TODO FIXME add when next release of XPP3 is fixed
      }
  
      public void testPI(final String piTarget,
                         final String s,
                         final String piData)
          throws IOException, XmlPullParserException
      {
          final String PI = piTarget + (s != null ? s : "") + (piData != null ? piData : "");
          final String XML_TEST_PI =
              "<tag><?"+PI+"?></tag>";
          final String PI_NORMALIZED = normalized(PI);
  
          XmlPullParser pp = factory.newPullParser();
          XmlPullWrapper pw = new XmlPullWrapper(pp);
          pp.setInput(new StringReader(XML_TEST_PI));
  
          assertEquals(XmlPullParser.START_TAG, pp.next());
          assertEquals("tag", pp.getName());
          assertEquals(XmlPullParser.PROCESSING_INSTRUCTION, pp.nextToken());
          assertEquals(printable(PI_NORMALIZED), printable(pp.getText()));
          assertEquals(PI_NORMALIZED, pp.getText());
          assertEquals(printable(piTarget), printable(pw.getPITarget()));
          assertEquals(piTarget, pw.getPITarget());
          if(piData != null) {
              assertEquals(printable(piData), printable(pw.getPIData()));
              assertEquals(piData, pw.getPIData());
          }
          assertEquals(pp.next(), XmlPullParser.END_TAG);
          assertEquals("tag", pp.getName());
  
          assertEquals(pp.next(), XmlPullParser.END_DOCUMENT);
      }
  
      private static String printable(char ch) {
          if(ch == '\n') {
              return "\\n";
          } else if(ch == '\r') {
              return "\\r";
          } else if(ch == '\t') {
              return "\\t";
          } if(ch > 127 || ch < 32) {
              StringBuffer buf = new StringBuffer("\\u");
              String hex = Integer.toHexString((int)ch);
              for (int i = 0; i < 4-hex.length(); i++)
              {
                  buf.append('0');
              }
              buf.append(hex);
              return buf.toString();
          }
          return ""+ch;
      }
  
      private static String printable(String s) {
          if(s == null) return null;
          StringBuffer buf = new StringBuffer();
          for(int i = 0; i < s.length(); ++i) {
              buf.append(printable(s.charAt(i)));
          }
          s = buf.toString();
          return s;
      }
  
      private static String normalized(String s) {
          if(s == null) return null;
          StringBuffer buf = new StringBuffer();
          boolean seenCR = false;
          for(int i = 0; i < s.length(); ++i) {
              char ch = s.charAt(i);
              if(ch == '\r') {
                  buf.append('\n');
                  seenCR = true;
              } else if(ch == '\n') {
                  if( !seenCR ) {
                      buf.append(ch);
                  }
                  seenCR = false;
              } else {
                  buf.append(ch);
                  seenCR = false;
              }
          }
          s = buf.toString();
          return s;
      }
  
  }
  
  
  
  
  1.1                  xmlpull-api-v1/addons/java/wrapper/src/org/xmlpull/v1/wrapper/perftest/Driver.java
  
  Index: Driver.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
  
  package org.xmlpull.v1.wrapper.perftest;
  
  //import junit.framework.Test;
  import java.io.StringReader;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserFactory;
  import org.xmlpull.v1.util.XmlPullUtil;
  import org.xmlpull.v1.wrapper.XmlPullParserWrapper;
  import org.xmlpull.v1.wrapper.XmlPullWrapperFactory;
  
  /**
   * Test overhead of wrapper approach.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class Driver
  {
  
      public static void main (String[] args) throws Exception
      {
          final int PASSES = 20;
          final int REPEAT = 5 * 10000;
  
          System.err.println("starting tests with PASSES="+PASSES+" REPEAT="+REPEAT);
          long startDirect=-1,endDirect=-1;
          long startStaticWrap=-1,endStaticWrap=-1;
          XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
          factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
          XmlPullWrapperFactory staticWrapperFactory = XmlPullWrapperFactory.newInstance(factory);
  
          // multiple passes necessary to do some warmup to remove HotSpot influences ...
          for (int count = 0; count < PASSES; count++)
          {
              System.err.println("pass "+(count+1)+" of "+PASSES);
              startDirect = System.currentTimeMillis();
  
              XmlPullParser pp = factory.newPullParser();
              for (int i = 0; i < REPEAT; i++)
              {
                  StringReader sr = new StringReader("<hello>world!</hello>");
                  pp.setInput(sr);
                  pp.nextTag();
                  pp.require(XmlPullParser.START_TAG, null, "hello");
                  pp.next();
                  XmlPullUtil.nextEndTag(pp);
                  pp.require(XmlPullParser.END_TAG, null, "hello");
              }
              endDirect = System.currentTimeMillis();
              System.err.println("direct test took "+(endDirect-startDirect)/1000.0+" seconds");
  
              startStaticWrap = System.currentTimeMillis();
              XmlPullParserWrapper spw = staticWrapperFactory.newPullWrapper();
              for (int i = 0; i < REPEAT; i++)
              {
                  StringReader sr = new StringReader("<hello>world!</hello>");
                  spw.setInput(sr);
                  spw.nextTag();
                  spw.require(XmlPullParser.START_TAG, null, "hello");
                  spw.next();
                  spw.nextEndTag();
                  spw.require(XmlPullParser.END_TAG, null, "hello");
              }
              endStaticWrap = System.currentTimeMillis();
              System.err.println("static wrap test took "+(endStaticWrap-startStaticWrap)/1000.0+" seconds");
  
  
          }
  
          double directSecs = (endDirect - startDirect) /1000.0;
          //System.err.println("direct test took "+directSecs+" seconds");
  
          {
              double staticWrapSecs = (endStaticWrap - startStaticWrap) /1000.0;
              double staticSpeedup = staticWrapSecs / directSecs;
              double percent = ((long)Math.round((staticSpeedup - 1)* 100.0 * 100.0))/100.0;
              System.err.println("speedup when using direct over static wrap "+staticSpeedup+" ("+percent+"%)");
          }
  
          System.err.println("finished");
  
      }
  
  
  }
  
  
  
  


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading!
Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/cjB9SD/od7FAA/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/