src/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java

Print this page




 208      */
 209     private Class m_DTMManagerClass;
 210 
 211     /**
 212      * <p>State of secure processing feature.</p>
 213      */
 214     private boolean _isNotSecureProcessing = true;
 215     /**
 216      * <p>State of secure mode.</p>
 217      */
 218     private boolean _isSecureMode = false;
 219 
 220     /**
 221      * Indicates whether implementation parts should use
 222      *   service loader (or similar).
 223      * Note the default value (false) is the safe option..
 224      */
 225     private boolean _useServicesMechanism;
 226 
 227     /**










 228      * javax.xml.transform.sax.TransformerFactory implementation.
 229      */
 230     public TransformerFactoryImpl() {
 231         this(true);
 232     }
 233 
 234     public static TransformerFactory newTransformerFactoryNoServiceLoader() {
 235         return new TransformerFactoryImpl(false);
 236     }
 237 
 238     private TransformerFactoryImpl(boolean useServicesMechanism) {
 239         this.m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass(useServicesMechanism);
 240         this._useServicesMechanism = useServicesMechanism;


 241         if (System.getSecurityManager() != null) {
 242             _isSecureMode = true;
 243             _isNotSecureProcessing = false;

 244         }




 245     }
 246 
 247     /**
 248      * javax.xml.transform.sax.TransformerFactory implementation.
 249      * Set the error event listener for the TransformerFactory, which is used
 250      * for the processing of transformation instructions, and not for the
 251      * transformation itself.
 252      *
 253      * @param listener The error listener to use with the TransformerFactory
 254      * @throws IllegalArgumentException
 255      */
 256     public void setErrorListener(ErrorListener listener)
 257         throws IllegalArgumentException
 258     {
 259         if (listener == null) {
 260             ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR,
 261                                         "TransformerFactory");
 262             throw new IllegalArgumentException(err.toString());
 263         }
 264         _errorListener = listener;


 284      */
 285     public Object getAttribute(String name)
 286         throws IllegalArgumentException
 287     {
 288         // Return value for attribute 'translet-name'
 289         if (name.equals(TRANSLET_NAME)) {
 290             return _transletName;
 291         }
 292         else if (name.equals(GENERATE_TRANSLET)) {
 293             return new Boolean(_generateTranslet);
 294         }
 295         else if (name.equals(AUTO_TRANSLET)) {
 296             return new Boolean(_autoTranslet);
 297         }
 298         else if (name.equals(ENABLE_INLINING)) {
 299             if (_enableInlining)
 300               return Boolean.TRUE;
 301             else
 302               return Boolean.FALSE;
 303         }






 304 
 305         // Throw an exception for all other attributes
 306         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
 307         throw new IllegalArgumentException(err.toString());
 308     }
 309 
 310     /**
 311      * javax.xml.transform.sax.TransformerFactory implementation.
 312      * Sets the value for a TransformerFactory attribute.
 313      *
 314      * @param name The attribute name
 315      * @param value An object representing the attribute value
 316      * @throws IllegalArgumentException
 317      */
 318     public void setAttribute(String name, Object value)
 319         throws IllegalArgumentException
 320     {
 321         // Set the default translet name (ie. class name), which will be used
 322         // for translets that cannot be given a name from their system-id.
 323         if (name.equals(TRANSLET_NAME) && value instanceof String) {


 384             else if (value instanceof String) {
 385                 _enableInlining = ((String) value).equalsIgnoreCase("true");
 386                 return;
 387             }
 388         }
 389         else if (name.equals(INDENT_NUMBER)) {
 390             if (value instanceof String) {
 391                 try {
 392                     _indentNumber = Integer.parseInt((String) value);
 393                     return;
 394                 }
 395                 catch (NumberFormatException e) {
 396                     // Falls through
 397                 }
 398             }
 399             else if (value instanceof Integer) {
 400                 _indentNumber = ((Integer) value).intValue();
 401                 return;
 402             }
 403         }








 404 
 405         // Throw an exception for all other attributes
 406         final ErrorMsg err
 407             = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
 408         throw new IllegalArgumentException(err.toString());
 409     }
 410 
 411     /**
 412      * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
 413      * or <code>Template</code>s created by this factory.</p>
 414      *
 415      * <p>
 416      * Feature names are fully qualified {@link java.net.URI}s.
 417      * Implementations may define their own features.
 418      * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
 419      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
 420      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
 421      * </p>
 422      *
 423      * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p>


 427      *
 428      * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
 429      *   or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
 430      * @throws NullPointerException If the <code>name</code> parameter is null.
 431      */
 432     public void setFeature(String name, boolean value)
 433         throws TransformerConfigurationException {
 434 
 435         // feature name cannot be null
 436         if (name == null) {
 437             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
 438             throw new NullPointerException(err.toString());
 439         }
 440         // secure processing?
 441         else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 442             if ((_isSecureMode) && (!value)) {
 443                 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SECUREPROCESSING_FEATURE);
 444                 throw new TransformerConfigurationException(err.toString());
 445             }
 446             _isNotSecureProcessing = !value;
 447             // all done processing feature





 448             return;
 449         }
 450         else if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
 451             //in secure mode, let _useServicesMechanism be determined by the constructor
 452             if (!_isSecureMode)
 453                 _useServicesMechanism = value;
 454         }
 455         else {
 456             // unknown feature
 457             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
 458             throw new TransformerConfigurationException(err.toString());
 459         }
 460     }
 461 
 462     /**
 463      * javax.xml.transform.sax.TransformerFactory implementation.
 464      * Look up the value of a feature (to see if it is supported).
 465      * This method must be updated as the various methods and features of this
 466      * class are implemented.
 467      *


 782                             ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
 783                 }
 784 
 785                 // Reset the per-session attributes to their default values
 786                 // after each newTemplates() call.
 787                 resetTransientAttributes();
 788 
 789                 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);
 790             }
 791         }
 792 
 793         // Create and initialize a stylesheet compiler
 794         final XSLTC xsltc = new XSLTC(_useServicesMechanism);
 795         if (_debug) xsltc.setDebug(true);
 796         if (_enableInlining)
 797                 xsltc.setTemplateInlining(true);
 798         else
 799                 xsltc.setTemplateInlining(false);
 800 
 801         if (!_isNotSecureProcessing) xsltc.setSecureProcessing(true);


 802         xsltc.init();
 803 
 804         // Set a document loader (for xsl:include/import) if defined
 805         if (_uriResolver != null) {
 806             xsltc.setSourceLoader(this);
 807         }
 808 
 809         // Pass parameters to the Parser to make sure it locates the correct
 810         // <?xml-stylesheet ...?> PI in an XML input document
 811         if ((_piParams != null) && (_piParams.get(source) != null)) {
 812             // Get the parameters for this Source object
 813             PIParamWrapper p = (PIParamWrapper)_piParams.get(source);
 814             // Pass them on to the compiler (which will pass then to the parser)
 815             if (p != null) {
 816                 xsltc.setPIParameters(p._media, p._title, p._charset);
 817             }
 818         }
 819 
 820         // Set the attributes for translet generation
 821         int outputType = XSLTC.BYTEARRAY_OUTPUT;


 863 
 864         // Reset the per-session attributes to their default values
 865         // after each newTemplates() call.
 866         resetTransientAttributes();
 867 
 868         // Pass compiler warnings to the error listener
 869         if (_errorListener != this) {
 870             try {
 871                 passWarningsToListener(xsltc.getWarnings());
 872             }
 873             catch (TransformerException e) {
 874                 throw new TransformerConfigurationException(e);
 875             }
 876         }
 877         else {
 878             xsltc.printWarnings();
 879         }
 880 
 881         // Check that the transformation went well before returning
 882     if (bytecodes == null) {
 883 
 884         Vector errs = xsltc.getErrors();
 885         ErrorMsg err = null;
 886         if (errs != null) {
 887             err = (ErrorMsg)errs.get(errs.size()-1);
 888         } else {
 889             err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
 890         }
 891         TransformerConfigurationException exc =  new TransformerConfigurationException(err.toString(), err.getCause());






 892 
 893         // Pass compiler errors to the error listener
 894         if (_errorListener != null) {
 895             passErrorsToListener(xsltc.getErrors());
 896 
 897             // As required by TCK 1.2, send a fatalError to the
 898             // error listener because compilation of the stylesheet
 899             // failed and no further processing will be possible.
 900             try {
 901                 _errorListener.fatalError(exc);
 902             } catch (TransformerException te) {
 903                 // well, we tried.
 904             }
 905         }
 906         else {
 907             xsltc.printErrors();
 908         }
 909         throw exc;
 910     }
 911 




 208      */
 209     private Class m_DTMManagerClass;
 210 
 211     /**
 212      * <p>State of secure processing feature.</p>
 213      */
 214     private boolean _isNotSecureProcessing = true;
 215     /**
 216      * <p>State of secure mode.</p>
 217      */
 218     private boolean _isSecureMode = false;
 219 
 220     /**
 221      * Indicates whether implementation parts should use
 222      *   service loader (or similar).
 223      * Note the default value (false) is the safe option..
 224      */
 225     private boolean _useServicesMechanism;
 226 
 227     /**
 228      * protocols allowed for external references set by the stylesheet processing instruction, Import and Include element.
 229      */
 230     private String _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
 231      /**
 232      * protocols allowed for external DTD references in source file and/or stylesheet.
 233      */
 234     private String _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
 235 
 236 
 237     /**
 238      * javax.xml.transform.sax.TransformerFactory implementation.
 239      */
 240     public TransformerFactoryImpl() {
 241         this(true);
 242     }
 243 
 244     public static TransformerFactory newTransformerFactoryNoServiceLoader() {
 245         return new TransformerFactoryImpl(false);
 246     }
 247 
 248     private TransformerFactoryImpl(boolean useServicesMechanism) {
 249         this.m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass(useServicesMechanism);
 250         this._useServicesMechanism = useServicesMechanism;
 251 
 252         String defaultAccess = XalanConstants.EXTERNAL_ACCESS_DEFAULT;
 253         if (System.getSecurityManager() != null) {
 254             _isSecureMode = true;
 255             _isNotSecureProcessing = false;
 256             defaultAccess = XalanConstants.getExternalAccessDefault(true);
 257         }
 258         _accessExternalStylesheet =  SecuritySupport.getDefaultAccessProperty(
 259                 XalanConstants.SP_ACCESS_EXTERNAL_STYLESHEET, defaultAccess);
 260         _accessExternalDTD =  SecuritySupport.getDefaultAccessProperty(
 261                 XalanConstants.SP_ACCESS_EXTERNAL_DTD, defaultAccess);
 262     }
 263 
 264     /**
 265      * javax.xml.transform.sax.TransformerFactory implementation.
 266      * Set the error event listener for the TransformerFactory, which is used
 267      * for the processing of transformation instructions, and not for the
 268      * transformation itself.
 269      *
 270      * @param listener The error listener to use with the TransformerFactory
 271      * @throws IllegalArgumentException
 272      */
 273     public void setErrorListener(ErrorListener listener)
 274         throws IllegalArgumentException
 275     {
 276         if (listener == null) {
 277             ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR,
 278                                         "TransformerFactory");
 279             throw new IllegalArgumentException(err.toString());
 280         }
 281         _errorListener = listener;


 301      */
 302     public Object getAttribute(String name)
 303         throws IllegalArgumentException
 304     {
 305         // Return value for attribute 'translet-name'
 306         if (name.equals(TRANSLET_NAME)) {
 307             return _transletName;
 308         }
 309         else if (name.equals(GENERATE_TRANSLET)) {
 310             return new Boolean(_generateTranslet);
 311         }
 312         else if (name.equals(AUTO_TRANSLET)) {
 313             return new Boolean(_autoTranslet);
 314         }
 315         else if (name.equals(ENABLE_INLINING)) {
 316             if (_enableInlining)
 317               return Boolean.TRUE;
 318             else
 319               return Boolean.FALSE;
 320         }
 321         else if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
 322             return _accessExternalStylesheet;
 323         }
 324         else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) {
 325             return _accessExternalDTD;
 326         }
 327 
 328         // Throw an exception for all other attributes
 329         ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
 330         throw new IllegalArgumentException(err.toString());
 331     }
 332 
 333     /**
 334      * javax.xml.transform.sax.TransformerFactory implementation.
 335      * Sets the value for a TransformerFactory attribute.
 336      *
 337      * @param name The attribute name
 338      * @param value An object representing the attribute value
 339      * @throws IllegalArgumentException
 340      */
 341     public void setAttribute(String name, Object value)
 342         throws IllegalArgumentException
 343     {
 344         // Set the default translet name (ie. class name), which will be used
 345         // for translets that cannot be given a name from their system-id.
 346         if (name.equals(TRANSLET_NAME) && value instanceof String) {


 407             else if (value instanceof String) {
 408                 _enableInlining = ((String) value).equalsIgnoreCase("true");
 409                 return;
 410             }
 411         }
 412         else if (name.equals(INDENT_NUMBER)) {
 413             if (value instanceof String) {
 414                 try {
 415                     _indentNumber = Integer.parseInt((String) value);
 416                     return;
 417                 }
 418                 catch (NumberFormatException e) {
 419                     // Falls through
 420                 }
 421             }
 422             else if (value instanceof Integer) {
 423                 _indentNumber = ((Integer) value).intValue();
 424                 return;
 425             }
 426         }
 427         else if (name.equals(XMLConstants.ACCESS_EXTERNAL_STYLESHEET)) {
 428             _accessExternalStylesheet = (String)value;
 429             return;
 430         }
 431         else if (name.equals(XMLConstants.ACCESS_EXTERNAL_DTD)) {
 432             _accessExternalDTD = (String)value;
 433             return;
 434         }
 435 
 436         // Throw an exception for all other attributes
 437         final ErrorMsg err
 438             = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name);
 439         throw new IllegalArgumentException(err.toString());
 440     }
 441 
 442     /**
 443      * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s
 444      * or <code>Template</code>s created by this factory.</p>
 445      *
 446      * <p>
 447      * Feature names are fully qualified {@link java.net.URI}s.
 448      * Implementations may define their own features.
 449      * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the
 450      * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.
 451      * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.
 452      * </p>
 453      *
 454      * <p>See {@link javax.xml.transform.TransformerFactory} for full documentation of specific features.</p>


 458      *
 459      * @throws TransformerConfigurationException if this <code>TransformerFactory</code>
 460      *   or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.
 461      * @throws NullPointerException If the <code>name</code> parameter is null.
 462      */
 463     public void setFeature(String name, boolean value)
 464         throws TransformerConfigurationException {
 465 
 466         // feature name cannot be null
 467         if (name == null) {
 468             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME);
 469             throw new NullPointerException(err.toString());
 470         }
 471         // secure processing?
 472         else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 473             if ((_isSecureMode) && (!value)) {
 474                 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SECUREPROCESSING_FEATURE);
 475                 throw new TransformerConfigurationException(err.toString());
 476             }
 477             _isNotSecureProcessing = !value;
 478 
 479             // set restriction, allowing no access to external stylesheet
 480             if (value) {
 481                 _accessExternalStylesheet = XalanConstants.EXTERNAL_ACCESS_DEFAULT_FSP;
 482                 _accessExternalDTD = XalanConstants.EXTERNAL_ACCESS_DEFAULT_FSP;                
 483             }
 484             return;
 485         }
 486         else if (name.equals(XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM)) {
 487             //in secure mode, let _useServicesMechanism be determined by the constructor
 488             if (!_isSecureMode)
 489                 _useServicesMechanism = value;
 490         }
 491         else {
 492             // unknown feature
 493             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name);
 494             throw new TransformerConfigurationException(err.toString());
 495         }
 496     }
 497 
 498     /**
 499      * javax.xml.transform.sax.TransformerFactory implementation.
 500      * Look up the value of a feature (to see if it is supported).
 501      * This method must be updated as the various methods and features of this
 502      * class are implemented.
 503      *


 818                             ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
 819                 }
 820 
 821                 // Reset the per-session attributes to their default values
 822                 // after each newTemplates() call.
 823                 resetTransientAttributes();
 824 
 825                 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);
 826             }
 827         }
 828 
 829         // Create and initialize a stylesheet compiler
 830         final XSLTC xsltc = new XSLTC(_useServicesMechanism);
 831         if (_debug) xsltc.setDebug(true);
 832         if (_enableInlining)
 833                 xsltc.setTemplateInlining(true);
 834         else
 835                 xsltc.setTemplateInlining(false);
 836 
 837         if (!_isNotSecureProcessing) xsltc.setSecureProcessing(true);
 838         xsltc.setProperty(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, _accessExternalStylesheet);
 839         xsltc.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, _accessExternalDTD);
 840         xsltc.init();
 841 
 842         // Set a document loader (for xsl:include/import) if defined
 843         if (_uriResolver != null) {
 844             xsltc.setSourceLoader(this);
 845         }
 846 
 847         // Pass parameters to the Parser to make sure it locates the correct
 848         // <?xml-stylesheet ...?> PI in an XML input document
 849         if ((_piParams != null) && (_piParams.get(source) != null)) {
 850             // Get the parameters for this Source object
 851             PIParamWrapper p = (PIParamWrapper)_piParams.get(source);
 852             // Pass them on to the compiler (which will pass then to the parser)
 853             if (p != null) {
 854                 xsltc.setPIParameters(p._media, p._title, p._charset);
 855             }
 856         }
 857 
 858         // Set the attributes for translet generation
 859         int outputType = XSLTC.BYTEARRAY_OUTPUT;


 901 
 902         // Reset the per-session attributes to their default values
 903         // after each newTemplates() call.
 904         resetTransientAttributes();
 905 
 906         // Pass compiler warnings to the error listener
 907         if (_errorListener != this) {
 908             try {
 909                 passWarningsToListener(xsltc.getWarnings());
 910             }
 911             catch (TransformerException e) {
 912                 throw new TransformerConfigurationException(e);
 913             }
 914         }
 915         else {
 916             xsltc.printWarnings();
 917         }
 918 
 919         // Check that the transformation went well before returning
 920     if (bytecodes == null) {

 921         Vector errs = xsltc.getErrors();
 922         ErrorMsg err = null;
 923         if (errs != null) {
 924             err = (ErrorMsg)errs.elementAt(errs.size()-1);
 925         } else {
 926             err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
 927         }
 928         Throwable cause = err.getCause();
 929         TransformerConfigurationException exc;
 930         if (cause != null) {
 931             exc =  new TransformerConfigurationException(cause.getMessage(), cause);
 932         } else {
 933             exc =  new TransformerConfigurationException(err.toString());
 934         }
 935 
 936         // Pass compiler errors to the error listener
 937         if (_errorListener != null) {
 938             passErrorsToListener(xsltc.getErrors());
 939 
 940             // As required by TCK 1.2, send a fatalError to the
 941             // error listener because compilation of the stylesheet
 942             // failed and no further processing will be possible.
 943             try {
 944                 _errorListener.fatalError(exc);
 945             } catch (TransformerException te) {
 946                 // well, we tried.
 947             }
 948         }
 949         else {
 950             xsltc.printErrors();
 951         }
 952         throw exc;
 953     }
 954