Search in sources :

Example 1 with Digester

use of org.apache.commons.digester.Digester in project sonarqube by SonarSource.

the class ActionServlet method initServlet.

/**
     * <p>Initialize the servlet mapping under which our controller servlet is
     * being accessed.  This will be used in the <code>&html:form&gt;</code>
     * tag to generate correct destination URLs for form submissions.</p>
     *
     * @throws ServletException if error happens while scanning web.xml
     */
protected void initServlet() throws ServletException {
    // Remember our servlet name
    this.servletName = getServletConfig().getServletName();
    // Prepare a Digester to scan the web application deployment descriptor
    Digester digester = new Digester();
    digester.push(this);
    digester.setNamespaceAware(true);
    digester.setValidating(false);
    // Register our local copy of the DTDs that we can find
    for (int i = 0; i < registrations.length; i += 2) {
        URL url = this.getClass().getResource(registrations[i + 1]);
        if (url != null) {
            digester.register(registrations[i], url.toString());
        }
    }
    // Configure the processing rules that we need
    digester.addCallMethod("web-app/servlet-mapping", "addServletMapping", 2);
    digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
    digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);
    // Process the web application deployment descriptor
    if (log.isDebugEnabled()) {
        log.debug("Scanning web.xml for controller servlet mapping");
    }
    InputStream input = getServletContext().getResourceAsStream("/WEB-INF/web.xml");
    if (input == null) {
        log.error(internal.getMessage("configWebXml"));
        throw new ServletException(internal.getMessage("configWebXml"));
    }
    try {
        digester.parse(input);
    } catch (IOException e) {
        log.error(internal.getMessage("configWebXml"), e);
        throw new ServletException(e);
    } catch (SAXException e) {
        log.error(internal.getMessage("configWebXml"), e);
        throw new ServletException(e);
    } finally {
        try {
            input.close();
        } catch (IOException e) {
            log.error(internal.getMessage("configWebXml"), e);
            throw new ServletException(e);
        }
    }
    // Record a servlet context attribute (if appropriate)
    if (log.isDebugEnabled()) {
        log.debug("Mapping for servlet '" + servletName + "' = '" + servletMapping + "'");
    }
    if (servletMapping != null) {
        getServletContext().setAttribute(Globals.SERVLET_KEY, servletMapping);
    }
}
Also used : ServletException(javax.servlet.ServletException) InputStream(java.io.InputStream) Digester(org.apache.commons.digester.Digester) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 2 with Digester

use of org.apache.commons.digester.Digester in project sonarqube by SonarSource.

the class ActionServlet method initModuleConfig.

/**
     * <p>Initialize the module configuration information for the specified
     * module.</p>
     *
     * @param prefix Module prefix for this module
     * @param paths  Comma-separated list of context-relative resource path(s)
     *               for this modules's configuration resource(s)
     * @return The new module configuration instance.
     * @throws ServletException if initialization cannot be performed
     * @since Struts 1.1
     */
protected ModuleConfig initModuleConfig(String prefix, String paths) throws ServletException {
    if (log.isDebugEnabled()) {
        log.debug("Initializing module path '" + prefix + "' configuration from '" + paths + "'");
    }
    // Parse the configuration for this module
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    ModuleConfig config = factoryObject.createModuleConfig(prefix);
    // Configure the Digester instance we will use
    Digester digester = initConfigDigester();
    List urls = splitAndResolvePaths(paths);
    URL url;
    for (Iterator i = urls.iterator(); i.hasNext(); ) {
        url = (URL) i.next();
        digester.push(config);
        this.parseModuleConfigFile(digester, url);
    }
    getServletContext().setAttribute(Globals.MODULE_KEY + config.getPrefix(), config);
    return config;
}
Also used : ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) Digester(org.apache.commons.digester.Digester) Iterator(java.util.Iterator) ModuleConfig(org.apache.struts.config.ModuleConfig) List(java.util.List) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 3 with Digester

use of org.apache.commons.digester.Digester in project sonarqube by SonarSource.

