Search in sources :

Example 1 with XmlErrorHandler

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

the class TestWebXml method doTestValidateVersion.

private void doTestValidateVersion(String version) throws IOException, SAXException {
    WebXml webxml = new WebXml();
    // Special cases
    if ("2.2".equals(version)) {
        webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
    } else if ("2.3".equals(version)) {
        webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
    } else {
        webxml.setVersion(version);
    }
    // Merged web.xml that is published as MERGED_WEB_XML context attribute
    // in the simplest case consists of webapp's web.xml file
    // plus the default conf/web.xml one.
    Set<WebXml> defaults = new HashSet<>();
    defaults.add(getDefaultWebXmlFragment());
    webxml.merge(defaults);
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
    XmlErrorHandler handler = new XmlErrorHandler();
    digester.setErrorHandler(handler);
    InputSource is = new InputSource(new StringReader(webxml.toXml()));
    WebXml webxmlResult = new WebXml();
    digester.push(webxmlResult);
    digester.parse(is);
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
    Assert.assertEquals(version, webxml.getVersion());
    Assert.assertEquals(version, webxmlResult.getVersion());
}
Also used : InputSource(org.xml.sax.InputSource) Digester(org.apache.tomcat.util.digester.Digester) StringReader(java.io.StringReader) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) HashSet(java.util.HashSet)

Example 2 with XmlErrorHandler

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

the class TldParser method parse.

public TaglibXml parse(TldResourcePath path) throws IOException, SAXException {
    ClassLoader original;
    if (Constants.IS_SECURITY_ENABLED) {
        PrivilegedGetTccl pa = new PrivilegedGetTccl();
        original = AccessController.doPrivileged(pa);
    } else {
        original = Thread.currentThread().getContextClassLoader();
    }
    try (InputStream is = path.openStream()) {
        if (Constants.IS_SECURITY_ENABLED) {
            PrivilegedSetTccl pa = new PrivilegedSetTccl(TldParser.class.getClassLoader());
            AccessController.doPrivileged(pa);
        } else {
            Thread.currentThread().setContextClassLoader(TldParser.class.getClassLoader());
        }
        XmlErrorHandler handler = new XmlErrorHandler();
        digester.setErrorHandler(handler);
        TaglibXml taglibXml = new TaglibXml();
        digester.push(taglibXml);
        InputSource source = new InputSource(path.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();
            }
        }
        return taglibXml;
    } finally {
        digester.reset();
        if (Constants.IS_SECURITY_ENABLED) {
            PrivilegedSetTccl pa = new PrivilegedSetTccl(original);
            AccessController.doPrivileged(pa);
        } else {
            Thread.currentThread().setContextClassLoader(original);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) PrivilegedGetTccl(org.apache.tomcat.util.security.PrivilegedGetTccl) InputStream(java.io.InputStream) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) PrivilegedSetTccl(org.apache.tomcat.util.security.PrivilegedSetTccl)

Example 3 with XmlErrorHandler

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

the class TestSchemaValidation method testWebapp_3_0.

@Test
public void testWebapp_3_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-3.0/WEB-INF/web.xml"));
    Assert.assertEquals("3.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)

Example 4 with XmlErrorHandler

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

the class TestSchemaValidation method testWebapp_2_5.

@Test
public void testWebapp_2_5() 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-2.5/WEB-INF/web.xml"));
    Assert.assertEquals("2.5", 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 5 with XmlErrorHandler

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

the class TestSchemaValidation method testWebapp_2_2.

@Test
public void testWebapp_2_2() 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-2.2/WEB-INF/web.xml"));
    Assert.assertEquals("2.2", desc.getVersion());
    Assert.assertEquals(XmlIdentifiers.WEB_22_PUBLIC, desc.getPublicId());
    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