CVS Update: xmlpull-api-v1/src/java/sax2_driver/org/xmlpull/v1/sax2

Aleksander Andrzej Slominski <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       02/09/23 11:48:58

  Modified:    src/java/sax2_driver/org/xmlpull/v1/sax2 Driver.java
  Added:       src/java/sax2_driver/org/xmlpull/v1/sax2
                        AttributesCachingDriver.java
  Log:
  added patch by Holger Krug to SAX2 Driver to allow modification in sub classes
  of how Attributes in startElement() are kept. Added also AttributesCachingDriver
  that extends SAX2 Driver and creates a new copy of Attributes for each startElement()
  so provided Attributes will not change during parsing and can be kept indefinitely.
  
  Revision  Changes    Path
  1.3       +29 -18    xmlpull-api-v1/src/java/sax2_driver/org/xmlpull/v1/sax2/Driver.java
  
  Index: Driver.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/sax2_driver/org/xmlpull/v1/sax2/Driver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -t -w -r1.2 -r1.3
  --- Driver.java	2002/08/27 03:28:00	1.2
  +++ Driver.java	2002/09/23 16:48:58	1.3
  @@ -37,6 +37,14 @@
   import org.xmlpull.v1.XmlPullParserException;
   import org.xmlpull.v1.XmlPullParserFactory;
   
  +/**
  + * SAX2 Driver that puslls events from XmlPullParser
  + * and comverts them into SAX2 callbacks.
  + *
  + * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
  + * @author <a href="mailto:[email protected]">Holger Krug</a>
  + */
  +
   public class Driver implements Locator, XMLReader, Attributes
   {
   
  @@ -70,11 +78,6 @@
   
       //private final static boolean DEBUG = false;
   
  -    // use in parse sub-tree - exposed to resue more efficiently
  -    //private char[] buf = new char[1024];
  -    //private String[] namespaces = new String[5];
  -    //private String[] prefixes = new String[5];
  -
       /**
        */
       public Driver() throws XmlPullParserException {
  @@ -197,9 +200,6 @@
                       pp.setFeature(pp.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, value);
                   }
               } else if(VALIDATION_FEATURE.equals(name)) {
  -                //              if(true == value) {
  -                //                  throw new SAXNotSupportedException("validation is not supported");
  -                //              }
                   pp.setFeature(pp.FEATURE_VALIDATION, value);
                   //          } else if(APACHE_SCHEMA_VALIDATION_FEATURE.equals(name)) {
                   //              // can ignore as validation must be false ...
  @@ -244,7 +244,7 @@
               try {
                   pp.setProperty(name, value);
               } catch(XmlPullParserException ex) {
  -                throw new SAXNotRecognizedException("not recognized set property "+name+" : "+ ex);
  +                throw new SAXNotSupportedException("not supported set property "+name+": "+ ex);
               }
               //throw new SAXNotRecognizedException("not recognized set property "+name);
           }
  @@ -392,15 +392,13 @@
                                   rawName.append(':');
                                   rawName.append(name);
                               }
  -                            contentHandler.startElement(pp.getNamespace(),
  +                            startElement(pp.getNamespace(),
                                                           name,
  -                                                        prefix != null ? name : rawName.toString(),
  -                                                        this);
  +                                         prefix != null ? name : rawName.toString());
                           } else {
  -                            contentHandler.startElement(pp.getNamespace(),
  -                                                        pp.getName(),
  +                            startElement(pp.getNamespace(),
                                                           pp.getName(),
  -                                                        this);
  +                                         pp.getName());
                           }
                           //++level;
   
  @@ -431,7 +429,7 @@
                               int depth = pp.getDepth();
                               int countPrev =
                                   (level > depth) ? pp.getNamespaceCount(pp.getDepth()) : 0;
  -                            int count = pp.getNamespaceCount(pp.getDepth() + 1);
  +                            int count = pp.getNamespaceCount(pp.getDepth() - 1);
                               // undeclare them in reverse order
                               for (int i = count - 1; i >= countPrev; i--)
                               {
  @@ -459,5 +457,18 @@
           }
       }
   
  +    /**
  +     * Calls {@link ContentHandler.startElement(String, String, String, Attributes) startElement}
  +     * on the <code>ContentHandler</code> with <code>this</code> driver object as the
  +     * {@link Attributes} implementation. In default implementation
  +     * {@link Attributes} object is valid only during this method call and may not
  +     * be stored. Sub-classes can overwrite this method to cache attributes.
  +     */
  +    protected void startElement(String namespace, String localName, String qName) throws SAXException {
  +        contentHandler.startElement(namespace, localName, qName, this);
  +    }
  +
   }
  +
  +
   
  
  
  
  1.1                  xmlpull-api-v1/src/java/sax2_driver/org/xmlpull/v1/sax2/AttributesCachingDriver.java
  
  Index: AttributesCachingDriver.java
  ===================================================================
  /* -*- mode: Java; c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  
  package org.xmlpull.v1.sax2;
  
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.AttributesImpl;
  
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  
  /**
   * Extensions of SAX2 Driver that provides a separate Attributes object
   * for each startElement call so it is safe to keep the reference to Attributes.
   *
   * @author <a href="mailto:[email protected]">Holger Krug</a>
   */
  public class AttributesCachingDriver extends Driver
  {
      public AttributesCachingDriver() throws XmlPullParserException {
          super();
      }
  
      public AttributesCachingDriver(XmlPullParser pp) throws XmlPullParserException {
          super(pp);
      }
  
      /**
       * Calls {@link ContentHandler.startElement(String, String,
       * String, Attributes) startElement} on the
       * <code>ContentHandler</code> with copied attribute values. The
       * {@link Attributes} object is may be stored.
       */
      protected void startElement(String namespace, String localName, String qName) throws SAXException {
          contentHandler.startElement(namespace, localName, qName, new AttributesImpl(this));
      }
  
  }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.