FileDocCategorySizeDatePackage
XSGrammarBucket.javaAPI DocJava SE 5 API7825Fri Aug 26 14:55:50 BST 2005com.sun.org.apache.xerces.internal.impl.xs

XSGrammarBucket

public class XSGrammarBucket extends Object
A class used to hold the internal schema grammar set for the current instance
author
Sandy Gao, IBM
version
$Id: XSGrammarBucket.java,v 1.8 2003/09/23 21:42:31 mrglavas Exp $

Fields Summary
Hashtable
fGrammarRegistry
Hashtable that maps between Namespace and a Grammar
SchemaGrammar
fNoNSGrammar
Constructors Summary
Methods Summary
public com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammargetGrammar(java.lang.String namespace)
Get the schema grammar for the specified namespace

param
namespace
return
SchemaGrammar associated with the namespace


                         
        
        if (namespace == null)
            return fNoNSGrammar;
        return (SchemaGrammar)fGrammarRegistry.get(namespace);
    
public com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar[]getGrammars()
get all grammars in the registry

return
an array of SchemaGrammars.

        // get the number of grammars
        int count = fGrammarRegistry.size() + (fNoNSGrammar==null ? 0 : 1);
        SchemaGrammar[] grammars = new SchemaGrammar[count];
        // get grammars with target namespace
        Enumeration schemas = fGrammarRegistry.elements();
        int i = 0;
        while (schemas.hasMoreElements())
            grammars[i++] = (SchemaGrammar)schemas.nextElement();
        // add the grammar without target namespace, if any
        if (fNoNSGrammar != null)
            grammars[count-1] = fNoNSGrammar;
        return grammars;
    
public voidputGrammar(com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar grammar)
Put a schema grammar into the registry This method is for internal use only: it assumes that a grammar with the same target namespace is not already in the bucket.

param
grammar the grammar to put in the registry

        if (grammar.getTargetNamespace() == null)
            fNoNSGrammar = grammar;
        else
            fGrammarRegistry.put(grammar.getTargetNamespace(), grammar);
    
public booleanputGrammar(com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar grammar, boolean deep)
put a schema grammar and any grammars imported by it (directly or inderectly) into the registry. when a grammar with the same target namespace is already in the bucket, and different from the one being added, it's an error, and no grammar will be added into the bucket.

param
grammar the grammar to put in the registry
param
deep whether to add imported grammars
return
whether the process succeeded

        // whether there is one with the same tns
        SchemaGrammar sg = getGrammar(grammar.fTargetNamespace);
        if (sg != null) {
            // if the one we have is different from the one passed, it's an error
            return sg == grammar;
        }
        // not deep import, then just add this one grammar
        if (!deep) {
            putGrammar(grammar);
            return true;
        }

        // get all imported grammars, and make a copy of the Vector, so that
        // we can recursively process the grammars, and add distinct ones
        // to the same vector
        Vector currGrammars = (Vector)grammar.getImportedGrammars();
        if (currGrammars == null) {
            putGrammar(grammar);
            return true;
        }
        
        Vector grammars = ((Vector)currGrammars.clone());
        SchemaGrammar sg1, sg2;
        Vector gs;
        // for all (recursively) imported grammars
        for (int i = 0; i < grammars.size(); i++) {
            // get the grammar
            sg1 = (SchemaGrammar)grammars.elementAt(i);
            // check whether the bucket has one with the same tns
            sg2 = getGrammar(sg1.fTargetNamespace);
            if (sg2 == null) {
                // we need to add grammars imported by sg1 too
                gs = sg1.getImportedGrammars();
                // for all grammars imported by sg2, but not in the vector
                // we add them to the vector
                if(gs == null) continue;
                for (int j = gs.size() - 1; j >= 0; j--) {
                    sg2 = (SchemaGrammar)gs.elementAt(j);
                    if (!grammars.contains(sg2))
                        grammars.addElement(sg2);
                }
            }
            // we found one with the same target namespace
            // if the two grammars are not the same object, then it's an error
            else if (sg2 != sg1) {
                return false;
            }
        }

        // now we have all imported grammars stored in the vector. add them
        putGrammar(grammar);
        for (int i = grammars.size() - 1; i >= 0; i--)
            putGrammar((SchemaGrammar)grammars.elementAt(i));

        return true;
    
public voidreset()
Clear the registry. REVISIT: update to use another XSGrammarBucket

        fNoNSGrammar = null;
        fGrammarRegistry.clear();