com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar

Here are the examples of the java api class com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar taken from open source projects.

1. XSDHandler#addGrammarComponents()

Project: openjdk
File: XSDHandler.java
private void addGrammarComponents(SchemaGrammar srcGrammar, SchemaGrammar dstGrammar) {
    if (dstGrammar == null) {
        createGrammarFrom(srcGrammar);
        return;
    }
    SchemaGrammar tmpGrammar = dstGrammar;
    if (tmpGrammar.isImmutable()) {
        tmpGrammar = createGrammarFrom(dstGrammar);
    }
    // add any new locations
    addNewGrammarLocations(srcGrammar, tmpGrammar);
    // add any new imported grammars
    addNewImportedGrammars(srcGrammar, tmpGrammar);
    // add any new global components
    addNewGrammarComponents(srcGrammar, tmpGrammar);
}

2. XSGrammarPool#toXSModel()

Project: openjdk
File: XSGrammarPool.java
public XSModel toXSModel(short schemaVersion) {
    ArrayList list = new ArrayList();
    for (int i = 0; i < fGrammars.length; i++) {
        for (Entry entry = fGrammars[i]; entry != null; entry = entry.next) {
            if (entry.desc.getGrammarType().equals(XMLGrammarDescription.XML_SCHEMA)) {
                list.add(entry.grammar);
            }
        }
    }
    int size = list.size();
    if (size == 0) {
        return toXSModel(new SchemaGrammar[0], schemaVersion);
    }
    SchemaGrammar[] gs = (SchemaGrammar[]) list.toArray(new SchemaGrammar[size]);
    return toXSModel(gs, schemaVersion);
}

3. XSDHandler#checkForDuplicateNames()

Project: openjdk
File: XSDHandler.java
// checkForDuplicateNames(String, Map, Element, XSDocumentInfo):void
void checkForDuplicateNames(String qName, int declType, Element currComp) {
    int namespaceEnd = qName.indexOf(',');
    String namespace = qName.substring(0, namespaceEnd);
    SchemaGrammar grammar = fGrammarBucket.getGrammar(emptyString2Null(namespace));
    if (grammar != null) {
        Object obj = getGlobalDeclFromGrammar(grammar, declType, qName.substring(namespaceEnd + 1));
        if (obj != null) {
            reportSchemaError("sch-props-correct.2", new Object[] { qName }, currComp);
        }
    }
}

4. XSDHandler#getSchemaGrammar()

Project: openjdk
File: XSDHandler.java
// NOTE: always assuming that fNamespaceGrowth is enabled
//       otherwise the grammar should have existed
private SchemaGrammar getSchemaGrammar(XSDDescription desc) {
    SchemaGrammar sg = findGrammar(desc, fNamespaceGrowth);
    if (sg == null) {
        sg = new SchemaGrammar(desc.getNamespace(), desc.makeClone(), fSymbolTable);
        fGrammarBucket.putGrammar(sg);
    } else if (sg.isImmutable()) {
        sg = createGrammarFrom(sg);
    }
    return sg;
}

5. XSDHandler#containedImportedGrammar()

Project: openjdk
File: XSDHandler.java
private boolean containedImportedGrammar(Vector importedGrammar, SchemaGrammar grammar) {
    final int size = importedGrammar.size();
    SchemaGrammar sg;
    for (int i = 0; i < size; i++) {
        sg = (SchemaGrammar) importedGrammar.elementAt(i);
        if (null2EmptyString(sg.getTargetNamespace()).equals(null2EmptyString(grammar.getTargetNamespace()))) {
            return true;
        }
    }
    return false;
}

6. XSDHandler#updateImportList()

Project: openjdk
File: XSDHandler.java
private void updateImportList(SchemaGrammar sg, Vector importedGrammars, Vector namespaceList) {
    final int size = namespaceList.size();
    SchemaGrammar isg;
    for (int i = 0; i < size; i++) {
        isg = fGrammarBucket.getGrammar((String) namespaceList.elementAt(i));
        if (isg != null) {
            if (!containedImportedGrammar(importedGrammars, isg)) {
                importedGrammars.add(isg);
            }
        } else {
        //REVIST: report an error message
        }
    }
}

7. XSDHandler#addImportList()

Project: openjdk
File: XSDHandler.java
private void addImportList(SchemaGrammar sg, Vector importedGrammars, Vector namespaceList) {
    final int size = namespaceList.size();
    SchemaGrammar isg;
    for (int i = 0; i < size; i++) {
        isg = fGrammarBucket.getGrammar((String) namespaceList.elementAt(i));
        if (isg != null) {
            importedGrammars.add(isg);
        } else {
        //REVIST: report an error message
        }
    }
}

8. XSDHandler#expandImportList()

