[jdom-interest] JDOM and XSLTC.

Cybrarian Rök cyb at popol.auteurs-associes.com
Mon Oct 8 07:52:00 PDT 2001


[not urgent, not critical]

Hello,

I wonder if it would be trivial to use a JDOM document and pass it to
a Translet for further transformations? Discussing this question would
help me to make better choices in designing my application.

*** Context:
I've build a servlet whose aim is to serve documentation;
it uses JDOMb7 for its configuration task and documents manipulation
tasks; I feel very well with JDOM and do not want to change; 

The final transformations tasks are performed using either:
- traX, Xalan-J2.2 (HTML, XML, PDF)
- org.apache.xalan.xsltc.Translet (HTML, XML)
all materials used during transformations are cached (XSL,
XML, Translets) and also the results of the transformations (HTML,
PDF...);
according to its configuration file, the servlet proceed with the
desired method for each document;

*** Problem:
As I guessed, it seems difficult to re-use the result of a JDOM
manipulation task, ie. a org.jdom.Document, with XSLTC;

to illustrate the point, the .transform() method of the
org.apache.xalan.xsltc.Translet interface accept only a
org.apache.xalan.xsltc.DOM interface as first arg; ie. a
org.apache.xalan.xsltc.dom.DOMImpl in common case;

even though I've seen many tricks about JDOM and TraX
(jdom-interest mailing list), I would like to keep advantages of the
compiled Translets to perform transfomations on newly created JDOM
Documents without outputing to XML file and re-parsing :(

*** Example code:
I submit an example code; it's the slow way to do that; I could not
find a solution using piped streams as shown in the mailing list for
TraX transformations;

import org.jdom.*;
import org.jdom.input.DOMBuilder;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import java.util.*;
import java.io.*;

public class JdomToXSLTC {
  public static void main(String[] args) {
    try {
      /* ... manipulation tasks ... */
      Element e_page = new Element("page");
      Element e_sections = new Element("sections");
      e_page.addContent(e_sections);
      /* ... and so on ... */
      Document doc_result = new Document(e_page);
      for (int i = 0; i < args.length; i++) {
	SAXBuilder builder = new SAXBuilder(false);
	Document doc = builder.build(new FileInputStream(args[i]));
	List li = doc.getRootElement().getChild("sections")
		.getChildren("section");
	for (Iterator it = li.iterator(); it.hasNext();) {
	  e_sections.addContent(((Element)it.next()).detach());
	  /* ... and so on ... */
	}
      }
      XMLOutputter x = new XMLOutputter("", false, "ISO-8859-1");
      x.setTextNormalize(false);
      File f = new File("out.xml");
      x.output(doc_result, new FileWriter(f));
      org.apache.xalan.xsltc.dom.DOMImpl domImpl =
	new org.apache.xalan.xsltc.dom.DOMImpl();

      domImpl = getDomImpl(f);
      /* ... now pass the domImpl to a Translet
	     for transformation ... */
      /*     goFurther(domImpl);    */

    } catch (Exception e) {e.printStackTrace();}
    System.exit(0);
  }

  static org.apache.xalan.xsltc.dom.DOMImpl getDomImpl(File f) {
    final javax.xml.parsers.SAXParserFactory factory =
	javax.xml.parsers.SAXParserFactory.newInstance();
    try {
factory.setFeature(org.apache.xalan.xsltc.compiler.Constants.NAMESPACE_FEATURE,
true);
    } catch (Exception e) {factory.setNamespaceAware(true);}

    final org.apache.xalan.xsltc.dom.DOMImpl domImpl =
	new org.apache.xalan.xsltc.dom.DOMImpl();
    org.xml.sax.XMLReader       reader = null;
    javax.xml.parsers.SAXParser parser = null;
    try {
      parser = factory.newSAXParser();
      reader = parser.getXMLReader();
      reader.setContentHandler(domImpl.getBuilder());
      reader.parse(new org.xml.sax.InputSource(new FileReader(f)));
    } catch (javax.xml.parsers.ParserConfigurationException e) {
    } catch (org.xml.sax.SAXException e ) {
    } catch (java.io.IOException e ) {
    }
    return domImpl;
  }
}

-Cheers.

-- 
Cybrarian Rök - CAFV administrator
Auteurs & Associés SARL - BP106
74502 Evian les Bains cedex - F
PERSO: http://auteurs-associes.com:443/~Cybrarian
HOME : http://auteurs-associes.com
EMAIL: cyb at popol.auteurs-associes.com
--
try me:be_cool(who);
except (E_ANY) $war_utils:kill(who);




More information about the jdom-interest mailing list