You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by an...@locus.apache.org on 2000/09/26 20:59:48 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/scanners XMLDocumentScanner.java

andyc       00/09/26 11:59:46

  Modified:    java/src/org/apache/xerces/framework Tag: xerces_j_2
                        XMLStringBuffer.java
               java/src/org/apache/xerces/readers Tag: xerces_j_2
                        XMLEntityScanner.java
               java/src/org/apache/xerces/scanners Tag: xerces_j_2
                        XMLDocumentScanner.java
  Log:
  0) Implemented XMLStringBuffer.java
  1) Added preliminary code to scan an attribute for elements.
  2) Made modifications to the XMLEntityScanner#scanAttContent
     method:
  
    A) Pass in the open quote character so that it can be
       recognized as the end of the attribute.
    B) Return a boolean that is true if there is more attribute
       content. This is needed so that:
  
        i) long attribute content can be split, making the
           scanner more efficient
       ii) so that we can detect and keep track of entities
           appearing in attribute content
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.7   +57 -40    xml-xerces/java/src/org/apache/xerces/framework/Attic/XMLStringBuffer.java
  
  Index: XMLStringBuffer.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/framework/Attic/XMLStringBuffer.java,v
  retrieving revision 1.1.2.6
  retrieving revision 1.1.2.7
  diff -u -r1.1.2.6 -r1.1.2.7
  --- XMLStringBuffer.java	2000/09/26 18:39:31	1.1.2.6
  +++ XMLStringBuffer.java	2000/09/26 18:59:40	1.1.2.7
  @@ -58,8 +58,26 @@
   package org.apache.xerces.framework;
   
   /**
  + * XMLString is a structure used to pass character arrays. However,
  + * XMLStringBuffer is a buffer in which characters can be appended
  + * and extends XMLString so that it can be passed to methods
  + * expecting an XMLString object. This is a safe operation because
  + * it is assumed that any callee will <strong>not</strong> modify
  + * the contents of the XMLString structure.
  + * <p> 
  + * The contents of the string are managed by the string buffer. As
  + * characters are appended, the string buffer will grow as needed.
  + * <p>
  + * <strong>Note:</strong> Never set the <code>ch</code>, 
  + * <code>offset</code>, and <code>length</code> fields directly.
  + * These fields are managed by the string buffer. In order to reset
  + * the buffer, call <code>clear()</code>.
  + * 
    * @author Stubs generated by DesignDoc on Mon Sep 11 11:10:57 PDT 2000
  - * @version $Id: XMLStringBuffer.java,v 1.1.2.6 2000/09/26 18:39:31 ericye Exp $
  + * @author Andy Clark, IBM
  + * @author Eric Ye, IBM
  + *
  + * @version $Id: XMLStringBuffer.java,v 1.1.2.7 2000/09/26 18:59:40 andyc Exp $
    */
   public class XMLStringBuffer
       extends XMLString {
  @@ -68,11 +86,19 @@
       // Constants
       //
   
  -    /** SIZE */
  -    /** initial buffer size when constructed from the default constructor */
  -    public static final int SIZE = 16;
  +    /** Default buffer size (32). */
  +    public static final int DEFAULT_SIZE = 32;
   
       //
  +    // Data
  +    //
  +
  +    // private
  +
  +    /** One char buffer. */
  +    private char[] fOneCharBuffer = new char[1];
  +
  +    //
       // Constructors
       //
   
  @@ -80,10 +106,8 @@
        * 
        */
       public XMLStringBuffer() {
  -        ch = new char[SIZE];
  -        offset = 0;
  -        length = 0;
  -    }
  +        this(DEFAULT_SIZE);
  +    } // <init>()
   
       /**
        * 
  @@ -92,47 +116,43 @@
        */
       public XMLStringBuffer(int size) {
           ch = new char[size];
  -        offset = 0;
  -        length = 0;
  -    }
  +    } // <init>(int)
   
       //
  -    // Methods
  +    // Public methods
       //
   
  +    /** Clears the string buffer. */
  +    public void clear() {
  +        offset = 0;
  +        length = 0;
  +    }
  +
       /**
        * append
        * 
        * @param c 
        */
       public void append(char c) {
  -        if ( (length+1) > ch.length ) {
  -            char[] newArray = new char[ch.length+SIZE];
  -            System.arraycopy(ch, 0, newArray, 0, length);
  -            ch = newArray;
  -        }
  +        fOneCharBuffer[0] = c;
  +        append(fOneCharBuffer, 0, 1);
  +    } // append(char)
   
  -        ch[length++] = c;
  -    } // append
  -
       /**
        * append
        * 
        * @param s 
        */
       public void append(String s) {
  -        int len = s.length();
  -        if ( (length+len) > ch.length) {
  -            char[] newArray = new char[ch.length+len+SIZE];
  -            System.arraycopy(ch, 0, newArray, 0, length);
  -            ch = newArray;
  -        }
  -
  -        for (int i=0; i<len; i++) {
  -            ch[length+i] = s.charAt(i);
  +        int length = s.length();
  +        if (this.length + length > this.ch.length) {
  +            char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
  +            System.arraycopy(this.ch, 0, newch, 0, this.length);
  +            this.ch = newch;
           }
  -        length += len;
  -    } // append
  +        s.getChars(0, length, this.ch, this.length);
  +        length += length;
  +    } // append(String)
   
       /**
        * append
  @@ -142,17 +162,14 @@
        * @param length 
        */
       public void append(char[] ch, int offset, int length) {
  -        if ( (this.length+length) > ch.length) {
  -            char[] newArray = new char[ch.length+length+SIZE];
  -            System.arraycopy(this.ch, 0, newArray, 0, this.length);
  -            this.ch = newArray;
  -        }
  -
  -        for (int i=0; i<length; i++) {
  -            this.ch[this.length+i] = ch[offset+i];
  +        if (this.length + length > this.ch.length) {
  +            char[] newch = new char[this.ch.length + length + DEFAULT_SIZE];
  +            System.arraycopy(this.ch, 0, newch, 0, this.length);
  +            this.ch = newch;
           }
  +        System.arraycopy(ch, offset, this.ch, this.length, length);
           this.length += length;
  -    } // append
  +    } // append(char[],int,int)
   
       /**
        * append
  @@ -161,6 +178,6 @@
        */
       public void append(XMLString s) {
           append(s.ch, s.offset, s.length);
  -    } // append
  +    } // append(XMLString)
   
   } // class XMLStringBuffer
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.5   +4 -2      xml-xerces/java/src/org/apache/xerces/readers/Attic/XMLEntityScanner.java
  
  Index: XMLEntityScanner.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/readers/Attic/XMLEntityScanner.java,v
  retrieving revision 1.1.2.4
  retrieving revision 1.1.2.5
  diff -u -r1.1.2.4 -r1.1.2.5
  --- XMLEntityScanner.java	2000/09/25 19:49:16	1.1.2.4
  +++ XMLEntityScanner.java	2000/09/26 18:59:42	1.1.2.5
  @@ -65,7 +65,7 @@
   
   /**
    * @author Stubs generated by DesignDoc on Mon Sep 18 18:23:16 PDT 2000
  - * @version $Id: XMLEntityScanner.java,v 1.1.2.4 2000/09/25 19:49:16 jeffreyr Exp $
  + * @version $Id: XMLEntityScanner.java,v 1.1.2.5 2000/09/26 18:59:42 andyc Exp $
    */
   public class XMLEntityScanner
       implements Locator {
  @@ -203,10 +203,12 @@
       /**
        * scanAttContent
        * 
  +     * @param quote
        * @param content 
        */
  -    public void scanAttContent(XMLString content)
  +    public boolean scanAttContent(int quote, XMLString content)
           throws IOException {
  +        return false;
       } // scanAttContent
   
       /**
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.6   +160 -14   xml-xerces/java/src/org/apache/xerces/scanners/Attic/XMLDocumentScanner.java
  
  Index: XMLDocumentScanner.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/scanners/Attic/XMLDocumentScanner.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- XMLDocumentScanner.java	2000/09/26 03:15:37	1.1.2.5
  +++ XMLDocumentScanner.java	2000/09/26 18:59:45	1.1.2.6
  @@ -59,10 +59,13 @@
   
   import java.io.EOFException;
   import java.io.IOException;
  +import java.util.Enumeration;
   
   import org.apache.xerces.framework.XMLComponent;
   import org.apache.xerces.framework.XMLComponentManager;
   import org.apache.xerces.framework.XMLErrorReporter;
  +import org.apache.xerces.framework.XMLString;
  +import org.apache.xerces.framework.XMLStringBuffer;
   import org.apache.xerces.readers.XMLEntityManager;
   import org.apache.xerces.readers.XMLEntityScanner;
   import org.apache.xerces.utils.SymbolTable;
  @@ -72,6 +75,7 @@
   import org.xml.sax.SAXException;
   import org.xml.sax.SAXNotRecognizedException;
   import org.xml.sax.SAXNotSupportedException;
  +import org.xml.sax.helpers.NamespaceSupport;
   
   /**
    * This class is responsible for scanning XML document structure
  @@ -90,12 +94,32 @@
    * @author Stubs generated by DesignDoc on Mon Sep 11 11:10:57 PDT 2000
    * @author Andy Clark, IBM
    *
  - * @version $Id: XMLDocumentScanner.java,v 1.1.2.5 2000/09/26 03:15:37 andyc Exp $
  + * @version $Id: XMLDocumentScanner.java,v 1.1.2.6 2000/09/26 18:59:45 andyc Exp $
    */
   public class XMLDocumentScanner
       implements XMLComponent, XMLDocumentSource {
   
       //
  +    // Constants
  +    //
  +
  +    // sax2 features
  +
  +    /** Namespaces feature id. */
  +    protected static final String NAMESPACES = "http://xml.org/sax/features/namespaces";
  +
  +    // xerces properties
  +
  +    /** Symbol table property id. */
  +    protected static final String SYMBOL_TABLE = "http://apache.org/xml/properties/internal/symbol-table";
  +
  +    /** Error reporter property id. */
  +    protected static final String ERROR_REPORTER = "http://apache.org/xml/properties/internal/error-reporter";
  +
  +    /** Entity manager property id. */
  +    protected static final String ENTITY_MANAGER = "http://apache.org/xml/properties/internal/entity-manager";
  +
  +    //
       // Data
       //
   
  @@ -128,6 +152,9 @@
       /** Entity scanner. */
       protected XMLEntityScanner fEntityScanner;
   
  +    /** Namespace support. */
  +    protected NamespaceSupport fNamespaceSupport = new NamespaceSupport();
  +
       // private data
   
       /** Element QName. */
  @@ -139,6 +166,12 @@
       /** Element attributes. */
       private XMLAttributes fAttributes = new XMLAttributes();
   
  +    /** String buffer. */
  +    private XMLStringBuffer fStringBuffer = new XMLStringBuffer();
  +
  +    /** Content buffer. */
  +    private XMLString fContent = new XMLString();
  +
       //
       // Constructors
       //
  @@ -157,10 +190,14 @@
       public boolean scanDocument(boolean complete) 
           throws IOException, SAXException {
   
  -        // scan document
  +        // initialize scanner
           fEntityScanner = fEntityManager.getEntityScanner();
  +        fNamespaceSupport.reset();
  +
  +        // scan document
           scanDocument();
   
  +        // return success
           return true;
   
       } // scanDocument(boolean):boolean
  @@ -315,13 +352,15 @@
                   fEntityScanner.scanChar();
                   c = fEntityScanner.scanChar();
                   if (c != '>') {
  +                    // REVISIT: report error
                       throw new SAXException("expected '>'");
                   }
                   empty = true;
                   break;
               }
               else if (!XMLChar.isNameStartChar(c)) {
  -                throw new SAXException("expected attribute name");
  +                // REVISIT: report error
  +                throw new SAXException("expected attribute name, found '"+(char)c+'\'');
               }
                   
               // attributes
  @@ -329,9 +368,13 @@
   
           } while (true);
   
  -        // bind namespaces
  +        // handle namespaces
           String uri = null;
           if (fNamespaces) {
  +            // push namespace context
  +            fNamespaceSupport.pushContext();
  +
  +            // bind namespaces
               bindNamespaces(fElemQName, fAttributes);
               uri = fElemQName.uri;
           }
  @@ -367,15 +410,18 @@
               fEntityScanner.skipSpaces();
               int c = fEntityScanner.scanChar();
               if (c != '/') {
  +                // REVISIT: report error
                   throw new SAXException("expected '/'");
               }
               c = fEntityScanner.scanChar();
               if (c != '>') {
  +                // REVISIT: report error
                   throw new SAXException("expected '>'");
               }
   
  -            // verify
  +            // tags must be balanced
               if (!fElemQName.rawname.equals(rawname)) {
  +                // REVISIT: report error
                   throw new SAXException("expected \""+rawname+"\""+
                                          " but found \""+fElemQName.rawname+'"');
               }
  @@ -386,6 +432,21 @@
               fDocumentHandler.endElement(fElemQName);
           }
   
  +        // handle namespaces
  +        if (fNamespaces) {
  +            // call handler
  +            if (fDocumentHandler != null) {
  +                Enumeration prefixes = fNamespaceSupport.getDeclaredPrefixes();
  +                while (prefixes.hasMoreElements()) {
  +                    String prefix = (String)prefixes.nextElement();
  +                    fDocumentHandler.endPrefixMapping(prefix);
  +                }
  +            }
  +
  +            // pop context
  +            fNamespaceSupport.popContext();
  +        }
  +
       } // scanElement()
   
       /** 
  @@ -405,6 +466,51 @@
       protected void scanAttribute(XMLAttributes attributes) 
           throws IOException, SAXException {
   
  +        // name
  +        if (fNamespaces) {
  +            fEntityScanner.scanQName(fAttrQName);
  +        }
  +        else {
  +            String name = fEntityScanner.scanName();
  +            fAttrQName.setValues(null, name, name, null);
  +        }
  +
  +        // equals
  +        fEntityScanner.skipSpaces();
  +        int equals = fEntityScanner.scanChar();
  +        if (equals != '=') {
  +            // REVISIT: report error
  +            throw new SAXException("expected '=', found '"+(char)equals+'\'');
  +        }
  +        fEntityScanner.skipSpaces();
  +
  +        // quote
  +        int oquote = fEntityScanner.scanChar();
  +        if (oquote != '\'' || oquote != '"') {
  +            // REVISIT: report error
  +            throw new SAXException("expected open quote");
  +        }
  +
  +        // content
  +        fStringBuffer.clear();
  +        attributes.setAttribute(fAttrQName, "CDATA", fStringBuffer);
  +        while (fEntityScanner.scanAttContent(oquote, fContent)) {
  +            fStringBuffer.append(fContent);
  +            /***
  +            if (fEntityScanner.peekChar() == '&') {
  +                // TODO: handle entities in value
  +            }
  +            /***/
  +        }
  +        attributes.setValue(attributes.getLength() - 1, fStringBuffer);
  +
  +        // quote
  +        int cquote = fEntityScanner.scanChar();
  +        if (cquote != cquote) {
  +            // REVISIT: report error
  +            throw new SAXException("expected close quote");
  +        }
  +
       } // scanAttribute()
   
       /**
  @@ -435,16 +541,56 @@
        * searchs for namespace binding attributes and adds the URI
        * information to the element and attribute qualified names.
        */
  -    protected void bindNamespaces(QName element, XMLAttributes attributes) {
  +    protected void bindNamespaces(QName element, XMLAttributes attributes) 
  +        throws SAXException {
   
           // search for new namespace bindings
  -        // TODO
  +        int length = attributes.getLength();
  +        for (int i = 0; i < length; i++) {
  +            String rawname = attributes.getQName(i);
  +            if (rawname.startsWith("xmlns")) {
  +                // declare prefix in context
  +                String prefix = rawname.length() > 5
  +                              ? attributes.getLocalName(i) : "";
  +                String uri = attributes.getValue(i);
  +                fNamespaceSupport.declarePrefix(prefix, uri);
  +
  +                // call handler
  +                if (fDocumentHandler != null) {
  +                    fDocumentHandler.startPrefixMapping(prefix, uri);
  +                }
  +            }
  +        }
   
           // bind the element
  -        // TODO
  +        element.uri = fNamespaceSupport.getURI(element.prefix);
  +        if (element.prefix != null && element.uri == null) {
  +            // REVISIT: report error
  +            throw new SAXException("element prefix \""+element.prefix+"\" not bound");
  +        }
   
           // bind the attributes
  -        // TODO
  +        for (int i = 0; i < length; i++) {
  +            attributes.getName(i, fAttrQName);
  +            String rawname = attributes.getQName(i);
  +            if (rawname.startsWith("xmlns")) {
  +                fAttrQName.uri = NamespaceSupport.XMLNS;
  +            }
  +            else {
  +                if (fAttrQName.prefix != null) {
  +                    fAttrQName.uri = fNamespaceSupport.getURI(fAttrQName.prefix);
  +                    if (fAttrQName.uri == null) {
  +                        // REVISIT: report error
  +                        throw new SAXException("attribute prefix \""+fAttrQName.prefix+"\" is not bound");
  +                    }
  +                }
  +                else {
  +                    // attributes with no prefix get element's uri
  +                    fAttrQName.uri = element.uri;
  +                }
  +            }
  +            attributes.setName(i, fAttrQName);
  +        }
   
       } // bindNamespaces(QName,XMLAttributes)
   
  @@ -464,13 +610,13 @@
           throws SAXException {
   
           // get features
  -        fNamespaces = componentManager.getFeature("http://xml.org/sax/features/namespaces");
  +        fNamespaces = componentManager.getFeature(NAMESPACES);
           fAttributes.setNamespaces(fNamespaces);
  -        
  +
           // get properties
  -        fSymbolTable = (SymbolTable)componentManager.getProperty("http://apache.org/xml/properties/internal/symbol-table");
  -        fErrorReporter = (XMLErrorReporter)componentManager.getProperty("http://apache.org/xml/properties/internal/error-reporter");
  -        fEntityManager = (XMLEntityManager)componentManager.getProperty("http://apache.org/xml/properties/internal/entity-manager");
  +        fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE);
  +        fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
  +        fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER);
           /*** REVISIT: Add DTD support. ***
           fDTDScanner = (XMLDTDScanner)componentManager.getProperty("http://apache.org/xml/properties/internal/dtd-scanner");
           /***/