mkwan 2003/02/03 11:31:13
Modified: java/src/org/apache/xalan/xsltc/trax TemplatesImpl.java
TransformerFactoryImpl.java TransformerImpl.java
Log:
Add a new attribute "use-classpath" to the XSLTC Trax API.
If this attribute is set, the Templates object is created from a translet
which is loaded from the CLASSPATH. The translet name is either set by
the "translet_name" attribute, or derived from the system ID, or the
default name. In TemplatesImpl, we add a new constructor to support
creating a TemplatesImpl from a Translet instance.
Revision Changes Path
1.26 +33 -6
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TemplatesImpl.java
Index: TemplatesImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TemplatesImpl.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- TemplatesImpl.java 30 Jan 2003 18:46:14 -0000 1.25
+++ TemplatesImpl.java 3 Feb 2003 19:31:12 -0000 1.26
@@ -100,6 +100,11 @@
* any auxiliary classes.
*/
private byte[][] _bytecodes = null;
+
+ /**
+ * The Translet instance from which the template is created.
+ */
+ private Translet _translet = null;
/**
* Contains the translet class definition(s). These are created when
@@ -146,9 +151,9 @@
/**
- * The only way to create an XSLTC emplate object
+ * Create an XSLTC template object from the bytecodes.
* The bytecodes for the translet and auxiliary classes, plus the name of
- * the main translet class, must be supplied
+ * the main translet class, must be supplied.
*/
protected TemplatesImpl(byte[][] bytecodes, String transletName,
Properties outputProperties, int indentNumber,
@@ -160,6 +165,21 @@
_indentNumber = indentNumber;
_tfactory = tfactory;
}
+
+ /**
+ * Create an XSLTC template object from a Translet instance.
+ */
+ protected TemplatesImpl(Translet translet, String transletName,
+ Properties outputProperties, int indentNumber,
+ TransformerFactoryImpl tfactory)
+ {
+ _translet = translet;
+ _name = transletName;
+ _outputProperties = outputProperties;
+ _indentNumber = indentNumber;
+ _tfactory = tfactory;
+ }
+
/**
* Need for de-serialization, see readObject().
@@ -337,9 +357,16 @@
public synchronized Transformer newTransformer()
throws TransformerConfigurationException
{
- final TransformerImpl transformer =
- new TransformerImpl(getTransletInstance(), _outputProperties,
- _indentNumber, _tfactory);
+ TransformerImpl transformer;
+ if (_translet != null) {
+ transformer = new TransformerImpl(_translet, _outputProperties,
+ _indentNumber, _tfactory);
+ }
+ else {
+ transformer = new TransformerImpl(getTransletInstance(),
_outputProperties,
+ _indentNumber, _tfactory);
+ }
+
if (_uriResolver != null) {
transformer.setURIResolver(_uriResolver);
}
1.57 +102 -30
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java
Index: TransformerFactoryImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerFactoryImpl.java,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -r1.56 -r1.57
--- TransformerFactoryImpl.java 30 Jan 2003 18:46:14 -0000 1.56
+++ TransformerFactoryImpl.java 3 Feb 2003 19:31:12 -0000 1.57
@@ -100,6 +100,7 @@
import org.apache.xalan.xsltc.compiler.SourceLoader;
import org.apache.xalan.xsltc.compiler.XSLTC;
import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
+import org.apache.xalan.xsltc.runtime.TransletLoader;
import org.xml.sax.InputSource;
import org.xml.sax.XMLFilter;
@@ -111,6 +112,18 @@
public class TransformerFactoryImpl
extends SAXTransformerFactory implements SourceLoader, ErrorListener
{
+ // Public constants for attributes supported by the XSLTC
TransformerFactory.
+ public final static String TRANSLET_NAME = "translet-name";
+ public final static String DESTINATION_DIRECTORY =
"destination-directory";
+ public final static String PACKAGE_NAME = "package-name";
+ public final static String JAR_NAME = "jar-name";
+ public final static String GENERATE_TRANSLET = "generate-translet";
+ public final static String AUTO_TRANSLET = "auto-translet";
+ public final static String USE_CLASSPATH = "use-classpath";
+ public final static String DEBUG = "debug";
+ public final static String ENABLE_INLINING = "enable-inlining";
+ public final static String INDENT_NUMBER = "indent-number";
+
/**
* This error listener is used only for this factory and is not passed to
* the Templates or Transformer objects that we create.
@@ -203,6 +216,12 @@
* is only used if its timestamp is newer than the timestamp of the
stylesheet.
*/
private boolean _autoTranslet = false;
+
+ /**
+ * If this is set to <code>true</code>, we attempt to load the translet
from the
+ * CLASSPATH.
+ */
+ private boolean _useClasspath = false;
/**
* Number of indent spaces when indentation is turned on.
@@ -263,13 +282,13 @@
throws IllegalArgumentException
{
// Return value for attribute 'translet-name'
- if (name.equals("translet-name")) {
+ if (name.equals(TRANSLET_NAME)) {
return _transletName;
}
- else if (name.equals("generate-translet")) {
+ else if (name.equals(GENERATE_TRANSLET)) {
return new Boolean(_generateTranslet);
}
- else if (name.equals("auto-translet")) {
+ else if (name.equals(AUTO_TRANSLET)) {
return new Boolean(_autoTranslet);
}
@@ -291,23 +310,23 @@
{
// Set the default translet name (ie. class name), which will be used
// for translets that cannot be given a name from their system-id.
- if (name.equals("translet-name") && value instanceof String) {
+ if (name.equals(TRANSLET_NAME) && value instanceof String) {
_transletName = (String) value;
return;
}
- else if (name.equals("destination-directory") && value instanceof
String) {
+ else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String)
{
_destinationDirectory = (String) value;
return;
}
- else if (name.equals("package-name") && value instanceof String) {
+ else if (name.equals(PACKAGE_NAME) && value instanceof String) {
_packageName = (String) value;
return;
}
- else if (name.equals("jar-name") && value instanceof String) {
+ else if (name.equals(JAR_NAME) && value instanceof String) {
_jarFileName = (String) value;
return;
}
- else if (name.equals("generate-translet")) {
+ else if (name.equals(GENERATE_TRANSLET)) {
if (value instanceof Boolean) {
_generateTranslet = ((Boolean) value).booleanValue();
return;
@@ -317,7 +336,7 @@
return;
}
}
- else if (name.equals("auto-translet")) {
+ else if (name.equals(AUTO_TRANSLET)) {
if (value instanceof Boolean) {
_autoTranslet = ((Boolean) value).booleanValue();
return;
@@ -327,7 +346,17 @@
return;
}
}
- else if (name.equals("debug")) {
+ else if (name.equals(USE_CLASSPATH)) {
+ if (value instanceof Boolean) {
+ _useClasspath = ((Boolean) value).booleanValue();
+ return;
+ }
+ else if (value instanceof String) {
+ _useClasspath = ((String) value).equalsIgnoreCase("true");
+ return;
+ }
+ }
+ else if (name.equals(DEBUG)) {
if (value instanceof Boolean) {
_debug = ((Boolean) value).booleanValue();
return;
@@ -337,7 +366,7 @@
return;
}
}
- else if (name.equals("enable-inlining")) {
+ else if (name.equals(ENABLE_INLINING)) {
if (value instanceof Boolean) {
_enableInlining = ((Boolean) value).booleanValue();
return;
@@ -347,7 +376,7 @@
return;
}
}
- else if (name.equals("indent-number")) {
+ else if (name.equals(INDENT_NUMBER)) {
if (value instanceof String) {
try {
_indentNumber = Integer.parseInt((String) value);
@@ -532,6 +561,24 @@
}
/**
+ * Load the translet class using the context class loader of the current
+ * thread or the default class loader.
+ */
+ private Class loadTranslet(String name)
+ throws ClassNotFoundException
+ {
+ // First try to load the class using the context class loader of the
current thread
+ try {
+ TransletLoader loader = new TransletLoader();
+ return loader.loadTranslet(name);
+ }
+ catch (ClassNotFoundException e) {
+ // Then try to load the class using the default class loader.
+ return Class.forName(name);
+ }
+ }
+
+ /**
* javax.xml.transform.sax.TransformerFactory implementation.
* Process the Source into a Templates object, which is a a compiled
* representation of the source.
@@ -543,11 +590,40 @@
public Templates newTemplates(Source source)
throws TransformerConfigurationException
{
+ // If the _useClasspath attribute is true, try to load the translet from
+ // the CLASSPATH and create a template object using the loaded
+ // translet.
+ if (_useClasspath) {
+ String transletName = getTransletBaseName(source);
+
+ if (_packageName != null)
+ transletName = _packageName + "." + transletName;
+
+ try {
+ final Class clazz = loadTranslet(transletName);
+ final Translet translet = (Translet)clazz.newInstance();
+ resetTransientAttributes();
+
+ return new TemplatesImpl(translet, transletName, null,
_indentNumber, this);
+ }
+ catch (ClassNotFoundException cnfe) {
+ ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR,
transletName);
+ throw new TransformerConfigurationException(err.toString());
+ }
+ catch (Exception e) {
+ ErrorMsg err = new ErrorMsg(ErrorMsg.getTransletErrorMessage()
+ e.getMessage());
+ throw new TransformerConfigurationException(err.toString());
+ }
+ }
+
// If _autoTranslet is true, we will try to load the bytecodes
// from the translet classes without compiling the stylesheet.
if (_autoTranslet) {
byte[][] bytecodes = null;
- String transletClassName = getTransletClassName(source);
+ String transletClassName = getTransletBaseName(source);
+
+ if (_packageName != null)
+ transletClassName = _packageName + "." + transletClassName;
if (_jarFileName != null)
bytecodes = getBytecodesFromJar(source, transletClassName);
@@ -598,8 +674,7 @@
int outputType = XSLTC.BYTEARRAY_OUTPUT;
if (_generateTranslet || _autoTranslet) {
// Set the translet name
- if (!_transletName.equals(DEFAULT_TRANSLET_NAME))
- xsltc.setClassName(_transletName);
+ xsltc.setClassName(getTransletBaseName(source));
if (_destinationDirectory != null)
xsltc.setDestDirectory(_destinationDirectory);
@@ -1182,22 +1257,25 @@
}
/**
- * Return the fully qualified class name of the translet
+ * Return the base class name of the translet.
+ * The translet name is resolved using the following rules:
+ * 1. if the _transletName attribute is set and its value is not
"GregorSamsa",
+ * then _transletName is returned.
+ * 2. otherwise get the translet name from the base name of the system ID
+ * 3. return "GregorSamsa" if the result from step 2 is null.
*
- * @param source The Source
- * @return The full name of the translet class
+ * @param source The input Source
+ * @return The name of the translet class
*/
- private String getTransletClassName(Source source)
+ private String getTransletBaseName(Source source)
{
String transletBaseName = null;
if (!_transletName.equals(DEFAULT_TRANSLET_NAME))
- transletBaseName = _transletName;
+ return _transletName;
else {
String systemId = source.getSystemId();
if (systemId != null) {
String baseName = Util.baseName(systemId);
- //if (baseName != null)
- // transletBaseName = Util.noExtName(baseName);
if (baseName != null) {
baseName = Util.noExtName(baseName);
transletBaseName = Util.toJavaName(baseName);
@@ -1205,13 +1283,7 @@
}
}
- if (transletBaseName == null)
- transletBaseName = DEFAULT_TRANSLET_NAME;
-
- if (_packageName != null)
- return _packageName + "." + transletBaseName;
- else
- return transletBaseName;
+ return (transletBaseName != null) ? transletBaseName :
DEFAULT_TRANSLET_NAME;
}
/**
1.62 +6 -1
xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java
Index: TransformerImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/trax/TransformerImpl.java,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -r1.61 -r1.62
--- TransformerImpl.java 3 Feb 2003 14:33:21 -0000 1.61
+++ TransformerImpl.java 3 Feb 2003 19:31:13 -0000 1.62
@@ -1009,6 +1009,11 @@
base.setProperty(name, outputProperties.getProperty(name));
}
}
+ else {
+ base.setProperty(OutputKeys.ENCODING, _translet._encoding);
+ if (_translet._method != null)
+ base.setProperty(OutputKeys.METHOD, _translet._method);
+ }
// Update defaults based on output method
final String method = base.getProperty(OutputKeys.METHOD);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]