Package org.apache.excalibur.source

Examples of org.apache.excalibur.source.ModifiableSource


        // Check that dest is writeable
        if (! (dest instanceof ModifiableSource)) {
            throw new IllegalArgumentException("Non-writeable URI : " + dest.getURI());
        }
       
        ModifiableSource wdest = (ModifiableSource)dest;
       
        // Get streams
        InputStream is = src.getInputStream();
        OutputStream os = wdest.getOutputStream();
       
        // And transfer all content.
        try {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer, 0, buffer.length)) > 0) {
                os.write(buffer, 0, len);
            }
            os.close();
        } catch(Exception e) {
            if (wdest.canCancel(os)) {
                wdest.cancel(os);
            }
        } finally {
            is.close();
        }
        // Success !
View Full Code Here


        try {
            Source source = resolver.resolveURI(systemID);           
            if ( ! (source instanceof ModifiableSource)) {
                throw new ProcessingException("Source '"+systemID+"' is not writeable.");
            }
            ModifiableSource ms = (ModifiableSource)source;
            ms.delete();
        } catch (SourceException se) {
            reportResult("none",
                "delete",
                "unable to delete Source:" + se.getMessage(),
                systemID,
View Full Code Here

        try {
            source = this.resolver.resolveURI( systemID );
            if ( ! (source instanceof ModifiableSource)) {
                throw new ProcessingException("Source '"+systemID+"' is not writeable.");
            }
            ModifiableSource ws = (ModifiableSource)source;
            exists = ws.exists();
            target = source.getURI();
            if ( exists == true && this.state == STATE_INSERT ) {
                                message = "content inserted at: " + path;
                resource = SourceUtil.toDOM( source );
                // import the fragment
                Node importNode = resource.importNode(fragment, true);
                // get the node
                Node parent = DOMUtil.selectSingleNode(resource, path);

                // replace?
                if (replacePath != null) {
                    try {
                        Node replaceNode = DOMUtil.getSingleNode(parent, replacePath);
                        // now get the parent of this node until it is the parent node for insertion
                        while (replaceNode != null && replaceNode.getParentNode().equals(parent) == false) {
                           replaceNode = replaceNode.getParentNode();
                        }
                        if (replaceNode != null) {
                            if (overwrite == true) {
                                if (parent.getNodeType() == Node.DOCUMENT_NODE) {
                                    // replacing of the document element is not allowed
                                    DOMParser parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
                                    try {
                                        resource = parser.createDocument();
                                    } finally {
                                        this.manager.release( (Component)parser );
                                    }

                                    resource.appendChild(resource.importNode(importNode, true));
                                    parent = resource;
                                    replaceNode = resource.importNode(replaceNode, true);
                                } else {
                                    parent.replaceChild(importNode, replaceNode);
                                }
                                message += ", replacing: " + replacePath;
                                if (reinsertPath != null) {
                                    Node insertAt = DOMUtil.getSingleNode(parent, reinsertPath);
                                    if (insertAt != null) {
                                        while (replaceNode.hasChildNodes() == true) {
                                            insertAt.appendChild(replaceNode.getFirstChild());
                                        }
                                    } else { // reinsert point null
                                        message = "replace failed, could not find your reinsert path: " + reinsertPath;
                                        resource = null;
                                    }
                                }
                            } else { // overwrite was false
                                message = "replace failed, no overwrite allowed.";
                                resource = null;
                            }
                        } else { // specified replaceNode was not found
                            parent.appendChild(importNode);
                        }
                    } catch (javax.xml.transform.TransformerException sax) {
                        throw new ProcessingException("TransformerException: " + sax, sax);
                    }
                } else { // no replace path, just do an insert at end
                    parent.appendChild(importNode);
                }
            } else if (create == true) {
                DOMParser parser = (DOMParser)this.manager.lookup(DOMParser.ROLE);
                try {
                    resource = parser.createDocument();
                } finally {
                    this.manager.release( (Component)parser );
                }
                // import the fragment
                Node importNode = resource.importNode(fragment, true);
                if ( path.equals("") ) {  // this is allowed in write
                    resource.appendChild(importNode.getFirstChild());
                    message = "entire source overwritten";

                } else {
                    // get the node
                    Node parent = DOMUtil.selectSingleNode(resource, path);
                    // add fragment
                    parent.appendChild(importNode);
                    message = "content appended to: " + path;
                }
            } else {
                            message = "create not allowed";
                resource = null;/**/
            }

            // write source
            if ( resource != null) {
                resource.normalize();
                // use serializer
                if (localSerializer == null) localSerializer = this.configuredSerializerName;
                if (localSerializer != null) {
                    // Lookup the Serializer
                    ComponentSelector selector = null;
                    Serializer serializer = null;
                    OutputStream oStream = null;
                    try {
                        selector = (ComponentSelector)manager.lookup(Serializer.ROLE + "Selector");
                        serializer = (Serializer)selector.select(localSerializer);
                        oStream = ws.getOutputStream();
                        serializer.setOutputStream(oStream);
                        DOMStreamer streamer = new DOMStreamer(serializer);
                        streamer.stream(resource);
                    } finally {
                        if (oStream != null) {
View Full Code Here

                    filename,
                    DefaultNotifyingBuilder.getRootCause(pe).getMessage());
            } finally {
                if (output != null) {

                    ModifiableSource source = target.getSource(filename);
                    try {
                        OutputStream stream = source.getOutputStream();

                        output.setFileOutputStream(stream);
                        output.flush();
                    } finally {
                        target.releaseSource(source);
View Full Code Here

            n.setMessage("Page Not Available.");
            n.setDescription("The requested resource couldn't be found.");
            n.addExtraDescription(Notifying.EXTRA_REQUESTURI, uri);
            n.addExtraDescription("missing-file", uri);

            ModifiableSource source = target.getSource(filename);
            try {
                OutputStream stream = source.getOutputStream();

                PrintStream out = new PrintStream(stream);
                Notifier.notify(n, out, "text/html");
                out.flush();
                out.close();
View Full Code Here

        try {
            source = SourceUtil.getSource(location, typeParameters,
                                          parameters, resolver);
            if (source instanceof ModifiableSource) {
                ModifiableSource ws = (ModifiableSource) source;

                frag.normalize();

                if ( null != serializerName) {
          ComponentManager manager = CocoonComponentManager.getSitemapComponentManager();

                  ComponentSelector selector = null;
                  Serializer serializer = null;
                  OutputStream oStream = null;
                  try {
                       selector = (ComponentSelector)manager.lookup(Serializer.ROLE + "Selector");
                       serializer = (Serializer)selector.select(serializerName);
                       oStream = ws.getOutputStream();
                       serializer.setOutputStream(oStream);
                         serializer.startDocument();
                       DOMStreamer streamer = new DOMStreamer(serializer);
                       streamer.stream(frag);
                         serializer.endDocument();
                  } catch (ComponentException e) {
                    throw new ProcessingException("Unable to lookup serializer.", e);
          } finally {
                      if (oStream != null) {
                          oStream.flush();
                          try {
                              oStream.close();
                          } catch (Exception ignore) {
                            }
                        }
            if ( selector != null ) {
              selector.release( serializer );
              manager.release( selector );
            }
                  }
                } else {
                  final String content = XMLUtils.serializeNode(frag,
                                             XMLUtils.defaultSerializeToXMLFormat(false));
                  OutputStream oStream = ws.getOutputStream();
 
                  oStream.write(content.getBytes());
                  oStream.flush();
                  oStream.close();
                }
View Full Code Here

     */
    protected void saveDocument(String encoding, String content) throws Exception {
        SourceResolver resolver = null;
        Source indexSource = null;
        Source tidySource = null;
        ModifiableSource xsltSource = null;
        Properties properties = null;
        try {
            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
            saveXMLFile(encoding, content, getSourceDocument().getOutputStream());

            Document xmlDoc = null;

            // Setup an instance of Tidy.
            Tidy tidy = new Tidy();

            String tidyProps = this.getParameterAsString(TIDY_CONFIG, null);
            if (tidyProps != null) {
                tidySource = resolver.resolveURI(tidyProps);
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Loading configuration from " + tidySource.getURI());
                }
                properties = new Properties();
                properties.load(tidySource.getInputStream());

                if (properties == null) {
                    tidy.setXHTML(true);
                } else {
                    tidy.setConfigurationFromProps(properties);
                }

                // Set Jtidy warnings on-off
                tidy.setShowWarnings(getLogger().isWarnEnabled());
                // Set Jtidy final result summary on-off
                tidy.setQuiet(!getLogger().isInfoEnabled());
                // Set Jtidy infos to a String (will be logged) instead of
                // System.out
                StringWriter stringWriter = new StringWriter();
                PrintWriter errorWriter = new PrintWriter(stringWriter);
                tidy.setErrout(errorWriter);

                xmlDoc = tidy.parseDOM(getSourceDocument().getInputStream(), null);

                // FIXME: Jtidy doesn't warn or strip duplicate attributes in
                // same
                // tag; stripping.
                XMLUtils.stripDuplicateAttributes(xmlDoc, null);

                StringWriter output = new StringWriter();
                StreamResult strResult = new StreamResult(output);
                TransformerFactory tfac = TransformerFactory.newInstance();
                try {
                    Transformer t = tfac.newTransformer();
                    t.setOutputProperty(OutputKeys.ENCODING, encoding);
                    t.setOutputProperty(OutputKeys.INDENT, "yes");
                    t.setOutputProperty(OutputKeys.METHOD, "xml");
                    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
                    t.transform(new DOMSource(xmlDoc.getDocumentElement()), strResult);

                    content = strResult.getWriter().toString();
                } catch (Exception e) {
                    addErrorMessage(e.getMessage());
                }

                saveXMLFile(encoding, content, getSourceDocument().getOutputStream());
            } else {
                try {
                    xmlDoc = DocumentHelper.readDocument(getSourceDocument().getInputStream());
                } catch (SAXException e) {
                    addErrorMessage("error-document-form", new String[] { e.getMessage() });
                }
            }



            // Try to clean the xml using xslt
            ResourceType resType = getSourceDocument().getResourceType();
            String[] formats = resType.getFormats();
            if (Arrays.asList(formats).contains(XSLT_CLEAN_FORMAT)) {
                StringWriter output = new StringWriter();
                StreamResult strResult = new StreamResult(output);
                TransformerFactory tfac = TransformerFactory.newInstance();
                try {
                    xsltSource = (ModifiableSource) resolver.resolveURI(resType
                            .getFormatURI(XSLT_CLEAN_FORMAT));
                    Transformer t = tfac.newTransformer(new StreamSource(xsltSource
                            .getInputStream()));
                    t.setOutputProperty(OutputKeys.ENCODING, encoding);
                    t.setOutputProperty(OutputKeys.INDENT, "yes");
                    t.setOutputProperty(OutputKeys.METHOD, "xml");
                    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
View Full Code Here

        }
    }

    protected void saveTransactionable(String realSourceUri) throws RepositoryException {
        SourceResolver resolver = null;
        ModifiableSource source = null;
        InputStream in = null;
        OutputStream out = null;

        try {
            resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
            source = (ModifiableSource) resolver.resolveURI(realSourceUri);

            out = source.getOutputStream();

            byte[] buf = new byte[4096];
            in = new ByteArrayInputStream(this.data);
            int read = in.read(buf);
View Full Code Here

                    filename,
                    DefaultNotifyingBuilder.getRootCause(pe).getMessage());
            } finally {
                if (output != null) {

                    ModifiableSource source = target.getSource(filename);
                    try {
                        OutputStream stream = source.getOutputStream();

                        output.setFileOutputStream(stream);
                        output.flush();
                    } finally {
                        target.releaseSource(source);
View Full Code Here

            n.setMessage("Page Not Available.");
            n.setDescription("The requested resource couldn't be found.");
            n.addExtraDescription(Notifying.EXTRA_REQUESTURI, uri);
            n.addExtraDescription("missing-file", uri);

            ModifiableSource source = target.getSource(filename);
            try {
                OutputStream stream = source.getOutputStream();

                PrintStream out = new PrintStream(stream);
                Notifier.notify(n, out, "text/html");
                out.flush();
                out.close();
View Full Code Here

TOP

Related Classes of org.apache.excalibur.source.ModifiableSource

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.