Search in sources :

Example 1 with XMLGrammarPreparser

use of org.apache.xerces.parsers.XMLGrammarPreparser in project jena by apache.

the class XSDDatatype method loadUserDefined.

/**
     * Internal implementation of loadUserDefined
     *
     * @param uri the absolute uri of the schema file to be loaded
     * @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
     * @param encoding the encoding of the source file (can be null)
     * @param tm the type mapper into which to load the definitions
     * @return a List of strings giving the uri's of the newly defined datatypes
     * @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
     * in default mode for load which may provide diagnostic output direct to stderr)
     */
private static List<String> loadUserDefined(XMLInputSource source, TypeMapper tm) throws DatatypeFormatException {
    XMLGrammarPreparser parser = new XMLGrammarPreparser();
    parser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
    try {
        XSGrammar xsg = (XSGrammar) parser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, source);
        org.apache.xerces.xs.XSModel xsm = xsg.toXSModel();
        XSNamedMap map = xsm.getComponents(XSTypeDefinition.SIMPLE_TYPE);
        int numDefs = map.getLength();
        ArrayList<String> names = new ArrayList<>(numDefs);
        for (int i = 0; i < numDefs; i++) {
            XSSimpleType xstype = (XSSimpleType) map.item(i);
            // Filter built in types - only needed for 2.6.0
            if (!XSD.equals(xstype.getNamespace())) {
                //xstype.derivedFrom()
                XSDDatatype definedType = new XSDGenericType(xstype, source.getSystemId());
                tm.registerDatatype(definedType);
                names.add(definedType.getURI());
            }
        }
        return names;
    } catch (Exception e) {
        // Temp
        e.printStackTrace();
        throw new DatatypeFormatException(e.toString());
    }
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) ArrayList(java.util.ArrayList) DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) XMLGrammarPreparser(org.apache.xerces.parsers.XMLGrammarPreparser) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) XSNamedMap(org.apache.xerces.xs.XSNamedMap)

Example 2 with XMLGrammarPreparser

use of org.apache.xerces.parsers.XMLGrammarPreparser in project webtools.sourceediting by eclipse.

the class XSDValidator method validate.

/**
 * Validate the XSD file specified by the URI.
 *
 * @param uri
 * 		The URI of the XSD file to validate.
 * @param inputStream
 * 		An input stream representing the XSD file to validate.
 * @param configuration
 * 		A configuration for this validation run.
 */
public ValidationReport validate(String uri, InputStream inputStream, XSDValidationConfiguration configuration) {
    if (configuration == null) {
        configuration = new XSDValidationConfiguration();
    }
    ValidationInfo valinfo = new ValidationInfo(uri);
    XSDErrorHandler errorHandler = new XSDErrorHandler(valinfo);
    try {
        XMLGrammarPreparser grammarPreparser = new XMLGrammarPreparser();
        grammarPreparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
        grammarPreparser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY, new XMLGrammarPoolImpl());
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE, false);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE, true);
        grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE, true);
        if (configuration.getFeature(XSDValidationConfiguration.HONOUR_ALL_SCHEMA_LOCATIONS)) {
            try {
                // $NON-NLS-1$
                grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + "honour-all-schemaLocations", true);
            } catch (Exception e) {
            // catch the exception and ignore
            }
        }
        if (configuration.getFeature(XSDValidationConfiguration.FULL_SCHEMA_CONFORMANCE)) {
            try {
                grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING, true);
            } catch (Exception e) {
            // ignore since we don't want to set it or can't.
            }
        }
        grammarPreparser.setErrorHandler(errorHandler);
        if (uriresolver != null) {
            XSDEntityResolver resolver = new XSDEntityResolver(uriresolver, uri);
            if (resolver != null) {
                grammarPreparser.setEntityResolver(resolver);
            }
        }
        try {
            XMLInputSource is = new XMLInputSource(null, uri, uri, inputStream, null);
            grammarPreparser.getLoader(XMLGrammarDescription.XML_SCHEMA);
            grammarPreparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, is);
        } catch (Exception e) {
        // parser will return null pointer exception if the document is structurally invalid
        // TODO: log error message
        // System.out.println(e);
        }
    } catch (Exception e) {
    // TODO: log error.
    // System.out.println(e);
    }
    return valinfo;
}
Also used : ValidationInfo(org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo) XMLGrammarPreparser(org.apache.xerces.parsers.XMLGrammarPreparser) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) XMLGrammarPoolImpl(org.apache.xerces.util.XMLGrammarPoolImpl) IOException(java.io.IOException) XMLParseException(org.apache.xerces.xni.parser.XMLParseException) XNIException(org.apache.xerces.xni.XNIException)

