mrglavas    2004/02/03 15:18:53

  Modified:    java/src/org/apache/xerces/parsers AbstractSAXParser.java
  Log:
  Fixing 2 bugs and improving performance.
  
  Bug #1: With namespace support turned off the parser
  emitted prefix mappings for the XML namespace 
  http://www.w3.org/XML/1998/namespace and the 
  XML namespace declaration namespace 
  http://www.w3.org/2000/xmlns/ for every element. It
  shouldn't emit these ever, especially when namespaces
  are off. This had been broken since Xerces 2.3.0.
  
  Bug #2: With namespace support turned off the SAX 
  parser was removing attributes named 'xmlns' from an
  attribute list even though this is just a regular attribute 
  when namespace support is not enabled. It seems like
  this bug was always been in Xerces2. Probably nobody's
  noticed it because it's unwise to name a regular
  attribute 'xmlns'.
  
  Performance Fix: The parser always iterated over the list
  of attributes on startElement to either remove all namespace 
  declaration attributes, or remove their namespace URI and 
  localname if the namespace-prefixes feature was on. This 
  search even occurred when namespace support is off. We
  now check whether there are local namespace declarations
  by querying the namespace context to prevent looping over
  the list if we don't have to.
  
  Revision  Changes    Path
  1.47      +60 -48    
xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java
  
  Index: AbstractSAXParser.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- AbstractSAXParser.java    26 Jan 2004 17:28:09 -0000      1.46
  +++ AbstractSAXParser.java    3 Feb 2004 23:18:53 -0000       1.47
  @@ -443,34 +443,41 @@
               }
   
               // SAX2
  -            if (fContentHandler != null) {
  -                
  -                // send prefix mapping events
  -                startNamespaceMapping();
  +            if (fContentHandler != null) {                
                   
  -                fAugmentations = augs;
  -
  -                int len = attributes.getLength();
  -                for (int i = len - 1; i >= 0; i--) {
  -                    attributes.getName(i, fQName);
  -
  -                    if ((fQName.prefix != null && fQName.prefix.equals("xmlns")) || 
  -                        fQName.rawname.equals("xmlns")) {
  -                        if (!fNamespacePrefixes) {
  -                            // remove namespace declaration attributes
  -                            attributes.removeAttributeAt(i);
  +                if (fNamespaces) {
  +                    // send prefix mapping events
  +                    int count = startNamespaceMapping();
  +
  +                    // If there were no new namespaces declared
  +                    // then we can skip searching the attribute list
  +                    // for namespace declarations.
  +                    if (count > 0) {
  +                        int len = attributes.getLength();
  +                        for (int i = len - 1; i >= 0; i--) {
  +                            attributes.getName(i, fQName);
  +
  +                            if ((fQName.prefix != null && 
fQName.prefix.equals("xmlns")) || 
  +                                fQName.rawname.equals("xmlns")) {
  +                                if (!fNamespacePrefixes) {
  +                                    // remove namespace declaration attributes
  +                                    attributes.removeAttributeAt(i);
  +                                }
  +                                else {
  +                                    // localpart should be empty string as per SAX 
documentation:
  +                                    // 
http://www.saxproject.org/?selected=namespaces
  +                                    fQName.prefix = "";
  +                                    fQName.uri = "";
  +                                    fQName.localpart = "";
  +                                    attributes.setName(i, fQName);
  +                                }
  +                            }
                           }
  -                        if (fNamespaces && fNamespacePrefixes) {
  -                            // localpart should be empty string as per SAX 
documentation:
  -                            // http://www.saxproject.org/?selected=namespaces
  -                            fQName.prefix = "";
  -                            fQName.uri = "";
  -                            fQName.localpart = "";
  -                            attributes.setName(i, fQName);
  -                        }
  -                    } 
  +                    }
                   }
                   
  +                fAugmentations = augs;
  +                
                   String uri = element.uri != null ? element.uri : "";
                   String localpart = fNamespaces ? element.localpart : "";
                   fAttributesProxy.setAttributes(attributes);
  @@ -576,7 +583,9 @@
                   String localpart = fNamespaces ? element.localpart : "";
                   fContentHandler.endElement(uri, localpart,
                                              element.rawname);
  -                endNamespaceMapping();
  +                if (fNamespaces) {
  +                    endNamespaceMapping();
  +                } 
               }
           }
           catch (SAXException e) {
  @@ -1890,33 +1899,36 @@
           return fLexicalHandler;
       } // getLexicalHandler():LexicalHandler
   
  -     /**
  -      * Send startPrefixMapping events
  -      */
  -     protected final void startNamespaceMapping() throws SAXException{
  -             int count = fNamespaceContext.getDeclaredPrefixCount();
  -             if (count > 0) {
  -                     String prefix = null;
  +    /**
  +     * Send startPrefixMapping events
  +     */
  +    protected final int startNamespaceMapping() throws SAXException{
  +        int count = fNamespaceContext.getDeclaredPrefixCount();
  +        if (count > 0) {
  +            String prefix = null;
               String uri = null;
  -                     for (int i = 0; i < count; i++) {
  -                             prefix = fNamespaceContext.getDeclaredPrefixAt(i);
  +            for (int i = 0; i < count; i++) {
  +                prefix = fNamespaceContext.getDeclaredPrefixAt(i);
                   uri = fNamespaceContext.getURI(prefix);
  -                             fContentHandler.startPrefixMapping(prefix, 
  -                (uri == null)?"":uri);
  -                     }
  -             }
  -     }
  +                fContentHandler.startPrefixMapping(prefix, 
  +                    (uri == null) ? "" : uri);
  +            }
  +        }
  +        return count;
  +    }
  +    
       /**
        * Send endPrefixMapping events
        */
  -     protected final void endNamespaceMapping() throws SAXException {
  -             int count = fNamespaceContext.getDeclaredPrefixCount();
  -             if (count > 0) {
  -                     for (int i = 0; i < count; i++) {
  -                     
fContentHandler.endPrefixMapping(fNamespaceContext.getDeclaredPrefixAt(i));
  -                     }
  -             }
  -     }
  +    protected final void endNamespaceMapping() throws SAXException {
  +        int count = fNamespaceContext.getDeclaredPrefixCount();
  +        if (count > 0) {
  +            for (int i = 0; i < count; i++) {
  +                
fContentHandler.endPrefixMapping(fNamespaceContext.getDeclaredPrefixAt(i));
  +            }
  +        }
  +    }
  +     
       //
       // XMLDocumentParser methods
       //
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to