com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription Java Examples

The following examples show how to use com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XSGrammarPool.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: SoftReferenceGrammarPool.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public boolean containsGrammar(XMLGrammarDescription desc) {
    synchronized (fGrammars) {
        clean();
        int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index]; entry != null ; entry = entry.next) {
            Grammar tempGrammar = (Grammar) entry.grammar.get();
            /** If the soft reference has been cleared, remove this entry from the pool. */
            if (tempGrammar == null) {
                removeEntry(entry);
            }
            else if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return true;
            }
        }
        return false;
    }
}
 
Example #3
Source File: SoftReferenceGrammarPool.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 */
public Grammar getGrammar(XMLGrammarDescription desc) {
    synchronized (fGrammars) {
        clean();
        int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
            Grammar tempGrammar = entry.grammar.get();
            /** If the soft reference has been cleared, remove this entry from the pool. */
            if (tempGrammar == null) {
                removeEntry(entry);
            }
            else if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return tempGrammar;
            }
        }
        return null;
    }
}
 
Example #4
Source File: SoftReferenceGrammarPool.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public Grammar getGrammar(XMLGrammarDescription desc) {
    synchronized (fGrammars) {
        clean();
        int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
            Grammar tempGrammar = (Grammar) entry.grammar.get();
            /** If the soft reference has been cleared, remove this entry from the pool. */
            if (tempGrammar == null) {
                removeEntry(entry);
            }
            else if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return tempGrammar;
            }
        }
        return null;
    }
}
 
Example #5
Source File: SoftReferenceGrammarPool.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public Grammar getGrammar(XMLGrammarDescription desc) {
    synchronized (fGrammars) {
        clean();
        int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
            Grammar tempGrammar = (Grammar) entry.grammar.get();
            /** If the soft reference has been cleared, remove this entry from the pool. */
            if (tempGrammar == null) {
                removeEntry(entry);
            }
            else if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return tempGrammar;
            }
        }
        return null;
    }
}
 
Example #6
Source File: XMLSchemaLoader.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
private void initGrammarBucket(){
    if(fGrammarPool != null) {
        Grammar [] initialGrammars = fGrammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
        for (int i = 0; i < initialGrammars.length; i++) {
            // put this grammar into the bucket, along with grammars
            // imported by it (directly or indirectly)
            if (!fGrammarBucket.putGrammar((SchemaGrammar)(initialGrammars[i]), true)) {
                // REVISIT: a conflict between new grammar(s) and grammars
                // in the bucket. What to do? A warning? An exception?
                fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
                        "GrammarConflict", null,
                        XMLErrorReporter.SEVERITY_WARNING);
            }
        }
    }
}
 
Example #7
Source File: XMLSchemaLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void initGrammarBucket(){
    if(fGrammarPool != null) {
        Grammar [] initialGrammars = fGrammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
        final int length = (initialGrammars != null) ? initialGrammars.length : 0;
        for (int i = 0; i < length; ++i) {
            // put this grammar into the bucket, along with grammars
            // imported by it (directly or indirectly)
            if (!fGrammarBucket.putGrammar((SchemaGrammar)(initialGrammars[i]), true)) {
                // REVISIT: a conflict between new grammar(s) and grammars
                // in the bucket. What to do? A warning? An exception?
                fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
                        "GrammarConflict", null,
                        XMLErrorReporter.SEVERITY_WARNING);
            }
        }
    }
}
 
Example #8
Source File: SoftReferenceGrammarPool.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public Grammar getGrammar(XMLGrammarDescription desc) {
    synchronized (fGrammars) {
        clean();
        int hash = hashCode(desc);
        int index = (hash & 0x7FFFFFFF) % fGrammars.length;
        for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
            Grammar tempGrammar = (Grammar) entry.grammar.get();
            /** If the soft reference has been cleared, remove this entry from the pool. */
            if (tempGrammar == null) {
                removeEntry(entry);
            }
            else if ((entry.hash == hash) && equals(entry.desc, desc)) {
                return tempGrammar;
            }
        }
        return null;
    }
}
 
Example #9
Source File: XMLGrammarCachingConfiguration.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
DTDGrammar parseDTD(XMLInputSource is)
            throws IOException {
    XMLEntityResolver resolver = getEntityResolver();
    if(resolver != null) {
        fDTDLoader.setEntityResolver(resolver);
    }
    fDTDLoader.setProperty(ERROR_REPORTER, fErrorReporter);

    // 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 the application...
    DTDGrammar grammar = (DTDGrammar)fDTDLoader.loadGrammar(is);
    // by default, hand it off to the grammar pool
    if (grammar != null) {
        fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_DTD,
                                  new Grammar[]{grammar});
    }

    return grammar;

}
 