Project: openjdk
File: XSDHandler.java
private void expandImportList(String namespace, Vector namespaceList) {
    SchemaGrammar sg = fGrammarBucket.getGrammar(namespace);
    // shouldn't be null
    if (sg != null) {
        Vector isgs = sg.getImportedGrammars();
        if (isgs == null) {
            isgs = new Vector();
            addImportList(sg, isgs, namespaceList);
            sg.setImportedGrammars(isgs);
        } else {
            updateImportList(sg, isgs, namespaceList);
        }
    }
}

9. XSDHandler#updateImportListWith()

Project: openjdk
File: XSDHandler.java
/**
     * Namespace growth
     *
     * Go throuth the grammar bucket, and for each grammar in the bucket
     * check the import list. If there exists a grammar in import list
     * that has the same namespace as newGrammar, but a different instance,
     * then update the import list and replace the old grammar instance with
     * the new one
     */
private void updateImportListWith(SchemaGrammar newGrammar) {
    SchemaGrammar[] schemaGrammars = fGrammarBucket.getGrammars();
    for (int i = 0; i < schemaGrammars.length; ++i) {
        SchemaGrammar sg = schemaGrammars[i];
        if (sg != newGrammar) {
            Vector importedGrammars = sg.getImportedGrammars();
            if (importedGrammars != null) {
                for (int j = 0; j < importedGrammars.size(); j++) {
                    SchemaGrammar isg = (SchemaGrammar) importedGrammars.elementAt(j);
                    if (null2EmptyString(isg.getTargetNamespace()).equals(null2EmptyString(newGrammar.getTargetNamespace()))) {
                        if (isg != newGrammar) {
                            importedGrammars.set(j, newGrammar);
                        }
                        break;
                    }
                }
            }
        }
    }
}

10. XSDHandler#isExistingGrammar()

Project: openjdk
File: XSDHandler.java
// end constructTrees
private boolean isExistingGrammar(XSDDescription desc, boolean ignoreConflict) {
    SchemaGrammar sg = fGrammarBucket.getGrammar(desc.getTargetNamespace());
    if (sg == null) {
        return findGrammar(desc, ignoreConflict) != null;
    } else if (sg.isImmutable()) {
        return true;
    } else {
        try {
            return sg.getDocumentLocations().contains(XMLEntityManager.expandSystemId(desc.getLiteralSystemId(), desc.getBaseSystemId(), false));
        } catch (MalformedURIException e) {
            return false;
        }
    }
}

11. XSDHandler#findGrammar()

Project: openjdk
File: XSDHandler.java
/**
     * First try to find a grammar in the bucket, if failed, consult the
     * grammar pool. If a grammar is found in the pool, then add it (and all
     * imported ones) into the bucket.
     */
protected SchemaGrammar findGrammar(XSDDescription desc, boolean ignoreConflict) {
    SchemaGrammar sg = fGrammarBucket.getGrammar(desc.getTargetNamespace());
    if (sg == null) {
        if (fGrammarPool != null) {
            sg = (SchemaGrammar) fGrammarPool.retrieveGrammar(desc);
            if (sg != null) {
                // imported by it (directly or indirectly)
                if (!fGrammarBucket.putGrammar(sg, true, ignoreConflict)) {
                    // REVISIT: a conflict between new grammar(s) and grammars
                    // in the bucket. What to do? A warning? An exception?
                    reportSchemaWarning("GrammarConflict", null, null);
                    sg = null;
                }
            }
        }
    }
    return sg;
}

12. XMLGrammarCachingConfiguration#parseXMLSchema()

Project: openjdk
File: XMLGrammarCachingConfiguration.java
// parseGrammar(String, XMLInputSource):  Grammar
//
// Protected methods
//
// package-protected methods
/* This method parses an XML Schema document.
     * It requires a GrammarBucket parameter so that DOMASBuilder can
     * extract the info it needs.
     * Therefore, bucket must not be null!
     */
SchemaGrammar parseXMLSchema(XMLInputSource is) throws IOException {
    XMLEntityResolver resolver = getEntityResolver();
    if (resolver != null) {
        fSchemaLoader.setEntityResolver(resolver);
    }
    if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
        fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
    }
    fSchemaLoader.setProperty(ERROR_REPORTER, fErrorReporter);
    String propPrefix = Constants.XERCES_PROPERTY_PREFIX;
    String propName = propPrefix + Constants.SCHEMA_LOCATION;
    fSchemaLoader.setProperty(propName, getProperty(propName));
    propName = propPrefix + Constants.SCHEMA_NONS_LOCATION;
    fSchemaLoader.setProperty(propName, getProperty(propName));
    propName = Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE;
    fSchemaLoader.setProperty(propName, getProperty(propName));
    fSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, getFeature(SCHEMA_FULL_CHECKING));
    // Should check whether the grammar with this namespace is already in
    // the grammar resolver. But since we don't know the target namespace
    // of the document here, we leave such check to XSDHandler
    SchemaGrammar grammar = (SchemaGrammar) fSchemaLoader.loadGrammar(is);
    // by default, hand it off to the grammar pool
    if (grammar != null) {
        fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, new Grammar[] { grammar });
    }
    return grammar;
}