the class TestModuleConfig method parseConfig.

// ------------------------------------------------ Individual Test Methods
private void parseConfig(String publicId, String entityURL, String strutsConfig) {
    // Prepare a Digester for parsing a struts-config.xml file
    Digester digester = new Digester();
    digester.push(config);
    digester.setNamespaceAware(true);
    digester.setValidating(true);
    digester.addRuleSet(new ConfigRuleSet());
    digester.register(publicId, this.getClass().getResource(entityURL).toString());
    // Parse the test struts-config.xml file
    try {
        InputStream input = this.getClass().getResourceAsStream(strutsConfig);
        assertNotNull("Got an input stream for " + strutsConfig, input);
        digester.parse(input);
        input.close();
    } catch (Throwable t) {
        t.printStackTrace(System.out);
        fail("Parsing threw exception:  " + t);
    }
}
Also used : InputStream(java.io.InputStream) Digester(org.apache.commons.digester.Digester)

Example 4 with Digester

use of org.apache.commons.digester.Digester in project gocd by gocd.

the class GoPluginDescriptorParser method initDigester.

private static Digester initDigester() throws SAXNotRecognizedException, SAXNotSupportedException {
    Digester digester = new Digester();
    digester.setValidating(true);
    digester.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) throws SAXException {
            throw new SAXException("XML Schema validation of Plugin Descriptor(plugin.xml) failed", exception);
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            throw new SAXException("XML Schema validation of Plugin Descriptor(plugin.xml) failed", exception);
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            throw new SAXException("XML Schema validation of Plugin Descriptor(plugin.xml) failed", exception);
        }
    });
    digester.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    digester.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", GoPluginDescriptorParser.class.getResourceAsStream("/plugin-descriptor.xsd"));
    return digester;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SAXParseException(org.xml.sax.SAXParseException) Digester(org.apache.commons.digester.Digester) SAXException(org.xml.sax.SAXException)

Example 5 with Digester

use of org.apache.commons.digester.Digester in project gocd by gocd.

the class GoPluginDescriptorParser method parseXML.

public static GoPluginDescriptor parseXML(InputStream pluginXML, String pluginJarFileLocation, File pluginBundleLocation, boolean isBundledPlugin) throws IOException, SAXException {
    Digester digester = initDigester();
    GoPluginDescriptorParser parserForThisXML = new GoPluginDescriptorParser(pluginJarFileLocation, pluginBundleLocation, isBundledPlugin);
    digester.push(parserForThisXML);
    digester.addCallMethod("go-plugin", "createPlugin", 2);
    digester.addCallParam("go-plugin", 0, "id");
    digester.addCallParam("go-plugin", 1, "version");
    digester.addCallMethod("go-plugin/about", "createAbout", 4);
    digester.addCallParam("go-plugin/about/name", 0);
    digester.addCallParam("go-plugin/about/version", 1);
    digester.addCallParam("go-plugin/about/target-go-version", 2);
    digester.addCallParam("go-plugin/about/description", 3);
    digester.addCallMethod("go-plugin/about/vendor", "createVendor", 2);
    digester.addCallParam("go-plugin/about/vendor/name", 0);
    digester.addCallParam("go-plugin/about/vendor/url", 1);
    digester.addCallMethod("go-plugin/about/target-os/value", "addTargetOS", 1);
    digester.addCallParam("go-plugin/about/target-os/value", 0);
    digester.parse(pluginXML);
    return parserForThisXML.descriptor;
}
Also used : Digester(org.apache.commons.digester.Digester)

Aggregations

Digester (org.apache.commons.digester.Digester)8 URL (java.net.URL)4 InputStream (java.io.InputStream)3 SAXException (org.xml.sax.SAXException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 ServletException (javax.servlet.ServletException)1 ConfigRuleSet (org.apache.struts.config.ConfigRuleSet)1 ModuleConfig (org.apache.struts.config.ModuleConfig)1 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)1 ErrorHandler (org.xml.sax.ErrorHandler)1 SAXParseException (org.xml.sax.SAXParseException)1