Example #10
Source File: XSGrammarPool.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #11
Source File: XMLSchemaLoader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void initGrammarBucket(){
    if(fGrammarPool != null) {
        Grammar [] initialGrammars = fGrammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
        for (int i = 0; i < initialGrammars.length; i++) {
            // put this grammar into the bucket, along with grammars
            // imported by it (directly or indirectly)
            if (!fGrammarBucket.putGrammar((SchemaGrammar)(initialGrammars[i]), true)) {
                // REVISIT: a conflict between new grammar(s) and grammars
                // in the bucket. What to do? A warning? An exception?
                fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
                        "GrammarConflict", null,
                        XMLErrorReporter.SEVERITY_WARNING);
            }
        }
    }
}
 
Example #12
Source File: XSGrammarPool.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public XSModel toXSModel(short schemaVersion) {
    List<Grammar> 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 = list.toArray(new SchemaGrammar[size]);
    return toXSModel(gs, schemaVersion);
}
 
Example #13
Source File: XMLGrammarPoolImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public Grammar removeGrammar(XMLGrammarDescription desc) {
    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;
    }
}
 
Example #14
Source File: XMLGrammarPoolImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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.
 */
public Grammar removeGrammar(XMLGrammarDescription desc) {
    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;
    }
}
 
Example #15
Source File: XSGrammarPool.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #16
Source File: SoftReferenceGrammarPool.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the hash code value for the given grammar description.
 *
 * @param desc The grammar description
 * @return     The hash code value
 */
public int hashCode(XMLGrammarDescription desc) {
    if (desc instanceof XMLSchemaDescription) {
        final XMLSchemaDescription sd = (XMLSchemaDescription) desc;
        final String targetNamespace = sd.getTargetNamespace();
        final String expandedSystemId = sd.getExpandedSystemId();
        int hash = (targetNamespace != null) ? targetNamespace.hashCode() : 0;
        hash ^= (expandedSystemId != null) ? expandedSystemId.hashCode() : 0;
        return hash;
    }
    return desc.hashCode();
}
 
Example #17
Source File: XMLGrammarPoolImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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.
 */
public boolean containsGrammar(XMLGrammarDescription desc) {
    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;
}
}
 
Example #18
Source File: XMLGrammarCachingConfiguration.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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;

}
 
Example #19
Source File: SoftReferenceGrammarPool.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the hash code value for the given grammar description.
 *
 * @param desc The grammar description
 * @return     The hash code value
 */
public int hashCode(XMLGrammarDescription desc) {
    if (desc instanceof XMLSchemaDescription) {
        final XMLSchemaDescription sd = (XMLSchemaDescription) desc;
        final String targetNamespace = sd.getTargetNamespace();
        final String expandedSystemId = sd.getExpandedSystemId();
        int hash = (targetNamespace != null) ? targetNamespace.hashCode() : 0;
        hash ^= (expandedSystemId != null) ? expandedSystemId.hashCode() : 0;
        return hash;
    }
    return desc.hashCode();
}
 
Example #20
Source File: PreParseGrammarTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException, XNIException, IOException {
    File xsdf = new File(System.getProperty("test.src", ".") + "/test.xsd");
    InputStream is = new BufferedInputStream(new FileInputStream(xsdf));
    XMLInputSource xis = new XMLInputSource(null, null, null, is, null);
    XMLGrammarPreparser gp = new XMLGrammarPreparser();
    gp.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
    //The NullPointerException is observed on next call during ant task
    // execution
    Grammar res = gp.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, xis);
    System.out.println("Grammar preparsed successfully:" + res);
    return;
}
 
Example #21
Source File: XMLSchemaLoader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a Grammar object by parsing the contents of the
 * entity pointed to by source.
 *
 * @param source        the location of the entity which forms
 *                          the starting point of the grammar to be constructed.
 * @throws IOException      When a problem is encountered reading the entity
 *          XNIException    When a condition arises (such as a FatalError) that requires parsing
 *                              of the entity be terminated.
 */