13. XSDHandler#addGlobalComponent()

Project: openjdk
File: XSDHandler.java
private void addGlobalComponent(XSObject component, XSDDescription desc) {
    final String namespace = component.getNamespace();
    desc.setNamespace(namespace);
    final SchemaGrammar sg = getSchemaGrammar(desc);
    short componentType = component.getType();
    final String name = component.getName();
    switch(componentType) {
        case XSConstants.TYPE_DEFINITION:
            if (!((XSTypeDefinition) component).getAnonymous()) {
                if (sg.getGlobalTypeDecl(name) == null) {
                    sg.addGlobalTypeDecl((XSTypeDefinition) component);
                }
                // store the declaration in the extended map, using an empty location
                if (sg.getGlobalTypeDecl(name, "") == null) {
                    sg.addGlobalTypeDecl((XSTypeDefinition) component, "");
                }
            }
            break;
        case XSConstants.ATTRIBUTE_DECLARATION:
            if (((XSAttributeDecl) component).getScope() == XSAttributeDecl.SCOPE_GLOBAL) {
                if (sg.getGlobalAttributeDecl(name) == null) {
                    sg.addGlobalAttributeDecl((XSAttributeDecl) component);
                }
                // store the declaration in the extended map, using an empty location
                if (sg.getGlobalAttributeDecl(name, "") == null) {
                    sg.addGlobalAttributeDecl((XSAttributeDecl) component, "");
                }
            }
            break;
        case XSConstants.ATTRIBUTE_GROUP:
            if (sg.getGlobalAttributeDecl(name) == null) {
                sg.addGlobalAttributeGroupDecl((XSAttributeGroupDecl) component);
            }
            // store the declaration in the extended map, using an empty location
            if (sg.getGlobalAttributeDecl(name, "") == null) {
                sg.addGlobalAttributeGroupDecl((XSAttributeGroupDecl) component, "");
            }
            break;
        case XSConstants.ELEMENT_DECLARATION:
            if (((XSElementDecl) component).getScope() == XSElementDecl.SCOPE_GLOBAL) {
                sg.addGlobalElementDeclAll((XSElementDecl) component);
                if (sg.getGlobalElementDecl(name) == null) {
                    sg.addGlobalElementDecl((XSElementDecl) component);
                }
                // store the declaration in the extended map, using an empty location
                if (sg.getGlobalElementDecl(name, "") == null) {
                    sg.addGlobalElementDecl((XSElementDecl) component, "");
                }
            }
            break;
        case XSConstants.MODEL_GROUP_DEFINITION:
            if (sg.getGlobalGroupDecl(name) == null) {
                sg.addGlobalGroupDecl((XSGroupDecl) component);
            }
            // store the declaration in the extended map, using an empty location
            if (sg.getGlobalGroupDecl(name, "") == null) {
                sg.addGlobalGroupDecl((XSGroupDecl) component, "");
            }
            break;
        case XSConstants.NOTATION_DECLARATION:
            if (sg.getGlobalNotationDecl(name) == null) {
                sg.addGlobalNotationDecl((XSNotationDecl) component);
            }
            // store the declaration in the extended map, using an empty location
            if (sg.getGlobalNotationDecl(name, "") == null) {
                sg.addGlobalNotationDecl((XSNotationDecl) component, "");
            }
            break;
        case XSConstants.IDENTITY_CONSTRAINT:
        case XSConstants.ATTRIBUTE_USE:
        default:
            break;
    }
}

14. XSDHandler#createGrammarFrom()

Project: openjdk
File: XSDHandler.java
private SchemaGrammar createGrammarFrom(SchemaGrammar grammar) {
    SchemaGrammar newGrammar = new SchemaGrammar(grammar);
    fGrammarBucket.putGrammar(newGrammar);
    // update all the grammars in the bucket to point to the new grammar.
    updateImportListWith(newGrammar);
    // update import list of the new grammar
    updateImportListFor(newGrammar);
    return newGrammar;
}

15. XSDHandler#canAddComponent()

