Search in sources :

Example 6 with XmlErrorHandler

use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.

the class WebXmlParser method parseWebXml.

public boolean parseWebXml(InputSource source, WebXml dest, boolean fragment) {
    boolean ok = true;
    if (source == null) {
        return ok;
    }
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester;
    WebRuleSet ruleSet;
    if (fragment) {
        digester = webFragmentDigester;
        ruleSet = webFragmentRuleSet;
    } else {
        digester = webDigester;
        ruleSet = webRuleSet;
    }
    digester.push(dest);
    digester.setErrorHandler(handler);
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("webXmlParser.applicationStart", source.getSystemId()));
    }
    try {
        digester.parse(source);
        if (handler.getWarnings().size() > 0 || handler.getErrors().size() > 0) {
            ok = false;
            handler.logFindings(log, source.getSystemId());
        }
    } catch (SAXParseException e) {
        log.error(sm.getString("webXmlParser.applicationParse", source.getSystemId()), e);
        log.error(sm.getString("webXmlParser.applicationPosition", "" + e.getLineNumber(), "" + e.getColumnNumber()));
        ok = false;
    } catch (Exception e) {
        log.error(sm.getString("webXmlParser.applicationParse", source.getSystemId()), e);
        ok = false;
    } finally {
        InputSourceUtil.close(source);
        digester.reset();
        ruleSet.recycle();
    }
    return ok;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException)

Example 7 with XmlErrorHandler

use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.

the class ContextConfig method processContextConfig.

/**
     * Process a context.xml.
     * @param digester The digester that will be used for XML parsing
     * @param contextXml The URL to the context.xml configuration
     */
protected void processContextConfig(Digester digester, URL contextXml) {
    if (log.isDebugEnabled()) {
        log.debug("Processing context [" + context.getName() + "] configuration file [" + contextXml + "]");
    }
    InputSource source = null;
    InputStream stream = null;
    try {
        source = new InputSource(contextXml.toString());
        URLConnection xmlConn = contextXml.openConnection();
        xmlConn.setUseCaches(false);
        stream = xmlConn.getInputStream();
    } catch (Exception e) {
        log.error(sm.getString("contextConfig.contextMissing", contextXml), e);
    }
    if (source == null) {
        return;
    }
    try {
        source.setByteStream(stream);
        digester.setClassLoader(this.getClass().getClassLoader());
        digester.setUseContextClassLoader(false);
        digester.push(context.getParent());
        digester.push(context);
        XmlErrorHandler errorHandler = new XmlErrorHandler();
        digester.setErrorHandler(errorHandler);
        digester.parse(source);
        if (errorHandler.getWarnings().size() > 0 || errorHandler.getErrors().size() > 0) {
            errorHandler.logFindings(log, contextXml.toString());
            ok = false;
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed context [" + context.getName() + "] configuration file [" + contextXml + "]");
        }
    } catch (SAXParseException e) {
        log.error(sm.getString("contextConfig.contextParse", context.getName()), e);
        log.error(sm.getString("contextConfig.defaultPosition", "" + e.getLineNumber(), "" + e.getColumnNumber()));
        ok = false;
    } catch (Exception e) {
        log.error(sm.getString("contextConfig.contextParse", context.getName()), e);
        ok = false;
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            log.error(sm.getString("contextConfig.contextClose"), e);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) URLConnection(java.net.URLConnection) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException)

Example 8 with XmlErrorHandler

use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.

the class TagPluginParser method parse.

public void parse(URL url) throws IOException, SAXException {
    try (InputStream is = url.openStream()) {
        XmlErrorHandler handler = new XmlErrorHandler();
        digester.setErrorHandler(handler);
        digester.push(this);
        InputSource source = new InputSource(url.toExternalForm());
        source.setByteStream(is);
        digester.parse(source);
        if (!handler.getWarnings().isEmpty() || !handler.getErrors().isEmpty()) {
            handler.logFindings(log, source.getSystemId());
            if (!handler.getErrors().isEmpty()) {
                // throw the first to indicate there was a error during processing
                throw handler.getErrors().iterator().next();
            }
        }
    } finally {
        digester.reset();
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler)

Example 9 with XmlErrorHandler

use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.

the class TestSchemaValidation method testWebapp_3_1.

@Test
public void testWebapp_3_1() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-3.1/WEB-INF/web.xml"));
    Assert.assertEquals("3.1", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Example 10 with XmlErrorHandler

use of org.apache.tomcat.util.descriptor.XmlErrorHandler in project tomcat by apache.

the class TestSchemaValidation method testWebapp_4_0.

@Test
public void testWebapp_4_0() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-4.0/WEB-INF/web.xml"));
    Assert.assertEquals("4.0", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Aggregations

XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)14 Digester (org.apache.tomcat.util.digester.Digester)11 File (java.io.File)9 WebRuleSet (org.apache.tomcat.util.descriptor.web.WebRuleSet)8 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)8 Test (org.junit.Test)8 InputSource (org.xml.sax.InputSource)5 InputStream (java.io.InputStream)3 IOException (java.io.IOException)2 SAXParseException (org.xml.sax.SAXParseException)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URLConnection (java.net.URLConnection)1 HashSet (java.util.HashSet)1 ClassFormatException (org.apache.tomcat.util.bcel.classfile.ClassFormatException)1 PrivilegedGetTccl (org.apache.tomcat.util.security.PrivilegedGetTccl)1 PrivilegedSetTccl (org.apache.tomcat.util.security.PrivilegedSetTccl)1