public Grammar loadGrammar(XMLInputSource source)
throws IOException, XNIException {

    // REVISIT: this method should have a namespace parameter specified by
    // user. In this case we can easily detect if a schema asked to be loaded
    // is already in the local cache.

    reset(fLoaderConfig);
    fSettingsChanged = false;
    XSDDescription desc = new XSDDescription();
    desc.fContextType = XSDDescription.CONTEXT_PREPARSE;
    desc.setBaseSystemId(source.getBaseSystemId());
    desc.setLiteralSystemId( source.getSystemId());
    // none of the other fields make sense for preparsing
    Map locationPairs = new HashMap();
    // Process external schema location properties.
    // We don't call tokenizeSchemaLocationStr here, because we also want
    // to check whether the values are valid URI.
    processExternalHints(fExternalSchemas, fExternalNoNSSchema,
            locationPairs, fErrorReporter);
    SchemaGrammar grammar = loadSchema(desc, source, locationPairs);

    if(grammar != null && fGrammarPool != null) {
        fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, fGrammarBucket.getGrammars());
        // NOTE: we only need to verify full checking in case the schema was not provided via JAXP
        // since full checking already verified for all JAXP schemas
        if(fIsCheckedFully && fJAXPCache.get(grammar) != grammar) {
            XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);
        }
    }
    return grammar;
}
 
Example #22
Source File: CachingParserPool.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the grammar associated to the specified description.
 *
 * @param desc The description of the grammar.
 */
public Grammar getGrammar(XMLGrammarDescription desc) {

    if (super.containsGrammar(desc)) {
        return super.getGrammar(desc);
    }
    return null;

}
 
Example #23
Source File: XSDHandler.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public Grammar[] retrieveInitialGrammarSet(String grammarType) {
    if (grammarType == XMLGrammarDescription.XML_SCHEMA) {
        if (fInitialGrammarSet == null) {
            if (fGrammarBucket == null) {
                fInitialGrammarSet = new Grammar [] {SchemaGrammar.Schema4Annotations.INSTANCE};
            }
            else {
                SchemaGrammar [] schemaGrammars = fGrammarBucket.getGrammars();
                /**
                 * If the grammar bucket already contains the schema for schemas
                 * then we already have the definitions for the parts relevant
                 * to annotations.
                 */
                for (int i = 0; i < schemaGrammars.length; ++i) {
                    if (SchemaSymbols.URI_SCHEMAFORSCHEMA.equals(schemaGrammars[i].getTargetNamespace())) {
                        fInitialGrammarSet = schemaGrammars;
                        return fInitialGrammarSet;
                    }
                }
                Grammar [] grammars = new Grammar[schemaGrammars.length + 1];
                System.arraycopy(schemaGrammars, 0, grammars, 0, schemaGrammars.length);
                grammars[grammars.length - 1] = SchemaGrammar.Schema4Annotations.INSTANCE;
                fInitialGrammarSet = grammars;
            }
        }
        return fInitialGrammarSet;
    }
    return new Grammar[0];
}
 
Example #24
Source File: DOMEntityResolverWrapper.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Determines the type of resource being resolved **/
private String getType(XMLResourceIdentifier resourceIdentifier) {
    if (resourceIdentifier instanceof XMLGrammarDescription) {
        XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
        if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
            return XSD_TYPE;
        }
    }
    return XML_TYPE;
}
 
Example #25
Source File: SoftReferenceGrammarPool.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected Entry(int hash, int bucket, XMLGrammarDescription desc, Grammar grammar, Entry next, ReferenceQueue queue) {
    this.hash = hash;
    this.bucket = bucket;
    this.prev = null;
    this.next = next;
    if (next != null) {
        next.prev = this;
    }
    this.desc = desc;
    this.grammar = new SoftGrammarReference(this, grammar, queue);
}
 
Example #26
Source File: XMLSchemaLoader.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Grammar object by parsing the contents of the
 * entity pointed to by source.
 *
 * @param source        the location of the entity which forms
 *                          the starting point of the grammar to be constructed.
 * @throws IOException      When a problem is encountered reading the entity
 *          XNIException    When a condition arises (such as a FatalError) that requires parsing
 *                              of the entity be terminated.
 */