Project: openjdk
File: XSDHandler.java
private boolean canAddComponent(XSObject component, XSDDescription desc) {
    desc.setNamespace(component.getNamespace());
    final SchemaGrammar sg = findGrammar(desc, false);
    if (sg == null) {
        return true;
    } else if (sg.isImmutable()) {
        return false;
    }
    short componentType = component.getType();
    final String name = component.getName();
    switch(componentType) {
        case XSConstants.TYPE_DEFINITION:
            if (sg.getGlobalTypeDecl(name) == component) {
                return true;
            }
            break;
        case XSConstants.ATTRIBUTE_DECLARATION:
            if (sg.getGlobalAttributeDecl(name) == component) {
                return true;
            }
            break;
        case XSConstants.ATTRIBUTE_GROUP:
            if (sg.getGlobalAttributeDecl(name) == component) {
                return true;
            }
            break;
        case XSConstants.ELEMENT_DECLARATION:
            if (sg.getGlobalElementDecl(name) == component) {
                return true;
            }
            break;
        case XSConstants.MODEL_GROUP_DEFINITION:
            if (sg.getGlobalGroupDecl(name) == component) {
                return true;
            }
            break;
        case XSConstants.NOTATION_DECLARATION:
            if (sg.getGlobalNotationDecl(name) == component) {
                return true;
            }
            break;
        case XSConstants.IDENTITY_CONSTRAINT:
        case XSConstants.ATTRIBUTE_USE:
        default:
            return true;
    }
    return false;
}

16. XSDHandler#expandGrammars()

Project: openjdk
File: XSDHandler.java
// getSchemaDocument(String, XSInputSource, boolean, short, Element): Element
private Vector expandGrammars(SchemaGrammar[] grammars) {
    Vector currGrammars = new Vector();
    for (int i = 0; i < grammars.length; i++) {
        if (!currGrammars.contains(grammars[i])) {
            currGrammars.add(grammars[i]);
        }
    }
    // for all (recursively) imported grammars
    SchemaGrammar sg1, sg2;
    Vector gs;
    for (int i = 0; i < currGrammars.size(); i++) {
        // get the grammar
        sg1 = (SchemaGrammar) currGrammars.elementAt(i);
        // we need to add grammars imported by sg1 too
        gs = sg1.getImportedGrammars();
        // 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 (!currGrammars.contains(sg2)) {
                currGrammars.addElement(sg2);
            }
        }
    }
    return currGrammars;
}

17. XSDHandler#getSchemaDocument()

Project: openjdk
File: XSDHandler.java
// getSchemaDocument1(boolean, boolean, XMLInputSource, Element): Element
/**
     * getSchemaDocument method uses XMLInputSource to parse a schema document.
     * @param schemaNamespace
     * @param schemaSource
     * @param mustResolve
     * @param referType
     * @param referElement
     * @return A schema Element.
     */
private Element getSchemaDocument(XSInputSource schemaSource, XSDDescription desc) {
    SchemaGrammar[] grammars = schemaSource.getGrammars();
    short referType = desc.getContextType();
    if (grammars != null && grammars.length > 0) {
        Vector expandedGrammars = expandGrammars(grammars);
        // not enabled - we do nothing
        if (fNamespaceGrowth || !existingGrammars(expandedGrammars)) {
            addGrammars(expandedGrammars);
            if (referType == XSDDescription.CONTEXT_PREPARSE) {
                desc.setTargetNamespace(grammars[0].getTargetNamespace());
            }
        }
    } else {
        XSObject[] components = schemaSource.getComponents();
        if (components != null && components.length > 0) {
            Map<String, Vector> importDependencies = new HashMap();
            Vector expandedComponents = expandComponents(components, importDependencies);
            if (fNamespaceGrowth || canAddComponents(expandedComponents)) {
                addGlobalComponents(expandedComponents, importDependencies);
                if (referType == XSDDescription.CONTEXT_PREPARSE) {
                    desc.setTargetNamespace(components[0].getNamespace());
                }
            }
        }
    }
    return null;
}

18. XSDHandler#getGlobalDecl()

