How to convert between various types of data:

Please help by adding best-practice sample code (and links to other pages with more detailed explanations) for various environments in Cocoon, such as plain Java objects, Flowscript, XSP, XSL, JXPath, etc. Also, please feel free to refactor, rename, or even rewrite this page as needed.

Unescaped String -> Escaped String

In Java code

  import org.apache.commons.lang.StringEscapeUtils;

    public String escape(String input) {
        return StringEscapeUtils.escapeJava(input);

    }

In Flowscript

  function escapeString(unescaped_string) {
      return Packages.org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(unescaped_string);
  }

The library commons-lang-2.0 is in the lib/core folder of cocoon distribution and contains a lot of other helpful functionality

Escaped String -> Unescaped String

In Java code

  import org.apache.commons.lang.StringEscapeUtils;

    public String escape(String input) {
        return StringEscapeUtils.unescapeJava(input);

    }

In Flowscript

  function unescapeString(escaped_string) {
      return Packages.org.apache.commons.lang.StringEscapeUtils.unescapeJavaScript(escaped_string);
  }

Unescaped String -> DOM

  import org.apache.excalibur.xml.dom.DOMParser;
  import org.w3c.dom.Document;
  import org.xml.sax.InputSource;

    ServiceManager manager;
   (obtained via Serviceable or something)


    public Document stringToDOM(String input) throws Exception {
        Document result = null;
        DOMParser parser = null;
        try {
            parser = (DOMParser)manager.lookup(DOMParser.ROLE);
            result = parser.parseDocument(new InputSource(new StringReader(input)));
        } finally {
            manager.release(parser);
        }
        return result;
    }

Unescaped String -> DOM fragment

DOM -> Unescaped String

FlowScript:

  function serializeNode(node) {
      return Packages.org.apache.cocoon.xml.XMLUtils.serializeNodeToXML(node);
  }

DOM Fragment -> Unescaped String

Escaped String -> DOM

Escaped String -> DOM fragment

DOM -> Escaped String

DOM Fragment -> Escaped String

Source -> DOM

DOM -> SAX stream

Validate a DOM against a DTD

Validate a DOM against an XML schema

Evaluate an XPath expression against a DOM

Others?

XMLProcessingHowTo (last edited 2009-09-20 23:40:36 by localhost)