Java Code Examples for org.apache.tomcat.util.descriptor.DigesterFactory#newDigester()

The following examples show how to use org.apache.tomcat.util.descriptor.DigesterFactory#newDigester() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TldConfig.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Create (if necessary) and return a Digester configured to process the
 * tld.
 */
private static synchronized Digester createTldDigester(boolean validation,
        boolean blockExternal) {

    Digester digester;
    int cacheIndex = 0;
    if (validation) {
        cacheIndex += 1;
    }
    if (blockExternal) {
        cacheIndex += 2;
    }
    digester = tldDigesters[cacheIndex];
    if (digester == null) {
        digester = DigesterFactory.newDigester(validation,
                true, new TldRuleSet(), blockExternal);
        digester.getParser();
        tldDigesters[cacheIndex] = digester;
    }
    return digester;
}
 
Example 2
Source File: TldConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Create (if necessary) and return a Digester configured to process the
 * tld.
 */
private static synchronized Digester createTldDigester(boolean validation,
        boolean blockExternal) {

    Digester digester;
    int cacheIndex = 0;
    if (validation) {
        cacheIndex += 1;
    }
    if (blockExternal) {
        cacheIndex += 2;
    }
    digester = tldDigesters[cacheIndex];
    if (digester == null) {
        digester = DigesterFactory.newDigester(validation,
                true, new TldRuleSet(), blockExternal);
        digester.getParser();
        tldDigesters[cacheIndex] = digester;
    }
    return digester;
}
 
Example 3
Source File: ContextConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Create and return a Digester configured to process the
 * web application deployment descriptor (web.xml).
 */
public void createWebXmlDigester(boolean namespaceAware,
        boolean validation) {

    boolean blockExternal = context.getXmlBlockExternal();
    
    webRuleSet = new WebRuleSet(false);
    webDigester = DigesterFactory.newDigester(validation,
            namespaceAware, webRuleSet, blockExternal);
    webDigester.getParser();

    webFragmentRuleSet = new WebRuleSet(true);
    webFragmentDigester = DigesterFactory.newDigester(validation,
            namespaceAware, webFragmentRuleSet, blockExternal);
    webFragmentDigester.getParser();
}
 
Example 4
Source File: TestSchemaValidation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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());
}
 
Example 5
Source File: TestSchemaValidation.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 6
Source File: TestSchemaValidation.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 7
Source File: TestSchemaValidation.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testWebapp_2_3() 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.3/WEB-INF/web.xml"));
    Assert.assertEquals("2.3", desc.getVersion());
    Assert.assertEquals(XmlIdentifiers.WEB_23_PUBLIC, desc.getPublicId());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
 
Example 8
Source File: TestSchemaValidation.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 9
Source File: TestWebXml.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
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<WebXml>();
    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());
}
 
Example 10
Source File: TestSchemaValidation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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());
}
 
Example 11
Source File: ValidatorTask.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Execute the specified command.  This logic only performs the common
 * attribute validation required by all subclasses; it does not perform
 * any functional logic directly.
 *
 * @exception BuildException if a validation error occurs
 */
@Override
public void execute() throws BuildException {

    if (path == null) {
        throw new BuildException("Must specify 'path'");
    }

    File file = new File(path, "WEB-INF/web.xml");
    if (!file.canRead()) {
        throw new BuildException("Cannot find web.xml");
    }

    // Commons-logging likes having the context classloader set
    ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader
        (ValidatorTask.class.getClassLoader());

    // Called through trusted manager interface. If running under a
    // SecurityManager assume that untrusted applications may be deployed.
    Digester digester = DigesterFactory.newDigester(
            true, true, null, Globals.IS_SECURITY_ENABLED);
    try (InputStream stream = new BufferedInputStream(new FileInputStream(file.getCanonicalFile()));) {
        InputSource is = new InputSource(file.toURI().toURL().toExternalForm());
        is.setByteStream(stream);
        digester.parse(is);
        handleOutput("web.xml validated");
    } catch (Exception e) {
        if (isFailOnError()) {
            throw new BuildException("Validation failure", e);
        } else {
            handleErrorOutput("Validation failure: " + e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCL);
        closeRedirector();
    }

}
 
Example 12
Source File: TestSchemaValidation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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());
}
 
Example 13
Source File: TestSchemaValidation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testWebapp_2_4() 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.4/WEB-INF/web.xml"));
    Assert.assertEquals("2.4", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
 
Example 14
Source File: TestSchemaValidation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testWebapp_2_3() 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.3/WEB-INF/web.xml"));
    Assert.assertEquals("2.3", desc.getVersion());
    Assert.assertEquals(XmlIdentifiers.WEB_23_PUBLIC, desc.getPublicId());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
 
Example 15
Source File: TestSchemaValidation.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@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());
}
 
Example 16
Source File: TestSchemaValidation.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testWebapp_2_3() 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.3/WEB-INF/web.xml"));
    Assert.assertEquals("2.3", desc.getVersion());
    Assert.assertEquals(XmlIdentifiers.WEB_23_PUBLIC, desc.getPublicId());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
 
Example 17
Source File: TestWebXml.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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());
}
 
Example 18
Source File: WebXmlParser.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public WebXmlParser(boolean namespaceAware, boolean validation,
        boolean blockExternal) {
    webRuleSet = new WebRuleSet(false);
    webDigester = DigesterFactory.newDigester(validation,
            namespaceAware, webRuleSet, blockExternal);
    webDigester.getParser();

    webFragmentRuleSet = new WebRuleSet(true);
    webFragmentDigester = DigesterFactory.newDigester(validation,
            namespaceAware, webFragmentRuleSet, blockExternal);
    webFragmentDigester.getParser();
}
 
Example 19
Source File: TagPluginParser.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public TagPluginParser(ServletContext context, boolean blockExternal) {
    digester = DigesterFactory.newDigester(
            false, false, new TagPluginRuleSet(), blockExternal);
    digester.setClassLoader(context.getClassLoader());
}
 
Example 20
Source File: TldParser.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public TldParser(boolean namespaceAware, boolean validation, RuleSet ruleSet,
        boolean blockExternal) {
    digester = DigesterFactory.newDigester(
            validation, namespaceAware, ruleSet, blockExternal);
}