Package org.apache.cocoon.environment

Examples of org.apache.cocoon.environment.Source


    public static String getSourceAsString(String uri, SourceResolver resolver) throws RuntimeException {

        StringBuffer result = new StringBuffer();
        InputStream stream = null;
        Source resource = null;
        try {
            resource = resolver.resolve(uri);
            long length = resource.getContentLength();
            stream = new BufferedInputStream(resource.getInputStream());
            if (length != -1) {
                byte[] buffer = new byte[(new Long(length)).intValue()];
                stream.read(buffer);
                stream.close();
                if (buffer != null) result.append(new String(buffer));
            } else {
                int readBytes = 0;
                do {
                    byte[] buffer = new byte[4*1024];
                    readBytes = stream.read(buffer);
                    if (readBytes == -1) break;
                    if (readBytes > 0) result.append(new String(buffer,0,readBytes));
                } while (true);
                stream.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            if ( stream != null )
                try {stream.close();} catch (Exception ase) {  throw new RuntimeException(ase.getMessage()); }
            if ( resource != null )
                resource.recycle();
        }
        return result.toString();
    }
View Full Code Here


    public static void includeSource(String uri, String base, SourceResolver resolver, ContentHandler contentHandler)
        throws RuntimeException {
       
        base = (base == null? "" : base);
        Source source = null;
        try {
            source = resolver.resolve(base+uri);
            source.toSAX(new org.apache.cocoon.xml.IncludeXMLConsumer(contentHandler));
          } catch (Exception e) {
              throw new RuntimeException("Error including source "+base+" "+uri+":"+e.getMessage());
          } finally {
              if (source != null)
                 source.recycle();
          }
    }
View Full Code Here

        if (descriptor == null) {
            throw new ConfigurationException("The form descriptor is not set!");
        }

        synchronized (AbstractComplementaryConfigurableAction.configurations) {
            Source resource = null;
            try {
                resource = resolver.resolve(descriptor);
                conf = (ConfigurationHelper) AbstractComplementaryConfigurableAction.configurations.get(resource.getSystemId());
                if (conf == null || (reloadable && conf.lastModified != resource.getLastModified())) {
                    getLogger().debug("(Re)Loading " + descriptor);

                    if (conf == null) {
                        conf = new ConfigurationHelper();
                    }

                    SAXConfigurationHandler builder = new SAXConfigurationHandler();
                    resource.toSAX(builder);

                    conf.lastModified = resource.getLastModified();
                    conf.configuration = builder.getConfiguration();

                    AbstractComplementaryConfigurableAction.configurations.put(resource.getSystemId(), conf);
                } else {
                    getLogger().debug("Using cached configuration for " + descriptor);
                }
            } catch (Exception e) {
                getLogger().error("Could not configure Database mapping environment", e);
                throw new ConfigurationException("Error trying to load configurations for resource: "
                    + (resource == null ? "null" : resource.getSystemId()));
            } finally {
                if (resource != null) resource.recycle();
            }
        }

        return conf.configuration;
    }
View Full Code Here

     */
    private Source getSource(String             uri,
                             Parameters         typeParameters,
                             SourceParameters resourceParameters)
    throws IOException, SAXException, ProcessingException {
        Source input;

        // Test: local uri (= same servlet/cocoon) ?
        if (uri.startsWith("/") == true) {
            // server-absolute url is transformed to absolute url
            Request request = ObjectModelHelper.getRequest(objectModel);
View Full Code Here

                                                   builder);
            filter = new IncludeXMLConsumer(consumer, consumer);
        } else {
            filter = new IncludeXMLConsumer(builder, builder);
        }
        Source input = null;
        try {
            input = this.getSource(uri, typeParameters, resourceParameters);

            if (input != null) {
                input.toSAX(filter);
            }
        } finally {
            if (input != null) input.recycle();
            input = null;
        }

        builder.endElement("", "sunShine", "sunShine");
        builder.endDocument();