public Grammar loadGrammar(XMLInputSource source)
throws IOException, XNIException {

    // REVISIT: this method should have a namespace parameter specified by
    // user. In this case we can easily detect if a schema asked to be loaded
    // is already in the local cache.

    reset(fLoaderConfig);
    fSettingsChanged = false;
    XSDDescription desc = new XSDDescription();
    desc.fContextType = XSDDescription.CONTEXT_PREPARSE;
    desc.setBaseSystemId(source.getBaseSystemId());
    desc.setLiteralSystemId( source.getSystemId());
    // none of the other fields make sense for preparsing
    Map locationPairs = new HashMap();
    // Process external schema location properties.
    // We don't call tokenizeSchemaLocationStr here, because we also want
    // to check whether the values are valid URI.
    processExternalHints(fExternalSchemas, fExternalNoNSSchema,
            locationPairs, fErrorReporter);
    SchemaGrammar grammar = loadSchema(desc, source, locationPairs);

    if(grammar != null && fGrammarPool != null) {
        fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA, fGrammarBucket.getGrammars());
        // NOTE: we only need to verify full checking in case the schema was not provided via JAXP
        // since full checking already verified for all JAXP schemas
        if(fIsCheckedFully && fJAXPCache.get(grammar) != grammar) {
            XSConstraints.fullSchemaChecking(fGrammarBucket, fSubGroupHandler, fCMBuilder, fErrorReporter);
        }
    }
    return grammar;
}
 
Example #27
Source File: XSDHandler.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public Grammar[] retrieveInitialGrammarSet(String grammarType) {
    if (grammarType == XMLGrammarDescription.XML_SCHEMA) {
        if (fInitialGrammarSet == null) {
            if (fGrammarBucket == null) {
                fInitialGrammarSet = new Grammar [] {SchemaGrammar.Schema4Annotations.INSTANCE};
            }
            else {
                SchemaGrammar [] schemaGrammars = fGrammarBucket.getGrammars();
                /**
                 * If the grammar bucket already contains the schema for schemas
                 * then we already have the definitions for the parts relevant
                 * to annotations.
                 */
                for (int i = 0; i < schemaGrammars.length; ++i) {
                    if (SchemaSymbols.URI_SCHEMAFORSCHEMA.equals(schemaGrammars[i].getTargetNamespace())) {
                        fInitialGrammarSet = schemaGrammars;
                        return fInitialGrammarSet;
                    }
                }
                Grammar [] grammars = new Grammar[schemaGrammars.length + 1];
                System.arraycopy(schemaGrammars, 0, grammars, 0, schemaGrammars.length);
                grammars[grammars.length - 1] = SchemaGrammar.Schema4Annotations.INSTANCE;
                fInitialGrammarSet = grammars;
            }
        }
        return fInitialGrammarSet;
    }
    return new Grammar[0];
}
 
Example #28
Source File: SoftReferenceGrammarPool.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected Entry(int hash, int bucket, XMLGrammarDescription desc, Grammar grammar, Entry next, ReferenceQueue queue) {
    this.hash = hash;
    this.bucket = bucket;
    this.prev = null;
    this.next = next;
    if (next != null) {
        next.prev = this;
    }
    this.desc = desc;
    this.grammar = new SoftGrammarReference(this, grammar, queue);
}
 
Example #29
Source File: SoftReferenceGrammarPool.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 desc1 The grammar description
 * @param desc2 The grammar description of the grammar to be compared to
 * @return      True if the grammars are equal, otherwise false
 */
public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) {
    if (desc1 instanceof XMLSchemaDescription) {
        if (!(desc2 instanceof XMLSchemaDescription)) {
            return false;
        }
        final XMLSchemaDescription sd1 = (XMLSchemaDescription) desc1;
        final XMLSchemaDescription sd2 = (XMLSchemaDescription) desc2;
        final String targetNamespace = sd1.getTargetNamespace();
        if (targetNamespace != null) {
            if (!targetNamespace.equals(sd2.getTargetNamespace())) {
                return false;
            }
        }
        else if (sd2.getTargetNamespace() != null) {
            return false;
        }
        // The JAXP 1.3 spec says that the implementation can assume that
        // if two schema location hints are the same they always resolve
        // to the same document. In the default grammar pool implementation
        // we only look at the target namespaces. Here we also compare
        // location hints.
        final String expandedSystemId = sd1.getExpandedSystemId();
        if (expandedSystemId != null) {
            if (!expandedSystemId.equals(sd2.getExpandedSystemId())) {
                return false;
            }
        }
        else if (sd2.getExpandedSystemId() != null) {
            return false;
        }
        return true;
    }
    return desc1.equals(desc2);
}
 
Example #30
Source File: PreParseGrammarTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException, XNIException, IOException {
    File xsdf = new File(System.getProperty("test.src", ".") + "/test.xsd");
    InputStream is = new BufferedInputStream(new FileInputStream(xsdf));
    XMLInputSource xis = new XMLInputSource(null, null, null, is, null);
    XMLGrammarPreparser gp = new XMLGrammarPreparser();
    gp.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
    //The NullPointerException is observed on next call during ant task
    // execution
    Grammar res = gp.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, xis);
    System.out.println("Grammar preparsed successfully:" + res);
    return;
}