Interfacing XT to Java

Interfacing XT to Java

You can also interface the XT processor to Java. The XT API is designed to interface easily with the classes defined in Suns Project X TR2, which supports XML handling. You need the file xml.jar from Sun, which you can get by downloading Suns Project X TR2. You need to be a member of Suns developer site, http://developer.java.sun.com, to get xml.jar. Fortunately, becoming a member is free, but you have to fill out a number of forms at http://developer.java.sun.com.

You need xml.jar for the com.sun.xml.tree.XmlDocument class. This class supports XML documents, and I start this new example, xtjava .java, by creating a new XmlDocument object for the source document, the XSLT stylesheet, and the result document:

 import java.io.IOException;  import java.io.OutputStream;  import java.io.FileOutputStream;  import org.xml.sax.SAXException;  import com.sun.xml.tree.XmlDocument;  import com.jclark.xsl.dom.Transform;  import com.jclark.xsl.dom.TransformEngine;  import com.jclark.xsl.dom.TransformException;  import com.jclark.xsl.dom.XSLTransformEngine;  class xtjava  {     public static void main(String[] args)          throws IOException, SAXException, TransformException      {         XmlDocument XMLdoc = new XmlDocument().createXmlDocument(args[0]);          XmlDocument XSLdoc = new XmlDocument().createXmlDocument(args[1]);          XmlDocument newdoc = new XmlDocument();          .          .          . 

Next, I create an XSLTranformationEngine object, and use this objects createTransform method to create a new Transform object based on the XSLT stylesheet:

 import java.io.IOException;          .          .          .  class xtjava  {     public static void main(String[] args)          throws IOException, SAXException, TransformException      {         XmlDocument doc = new XmlDocument();          XSLTransformEngine transformEngine = new XSLTransformEngine();          Transform transform = transformEngine.createTransform(XSLdoc);          .          .          . 

Next, I can transform the XML document into the result documents object this way:

 import java.io.IOException;          .          .          .  class xtjava  {     public static void main(String[] args)          throws IOException, SAXException, TransformException      {         XmlDocument XMLdoc = new XmlDocument().createXmlDocument(args[0]);          XmlDocument XSLdoc = new XmlDocument().createXmlDocument(args[1]);          XmlDocument newdoc = new XmlDocument();          XSLTransformEngine transformEngine = new XSLTransformEngine();          Transform transform = transformEngine.createTransform(XSLdoc);          transform.transform(XMLdoc, newdoc);          .          .          . 

That completes the transformation. All that remains is to write the result document, newdoc , to disk, and I do that as follows with a FileOutputStream object:

Listing 10.10 xtjava.java, Interfacing XT to Java
 import java.io.IOException;  import java.io.OutputStream;  import java.io.FileOutputStream;  import org.xml.sax.SAXException;  import com.sun.xml.tree.XmlDocument;  import com.jclark.xsl.dom.Transform;  import com.jclark.xsl.dom.TransformEngine;  import com.jclark.xsl.dom.TransformException;  import com.jclark.xsl.dom.XSLTransformEngine;  class xtjava  {     public static void main(String[] args)          throws IOException, SAXException, TransformException      {         XmlDocument XMLdoc = new XmlDocument().createXmlDocument(args[0]);          XmlDocument XSLdoc = new XmlDocument().createXmlDocument(args[1]);          XmlDocument newdoc = new XmlDocument();          XSLTransformEngine transformEngine = new XSLTransformEngine();          Transform transform = transformEngine.createTransform(XSLdoc);          transform.transform(XMLdoc, newdoc);          OutputStream out = new FileOutputStream(args[2]);          newdoc.write(out);          out.close();      }  } 

To run this example, you include xt.jar and xml.jar in the classpath:

 C:\>set classpath=.;d:\xt\xt.jar;xml.jar 

Then you compile xtjava.java with the Java compiler, javac, and run it as follows, supplying the URLs of the XML and XSL documents (you can also use file URLs, as you saw earlier in this chapter):

 C:\>java xtjava http://www.starpowder.com/planets.xml http://www.starpowder.com/ graphics/ccc.gif planets.xsl planets.html 

And thats all it takes.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net