Example 3 with XMLGrammarPreparser

use of org.apache.xerces.parsers.XMLGrammarPreparser in project iaf by ibissource.

the class MyErrorHandler method preparse.

private synchronized PreparseResult preparse(String schemasId, List<Schema> schemas) throws ConfigurationException {
    SymbolTable symbolTable = getSymbolTable();
    XMLGrammarPool grammarPool = new XMLGrammarPoolImpl();
    Set<String> namespaceSet = new HashSet<String>();
    XMLGrammarPreparser preparser = new XMLGrammarPreparser(symbolTable);
    preparser.setEntityResolver(new ClassLoaderXmlEntityResolver(this));
    preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
    preparser.setProperty(GRAMMAR_POOL, grammarPool);
    preparser.setFeature(NAMESPACES_FEATURE_ID, true);
    preparser.setFeature(VALIDATION_FEATURE_ID, true);
    preparser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
    preparser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, isFullSchemaChecking());
    try {
        preparser.setProperty(XML_SCHEMA_VERSION_PROPERTY, isXmlSchema1_0() ? Constants.W3C_XML_SCHEMA10EX_NS_URI : Constants.W3C_XML_SCHEMA11_NS_URI);
    } catch (NoSuchFieldError e) {
        String msg = "Cannot set property [" + XML_SCHEMA_VERSION_PROPERTY + "], requested xmlSchemaVersion [" + getXmlSchemaVersion() + "] xercesVersion [" + org.apache.xerces.impl.Version.getVersion() + "]";
        if (isXmlSchema1_0()) {
            log.warn(msg + ", assuming XML-Schema version 1.0 will be supported", e);
        } else {
            throw new ConfigurationException(msg, e);
        }
    }
    MyErrorHandler errorHandler = new MyErrorHandler(this);
    errorHandler.warn = warn;
    preparser.setErrorHandler(errorHandler);
    for (Schema schema : schemas) {
        Grammar grammar = preparse(preparser, schemasId, schema);
        registerNamespaces(grammar, namespaceSet);
    }
    grammarPool.lockPool();
    PreparseResult preparseResult = new PreparseResult();
    preparseResult.setSchemasId(schemasId);
    preparseResult.setSymbolTable(symbolTable);
    preparseResult.setGrammarPool(grammarPool);
    preparseResult.setNamespaceSet(namespaceSet);
    return preparseResult;
}
Also used : XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) SymbolTable(org.apache.xerces.util.SymbolTable) ShadowedSymbolTable(org.apache.xerces.util.ShadowedSymbolTable) Grammar(org.apache.xerces.xni.grammars.Grammar) SchemaGrammar(org.apache.xerces.impl.xs.SchemaGrammar) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) XMLGrammarPreparser(org.apache.xerces.parsers.XMLGrammarPreparser) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XMLGrammarPoolImpl(org.apache.xerces.util.XMLGrammarPoolImpl) HashSet(java.util.HashSet) ClassLoaderXmlEntityResolver(nl.nn.adapterframework.xml.ClassLoaderXmlEntityResolver)

Aggregations

XMLGrammarPreparser (org.apache.xerces.parsers.XMLGrammarPreparser)3 XMLGrammarPoolImpl (org.apache.xerces.util.XMLGrammarPoolImpl)2 XSGrammar (org.apache.xerces.xni.grammars.XSGrammar)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 ClassLoaderXmlEntityResolver (nl.nn.adapterframework.xml.ClassLoaderXmlEntityResolver)1 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)1 SchemaGrammar (org.apache.xerces.impl.xs.SchemaGrammar)1 ShadowedSymbolTable (org.apache.xerces.util.ShadowedSymbolTable)1 SymbolTable (org.apache.xerces.util.SymbolTable)1 XNIException (org.apache.xerces.xni.XNIException)1 Grammar (org.apache.xerces.xni.grammars.Grammar)1 XMLGrammarPool (org.apache.xerces.xni.grammars.XMLGrammarPool)1 XMLInputSource (org.apache.xerces.xni.parser.XMLInputSource)1 XMLParseException (org.apache.xerces.xni.parser.XMLParseException)1 XSNamedMap (org.apache.xerces.xs.XSNamedMap)1 ValidationInfo (org.eclipse.wst.xml.core.internal.validation.core.ValidationInfo)1