FileDocCategorySizeDatePackage
SmartTransformerFactoryImpl.javaAPI DocJava SE 6 API15088Tue Jun 10 00:22:34 BST 2008com.sun.org.apache.xalan.internal.xsltc.trax

SmartTransformerFactoryImpl

public class SmartTransformerFactoryImpl extends SAXTransformerFactory
Implementation of a transformer factory that uses an XSLTC transformer factory for the creation of Templates objects and uses the Xalan processor transformer factory for the creation of Transformer objects.
author
G. Todd Miller

Fields Summary
private static final String
CLASS_NAME

Name of class as a constant to use for debugging.

private SAXTransformerFactory
_xsltcFactory
private SAXTransformerFactory
_xalanFactory
private SAXTransformerFactory
_currFactory
private ErrorListener
_errorlistener
private URIResolver
_uriresolver
private boolean
featureSecureProcessing

State of secure processing feature.

Constructors Summary
public SmartTransformerFactoryImpl()
implementation of the SmartTransformerFactory. This factory uses com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory to return Templates objects; and uses com.sun.org.apache.xalan.internal.processor.TransformerFactory to return Transformer objects.


                               
       
Methods Summary
private voidcreateXSLTCTransformerFactory()

	_xsltcFactory = new TransformerFactoryImpl();
	_currFactory = _xsltcFactory;
    
private voidcreateXalanTransformerFactory()

 	final String xalanMessage =
	    "com.sun.org.apache.xalan.internal.xsltc.trax.SmartTransformerFactoryImpl "+
	    "could not create an "+
	    "com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl.";
	// try to create instance of Xalan factory...	
	try {
            Class xalanFactClass = ObjectFactory.findProviderClass(
                "com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl",
                ObjectFactory.findClassLoader(), true);
	    _xalanFactory = (SAXTransformerFactory)
		xalanFactClass.newInstance();
	} 
	catch (ClassNotFoundException e) {
	    System.err.println(xalanMessage);
        }
 	catch (InstantiationException e) {
	    System.err.println(xalanMessage);
	}
 	catch (IllegalAccessException e) {
	    System.err.println(xalanMessage);
	}
	_currFactory = _xalanFactory;
    
public javax.xml.transform.SourcegetAssociatedStylesheet(javax.xml.transform.Source source, java.lang.String media, java.lang.String title, java.lang.String charset)

	if (_currFactory == null) {
            createXSLTCTransformerFactory();
        }
	return _currFactory.getAssociatedStylesheet(source, media,
		title, charset);
    
public java.lang.ObjectgetAttribute(java.lang.String name)

	// GTM: NB: 'debug' should change to something more unique... 
	if ((name.equals("translet-name")) || (name.equals("debug"))) { 
	    if (_xsltcFactory == null) {
                createXSLTCTransformerFactory();
            }
            return _xsltcFactory.getAttribute(name); 
        }
        else {
	    if (_xalanFactory == null) {
	        createXalanTransformerFactory();
	    } 
	    return _xalanFactory.getAttribute(name);
        }
    
public javax.xml.transform.ErrorListenergetErrorListener()

 
	return _errorlistener;
    
public booleangetFeature(java.lang.String name)
javax.xml.transform.sax.TransformerFactory implementation. Look up the value of a feature (to see if it is supported). This method must be updated as the various methods and features of this class are implemented.

param
name The feature name
return
'true' if feature is supported, 'false' if not

 
	// All supported features should be listed here
        String[] features = {
            DOMSource.FEATURE,
            DOMResult.FEATURE,
            SAXSource.FEATURE,
            SAXResult.FEATURE,
            StreamSource.FEATURE,
            StreamResult.FEATURE
        };
		
	// feature name cannot be null
	if (name == null) {
    	    ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
    	    throw new NullPointerException(err.toString());
	}

	// Inefficient, but it really does not matter in a function like this
	for (int i = 0; i < features.length; i++) {
	    if (name.equals(features[i]))
		return true;
	}

	// secure processing?
	if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
	    return featureSecureProcessing;
	}

	// unknown feature
        return false;
    
public javax.xml.transform.URIResolvergetURIResolver()

	return _uriresolver; 
    
public javax.xml.transform.TemplatesnewTemplates(javax.xml.transform.Source source)
Create a Templates object that from the input stylesheet Uses the com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.

param
source the stylesheet.
return
A Templates object.

        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xsltcFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xsltcFactory.setURIResolver(_uriresolver);
	}
 	_currFactory = _xsltcFactory;	 
	return _currFactory.newTemplates(source); 
    
public javax.xml.transform.sax.TemplatesHandlernewTemplatesHandler()
Get a TemplatesHandler object that can process SAX ContentHandler events into a Templates object. Uses the com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.

        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xsltcFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xsltcFactory.setURIResolver(_uriresolver);
	}
	return _xsltcFactory.newTemplatesHandler();
    
public javax.xml.transform.TransformernewTransformer()
Create a Transformer object that copies the input document to the result. Uses the com.sun.org.apache.xalan.internal.processor.TransformerFactory.

