Search in sources :

Example 1 with Grammar

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

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

Aggregations

ValidateXmlActionHandler (com.intellij.xml.actions.validate.ValidateXmlActionHandler)2 SAXParser (javax.xml.parsers.SAXParser)2 Grammar (org.apache.xerces.xni.grammars.Grammar)2 XMLGrammarPool (org.apache.xerces.xni.grammars.XMLGrammarPool)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 Nullable (org.jetbrains.annotations.Nullable)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1