Project: openjdk
File: XSDHandler.java
// since it is forbidden for traversers to talk to each other
// directly (except wen a traverser encounters a local declaration),
// this provides a generic means for a traverser to call
// for the traversal of some declaration.  An XSDocumentInfo is
// required because the XSDocumentInfo that the traverser is traversing
// may bear no relation to the one the handler is operating on.
// This method will:
// 1.  See if a global definition matching declToTraverse exists;
// 2. if so, determine if there is a path from currSchema to the
// schema document where declToTraverse lives (i.e., do a lookup
// in DependencyMap);
// 3. depending on declType (which will be relevant to step 1 as
// well), call the appropriate traverser with the appropriate
// XSDocumentInfo object.
// This method returns whatever the traverser it called returned;
// this will be an Object of some kind
// that lives in the Grammar.
protected Object getGlobalDecl(XSDocumentInfo currSchema, int declType, QName declToTraverse, Element elmNode) {
    if (DEBUG_NODE_POOL) {
        System.out.println("TRAVERSE_GL: " + declToTraverse.toString());
    }
    // use the normal way to get the decl
    if (declToTraverse.uri != null && declToTraverse.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA) {
        if (declType == TYPEDECL_TYPE) {
            Object retObj = SchemaGrammar.SG_SchemaNS.getGlobalTypeDecl(declToTraverse.localpart);
            if (retObj != null)
                return retObj;
        }
    }
    // now check whether this document can access the requsted namespace
    if (!currSchema.isAllowedNS(declToTraverse.uri)) {
        // cannot get to this schema from the one containing the requesting decl
        if (currSchema.needReportTNSError(declToTraverse.uri)) {
            String code = declToTraverse.uri == null ? "src-resolve.4.1" : "src-resolve.4.2";
            reportSchemaError(code, new Object[] { fDoc2SystemId.get(currSchema.fSchemaElement), declToTraverse.uri, declToTraverse.rawname }, elmNode);
        }
    // Recover and continue to look for the component.
    // return null;
    }
    // check whether there is grammar for the requested namespace
    SchemaGrammar sGrammar = fGrammarBucket.getGrammar(declToTraverse.uri);
    if (sGrammar == null) {
        if (needReportTNSError(declToTraverse.uri))
            reportSchemaError("src-resolve", new Object[] { declToTraverse.rawname, COMP_TYPE[declType] }, elmNode);
        return null;
    }
    // if there is such grammar, check whether the requested component is in the grammar
    Object retObj = getGlobalDeclFromGrammar(sGrammar, declType, declToTraverse.localpart);
    String declKey = declToTraverse.uri == null ? "," + declToTraverse.localpart : declToTraverse.uri + "," + declToTraverse.localpart;
    // if the component is parsed, return it
    if (!fTolerateDuplicates) {
        if (retObj != null) {
            return retObj;
        }
    } else {
        Object retObj2 = getGlobalDecl(declKey, declType);
        if (retObj2 != null) {
            return retObj2;
        }
    }
    XSDocumentInfo schemaWithDecl = null;
    Element decl = null;
    XSDocumentInfo declDoc = null;
    // the component is not parsed, try to find a DOM element for it
    switch(declType) {
        case ATTRIBUTE_TYPE:
            decl = getElementFromMap(fUnparsedAttributeRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedAttributeRegistrySub, declKey);
            break;
        case ATTRIBUTEGROUP_TYPE:
            decl = getElementFromMap(fUnparsedAttributeGroupRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedAttributeGroupRegistrySub, declKey);
            break;
        case ELEMENT_TYPE:
            decl = getElementFromMap(fUnparsedElementRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedElementRegistrySub, declKey);
            break;
        case GROUP_TYPE:
            decl = getElementFromMap(fUnparsedGroupRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedGroupRegistrySub, declKey);
            break;
        case IDENTITYCONSTRAINT_TYPE:
            decl = getElementFromMap(fUnparsedIdentityConstraintRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedIdentityConstraintRegistrySub, declKey);
            break;
        case NOTATION_TYPE:
            decl = getElementFromMap(fUnparsedNotationRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedNotationRegistrySub, declKey);
            break;
        case TYPEDECL_TYPE:
            decl = getElementFromMap(fUnparsedTypeRegistry, declKey);
            declDoc = getDocInfoFromMap(fUnparsedTypeRegistrySub, declKey);
            break;
        default:
            reportSchemaError("Internal-Error", new Object[] { "XSDHandler asked to locate component of type " + declType + "; it does not recognize this type!" }, elmNode);
    }
    // no DOM element found, so the component can't be located
    if (decl == null) {
        if (retObj == null) {
            reportSchemaError("src-resolve", new Object[] { declToTraverse.rawname, COMP_TYPE[declType] }, elmNode);
        }
        return retObj;
    }
    // get the schema doc containing the component to be parsed
    // it should always return non-null value, but since null-checking
    // comes for free, let's be safe and check again
    schemaWithDecl = findXSDocumentForDecl(currSchema, decl, declDoc);
    if (schemaWithDecl == null) {
        // cannot get to this schema from the one containing the requesting decl
        if (retObj == null) {
            String code = declToTraverse.uri == null ? "src-resolve.4.1" : "src-resolve.4.2";
            reportSchemaError(code, new Object[] { fDoc2SystemId.get(currSchema.fSchemaElement), declToTraverse.uri, declToTraverse.rawname }, elmNode);
        }
        return retObj;
    }
    // a circular reference. error!
    if (DOMUtil.isHidden(decl, fHiddenNodes)) {
        if (retObj == null) {
            String code = CIRCULAR_CODES[declType];
            if (declType == TYPEDECL_TYPE) {
                if (SchemaSymbols.ELT_COMPLEXTYPE.equals(DOMUtil.getLocalName(decl))) {
                    code = "ct-props-correct.3";
                }
            }
            // decl must not be null if we're here...
            reportSchemaError(code, new Object[] { declToTraverse.prefix + ":" + declToTraverse.localpart }, elmNode);
        }
        return retObj;
    }
    return traverseGlobalDecl(declType, decl, schemaWithDecl, sGrammar);
}

