Package org.apache.cocoon.components.parser

Examples of org.apache.cocoon.components.parser.Parser


       }
    }

    /** Configure this <code>Cocoon</code> instance. */
    public Configuration configure(ExcaliburComponentManager startupManager) throws ConfigurationException, ContextException {
        Parser p = null;
        Configuration roleConfig = null;
        Configuration sitemapConfig = null;

        try {
            this.configurationFile.refresh();
            p = (Parser)startupManager.lookup(Parser.ROLE);
            SAXConfigurationHandler b = new SAXConfigurationHandler();
            InputStream inputStream = ClassUtils.getResource("org/apache/cocoon/cocoon.roles").openStream();
            InputSource is = new InputSource(inputStream);
            is.setSystemId(this.configurationFile.getSystemId());
            p.setContentHandler(b);
            p.parse(is);
            roleConfig = b.getConfiguration();
        } catch (Exception e) {
            getLogger().error("Could not configure Cocoon environment", e);
            throw new ConfigurationException("Error trying to load configurations", e);
        } finally {
            if (p != null) startupManager.release(p);
        }

        DefaultRoleManager drm = new DefaultRoleManager();
        drm.setLogger(getLogger());
        drm.configure(roleConfig);
        roleConfig = null;

        try {
            this.configurationFile.refresh();
            p = (Parser)startupManager.lookup(Parser.ROLE);
            SAXConfigurationHandler b = new SAXConfigurationHandler();
            InputStream inputStream = ClassUtils.getResource("org/apache/cocoon/sitemap/sitemap.roles").openStream();
            InputSource is = new InputSource(inputStream);
            is.setSystemId(this.configurationFile.getSystemId());
            p.setContentHandler(b);
            p.parse(is);
            sitemapConfig = b.getConfiguration();
        } catch (Exception e) {
            getLogger().error("Could not configure Cocoon environment", e);
            throw new ConfigurationException("Error trying to load configurations", e);
        } finally {
            if (p != null) startupManager.release(p);
        }

        DefaultRoleManager sitemapRoleManager = new DefaultRoleManager();
        sitemapRoleManager.setLogger(getLogger());
        sitemapRoleManager.configure(sitemapConfig);

        try {
            p = (Parser)startupManager.lookup(Parser.ROLE);
            SAXConfigurationHandler b = new SAXConfigurationHandler();
            InputSource is = this.configurationFile.getInputSource();
            p.setContentHandler(b);
            p.parse(is);
            this.configuration = b.getConfiguration();
        } catch (Exception e) {
            getLogger().error("Could not configure Cocoon environment", e);
            throw new ConfigurationException("Error trying to load configurations",e);
        } finally {
            if (p != null) startupManager.release(p);
        }

        Configuration conf = this.configuration;

        AbstractSitemap.setRoleManager(sitemapRoleManager, conf);
        AbstractSitemap.setLogKitManager(this.logKitManager);

        getLogger().debug("Root configuration: " + conf.getName());
        if (! "cocoon".equals(conf.getName())) {
            throw new ConfigurationException("Invalid configuration file\n" + conf.toString());
        }
        getLogger().debug("Configuration version: " + conf.getAttribute("version"));
        if (Constants.CONF_VERSION.equals(conf.getAttribute("version")) == false) {
            throw new ConfigurationException("Invalid configuration schema version. Must be '" + Constants.CONF_VERSION + "'.");
        }

        String userRoles = conf.getAttribute("user-roles", "");
        if ("".equals(userRoles) == false) {
            try {
                p = (Parser)startupManager.lookup(Parser.ROLE);
                SAXConfigurationHandler b = new SAXConfigurationHandler();
                org.apache.cocoon.environment.Context context =
                    (org.apache.cocoon.environment.Context) this.context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
                URL url = context.getResource(userRoles);
                InputSource is = new InputSource(new BufferedInputStream(url.openStream()));
                is.setSystemId(this.configurationFile.getSystemId());
                p.setContentHandler(b);
                p.parse(is);
                roleConfig = b.getConfiguration();
            } catch (Exception e) {
                getLogger().error("Could not configure Cocoon environment", e);
                throw new ConfigurationException("Error trying to load configurations", e);
            } finally {
View Full Code Here


    public void compose(ComponentManager manager) {
        this.manager = manager;
        // Get a parser and use it as a DOM factory
        try {
            log.debug("Looking up " + Parser.ROLE);
            Parser p = (Parser)manager.lookup(Parser.ROLE);
            super.factory = p;
        } catch (Exception e) {
            log.error("Could not find component", e);
        }
    }
View Full Code Here

                }
                reader.close();
            }
        } else if (parse.equals("xml")) {
            getLogger().debug("Parse type is XML");
            Parser parser = null;
            try
            {
                getLogger().debug("Looking up " + Parser.ROLE);
                parser = (Parser)manager.lookup(Parser.ROLE);

                InputSource input = url.getInputSource();
                if (suffix.startsWith("xpointer(") && suffix.endsWith(")")) {
                    String xpath = suffix.substring(9,suffix.length()-1);
                    getLogger().debug("XPath is "+xpath);
                    Document document = parser.parseDocument(input);
                    try {
                        NodeList list = XPathAPI.selectNodeList(document,xpath);
                        DOMStreamer streamer = new DOMStreamer(super.contentHandler,super.lexicalHandler);
                        int length = list.getLength();
                        for (int i=0; i<length; i++) {
                            streamer.stream(list.item(i));
                        }
                    } catch (TransformerException e){
                        getLogger().error("TransformerException", e);
                        return;
                    }
                } else {
                    IncludeXMLConsumer xinclude_handler = new IncludeXMLConsumer(super.contentHandler,super.lexicalHandler);
                    xinclude_handler.setLogger(getLogger());
                    parser.setConsumer(xinclude_handler);
                    parser.parse(input);
                }
            } catch(SAXException e) {
                getLogger().error("Error in processXIncludeElement", e);
                throw e;
            } catch(ProcessingException e) {
View Full Code Here

     * @throws SAXException if failed to parse source document.
     */
    public void toSAX(ContentHandler handler)
        throws SAXException, ProcessingException
    {
        Parser parser = null;
        try {
            parser = (Parser)this.manager.lookup(Parser.ROLE);

            if (handler instanceof XMLConsumer) {
                parser.setConsumer((XMLConsumer)handler);
            } else {
                parser.setContentHandler(handler);
                if (handler instanceof LexicalHandler) {
                    parser.setLexicalHandler((LexicalHandler)handler);
                }
            }
            parser.parse(this.getInputSource());
        } catch (ProcessingException e){
            // Preserve original exception
            throw e;
        } catch (SAXException e) {
            // Preserve original exception
View Full Code Here

    throws Exception
  {
    ComponentManager manager = componentManager(sManager);
    Source source = source(ssource);

    Parser parser = (Parser)manager.lookup(Parser.ROLE);
    XMLtoSXML handler = new XMLtoSXML();
    parser.setContentHandler(handler);
    InputSource input = new InputSource(source.getInputStream());
    parser.parse(input);

    SchemeInterpreter interpreters
      = (SchemeInterpreter)manager.lookup(SchemeInterpreter.ROLE);
    Symbol mainFunction = interpreters.getMainFunction();
    Interpreter interp = interpreters.getInterpreter();
View Full Code Here

     * @exception Exception
     */
    public byte[] executeJSP(String url, HttpServletRequest httpRequest, HttpServletResponse httpResponse, ServletContext context)
        throws IOException, ServletException, SAXException, Exception {

        Parser parser = null;
        byte[] bytes = null;

        MyServletRequest request = new MyServletRequest(httpRequest, url);
        MyServletResponse response = new MyServletResponse(httpResponse);

View Full Code Here

            this.inputSource = null;
        }
    }

    public void generate() throws ProcessingException {
        Parser parser = null;
        try {
            // Figure out what file to open and do so

            getLogger().debug("processing file [" + super.source + "]");
            this.inputSource = this.resolver.resolve(super.source);

            getLogger().debug("file resolved to [" + this.inputSource.getSystemId() + "]");

            // TODO: why doesn't this work?
            // Reader in = src.getCharacterStream();

            Reader in = new java.io.InputStreamReader(this.inputSource.getInputStream());

            // Set up the BSF manager and register relevant helper "beans"

            BSFManager mgr = new BSFManager();

            // add support for additional languages

            if (this.additionalLanguages != null)
            {
                for (int i = 0; i < this.additionalLanguages.length; ++i)
                {
                    getLogger().debug("adding BSF language " + this.additionalLanguages[i].name + " with engine " + this.additionalLanguages[i].engineSrc);

                    mgr.registerScriptingEngine(this.additionalLanguages[i].name,
                                                this.additionalLanguages[i].engineSrc,
                                                this.additionalLanguages[i].extensions);
                }
            }

            StringBuffer output = new StringBuffer();

            mgr.registerBean("resolver", this.resolver);
            mgr.registerBean("source", super.source);
            mgr.registerBean("objectModel", this.objectModel);
            mgr.registerBean("parameters", this.parameters);
            mgr.registerBean("output", output);
            mgr.registerBean("logger", getLogger());

            getLogger().debug("BSFManager execution begining");

            // Execute the script

            mgr.exec(BSFManager.getLangFromFilename(this.inputSource.getSystemId()),
                     this.inputSource.getSystemId(), 0, 0, IOUtils.getStringFromReader(in));

            getLogger().debug("BSFManager execution complete");
            getLogger().debug("output = [" + output.toString() + "]");

            // Extract the XML string from the BSFManager and parse it

            InputSource xmlInput =
                    new InputSource(new StringReader(output.toString()));
            parser = (Parser)(this.manager.lookup(Parser.ROLE));
            parser.setConsumer(this.xmlConsumer);
            parser.parse(xmlInput);
        } catch (FileNotFoundException e) {
            throw new ResourceNotFoundException(
                "Could not load script " + this.inputSource.getSystemId(), e);
        } catch (BSFException e) {
            throw new ProcessingException(
View Full Code Here

    /**
     * Generate XML data out of request InputStream.
     */
    public void generate() throws IOException, SAXException, ProcessingException
    {
        Parser parser = null;
        String parameter = parameters.getParameter(StreamGenerator.FORM_NAME, null);
        int len = 0;
    String contentType = null;
        try
        {
            HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
            contentType = request.getContentType();
            if (contentType.startsWith("application/x-www-form-urlencoded")) {
                String sXml = request.getParameter(parameter);
                inputSource = new InputSource(new StringReader(sXml));
            } else if (contentType.startsWith("text/plain") ||
                    contentType.startsWith("text/xml") ||
                    contentType.startsWith("application/xml")) {
                len = request.getContentLength();

                if (len > 0) {
                        PostInputStream anStream = new PostInputStream(request.getInputStream(), len);
                        inputSource = new InputSource(anStream);
                } else {
                    throw new IOException("getContentLen() == 0");
                }
            } else {
                throw new IOException("Unexpected getContentType(): " + request.getContentType());
            }

            if (getLogger().isDebugEnabled()) {
                getLogger().debug("processing stream ContentType= " + request.getContentType() + "ContentLen= " + len);
            }
            String charset =  getCharacterEncoding(request, contentType) ;
      if( charset != null)
      {
        this.inputSource.setEncoding(charset);
            }
            parser = (Parser)this.manager.lookup(Parser.ROLE);
            parser.setContentHandler(super.contentHandler);
            parser.setLexicalHandler(super.lexicalHandler);
            parser.parse(this.inputSource);
        } catch (IOException e) {
            getLogger().error("StreamGenerator.generate()", e);
            throw new ResourceNotFoundException("StreamGenerator could not find resource", e);
        } catch (SAXException e) {
            getLogger().error("StreamGenerator.generate()", e);
View Full Code Here

        if (httpResponse == null || httpRequest == null || httpContext == null) {
            throw new ProcessingException("HttpServletRequest or HttpServletResponse or ServletContext object not available");
        }

        JSPEngine engine = null;
        Parser parser = null;
        Source src = null;
        try {
            src = this.resolver.resolve(this.source);
            String url = src.getSystemId();
            // Guarantee src parameter is a file
            if (!url.startsWith("file:/"))
                throw new IOException("Protocol not supported: " + url);

            url = url.substring(5);
            getLogger().debug("JspGenerator executing JSP:" + url);

            engine = (JSPEngine)this.manager.lookup(JSPEngine.ROLE);
            byte[] bytes = engine.executeJSP(url, httpRequest, httpResponse, httpContext);
            ByteArrayInputStream input = new ByteArrayInputStream(bytes);

            // pipe the results into the parser
            parser = (Parser)this.manager.lookup(Parser.ROLE);
            parser.setConsumer(this.xmlConsumer);
            parser.parse(new InputSource(input));
        } catch (ServletException e) {
            getLogger().debug("ServletException in JspGenerator.generate()", e);
            getLogger().debug("Embedded ServletException JspGenerator.generate()", e.getRootCause());
            throw new ProcessingException("ServletException in JspGenerator.generate()",e.getRootCause());
        } catch (SAXException e) {
View Full Code Here

            throw new ProcessingException("HttpServletRequest or HttpServletResponse object not available");
        }

        // ensure that we are serving a file...
        Source inputSource = null;
        Parser parser = null;
        try {
            inputSource = this.resolver.resolve(this.source);
            String systemId = inputSource.getSystemId();
            if (!systemId.startsWith("file:/"))
                throw new IOException("protocol not supported: " + systemId);

            // construct both ends of the pipe
            PipedInputStream input = new PipedInputStream();

            // start PHP producing results into the pipe
            PhpServlet php = new PhpServlet();
            php.init(new config((ServletContext)this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT)));
            php.setInput(systemId.substring(6));
            php.setOutput(new PipedOutputStream(input));
            php.setRequest(httpRequest);
            php.setResponse(httpResponse);
            new Thread(php).start();

            // pipe the results into the parser
            parser = (Parser)this.manager.lookup(Parser.ROLE);
            parser.setConsumer(this.xmlConsumer);
            parser.parse(new InputSource(input));

            // clean up
            php.destroy();
        } catch (IOException e) {
            getLogger().debug("PhpGenerator.generate()", e);
View Full Code Here

TOP

Related Classes of org.apache.cocoon.components.parser.Parser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.