View Full Code Here

        }
        DocumentFragment frag = null;

        long startTime = System.currentTimeMillis();

        Source input = null;
        try {
            input = this.resolver.resolve(fileName);

            DOMBuilder builder = new DOMBuilder();
            builder.startDocument();
            builder.startElement("", "sunShine", "sunShine", this.emptyAttributes);

            IncludeXMLConsumer filter = new IncludeXMLConsumer(builder, builder);
            input.toSAX(filter);

            builder.endElement("", "sunShine", "sunShine");
            builder.endDocument();

            // Create Document Fragment
            final Document doc = builder.getDocument();
            frag = doc.createDocumentFragment();
            final Node root = doc.getDocumentElement();
            Node child;
            while (root.hasChildNodes() == true) {
                child = root.getFirstChild();
                child.normalize();
                root.removeChild(child);
                frag.appendChild(child);
            }
        } finally {
            if (input != null) input.recycle();
            input = null;
        }


        if (this.getLogger().isDebugEnabled() == true) {
View Full Code Here

                        }
                    }
                }
            }

            Source input = null;
            try {
                Properties format = XMLUtils.defaultSerializeToXMLFormat(false);
                this.setOutputKeys(format, typeParameters);
                input = this.resolver.resolve(fileName);
                String absolutePath = input.getSystemId();
                if (absolutePath.startsWith("file:") == false) {
                    throw new ProcessingException("Saving to " + fileName + " is not possible.");
                }
                File file = new File(absolutePath.substring("file:".length()));
                if (file.exists() == false) {
                    File directory = file.getParentFile();
                    if (directory.exists() == false) {
                        directory.mkdirs();
                    }
                    file.createNewFile();
                }
                String xml = XMLUtils.serializeNode(fragment, format);
                Writer writer = new BufferedWriter(new FileWriter(file));;
                writer.write(xml);
                writer.flush();
                writer.close();
                writer = null;
            } finally {
                if (input != null) input.recycle();
                input = null;
            }

        } catch (IOException ioe) {
            throw new ProcessingException("saveXMLToFile: IOException: " + ioe, ioe);
View Full Code Here

                              ", typeParams="+typeParameters+
                              ", file=" + fileName +
                              ", params="+resourceParameters);
        }
        try {
            Source input = null;
            try {
                input = this.resolver.resolve(fileName);
                String absolutePath = input.getSystemId();
                if (absolutePath.startsWith("file:") == false) {
                    throw new ProcessingException("Saving to " + fileName + " is not possible.");
                }
                File file = new File(absolutePath.substring("file:".length()));
                if (file.exists() == false) {
                    File directory = file.getParentFile();
                    if (directory.exists() == false) {
                        directory.mkdirs();
                    }
                    file.createNewFile();
                }
                Writer writer = new BufferedWriter(new FileWriter(file));
                writer.write(content);
                writer.flush();
                writer.close();
                writer = null;
            } finally {
                if (input != null) input.recycle();
                input = null;
            }

        } catch (IOException ioe) {
            throw new ProcessingException("saveContentToFile: IOException: " + ioe, ioe);
View Full Code Here

            this.getLogger().debug("BEGIN streamXMLFromFile fileName=" + fileName +
                              ", typeParams="+typeParameters+
                              ", parameters="+resourceParameters);
        }
        long startTime = System.currentTimeMillis();
        Source input = null;
        try {
            input = this.resolver.resolve(fileName);

            IncludeXMLConsumer filter = new IncludeXMLConsumer(contentHandler, lexicalHandler);
            input.toSAX(filter);

        } catch (SAXException sax) {
            throw new ProcessingException("SAXException: " + sax, sax);
        } catch (IOException ioe) {
            throw new ProcessingException("IOException: " + ioe, ioe);
        } finally {
            if (input != null) input.recycle();
            input = null;
        }

        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("END streamXMLFromFile");
View Full Code Here

            contentHandler = consumer;
            lexicalHandler = consumer;
        }
        IncludeXMLConsumer filter = new IncludeXMLConsumer(contentHandler, lexicalHandler);

        Source input = null;
        try {
            input = this.getSource(uri, typeParameters, resourceParameters);
            input.toSAX(filter);
        } finally {
            if (input != null) input.recycle();
            input = null;
        }

        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("END streamXMLFromURI");
View Full Code Here

TOP

Related Classes of org.apache.cocoon.environment.Source

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.