Guido Casper wrote:

Did anyone else also feel the need to have something like processPipelineTo but getting a DOM or just an InputStream instead of just streaming it directly to another OutputStream?



Rather than extending the FOM, what we need is a helper JS library that is a simple wrapper around the SourceResolver and the SourceUtil.toDOM() method. I have this on my projects in a "flow-utils.js" file.


I'll add it in the CVS (needs a little polishing). In the meantime, here are the DOM-related functions:

========================================================

// Create a new document, given the name of its root element
function newDocument(root) {
var result = Packages.javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
result.appendChild(result.createElement("News"));
return result;
}


// Load a document from an URI, absolute or relative to the current sitemap
function loadDocument(uri) {
var resolver = cocoon.getComponent("org.apache.excalibur.source.SourceResolver");
var source = resolver.resolveURI(uri);
try {
var document = Packages.org.apache.cocoon.components.source.SourceUtil.toDOM(source);
} finally {
resolver.release(source);
cocoon.releaseComponent(resolver);
}
return document;
}


// Save a document to an URI (which should be writeable)
function saveDocument(document, uri) {
var resolver = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
var source = resolver.resolveURI(uri);
var output = null;
try {
output = source.getOutputStream();
var transformer = Packages.javax.xml.transform.TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "true");
transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, "xml");
transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.ENCODING, "ISO-8859-1");
transformer.transform(
new Packages.javax.xml.transform.dom.DOMSource(document),
new Packages.javax.xml.transform.stream.StreamResult(output)
);
} finally {
if (output != null) output.close();
resolver.release(source);
cocoon.releaseComponent(resolver);
}
}


========================================================

Note that processPipelineTo is nothing more than copying the result of a "cocoon:/" uri to an outputstream after having called FlowHelper.setContextObject(objectModel, viewdata);

Sylvain

--
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com




Reply via email to