return
A Transformer object.

	if (_xalanFactory == null) {
            createXalanTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xalanFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xalanFactory.setURIResolver(_uriresolver);
	}
 	_currFactory = _xalanFactory;	 
	return _currFactory.newTransformer(); 
    
public javax.xml.transform.TransformernewTransformer(javax.xml.transform.Source source)
Create a Transformer object that from the input stylesheet Uses the com.sun.org.apache.xalan.internal.processor.TransformerFactory.

param
source the stylesheet.
return
A Transformer object.

        if (_xalanFactory == null) {
            createXalanTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xalanFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xalanFactory.setURIResolver(_uriresolver);
	}
 	_currFactory = _xalanFactory;	 
	return _currFactory.newTransformer(source); 
    
public javax.xml.transform.sax.TransformerHandlernewTransformerHandler()
Get a TransformerHandler object that can process SAX ContentHandler events based on a copy transformer. Uses com.sun.org.apache.xalan.internal.processor.TransformerFactory.

        if (_xalanFactory == null) {
            createXalanTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xalanFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xalanFactory.setURIResolver(_uriresolver);
	}
	return _xalanFactory.newTransformerHandler(); 
    
public javax.xml.transform.sax.TransformerHandlernewTransformerHandler(javax.xml.transform.Source src)
Get a TransformerHandler object that can process SAX ContentHandler events based on a transformer specified by the stylesheet Source. Uses com.sun.org.apache.xalan.internal.processor.TransformerFactory.

        if (_xalanFactory == null) {
            createXalanTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xalanFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xalanFactory.setURIResolver(_uriresolver);
	}
	return _xalanFactory.newTransformerHandler(src); 
    
public javax.xml.transform.sax.TransformerHandlernewTransformerHandler(javax.xml.transform.Templates templates)
Get a TransformerHandler object that can process SAX ContentHandler events based on a transformer specified by the stylesheet Source. Uses com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.

        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xsltcFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xsltcFactory.setURIResolver(_uriresolver);
	}
        return _xsltcFactory.newTransformerHandler(templates);
    
public org.xml.sax.XMLFilternewXMLFilter(javax.xml.transform.Source src)
Create an XMLFilter that uses the given source as the transformation instructions. Uses com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactory.

        if (_xsltcFactory == null) {
            createXSLTCTransformerFactory();
        }
	if (_errorlistener != null) {
	    _xsltcFactory.setErrorListener(_errorlistener);	    
	}
	if (_uriresolver != null) {
	    _xsltcFactory.setURIResolver(_uriresolver);
	}
	Templates templates = _xsltcFactory.newTemplates(src);
	if (templates == null ) return null;
	return newXMLFilter(templates); 
    
public org.xml.sax.XMLFilternewXMLFilter(javax.xml.transform.Templates templates)

	try {
            return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates);
        }
        catch(TransformerConfigurationException e1) {
            if (_xsltcFactory == null) {
                createXSLTCTransformerFactory();
            }
	    ErrorListener errorListener = _xsltcFactory.getErrorListener();
            if(errorListener != null) {
                try {
                    errorListener.fatalError(e1);
                    return null;
                }
                catch( TransformerException e2) {
                    new TransformerConfigurationException(e2);
                }
            }
            throw e1;
        }
    
public voidsetAttribute(java.lang.String name, java.lang.Object value)

 
	// GTM: NB: 'debug' should change to something more unique... 
	if ((name.equals("translet-name")) || (name.equals("debug"))) { 
	    if (_xsltcFactory == null) {
                createXSLTCTransformerFactory();
            }
            _xsltcFactory.setAttribute(name, value); 
        }
        else {
	    if (_xalanFactory == null) {
	        createXalanTransformerFactory();
	    } 
	    _xalanFactory.setAttribute(name, value);
        }
    
public voidsetErrorListener(javax.xml.transform.ErrorListener listener)

	_errorlistener = listener;
    
public voidsetFeature(java.lang.String name, boolean value)

Set a feature for this SmartTransformerFactory and Transformers or Templates created by this factory.

Feature names are fully qualified {@link java.net.URI}s. Implementations may define their own features. An {@link TransformerConfigurationException} is thrown if this TransformerFactory or the Transformers or Templates it creates cannot support the feature. It is possible for an TransformerFactory to expose a feature value but be unable to change its state.

See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.

param
name Feature name.
param
value Is feature state true or false.
throws
TransformerConfigurationException if this TransformerFactory or the Transformers or Templates it creates cannot support this feature.
throws
NullPointerException If the name parameter is null.


	// feature name cannot be null
	if (name == null) {
            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
    	    throw new NullPointerException(err.toString());
	}
	// secure processing?
	else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
	    featureSecureProcessing = value;		
	    // all done processing feature
	    return;
	}
	else {	
	    // unknown feature
            ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
            throw new TransformerConfigurationException(err.toString());
        }
    
public voidsetURIResolver(javax.xml.transform.URIResolver resolver)

	_uriresolver = resolver;