19. XSDHandler#parseSchema()

Project: openjdk
File: XSDHandler.java
// end constructor
/**
     * This method initiates the parse of a schema.  It will likely be
     * called from the Validator and it will make the
     * resulting grammar available; it returns a reference to this object just
     * in case.  A reset(XMLComponentManager) must be called before this methods is called.
     * @param is
     * @param desc
     * @param locationPairs
     * @return the SchemaGrammar
     * @throws IOException
     */
public SchemaGrammar parseSchema(XMLInputSource is, XSDDescription desc, Map<String, XMLSchemaLoader.LocationArray> locationPairs) throws IOException {
    fLocationPairs = locationPairs;
    fSchemaParser.resetNodePool();
    SchemaGrammar grammar = null;
    String schemaNamespace = null;
    short referType = desc.getContextType();
    // no namespace schema.
    if (referType != XSDDescription.CONTEXT_PREPARSE) {
        // first try to find it in the bucket/pool, return if one is found
        if (fHonourAllSchemaLocations && referType == XSDDescription.CONTEXT_IMPORT && isExistingGrammar(desc, fNamespaceGrowth)) {
            grammar = fGrammarBucket.getGrammar(desc.getTargetNamespace());
        } else {
            grammar = findGrammar(desc, fNamespaceGrowth);
        }
        if (grammar != null) {
            if (!fNamespaceGrowth) {
                return grammar;
            } else {
                try {
                    if (grammar.getDocumentLocations().contains(XMLEntityManager.expandSystemId(is.getSystemId(), is.getBaseSystemId(), false))) {
                        return grammar;
                    }
                } catch (MalformedURIException e) {
                }
            }
        }
        schemaNamespace = desc.getTargetNamespace();
        // handle empty string URI as null
        if (schemaNamespace != null) {
            schemaNamespace = fSymbolTable.addSymbol(schemaNamespace);
        }
    }
    // before parsing a schema, need to clear registries associated with
    // parsing schemas
    prepareForParse();
    Element schemaRoot = null;
    // first phase:  construct trees.
    if (is instanceof DOMInputSource) {
        schemaRoot = getSchemaDocument(schemaNamespace, (DOMInputSource) is, referType == XSDDescription.CONTEXT_PREPARSE, referType, null);
    } else // DOMInputSource
    if (is instanceof SAXInputSource) {
        schemaRoot = getSchemaDocument(schemaNamespace, (SAXInputSource) is, referType == XSDDescription.CONTEXT_PREPARSE, referType, null);
    } else // SAXInputSource
    if (is instanceof StAXInputSource) {
        schemaRoot = getSchemaDocument(schemaNamespace, (StAXInputSource) is, referType == XSDDescription.CONTEXT_PREPARSE, referType, null);
    } else // StAXInputSource
    if (is instanceof XSInputSource) {
        schemaRoot = getSchemaDocument((XSInputSource) is, desc);
    } else // XSInputSource
    {
        schemaRoot = getSchemaDocument(schemaNamespace, is, referType == XSDDescription.CONTEXT_PREPARSE, referType, null);
    }
    if (schemaRoot == null) {
        // something went wrong right off the hop
        if (is instanceof XSInputSource) {
            return fGrammarBucket.getGrammar(desc.getTargetNamespace());
        }
        return grammar;
    }
    if (referType == XSDDescription.CONTEXT_PREPARSE) {
        Element schemaElem = schemaRoot;
        schemaNamespace = DOMUtil.getAttrValue(schemaElem, SchemaSymbols.ATT_TARGETNAMESPACE);
        if (schemaNamespace != null && schemaNamespace.length() > 0) {
            // Since now we've discovered a namespace, we need to update xsd key
            // and store this schema in traversed schemas bucket
            schemaNamespace = fSymbolTable.addSymbol(schemaNamespace);
            desc.setTargetNamespace(schemaNamespace);
        } else {
            schemaNamespace = null;
        }
        grammar = findGrammar(desc, fNamespaceGrowth);
        String schemaId = XMLEntityManager.expandSystemId(is.getSystemId(), is.getBaseSystemId(), false);
        if (grammar != null) {
            // whether we've loaded this schema document before so we must assume that we haven't.
            if (!fNamespaceGrowth || (schemaId != null && grammar.getDocumentLocations().contains(schemaId))) {
                return grammar;
            }
        }
        XSDKey key = new XSDKey(schemaId, referType, schemaNamespace);
        fTraversed.put(key, schemaRoot);
        if (schemaId != null) {
            fDoc2SystemId.put(schemaRoot, schemaId);
        }
    }
    // before constructing trees and traversing a schema, need to reset
    // all traversers and clear all registries
    prepareForTraverse();
    fRoot = constructTrees(schemaRoot, is.getSystemId(), desc, grammar != null);
    if (fRoot == null) {
        return null;
    }
    // second phase:  fill global registries.
    buildGlobalNameRegistries();
    // third phase:  call traversers
    ArrayList annotationInfo = fValidateAnnotations ? new ArrayList() : null;
    traverseSchemas(annotationInfo);
    // fourth phase: handle local element decls
    traverseLocalElements();
    // fifth phase:  handle Keyrefs
    resolveKeyRefs();
    // for all grammars with <import>s
    for (int i = fAllTNSs.size() - 1; i >= 0; i--) {
        // get its target namespace
        String tns = fAllTNSs.elementAt(i);
        // get all namespaces it imports
        Vector ins = (Vector) fImportMap.get(tns);
        // get the grammar
        SchemaGrammar sg = fGrammarBucket.getGrammar(emptyString2Null(tns));
        if (sg == null)
            continue;
        SchemaGrammar isg;
        // for imported namespace
        int count = 0;
        for (int j = 0; j < ins.size(); j++) {
            // get imported grammar
            isg = fGrammarBucket.getGrammar((String) ins.elementAt(j));
            // reuse the same vector
            if (isg != null)
                ins.setElementAt(isg, count++);
        }
        ins.setSize(count);
        // set the imported grammars
        sg.setImportedGrammars(ins);
    }
    /** validate annotations **/
    if (fValidateAnnotations && annotationInfo.size() > 0) {
        validateAnnotations(annotationInfo);
    }
    // and return.
    return fGrammarBucket.getGrammar(fRoot.fTargetNamespace);
}

