Search in sources :

Example 1 with XSGrammar

use of org.apache.xerces.xni.grammars.XSGrammar in project intellij-community by JetBrains.

the class XmlConstraintsTest method getXSModel.

private XSModel getXSModel(String... files) {
    myFixture.configureByFiles(files);
    XmlFile file = (XmlFile) myFixture.getFile();
    ValidateXmlActionHandler handler = new ValidateXmlActionHandler(false) {

        @Override
        protected SAXParser createParser() throws SAXException, ParserConfigurationException {
            SAXParser parser = super.createParser();
            parser.getXMLReader().setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE, true);
            return parser;
        }
    };
    handler.setErrorReporter(new TestErrorReporter(handler));
    handler.doValidate(file);
    XMLGrammarPool grammarPool = ValidateXmlActionHandler.getGrammarPool(file);
    assert grammarPool != null;
    Grammar[] grammars = grammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
    XSGrammar grammar = (XSGrammar) grammars[0];
    return grammar.toXSModel();
}
Also used : XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) TestErrorReporter(com.intellij.xml.actions.validate.TestErrorReporter) XmlFile(com.intellij.psi.xml.XmlFile) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) SAXParser(javax.xml.parsers.SAXParser) ValidateXmlActionHandler(com.intellij.xml.actions.validate.ValidateXmlActionHandler) Grammar(org.apache.xerces.xni.grammars.Grammar) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar)

Example 2 with XSGrammar

use of org.apache.xerces.xni.grammars.XSGrammar 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 3 with XSGrammar

use of org.apache.xerces.xni.grammars.XSGrammar in project webtools.sourceediting by eclipse.

the class CompleteNewApiTest method getGrammar.

// private Schema getSchema(InputStream schemaIs) throws SAXException {
// SchemaFactory sf = SchemaFactory
// .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Schema schema = sf.newSchema(new StreamSource(schemaIs));
// return schema;
// }
protected XSModel getGrammar(URL schemaURL) throws IOException, SAXException {
    InputStream schemaIs = schemaURL.openStream();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new StreamSource(schemaIs));
    XSGrammarPoolContainer poolContainer = (XSGrammarPoolContainer) schema;
    XMLGrammarPool pool = poolContainer.getGrammarPool();
    Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
    XSGrammar[] xsGrammars = new XSGrammar[grammars.length];
    System.arraycopy(grammars, 0, xsGrammars, 0, grammars.length);
    return xsGrammars[0].toXSModel(xsGrammars);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) InputStream(java.io.InputStream) XSGrammarPoolContainer(org.apache.xerces.jaxp.validation.XSGrammarPoolContainer) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) Grammar(org.apache.xerces.xni.grammars.Grammar) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar)

Example 4 with XSGrammar

use of org.apache.xerces.xni.grammars.XSGrammar in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathTest method getGrammar.

protected XSModel getGrammar(URL schemaURL) throws IOException, SAXException {
    InputStream schemaIs = testResolve(schemaURL);
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new StreamSource(schemaIs));
    XSGrammarPoolContainer poolContainer = (XSGrammarPoolContainer) schema;
    XMLGrammarPool pool = poolContainer.getGrammarPool();
    Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
    XSGrammar[] xsGrammars = new XSGrammar[grammars.length];
    System.arraycopy(grammars, 0, xsGrammars, 0, grammars.length);
    return xsGrammars[0].toXSModel(xsGrammars);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XSGrammarPoolContainer(org.apache.xerces.jaxp.validation.XSGrammarPoolContainer) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) Grammar(org.apache.xerces.xni.grammars.Grammar)

Example 5 with XSGrammar

use of org.apache.xerces.xni.grammars.XSGrammar in project iaf by ibissource.

the class JavaxValidationContext method createValidationContext.

@Override
public JavaxValidationContext createValidationContext(PipeLineSession session, RootValidations rootValidations, Map<List<String>, List<String>> invalidRootNamespaces) throws ConfigurationException, PipeRunException {
    // clear session variables
    super.createValidationContext(session, rootValidations, invalidRootNamespaces);
    String schemasId;
    Schema schema;
    Set<String> namespaceSet = new HashSet<String>();
    List<XSModel> xsModels = null;
    start();
    schemasId = schemasProvider.getSchemasId();
    if (schemasId == null) {
        schemasId = schemasProvider.getSchemasId(session);
        getSchemaObject(schemasId, schemasProvider.getSchemas(session));
    }
    schema = javaxSchemas.get(schemasId);
    if (schema != null) {
        org.apache.xerces.jaxp.validation.XSGrammarPoolContainer xercesSchema = (org.apache.xerces.jaxp.validation.XSGrammarPoolContainer) schema;
        xercesSchema.getGrammarPool();
        xsModels = new LinkedList<XSModel>();
        Grammar[] grammars = xercesSchema.getGrammarPool().retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
        for (int i = 0; i < grammars.length; i++) {
            XSModel model = ((XSGrammar) grammars[i]).toXSModel();
            xsModels.add(model);
            StringList namespaces = model.getNamespaces();
            for (int j = 0; j < namespaces.getLength(); j++) {
                String namespace = namespaces.item(j);
                namespaceSet.add(namespace);
            }
        }
    }
    JavaxValidationContext result = new JavaxValidationContext(schemasId, schema, namespaceSet, xsModels);
    result.init(schemasProvider, schemasId, namespaceSet, rootValidations, invalidRootNamespaces, ignoreUnknownNamespaces);
    return result;
}
Also used : StringList(org.apache.xerces.xs.StringList) Schema(javax.xml.validation.Schema) Grammar(org.apache.xerces.xni.grammars.Grammar) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) XSModel(org.apache.xerces.xs.XSModel) HashSet(java.util.HashSet)

Aggregations

XSGrammar (org.apache.xerces.xni.grammars.XSGrammar)8 Grammar (org.apache.xerces.xni.grammars.Grammar)7 Schema (javax.xml.validation.Schema)5 XMLGrammarPool (org.apache.xerces.xni.grammars.XMLGrammarPool)5 InputStream (java.io.InputStream)3 StreamSource (javax.xml.transform.stream.StreamSource)3 SchemaFactory (javax.xml.validation.SchemaFactory)3 XSGrammarPoolContainer (org.apache.xerces.jaxp.validation.XSGrammarPoolContainer)3 ValidateXmlActionHandler (com.intellij.xml.actions.validate.ValidateXmlActionHandler)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashSet (java.util.HashSet)2 SAXParser (javax.xml.parsers.SAXParser)2 StringList (org.apache.xerces.xs.StringList)2 XSModel (org.apache.xerces.xs.XSModel)2 XmlFile (com.intellij.psi.xml.XmlFile)1 ErrorReporter (com.intellij.xml.actions.validate.ErrorReporter)1 TestErrorReporter (com.intellij.xml.actions.validate.TestErrorReporter)1 ArrayList (java.util.ArrayList)1 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)1 XMLGrammarPreparser (org.apache.xerces.parsers.XMLGrammarPreparser)1