zongaro 2003/02/04 08:58:17
Modified: java/samples/CompiledJAXP Tag: XSLTC_DTM Compile.java
Transform.java README.cjaxp
Log:
Updated samples to use the JAXP transform API's, along with XSLTC's
"generate-translet" and "use-classpath" attributes to write translets to
class files and from there to load them.
Revision Changes Path
No revision
No revision
1.1.12.2 +2 -40 xml-xalan/java/samples/CompiledJAXP/Compile.java
Index: Compile.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledJAXP/Compile.java,v
retrieving revision 1.1.12.1
retrieving revision 1.1.12.2
diff -u -r1.1.12.1 -r1.1.12.2
--- Compile.java 27 Jan 2003 19:43:38 -0000 1.1.12.1
+++ Compile.java 4 Feb 2003 16:58:16 -0000 1.1.12.2
@@ -56,17 +56,9 @@
*
*/
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.ObjectOutputStream;
-
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Templates;
-import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
-
-import org.apache.xalan.xsltc.trax.TransformerFactoryImpl;
+import javax.xml.transform.stream.StreamSource;
public class Compile {
@@ -87,44 +79,14 @@
// The TransformerFactory will compile the stylesheet and
// put the translet classes inside the Templates object
TransformerFactory factory = TransformerFactory.newInstance();
+ factory.setAttribute("generate-translet", Boolean.TRUE);
Templates templates = factory.newTemplates(stylesheet);
-
- // Send the Templates object to a '.translet' file
- dumpTemplate(getBaseName(xsl)+".translet", templates);
}
catch (Exception e) {
System.err.println("Exception: " + e);
e.printStackTrace();
}
System.exit(0);
- }
-
- /**
- * Returns the base-name of a file/url
- */
- private String getBaseName(String filename) {
- int start = filename.lastIndexOf(File.separatorChar);
- int stop = filename.lastIndexOf('.');
- if (stop <= start) stop = filename.length() - 1;
- return filename.substring(start+1, stop);
- }
-
- /**
- * Writes a Templates object to a file
- */
- private void dumpTemplate(String file, Templates templates) {
- try {
- FileOutputStream ostream = new FileOutputStream(file);
- ObjectOutputStream p = new ObjectOutputStream(ostream);
- p.writeObject(templates);
- p.flush();
- ostream.close();
- }
- catch (Exception e) {
- System.err.println(e);
- e.printStackTrace();
- System.err.println("Could not write file "+file);
- }
}
private void usage() {
1.2.2.2 +14 -44 xml-xalan/java/samples/CompiledJAXP/Transform.java
Index: Transform.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledJAXP/Transform.java,v
retrieving revision 1.2.2.1
retrieving revision 1.2.2.2
diff -u -r1.2.2.1 -r1.2.2.2
--- Transform.java 27 Jan 2003 19:43:38 -0000 1.2.2.1
+++ Transform.java 4 Feb 2003 16:58:17 -0000 1.2.2.2
@@ -56,18 +56,12 @@
*
*/
-import java.io.File;
import java.io.OutputStreamWriter;
-import java.io.FileInputStream;
-import java.io.ObjectInputStream;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
-
-import org.apache.xalan.xsltc.trax.TransformerFactoryImpl;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
public class Transform {
@@ -77,19 +71,24 @@
}
/**
- * Reads a Templates object from a file, the Templates object creates
- * a translet and wraps it in a Transformer. The translet performs the
- * transformation on behalf of the Transformer.transform() method.
+ * Asks the TransformerFactory to try to load a precompiled version of
+ * the translet from the class path to construct a Transformer object.
+ * The translet performs the transformation on behalf of the
+ * Transformer.transform() method.
*/
public void run(String[] args){
String xml = args[0];
- String translet = args[1];
+ String transletURI = args[1];
try {
+ TransformerFactory tf = TransformerFactory.newInstance();
+ tf.setAttribute("use-classpath", Boolean.TRUE);
+ Transformer transformer = tf.newTransformer(
+ new
StreamSource(transletURI));
+
StreamSource document = new StreamSource(xml);
- StreamResult result = new StreamResult(new
OutputStreamWriter(System.out));
- Templates templates = readTemplates(translet);
- Transformer transformer = templates.newTransformer();
+ StreamResult result = new StreamResult(
+ new
OutputStreamWriter(System.out));
transformer.transform(document, result);
}
catch (Exception e) {
@@ -97,35 +96,6 @@
e.printStackTrace();
}
System.exit(0);
- }
-
- /**
- * Reads a Templates object from a file
- */
- private Templates readTemplates(String file) {
- try {
- FileInputStream ostream = new FileInputStream(file);
- ObjectInputStream p = new ObjectInputStream(ostream);
- Templates templates = (Templates)p.readObject();
- ostream.close();
- return(templates);
- }
- catch (Exception e) {
- System.err.println(e);
- e.printStackTrace();
- System.err.println("Could not write file "+file);
- return null;
- }
- }
-
- /**
- * Returns the base-name of a file/url
- */
- private String getBaseName(String filename) {
- int start = filename.lastIndexOf(File.separatorChar);
- int stop = filename.lastIndexOf('.');
- if (stop <= start) stop = filename.length() - 1;
- return filename.substring(start+1, stop);
}
public void usage() {
1.1.12.1 +9 -11 xml-xalan/java/samples/CompiledJAXP/README.cjaxp
Index: README.cjaxp
===================================================================
RCS file: /home/cvs/xml-xalan/java/samples/CompiledJAXP/README.cjaxp,v
retrieving revision 1.1
retrieving revision 1.1.12.1
diff -u -r1.1 -r1.1.12.1
--- README.cjaxp 1 Oct 2001 13:41:50 -0000 1.1
+++ README.cjaxp 4 Feb 2003 16:58:17 -0000 1.1.12.1
@@ -1,25 +1,23 @@
======================================================================
-The Sun XSLT Compiler (XSLTC) is a Java-based tool for compiling XSL
+The XSLT Compiler (XSLTC) is a Java-based tool for compiling XSL
stylesheets into extremely lightweight and portable Java byte code.
-This Compiled JAXP Demo shows you how can compile and use compiled
+This Compiled JAXP Demo shows you one way to compile and use compiled
translets with JAXP.
-You use the two classes provided, Compile and Transform, just like the
+Use the two classes provided, Compile and Transform, just like the
org.apache.xalan.xsltc.cmdline Compile and Transform classes.
Compile.java
- Compiles an XSL stylesheet into a translet, wraps the translet
- inside a Templates object and serializes it to a
- '.translet' file.
+ Compiles an XSL stylesheet into a translet, which is written to
+ a '.class' file.
Transform.java
- Reads a Templates object from the .translet file, the Templates
- object creates a translet and wraps it in a Transformer. The
- translet performs the transformation on behalf of the
- Transformer.transform() method.
+ Constructs a TransformerFactory and asks it to load a translet
+ in the form of a Transformer. The translet performs the
+ transformation on behalf of the Transformer.transform() method.
-Usuage
+Usage
java Compile <stylesheet.xsl>
java Transform <xmlfile.xml> <stylesheet>.translet
----------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]