20. XSDAbstractTraverser#traverseSyntheticAnnotation()

Project: openjdk
File: XSDAbstractTraverser.java
XSAnnotationImpl traverseSyntheticAnnotation(Element annotationParent, String initialContent, Object[] parentAttrs, boolean isGlobal, XSDocumentInfo schemaDoc) {
    String contents = initialContent;
    // find the grammar; fSchemaHandler must be known!
    SchemaGrammar grammar = fSchemaHandler.getGrammar(schemaDoc.fTargetNamespace);
    // fish out local attributes passed from parent
    Vector annotationLocalAttrs = (Vector) parentAttrs[XSAttributeChecker.ATTIDX_NONSCHEMA];
    // optimize for case where there are no local attributes
    if (annotationLocalAttrs != null && !annotationLocalAttrs.isEmpty()) {
        StringBuffer localStrBuffer = new StringBuffer(64);
        localStrBuffer.append(" ");
        // Vector should contain rawname value pairs
        int i = 0;
        while (i < annotationLocalAttrs.size()) {
            String rawname = (String) annotationLocalAttrs.elementAt(i++);
            int colonIndex = rawname.indexOf(':');
            String prefix, localpart;
            if (colonIndex == -1) {
                prefix = "";
                localpart = rawname;
            } else {
                prefix = rawname.substring(0, colonIndex);
                localpart = rawname.substring(colonIndex + 1);
            }
            String uri = schemaDoc.fNamespaceSupport.getURI(fSymbolTable.addSymbol(prefix));
            localStrBuffer.append(rawname).append("=\"");
            String value = (String) annotationLocalAttrs.elementAt(i++);
            // search for pesky "s and <s within attr value:
            value = processAttValue(value);
            localStrBuffer.append(value).append("\" ");
        }
        // and now splice it into place; immediately after the annotation token, for simplicity's sake
        StringBuffer contentBuffer = new StringBuffer(contents.length() + localStrBuffer.length());
        int annotationTokenEnd = contents.indexOf(SchemaSymbols.ELT_ANNOTATION);
        // annotation must occur somewhere or we're in big trouble...
        if (annotationTokenEnd == -1)
            return null;
        annotationTokenEnd += SchemaSymbols.ELT_ANNOTATION.length();
        contentBuffer.append(contents.substring(0, annotationTokenEnd));
        contentBuffer.append(localStrBuffer.toString());
        contentBuffer.append(contents.substring(annotationTokenEnd, contents.length()));
        final String annotation = contentBuffer.toString();
        if (fValidateAnnotations) {
            schemaDoc.addAnnotation(new XSAnnotationInfo(annotation, annotationParent));
        }
        return new XSAnnotationImpl(annotation, grammar);
    } else {
        if (fValidateAnnotations) {
            schemaDoc.addAnnotation(new XSAnnotationInfo(contents, annotationParent));
        }
        return new XSAnnotationImpl(contents, grammar);
    }
}

