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/06/08 03:42:55 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/utils MessageFormatter.java NamespaceScope.java QName.java SymbolHasher.java SymbolTable.java

andyc       00/06/07 18:42:55

  Modified:    java/src/org/apache/xerces/utils Tag: xerces_j_2
                        MessageFormatter.java NamespaceScope.java
                        QName.java SymbolHasher.java SymbolTable.java
  Log:
  Code update.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.4   +23 -7     xml-xerces/java/src/org/apache/xerces/utils/Attic/MessageFormatter.java
  
  Index: MessageFormatter.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/Attic/MessageFormatter.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- MessageFormatter.java	2000/06/08 00:09:28	1.1.2.3
  +++ MessageFormatter.java	2000/06/08 01:42:54	1.1.2.4
  @@ -57,24 +57,40 @@
   
   package org.apache.xerces.utils;
   
  +import java.util.Locale;
  +import java.util.MissingResourceException;
  +
   /**
  + * This interface provides a generic message formatting mechanism and
  + * is useful for producing messages that must be localed and/or formatted
  + * with replacement text.
  + *
  + * @see org.apache.xerces.framework.ErrorReporter
  + *
    * @author Stubs generated by DesignDoc on Wed Jun 07 11:58:44 PDT 2000
  - * @version $Id: MessageFormatter.java,v 1.1.2.3 2000/06/08 00:09:28 andyc Exp $
  + * @author Andy Clark
  + *
  + * @version $Id: MessageFormatter.java,v 1.1.2.4 2000/06/08 01:42:54 andyc Exp $
    */
   public interface MessageFormatter {
   
       //
  -    // Methods
  +    // MessageFormatter methods
       //
   
       /**
  -     * formatMessage
  +     * Formats a message with the specified arguments using the given
  +     * locale information.
        * 
  -     * @param locale 
  -     * @param key 
  -     * @param arguments 
  +     * @param locale    The locale of the message.
  +     * @param key       The message key.
  +     * @param arguments The message replacement text arguments. The order
  +     *                  of the arguments must match the 
        * 
  -     * @return 
  +     * @return Returns the formatted message.
  +     *
  +     * @throws MissingResourceException Thrown if the message with the
  +     *                                  specified key cannot be found.
        */
       public String formatMessage(Locale locale, String key, Object[] arguments)
           throws MissingResourceException;
  
  
  
  1.1.2.4   +105 -27   xml-xerces/java/src/org/apache/xerces/utils/Attic/NamespaceScope.java
  
  Index: NamespaceScope.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/Attic/NamespaceScope.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- NamespaceScope.java	2000/06/08 00:09:28	1.1.2.3
  +++ NamespaceScope.java	2000/06/08 01:42:54	1.1.2.4
  @@ -58,8 +58,25 @@
   package org.apache.xerces.utils;
   
   /**
  + * Convenience class for keeping track of namespace information. This
  + * class allows the user to bind namespace URIs to qname prefixes
  + * that relates to the namespace depth. New namespace bindings at
  + * increasing depths take precedence, when queried, over earlier
  + * bindings. Namespace bindings are then popped off of the stack as 
  + * the depth decreases below the depth where the namespace prefix 
  + * was bound.
  + * <p>
  + * To be used correctly, the strings must be identical references for
  + * equal strings. Within the parser, these values are considered symbols
  + * and should always be retrieved from the <code>SymbolTable</code>.
  + *
  + * @see SymbolTable
  + *
  + * @author Glenn Marcy
    * @author Stubs generated by DesignDoc on Wed Jun 07 11:58:44 PDT 2000
  - * @version $Id: NamespaceScope.java,v 1.1.2.3 2000/06/08 00:09:28 andyc Exp $
  + * @author Andy Clark
  + *
  + * @version $Id: NamespaceScope.java,v 1.1.2.4 2000/06/08 01:42:54 andyc Exp $
    */
   public class NamespaceScope {
   
  @@ -67,53 +84,114 @@
       // Data
       //
   
  -    /** fDepth */
  +    /** The namespace depth. */
       protected int fDepth;
   
  +    /** The namespace mappings. */
  +    private Bindings[] fMappings = new Bindings[8];
  +
       //
       // Constructors
       //
   
  -    /**
  -     * 
  -     */
  -    public NamespaceScope() {
  -    }
  +    /** Constructs a namespace scope. */
  +    public NamespaceScope() {}
   
       //
  -    // Methods
  +    // Public methods
       //
   
  -    /**
  -     * increaseDepth
  -     */
  +    /** Increases the namespace depth. */
       public void increaseDepth() {
  -    } // increaseDepth
  +        fDepth++;
  +        if (fDepth == fMappings.length) {
  +            Bindings[] newMappings = new Bindings[fDepth + 8];
  +            System.arraycopy(fMappings, 0, newMappings, 0, fDepth);
  +            fMappings = newMappings;
  +        }
  +        if (fMappings[fDepth] == null)
  +            fMappings[fDepth] = new Bindings();
  +    }
   
  -    /**
  -     * decreaseDepth
  -     */
  +    /** Decreases the namespace depth. */
       public void decreaseDepth() {
  -    } // decreaseDepth
  +        fDepth--;
  +    }
   
       /**
  -     * setNamespaceForPrefix
  +     * Sets the namespace URI for the specified prefix.
        * 
  -     * @param prefix 
  -     * @param namespace 
  +     * @param prefix The namespace prefix.
  +     * @param uri    The namespace URI.
        */
  -    public void setNamespaceForPrefix(String prefix, String namespace) {
  -    } // setNamespaceForPrefix
  +    public void setURIForPrefix(String prefix, String uri) {
  +
  +        // check length
  +        Bindings bindings = fMappings[fDepth];
  +        int offset = bindings.offset;
  +        if (offset == bindings.info.length) {
  +            String[] newInfo = new String[offset + 8];
  +            System.arraycopy(bindings.info, 0, newInfo, 0, offset);
  +            bindings.info = newInfo;
  +        }
  +
  +        // set information
  +        bindings.info[offset++] = prefix;
  +        bindings.info[offset++] = uri;
  +        bindings.offset = offset;
   
  +    } // setURIForPrefix(String,String)
  +
       /**
  -     * getNamespaceForPrefix
  -     * 
  -     * @param prefix 
  +     * Returns the namespace URI that is bound to the specified prefix,
  +     * or <code>null</code> if no such binding exists.
        * 
  -     * @return 
  +     * @param prefix The namespace prefix to search for.
        */
  -    public String getNamespaceForPrefix(String prefix) {
  +    public String getURIForPrefix(String prefix) {
  +
  +        // search for binding
  +        for (int depth = fDepth; depth >= 0; depth--) {
  +            Bindings bindings = fMappings[depth];
  +            int offset = bindings.offset;
  +            for (int i = 1; i < offset; i += 2) {
  +                if (prefix == bindings.info[i]) {
  +                    return bindings.info[i + 1];
  +                }
  +            }
  +        }
  +
  +        // not found
           return null;
  -    } // getNamespaceForPrefix
  +
  +    } // getURIForPrefix(String):String
  +
  +    //
  +    // Classes
  +    //
  +
  +    /**
  +     * This class represents the namespace bindings at a particular depth.
  +     */
  +    protected static final class Bindings {
  +
  +        //
  +        // Data
  +        //
  +
  +        /** Offset to the next available binding slot. */
  +        public int offset = 0;
  +
  +        /** The binding information. */
  +        public String[] info = new String[8];
  +
  +        //
  +        // Constructors
  +        //
  +
  +        /** Default constructor. */
  +        public Bindings() {}
  +
  +    } // class Bindings
   
   } // class NamespaceScope
  
  
  
  1.2.2.5   +48 -18    xml-xerces/java/src/org/apache/xerces/utils/QName.java
  
  Index: QName.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/QName.java,v
  retrieving revision 1.2.2.4
  retrieving revision 1.2.2.5
  diff -u -r1.2.2.4 -r1.2.2.5
  --- QName.java	2000/06/08 00:09:28	1.2.2.4
  +++ QName.java	2000/06/08 01:42:54	1.2.2.5
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 1999,2000 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2000 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,8 +58,19 @@
   package org.apache.xerces.utils;
   
   /**
  + * A structure that holds the components of an XML Namespaces qualified
  + * name.
  + * <p>
  + * To be used correctly, the strings must be identical references for
  + * equal strings. Within the parser, these values are considered symbols
  + * and should always be retrieved from the <code>SymbolTable</code>.
  + *
  + * @see SymbolTable
  + *
    * @author Stubs generated by DesignDoc on Wed Jun 07 11:58:44 PDT 2000
  - * @version $Id: QName.java,v 1.2.2.4 2000/06/08 00:09:28 andyc Exp $
  + * @author Andy Clark
  + *
  + * @version $Id: QName.java,v 1.2.2.5 2000/06/08 01:42:54 andyc Exp $
    */
   public class QName {
   
  @@ -67,37 +78,56 @@
       // Data
       //
   
  -    /** prefix */
  +    /** 
  +     * The qname prefix. For example, the prefix for the qname "a:foo"
  +     * is "a".
  +     */
       public String prefix;
   
  -    /** localpart */
  +    /** 
  +     * The qname localpart. For example, the localpart for the qname "a:foo"
  +     * is "foo".
  +     */
       public String localpart;
   
  -    /** rawname */
  +    /** 
  +     * The qname rawname. For example, the rawname for the qname "a:foo"
  +     * is "a:foo".
  +     */
       public String rawname;
   
  -    /** uri */
  +    /** 
  +     * The URI to which the qname prefix is bound. This binding must be
  +     * performed by a XML Namespaces aware processor.
  +     */
       public String uri;
   
       //
  -    // Methods
  +    // Public methods
       //
   
       /**
  -     * setValues
  +     * Convenience method to set the values of the qname components.
        * 
  -     * @param prefix 
  -     * @param localpart 
  -     * @param rawname 
  -     * @param uri 
  +     * @param prefix    The qname prefix. (e.g. "a")
  +     * @param localpart The qname localpart. (e.g. "foo")
  +     * @param rawname   The qname rawname. (e.g. "a:foo")
  +     * @param uri       The URI binding. (e.g. "http://foo.com/mybinding")
        */
  -    public void setValues(String prefix, String localpart, String rawname, String uri) {
  -    } // setValues
  +    public void setValues(String prefix, String localpart, String rawname, 
  +                          String uri) {
  +        this.prefix = prefix;
  +        this.localpart = localpart;
  +        this.rawname = rawname;
  +        this.uri = uri;
  +    }
   
  -    /**
  -     * clear
  -     */
  +    /** Clears the values of the qname components. */
       public void clear() {
  -    } // clear
  +        prefix = null;
  +        localpart = null;
  +        rawname = null;
  +        uri = null;
  +    }
   
   } // class QName
  
  
  
  1.1.2.4   +26 -13    xml-xerces/java/src/org/apache/xerces/utils/Attic/SymbolHasher.java
  
  Index: SymbolHasher.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/Attic/SymbolHasher.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- SymbolHasher.java	2000/06/08 00:09:28	1.1.2.3
  +++ SymbolHasher.java	2000/06/08 01:42:54	1.1.2.4
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 1999,2000 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2000 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,32 +58,45 @@
   package org.apache.xerces.utils;
   
   /**
  + * This interface defines a mechanism to produce hashcodes for symbols.
  + * Hashcode values returned by the <code>hash</code> methods in this
  + * interface do not have to be unique but <strong>must</strong> be
  + * identical regardless of which <code>hash</code> method is called
  + * for an equal symbol.
  + *
  + * @see SymbolTable
  + *
    * @author Stubs generated by DesignDoc on Wed Jun 07 11:58:44 PDT 2000
  - * @version $Id: SymbolHasher.java,v 1.1.2.3 2000/06/08 00:09:28 andyc Exp $
  + * @author Andy Clark
  + *
  + * @version $Id: SymbolHasher.java,v 1.1.2.4 2000/06/08 01:42:54 andyc Exp $
    */
   public interface SymbolHasher {
   
       //
  -    // Methods
  +    // SymbolHasher methods
       //
   
       /**
  -     * hash
  +     * Returns a hashcode value for the specified symbol. The value
  +     * returned by this method must be identical to the value returned
  +     * by the <code>hash(char[],int,int)</code> method when called
  +     * with the character array that comprises the symbol string.
        * 
  -     * @param symbol 
  -     * 
  -     * @return 
  +     * @param symbol The symbol to hash.
        */
       public int hash(String symbol);
   
       /**
  -     * hash
  -     * 
  -     * @param buffer 
  -     * @param offset 
  -     * @param length 
  +     * Returns a hashcode value for the specified symbol information. 
  +     * The value returned by this method must be identical to the value
  +     * returned by the <code>hash(String)</code> method when called
  +     * with the string object created from the symbol information.
        * 
  -     * @return 
  +     * @param buffer The character buffer containing the symbol.
  +     * @param offset The offset into the character buffer of the start
  +     *               of the symbol.
  +     * @param length The length of the symbol.
        */
       public int hash(char[] buffer, int offset, int length);
   
  
  
  
  1.1.2.4   +192 -14   xml-xerces/java/src/org/apache/xerces/utils/Attic/SymbolTable.java
  
  Index: SymbolTable.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/Attic/SymbolTable.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- SymbolTable.java	2000/06/08 00:09:28	1.1.2.3
  +++ SymbolTable.java	2000/06/08 01:42:54	1.1.2.4
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 1999,2000 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2000 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,34 +58,73 @@
   package org.apache.xerces.utils;
   
   /**
  + * This class is a symbol table implementation that guarantees that
  + * strings used as identifiers are unique references. Multiple calls
  + * to <code>addSymbol</code> will always return the same string
  + * reference.
  + * <p>
  + * The symbol table performs the same task as <code>String.intern()</code>
  + * with the following differences:
  + * <ul>
  + *  <li>
  + *   A new string object does not need to be created in order to
  + *   retrieve a unique reference. Symbols can be added by using
  + *   a series of characters in a character array.
  + *  </li>
  + *  <li>
  + *   Users of the symbol table can provide their own symbol hashing
  + *   implementation. For example, a simple string hashing algorithm
  + *   may fail to produce a balanced set of hashcodes for symbols 
  + *   that are <em>mostly</em> unique. Strings with similar leading
  + *   characters are especially prone to this poor hashing behavior.
  + *  </li>
  + * </ul>
  + *
  + * @see SymbolHasher
  + *
    * @author Stubs generated by DesignDoc on Wed Jun 07 11:58:44 PDT 2000
  - * @version $Id: SymbolTable.java,v 1.1.2.3 2000/06/08 00:09:28 andyc Exp $
  + * @author Andy Clark
  + *
  + * @version $Id: SymbolTable.java,v 1.1.2.4 2000/06/08 01:42:54 andyc Exp $
    */
   public class SymbolTable {
   
       //
  +    // Constants
  +    //
  +
  +    /** Default table size. */
  +    protected static final int TABLE_SIZE = 101;
  +
  +    //
       // Data
       //
   
  -    /** fSymbolHasher */
  +    /** Symbol hasher. */
       protected SymbolHasher fSymbolHasher;
   
  +    /** Buckets. */
  +    protected Entry[] fBuckets = new Entry[TABLE_SIZE];
  +
       //
       // Constructors
       //
   
  -    /**
  -     * 
  +    /** 
  +     * Constructs a symbol table that uses the default hashing
  +     * algorithm.
        */
       public SymbolTable() {
  +        this(new Hasher());
       }
   
       /**
  +     * Constructs a symbol table with the specified symbol hasher.
        * 
  -     * 
  -     * @param symbolHasher 
  +     * @param symbolHasher The symbol hasher to use.
        */
       public SymbolTable(SymbolHasher symbolHasher) {
  +        fSymbolHasher = symbolHasher;
       }
   
       //
  @@ -98,8 +137,8 @@
        * @return 
        */
       public SymbolHasher getSymbolHasher() {
  -        return null;
  -    } // getSymbolHasher
  +        return fSymbolHasher;
  +    }
   
       /**
        * addSymbol
  @@ -109,9 +148,28 @@
        * @return 
        */
       public String addSymbol(String symbol) {
  -        return null;
  -    } // addSymbol
   
  +        // search for identical symbol
  +        int bucket = fSymbolHasher.hash(symbol) % TABLE_SIZE;
  +        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  +            int length = symbol.length();
  +            if (length == entry.characters.length) {
  +                for (int i = 0; i < length; i++) {
  +                    if (symbol.charAt(i) != entry.characters[i]) {
  +                        continue OUTER;
  +                    }
  +                }
  +                return entry.symbol;
  +            }
  +        }
  +
  +        // create new entry
  +        Entry entry = new Entry(symbol, fBuckets[bucket]);
  +        fBuckets[bucket] = entry;
  +        return symbol;
  +
  +    } // addSymbol(String):String
  +
       /**
        * addSymbol
        * 
  @@ -121,8 +179,128 @@
        * 
        * @return 
        */
  -    public String addSymbol(char[] buffer, int offset, String length) {
  -        return null;
  -    } // addSymbol
  +    public String addSymbol(char[] buffer, int offset, int length) {
  +
  +        // search for identical symbol
  +        int bucket = fSymbolHasher.hash(buffer, offset, length) % TABLE_SIZE;
  +        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  +            if (length == entry.characters.length) {
  +                for (int i = 0; i < length; i++) {
  +                    if (buffer[offset + i] != entry.characters[i]) {
  +                        continue OUTER;
  +                    }
  +                }
  +                return entry.symbol;
  +            }
  +        }
  +
  +        // add new entry
  +        Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]);
  +        fBuckets[bucket] = entry;
  +        return entry.symbol;
  +
  +    } // addSymbol(char[],int,int):String
  +
  +    //
  +    // Classes
  +    //
  +
  +    /**
  +     * Default symbol hasher implementation.
  +     */
  +    protected static final class Hasher 
  +        implements SymbolHasher {
  +
  +        //
  +        // SymbolHasher methods
  +        //
  +
  +        /**
  +         * Returns a hashcode value for the specified symbol. The value
  +         * returned by this method must be identical to the value returned
  +         * by the <code>hash(char[],int,int)</code> method when called
  +         * with the character array that comprises the symbol string.
  +         * 
  +         * @param symbol The symbol to hash.
  +         */
  +        public int hash(String symbol) {
  +            int code = 0;
  +            int length = symbol.length();
  +            for (int i = 0; i < length; i++) {
  +                code = code * 37 + symbol.charAt(i);
  +            }
  +            return code & 0x7FFFFFF;
  +        }
  +
  +        /**
  +         * Returns a hashcode value for the specified symbol information. 
  +         * The value returned by this method must be identical to the value
  +         * returned by the <code>hash(String)</code> method when called
  +         * with the string object created from the symbol information.
  +         * 
  +         * @param buffer The character buffer containing the symbol.
  +         * @param offset The offset into the character buffer of the start
  +         *               of the symbol.
  +         * @param length The length of the symbol.
  +         */
  +        public int hash(char[] buffer, int offset, int length) {
  +            int code = 0;
  +            for (int i = 0; i < length; i++) {
  +                code = code * 37 + buffer[offset + i];
  +            }
  +            return code & 0x7FFFFFF;
  +        }
  +
  +    } // class Hasher
  +
  +    /**
  +     * This class is a symbol table entry. Each entry acts as a node
  +     * in a linked list.
  +     */
  +    protected static final class Entry {
  +
  +        //
  +        // Data
  +        //
  +
  +        /** Symbol. */
  +        public String symbol;
  +
  +        /** 
  +         * Symbol characters. This information is duplicated here for
  +         * comparison performance.
  +         */
  +        public char[] characters;
  +
  +        /** The next entry. */
  +        public Entry next;
  +
  +        //
  +        // Constructors
  +        //
  +
  +        /** 
  +         * Constructs a new entry from the specified symbol and next entry
  +         * reference. 
  +         */
  +        public Entry(String symbol, Entry next) {
  +            this.symbol = symbol;
  +            characters = new char[symbol.length()];
  +            symbol.getChars(0, characters.length, characters, 0);
  +            this.next = next;
  +        }
  +
  +        /** 
  +         * Constructs a new entry from the specified symbol information and
  +         * next entry reference. 
  +         */
  +        public Entry(char[] ch, int offset, int length, Entry next) {
  +            characters = new char[length];
  +            System.arraycopy(ch, offset, characters, 0, length);
  +            symbol = new String(characters);
  +            this.next = next;
  +        }
  +
  +    } // class Entry
   
   } // class SymbolTable