mkwan       2003/02/03 12:08:04

  Modified:    java/src/org/apache/xalan/xsltc/trax Tag: XSLTC_DTM
                        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
  No                   revision
  
  
  No                   revision
  
  
  1.10.2.8  +32 -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.10.2.7
  retrieving revision 1.10.2.8
  diff -u -r1.10.2.7 -r1.10.2.8
  --- TemplatesImpl.java        30 Jan 2003 18:41:50 -0000      1.10.2.7
  +++ TemplatesImpl.java        3 Feb 2003 20:08:02 -0000       1.10.2.8
  @@ -102,6 +102,11 @@
       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 
        * this Templates is created or when it is read back from disk.
        */
  @@ -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,20 @@
        _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 +356,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.34.2.12 +104 -31   
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.34.2.11
  retrieving revision 1.34.2.12
  diff -u -r1.34.2.11 -r1.34.2.12
  --- TransformerFactoryImpl.java       30 Jan 2003 18:41:50 -0000      
1.34.2.11
  +++ TransformerFactoryImpl.java       3 Feb 2003 20:08:02 -0000       
1.34.2.12
  @@ -97,9 +97,11 @@
   import javax.xml.transform.stream.StreamResult;
   import javax.xml.transform.stream.StreamSource;
   
  +import org.apache.xalan.xsltc.Translet;
   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 +113,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.
  @@ -205,6 +219,12 @@
       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;
  +
  +    /**
        * The name of the feature for incremental building of the DTM.
     */
       public static String FEATURE_INCREMENTAL = 
"http://xml.apache.org/xalan/features/incremental";;
       
  @@ -271,13 +291,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);
        }
        else if (name.equals(FEATURE_INCREMENTAL)) {
  @@ -302,23 +322,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;
  @@ -328,7 +348,7 @@
                return;
            }
        }
  -     else if (name.equals("auto-translet")) {
  +     else if (name.equals(AUTO_TRANSLET)) {
            if (value instanceof Boolean) {
                _autoTranslet = ((Boolean) value).booleanValue();
                return;
  @@ -338,7 +358,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;
  @@ -348,7 +378,7 @@
                return;
            }
        }
  -     else if (name.equals("enable-inlining")) {
  +     else if (name.equals(ENABLE_INLINING)) {
            if (value instanceof Boolean) {
                _enableInlining = ((Boolean) value).booleanValue();
                return;
  @@ -368,7 +398,7 @@
                return;
            }
        }
  -     else if (name.equals("indent-number")) {
  +     else if (name.equals(INDENT_NUMBER)) {
            if (value instanceof String) {
                try {
                    _indentNumber = Integer.parseInt((String) value);
  @@ -553,6 +583,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.
  @@ -564,11 +612,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);
  @@ -619,9 +696,8 @@
        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);
            else {
  @@ -1203,22 +1279,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);
  @@ -1226,13 +1305,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.37.2.14 +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.37.2.13
  retrieving revision 1.37.2.14
  diff -u -r1.37.2.13 -r1.37.2.14
  --- TransformerImpl.java      30 Jan 2003 18:41:51 -0000      1.37.2.13
  +++ TransformerImpl.java      3 Feb 2003 20:08:03 -0000       1.37.2.14
  @@ -960,6 +960,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]

Reply via email to