21. XSDAbstractTraverser#traverseAnnotationDecl()

Project: openjdk
File: XSDAbstractTraverser.java
// traverse the annotation declaration
// REVISIT: how to pass the parentAttrs? as DOM attributes?
//          as name/value pairs (string)? in parsed form?
// @return XSAnnotationImpl object
XSAnnotationImpl traverseAnnotationDecl(Element annotationDecl, Object[] parentAttrs, boolean isGlobal, XSDocumentInfo schemaDoc) {
    // General Attribute Checking
    Object[] attrValues = fAttrChecker.checkAttributes(annotationDecl, isGlobal, schemaDoc);
    fAttrChecker.returnAttrArray(attrValues, schemaDoc);
    String contents = DOMUtil.getAnnotation(annotationDecl);
    Element child = DOMUtil.getFirstChildElement(annotationDecl);
    if (child != null) {
        do {
            String name = DOMUtil.getLocalName(child);
            // "appinfo" and "documentation"
            if (!((name.equals(SchemaSymbols.ELT_APPINFO)) || (name.equals(SchemaSymbols.ELT_DOCUMENTATION)))) {
                reportSchemaError("src-annotation", new Object[] { name }, child);
            } else {
                // General Attribute Checking
                // There is no difference between global or local appinfo/documentation,
                // so we assume it's always global.
                attrValues = fAttrChecker.checkAttributes(child, true, schemaDoc);
                fAttrChecker.returnAttrArray(attrValues, schemaDoc);
            }
            child = DOMUtil.getNextSiblingElement(child);
        } while (child != null);
    }
    // nothing to contribute to PSVI
    if (contents == null)
        return null;
    // find the grammar; fSchemaHandler must be known!
    SchemaGrammar grammar = fSchemaHandler.getGrammar(schemaDoc.fTargetNamespace);
    // fish out local attributes passed from parent
    Vector annotationLocalAttrs = (Vector) parentAttrs[XSAttributeChecker.ATTIDX_NONSCHEMA];
    // optimize for case where there are no local attributes
    if (annotationLocalAttrs != null && !annotationLocalAttrs.isEmpty()) {
        StringBuffer localStrBuffer = new StringBuffer(64);
        localStrBuffer.append(" ");
        // Vector should contain rawname value pairs
        int i = 0;
        while (i < annotationLocalAttrs.size()) {
            String rawname = (String) annotationLocalAttrs.elementAt(i++);
            int colonIndex = rawname.indexOf(':');
            String prefix, localpart;
            if (colonIndex == -1) {
                prefix = "";
                localpart = rawname;
            } else {
                prefix = rawname.substring(0, colonIndex);
                localpart = rawname.substring(colonIndex + 1);
            }
            String uri = schemaDoc.fNamespaceSupport.getURI(fSymbolTable.addSymbol(prefix));
            if (annotationDecl.getAttributeNS(uri, localpart).length() != 0) {
                // skip the next value, too
                i++;
                continue;
            }
            localStrBuffer.append(rawname).append("=\"");
            String value = (String) annotationLocalAttrs.elementAt(i++);
            // search for pesky "s and <s within attr value:
            value = processAttValue(value);
            localStrBuffer.append(value).append("\" ");
        }
        // and now splice it into place; immediately after the annotation token, for simplicity's sake
        StringBuffer contentBuffer = new StringBuffer(contents.length() + localStrBuffer.length());
        int annotationTokenEnd = contents.indexOf(SchemaSymbols.ELT_ANNOTATION);
        // annotation must occur somewhere or we're in big trouble...
        if (annotationTokenEnd == -1)
            return null;
        annotationTokenEnd += SchemaSymbols.ELT_ANNOTATION.length();
        contentBuffer.append(contents.substring(0, annotationTokenEnd));
        contentBuffer.append(localStrBuffer.toString());
        contentBuffer.append(contents.substring(annotationTokenEnd, contents.length()));
        final String annotation = contentBuffer.toString();
        if (fValidateAnnotations) {
            schemaDoc.addAnnotation(new XSAnnotationInfo(annotation, annotationDecl));
        }
        return new XSAnnotationImpl(annotation, grammar);
    } else {
        if (fValidateAnnotations) {
            schemaDoc.addAnnotation(new XSAnnotationInfo(contents, annotationDecl));
        }
        return new XSAnnotationImpl(contents, grammar);
    }
}