Search in sources :

Example 1 with XMLGrammarPool

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

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

the class ValidateXmlActionHandler method createParser.

protected SAXParser createParser() throws SAXException, ParserConfigurationException {
    if (!needsDtdChecking() && !needsSchemaChecking() && !myForceChecking) {
        return null;
    }
    SAXParserFactory factory = new SAXParserFactoryImpl();
    boolean schemaChecking = false;
    if (hasDtdDeclaration()) {
        factory.setValidating(true);
    }
    if (needsSchemaChecking()) {
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        //jdk 1.5 API
        try {
            factory.setXIncludeAware(true);
        } catch (NoSuchMethodError ignore) {
        }
        schemaChecking = true;
    }
    try {
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    } catch (Exception ignore) {
    }
    SAXParser parser = factory.newSAXParser();
    parser.setProperty(ENTITY_RESOLVER_PROPERTY_NAME, myXmlResourceResolver);
    try {
        parser.getXMLReader().setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    } catch (Exception ignore) {
    }
    if (schemaChecking) {
        // when dtd checking schema refs could not be validated @see http://marc.theaimsgroup.com/?l=xerces-j-user&m=112504202423704&w=2
        XMLGrammarPool grammarPool = getGrammarPool(myFile, myForceChecking);
        configureEntityManager(myFile, parser);
        parser.getXMLReader().setProperty(GRAMMAR_FEATURE_ID, grammarPool);
    }
    try {
        if (schemaChecking) {
            parser.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, JAXPConstants.W3C_XML_SCHEMA);
            parser.getXMLReader().setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
            if (Boolean.TRUE.equals(Boolean.getBoolean(XmlResourceResolver.HONOUR_ALL_SCHEMA_LOCATIONS_PROPERTY_KEY))) {
                parser.getXMLReader().setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);
            }
            parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/warn-on-undeclared-elemdef", Boolean.TRUE);
            parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/warn-on-duplicate-attdef", Boolean.TRUE);
        }
        parser.getXMLReader().setFeature("http://apache.org/xml/features/warn-on-duplicate-entitydef", Boolean.TRUE);
        parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/unparsed-entity-checking", Boolean.FALSE);
    } catch (SAXNotRecognizedException ex) {
        // it is possible to continue work with configured parser
        LOG.info("Xml parser installation seems screwed", ex);
    }
    return parser;
}
Also used : XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) SAXParserFactoryImpl(org.apache.xerces.jaxp.SAXParserFactoryImpl) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with XMLGrammarPool

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

the class XsContentDFA method getXSModel.

@Nullable
private static XSModel getXSModel(XmlFile file) {
    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 ErrorReporter(handler) {

        int count;

        @Override
        public void processError(SAXParseException ex, ValidateXmlActionHandler.ProblemType warning) throws SAXException {
            if (warning != ValidateXmlActionHandler.ProblemType.WARNING && count++ > 100) {
                throw new SAXException(ex);
            }
        }

        @Override
        public boolean isUniqueProblem(SAXParseException e) {
            return true;
        }
    });
    handler.doValidate(file);
    XMLGrammarPool grammarPool = ValidateXmlActionHandler.getGrammarPool(file);
    if (grammarPool == null) {
        return null;
    }
    Grammar[] grammars = grammarPool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
    return grammars.length == 0 ? null : ((XSGrammar) grammars[0]).toXSModel(ContainerUtil.map(grammars, grammar -> (XSGrammar) grammar, new XSGrammar[0]));
}
Also used : XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) ErrorReporter(com.intellij.xml.actions.validate.ErrorReporter) SAXParseException(org.xml.sax.SAXParseException) 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) SAXException(org.xml.sax.SAXException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with XMLGrammarPool

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

the class ValidateXmlActionHandler method getGrammarPool.

public static XMLGrammarPool getGrammarPool(XmlFile file, boolean forceChecking) {
    final XMLGrammarPool previousGrammarPool = getGrammarPool(file);
    XMLGrammarPool grammarPool = null;
    // check if the pool is valid
    if (!forceChecking && !isValidationDependentFilesOutOfDate(file)) {
        grammarPool = previousGrammarPool;
    }
    if (grammarPool == null) {
        invalidateEntityManager(file);
        grammarPool = new XMLGrammarPoolImpl();
        file.putUserData(GRAMMAR_POOL_KEY, grammarPool);
    }
    return grammarPool;
}
Also used : XMLGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool) XMLGrammarPoolImpl(org.apache.xerces.util.XMLGrammarPoolImpl)

Aggregations

XMLGrammarPool (org.apache.xerces.xni.grammars.XMLGrammarPool)4 SAXParser (javax.xml.parsers.SAXParser)3 ValidateXmlActionHandler (com.intellij.xml.actions.validate.ValidateXmlActionHandler)2 Grammar (org.apache.xerces.xni.grammars.Grammar)2 XSGrammar (org.apache.xerces.xni.grammars.XSGrammar)2 XmlFile (com.intellij.psi.xml.XmlFile)1 ErrorReporter (com.intellij.xml.actions.validate.ErrorReporter)1 TestErrorReporter (com.intellij.xml.actions.validate.TestErrorReporter)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 SAXParserFactoryImpl (org.apache.xerces.jaxp.SAXParserFactoryImpl)1 XMLGrammarPoolImpl (org.apache.xerces.util.XMLGrammarPoolImpl)1 Nullable (org.jetbrains.annotations.Nullable)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1