FileDocCategorySizeDatePackage
XMLGrammarPoolImpl.javaAPI DocJava SE 5 API15458Fri Aug 26 14:55:56 BST 2005com.sun.org.apache.xerces.internal.util

XMLGrammarPoolImpl

public class XMLGrammarPoolImpl extends Object implements XMLGrammarPool
Stores grammars in a pool associated to a specific key. This grammar pool implementation stores two types of grammars: those keyed by the root element name, and those keyed by the grammar's target namespace. This is the default implementation of the GrammarPool interface. As we move forward, this will become more function-rich and robust.
author
Jeffrey Rodriguez, IBM
author
Andy Clark, IBM
author
Neil Graham, IBM
author
Pavani Mukthipudi, Sun Microsystems
author
Neeraj Bajaj, SUN Microsystems
version
$Id: XMLGrammarPoolImpl.java,v 1.6 2003/07/30 21:30:04 elena Exp $

Fields Summary
protected static final int
TABLE_SIZE
Default size.
protected Entry[]
fGrammars
Grammars.
protected boolean
fPoolIsLocked
protected int
fGrammarCount
private static final boolean
DEBUG
Constructors Summary
public XMLGrammarPoolImpl()
Constructs a grammar pool with a default number of buckets.


    //
    // Constructors
    //

               
      
        fGrammars = new Entry[TABLE_SIZE];
        fPoolIsLocked = false;
    
public XMLGrammarPoolImpl(int initialCapacity)
Constructs a grammar pool with a specified number of buckets.

        fGrammars = new Entry[initialCapacity];
        fPoolIsLocked = false;
    
Methods Summary
public voidcacheGrammars(java.lang.String grammarType, com.sun.org.apache.xerces.internal.xni.grammars.Grammar[] grammars)

        if(!fPoolIsLocked) {
            for (int i = 0; i < grammars.length; i++) {
                if(DEBUG) {
                    System.out.println("CACHED GRAMMAR " + (i+1) ) ;
                    Grammar temp = grammars[i] ;
                    //print(temp.getGrammarDescription());
                }
                putGrammar(grammars[i]);
            }
        }
    
public voidclear()

        for (int i=0; i<fGrammars.length; i++) {
            if(fGrammars[i] != null) {
                fGrammars[i].clear();
                fGrammars[i] = null;
            }
        }
        fGrammarCount = 0;
    
public booleancontainsGrammar(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc)
Returns true if the grammar pool contains a grammar associated to the specified grammar description. Currently, the root element name is used as the key for DTD grammars and the target namespace is used as the key for Schema grammars.

param
desc The Grammar Description.

        synchronized (fGrammars) {
            int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return true;
            }
        }
        return false;
    }
    
public booleanequals(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc1, com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc2)
This method checks whether two grammars are the same. Currently, we compare the root element names for DTD grammars and the target namespaces for Schema grammars. The application can override this behaviour and add its own logic.

param
gDesc1 The grammar description
param
gDesc2 The grammar description of the grammar to be compared to
return
True if the grammars are equal, otherwise false

        return desc1.equals(desc2);
    
public com.sun.org.apache.xerces.internal.xni.grammars.GrammargetGrammar(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc)
Returns the grammar associated to the specified grammar description. Currently, the root element name is used as the key for DTD grammars and the target namespace is used as the key for Schema grammars.

param
desc The Grammar Description.

        synchronized (fGrammars) {
            int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return entry.grammar;
            }
        }
        return null;
    }
    
public inthashCode(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc)
Returns the hash code value for the given grammar description.

param
desc The grammar description
return
The hash code value

        return desc.hashCode();
    
public voidlockPool()

        fPoolIsLocked = true;
    
public voidputGrammar(com.sun.org.apache.xerces.internal.xni.grammars.Grammar grammar)
Puts the specified grammar into the grammar pool and associates it to its root element name or its target namespace.

param
grammar The Grammar.

        if(!fPoolIsLocked) {
            synchronized (fGrammars) {
                XMLGrammarDescription desc = grammar.getGrammarDescription();
                int hash = hashCode(desc);
                int index = (hash & 0x7FFFFFFF) % fGrammars.length;
                for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
                    if (entry.hash == hash && equals(entry.desc, desc)) {
                        entry.grammar = grammar;
                        return;
                    }
                }
                // create a new entry
                Entry entry = new Entry(hash, desc, grammar, fGrammars[index]);
                fGrammars[index] = entry;
                fGrammarCount++;
            }
        }
    
public com.sun.org.apache.xerces.internal.xni.grammars.GrammarremoveGrammar(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc)
Removes the grammar associated to the specified grammar description from the grammar pool and returns the removed grammar. Currently, the root element name is used as the key for DTD grammars and the target namespace is used as the key for Schema grammars.

param
desc The Grammar Description.
return
The removed grammar.

        synchronized (fGrammars) {
            int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index], prev = null ; entry != null ; prev = entry, entry = entry.next) {
            if ((entry.hash == hash) && equals(entry.desc, desc)) {
                if (prev != null) {
                        prev.next = entry.next;
            }
            else {
                fGrammars[index] = entry.next;
            }
                Grammar tempGrammar = entry.grammar;
                entry.grammar = null;
                fGrammarCount--;
                return tempGrammar;
            }
        }
        return null;
        }
    
public com.sun.org.apache.xerces.internal.xni.grammars.GrammarretrieveGrammar(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription desc)

        if(DEBUG){
            System.out.println("RETRIEVING GRAMMAR FROM THE APPLICATION WITH FOLLOWING DESCRIPTION :");
            //print(desc);
        }
        return getGrammar(desc);
    
public com.sun.org.apache.xerces.internal.xni.grammars.Grammar[]retrieveInitialGrammarSet(java.lang.String grammarType)

        synchronized (fGrammars) {
            int grammarSize = fGrammars.length ;
            Grammar [] tempGrammars = new Grammar[fGrammarCount];
            int pos = 0;
            for (int i = 0; i < grammarSize; i++) {
                for (Entry e = fGrammars[i]; e != null; e = e.next) {
                    if (e.desc.getGrammarType().equals(grammarType)) {
                        tempGrammars[pos++] = e.grammar;
                    }
                }
            }
            Grammar[] toReturn = new Grammar[pos];
            System.arraycopy(tempGrammars, 0, toReturn, 0, pos);
            return toReturn;
        }
    
public voidunlockPool()

        fPoolIsLocked = false;