- 0001

- 0002

- 0003

- 0004

- 0005

- 0006

- 0007

- 0008

- 0009

- 0010

- 0011

- 0012

- 0013

- 0014

- 0015

- 0016

- 0017

- 0018

- 0019

- 0020
package com.sun.org.apache.xalan.internal.xsltc.trax; - 0021

- 0022
import java.io.File; - 0023
import java.io.FileInputStream; - 0024
import java.io.FileNotFoundException; - 0025
import java.io.FilenameFilter; - 0026
import java.io.IOException; - 0027
import java.io.InputStream; - 0028
import java.net.MalformedURLException; - 0029
import java.net.URL; - 0030
import java.util.Enumeration; - 0031
import java.util.Hashtable; - 0032
import java.util.Properties; - 0033
import java.util.Vector; - 0034
import java.util.zip.ZipEntry; - 0035
import java.util.zip.ZipFile; - 0036

- 0037
import javax.xml.XMLConstants; - 0038
import javax.xml.parsers.SAXParserFactory; - 0039
import javax.xml.parsers.SAXParser; - 0040
import javax.xml.parsers.ParserConfigurationException; - 0041

- 0042
import javax.xml.transform.ErrorListener; - 0043
import javax.xml.transform.Source; - 0044
import javax.xml.transform.Templates; - 0045
import javax.xml.transform.Transformer; - 0046
import javax.xml.transform.TransformerConfigurationException; - 0047
import javax.xml.transform.TransformerException; - 0048
import javax.xml.transform.URIResolver; - 0049
import javax.xml.transform.dom.DOMResult; - 0050
import javax.xml.transform.dom.DOMSource; - 0051
import javax.xml.transform.sax.SAXResult; - 0052
import javax.xml.transform.sax.SAXSource; - 0053
import javax.xml.transform.sax.SAXTransformerFactory; - 0054
import javax.xml.transform.sax.TemplatesHandler; - 0055
import javax.xml.transform.sax.TransformerHandler; - 0056
import javax.xml.transform.stream.StreamResult; - 0057
import javax.xml.transform.stream.StreamSource; - 0058

- 0059
import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler; - 0060
import com.sun.org.apache.xml.internal.utils.StopParseException; - 0061

- 0062
import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader; - 0063
import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC; - 0064
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; - 0065
import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager; - 0066

- 0067

- 0068
import org.xml.sax.InputSource; - 0069
import org.xml.sax.XMLFilter; - 0070
import org.xml.sax.XMLReader; - 0071
import org.xml.sax.helpers.XMLReaderFactory; - 0072

- 0073

- 0074

- 0075

- 0076

- 0077

- 0078

- 0079
public class TransformerFactoryImpl - 0080
extends SAXTransformerFactory implements SourceLoader, ErrorListener - 0081
{ - 0082
- 0083
public final static String TRANSLET_NAME = "translet-name"; - 0084
public final static String DESTINATION_DIRECTORY = "destination-directory"; - 0085
public final static String PACKAGE_NAME = "package-name"; - 0086
public final static String JAR_NAME = "jar-name"; - 0087
public final static String GENERATE_TRANSLET = "generate-translet"; - 0088
public final static String AUTO_TRANSLET = "auto-translet"; - 0089
public final static String USE_CLASSPATH = "use-classpath"; - 0090
public final static String DEBUG = "debug"; - 0091
public final static String ENABLE_INLINING = "enable-inlining"; - 0092
public final static String INDENT_NUMBER = "indent-number"; - 0093
- 0094
- 0095

- 0096

- 0097

- 0098
private ErrorListener _errorListener = this; - 0099

- 0100
- 0101

- 0102

- 0103
private URIResolver _uriResolver = null; - 0104

- 0105
- 0106

- 0107

- 0108

- 0109

- 0110

- 0111

- 0112

- 0113

- 0114

- 0115
protected final static String DEFAULT_TRANSLET_NAME = "GregorSamsa"; - 0116
- 0117
- 0118

- 0119

- 0120
private String _transletName = DEFAULT_TRANSLET_NAME; - 0121
- 0122
- 0123

- 0124

- 0125
private String _destinationDirectory = null; - 0126
- 0127
- 0128

- 0129

- 0130
private String _packageName = null; - 0131
- 0132
- 0133

- 0134

- 0135
private String _jarFileName = null; - 0136

- 0137
- 0138

- 0139

- 0140

- 0141
private Hashtable _piParams = null; - 0142

- 0143
- 0144

- 0145

- 0146
private static class PIParamWrapper { - 0147
public String _media = null; - 0148
public String _title = null; - 0149
public String _charset = null; - 0150
- 0151
public PIParamWrapper(String media, String title, String charset) { - 0152
_media = media; - 0153
_title = title; - 0154
_charset = charset; - 0155
} - 0156
} - 0157

- 0158
- 0159

- 0160

- 0161
private boolean _debug = false; - 0162

- 0163
- 0164

- 0165

- 0166
private boolean _enableInlining = false; - 0167
- 0168
- 0169

- 0170

- 0171

- 0172
private boolean _generateTranslet = false; - 0173
- 0174
- 0175

- 0176

- 0177

- 0178

- 0179

- 0180
private boolean _autoTranslet = false; - 0181
- 0182
- 0183

- 0184

- 0185

- 0186
private boolean _useClasspath = false; - 0187

- 0188
- 0189

- 0190

- 0191
private int _indentNumber = -1; - 0192

- 0193
- 0194

- 0195

- 0196

- 0197

- 0198

- 0199
private Class m_DTMManagerClass; - 0200

- 0201
- 0202

- 0203

- 0204
private boolean _isSecureProcessing = false; - 0205

- 0206
- 0207

- 0208

- 0209
public TransformerFactoryImpl() { - 0210
m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass(); - 0211
} - 0212

- 0213
- 0214

- 0215

- 0216

- 0217

- 0218

- 0219

- 0220

- 0221

- 0222
public void setErrorListener(ErrorListener listener) - 0223
throws IllegalArgumentException - 0224
{ - 0225
if (listener == null) { - 0226
ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR, - 0227
"TransformerFactory"); - 0228
throw new IllegalArgumentException(err.toString()); - 0229
} - 0230
_errorListener = listener; - 0231
} - 0232

- 0233
- 0234

- 0235

- 0236

- 0237

- 0238

- 0239
public ErrorListener getErrorListener() { - 0240
return _errorListener; - 0241
} - 0242

- 0243
- 0244

- 0245

- 0246

- 0247

- 0248

- 0249

- 0250

- 0251
public Object getAttribute(String name) - 0252
throws IllegalArgumentException - 0253
{ - 0254
- 0255
if (name.equals(TRANSLET_NAME)) { - 0256
return _transletName; - 0257
} - 0258
else if (name.equals(GENERATE_TRANSLET)) { - 0259
return new Boolean(_generateTranslet); - 0260
} - 0261
else if (name.equals(AUTO_TRANSLET)) { - 0262
return new Boolean(_autoTranslet); - 0263
} - 0264

- 0265
- 0266
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); - 0267
throw new IllegalArgumentException(err.toString()); - 0268
} - 0269

- 0270
- 0271

- 0272

- 0273

- 0274

- 0275

- 0276

- 0277

- 0278
public void setAttribute(String name, Object value) - 0279
throws IllegalArgumentException - 0280
{ - 0281
- 0282
- 0283
if (name.equals(TRANSLET_NAME) && value instanceof String) { - 0284
_transletName = (String) value; - 0285
return; - 0286
} - 0287
else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String) { - 0288
_destinationDirectory = (String) value; - 0289
return; - 0290
} - 0291
else if (name.equals(PACKAGE_NAME) && value instanceof String) { - 0292
_packageName = (String) value; - 0293
return; - 0294
} - 0295
else if (name.equals(JAR_NAME) && value instanceof String) { - 0296
_jarFileName = (String) value; - 0297
return; - 0298
} - 0299
else if (name.equals(GENERATE_TRANSLET)) { - 0300
if (value instanceof Boolean) { - 0301
_generateTranslet = ((Boolean) value).booleanValue(); - 0302
return; - 0303
} - 0304
else if (value instanceof String) { - 0305
_generateTranslet = ((String) value).equalsIgnoreCase("true"); - 0306
return; - 0307
} - 0308
} - 0309
else if (name.equals(AUTO_TRANSLET)) { - 0310
if (value instanceof Boolean) { - 0311
_autoTranslet = ((Boolean) value).booleanValue(); - 0312
return; - 0313
} - 0314
else if (value instanceof String) { - 0315
_autoTranslet = ((String) value).equalsIgnoreCase("true"); - 0316
return; - 0317
} - 0318
} - 0319
else if (name.equals(USE_CLASSPATH)) { - 0320
if (value instanceof Boolean) { - 0321
_useClasspath = ((Boolean) value).booleanValue(); - 0322
return; - 0323
} - 0324
else if (value instanceof String) { - 0325
_useClasspath = ((String) value).equalsIgnoreCase("true"); - 0326
return; - 0327
} - 0328
} - 0329
else if (name.equals(DEBUG)) { - 0330
if (value instanceof Boolean) { - 0331
_debug = ((Boolean) value).booleanValue(); - 0332
return; - 0333
} - 0334
else if (value instanceof String) { - 0335
_debug = ((String) value).equalsIgnoreCase("true"); - 0336
return; - 0337
} - 0338
} - 0339
else if (name.equals(ENABLE_INLINING)) { - 0340
if (value instanceof Boolean) { - 0341
_enableInlining = ((Boolean) value).booleanValue(); - 0342
return; - 0343
} - 0344
else if (value instanceof String) { - 0345
_enableInlining = ((String) value).equalsIgnoreCase("true"); - 0346
return; - 0347
} - 0348
} - 0349
else if (name.equals(INDENT_NUMBER)) { - 0350
if (value instanceof String) { - 0351
try { - 0352
_indentNumber = Integer.parseInt((String) value); - 0353
return; - 0354
} - 0355
catch (NumberFormatException e) { - 0356
- 0357
} - 0358
} - 0359
else if (value instanceof Integer) { - 0360
_indentNumber = ((Integer) value).intValue(); - 0361
return; - 0362
} - 0363
} - 0364

- 0365
- 0366
final ErrorMsg err - 0367
= new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); - 0368
throw new IllegalArgumentException(err.toString()); - 0369
} - 0370

- 0371
- 0372

- 0373

- 0374

- 0375

- 0376

- 0377

- 0378

- 0379

- 0380

- 0381

- 0382

- 0383

- 0384

- 0385

- 0386

- 0387

- 0388

- 0389

- 0390

- 0391

- 0392
public void setFeature(String name, boolean value) - 0393
throws TransformerConfigurationException { - 0394

- 0395
- 0396
if (name == null) { - 0397
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME); - 0398
throw new NullPointerException(err.toString()); - 0399
} - 0400
- 0401
else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { - 0402
_isSecureProcessing = value; - 0403
- 0404
return; - 0405
} - 0406
else { - 0407
- 0408
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name); - 0409
throw new TransformerConfigurationException(err.toString()); - 0410
} - 0411
} - 0412

- 0413
- 0414

- 0415

- 0416

- 0417

- 0418

- 0419

- 0420

- 0421

- 0422
public boolean getFeature(String name) { - 0423
- 0424
String[] features = { - 0425
DOMSource.FEATURE, - 0426
DOMResult.FEATURE, - 0427
SAXSource.FEATURE, - 0428
SAXResult.FEATURE, - 0429
StreamSource.FEATURE, - 0430
StreamResult.FEATURE, - 0431
SAXTransformerFactory.FEATURE, - 0432
SAXTransformerFactory.FEATURE_XMLFILTER - 0433
}; - 0434

- 0435
- 0436
if (name == null) { - 0437
ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME); - 0438
throw new NullPointerException(err.toString()); - 0439
} - 0440

- 0441
- 0442
for (int i =0; i < features.length; i++) { - 0443
if (name.equals(features[i])) { - 0444
return true; - 0445
} - 0446
} - 0447
- 0448
if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { - 0449
return _isSecureProcessing; - 0450
} - 0451

- 0452
- 0453
return false; - 0454
} - 0455

- 0456
- 0457

- 0458

- 0459

- 0460

- 0461

- 0462

- 0463
- 0464
public URIResolver getURIResolver() { - 0465
return _uriResolver; - 0466
} - 0467

- 0468
- 0469

- 0470

- 0471

- 0472

- 0473

- 0474

- 0475

- 0476

- 0477
- 0478
public void setURIResolver(URIResolver resolver) { - 0479
_uriResolver = resolver; - 0480
} - 0481

- 0482
- 0483

- 0484

- 0485

- 0486

- 0487

- 0488

- 0489

- 0490

- 0491

- 0492

- 0493

- 0494

- 0495

- 0496

- 0497
public Source getAssociatedStylesheet(Source source, String media, - 0498
String title, String charset) - 0499
throws TransformerConfigurationException { - 0500

- 0501
String baseId; - 0502
XMLReader reader = null; - 0503
InputSource isource = null; - 0504

- 0505

- 0506
- 0507

- 0508

- 0509
StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset); - 0510

- 0511
try { - 0512
- 0513
if (source instanceof DOMSource ) { - 0514
final DOMSource domsrc = (DOMSource) source; - 0515
baseId = domsrc.getSystemId(); - 0516
final org.w3c.dom.Node node = domsrc.getNode(); - 0517
final DOM2SAX dom2sax = new DOM2SAX(node); - 0518

- 0519
_stylesheetPIHandler.setBaseId(baseId); - 0520

- 0521
dom2sax.setContentHandler( _stylesheetPIHandler); - 0522
dom2sax.parse(); - 0523
} else { - 0524
isource = SAXSource.sourceToInputSource(source); - 0525
baseId = isource.getSystemId(); - 0526

- 0527
SAXParserFactory factory = SAXParserFactory.newInstance(); - 0528
factory.setNamespaceAware(true); - 0529
- 0530
if (_isSecureProcessing) { - 0531
try { - 0532
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - 0533
} - 0534
catch (org.xml.sax.SAXException e) {} - 0535
} - 0536
- 0537
SAXParser jaxpParser = factory.newSAXParser(); - 0538

- 0539
reader = jaxpParser.getXMLReader(); - 0540
if (reader == null) { - 0541
reader = XMLReaderFactory.createXMLReader(); - 0542
} - 0543

- 0544
_stylesheetPIHandler.setBaseId(baseId); - 0545
reader.setContentHandler(_stylesheetPIHandler); - 0546
reader.parse(isource); - 0547

- 0548
} - 0549

- 0550
if (_uriResolver != null ) { - 0551
_stylesheetPIHandler.setURIResolver(_uriResolver); - 0552
} - 0553

- 0554
} catch (StopParseException e ) { - 0555
- 0556

- 0557
} catch (javax.xml.parsers.ParserConfigurationException e) { - 0558

- 0559
throw new TransformerConfigurationException( - 0560
"getAssociatedStylesheets failed", e); - 0561

- 0562
} catch (org.xml.sax.SAXException se) { - 0563

- 0564
throw new TransformerConfigurationException( - 0565
"getAssociatedStylesheets failed", se); - 0566

- 0567

- 0568
} catch (IOException ioe ) { - 0569
throw new TransformerConfigurationException( - 0570
"getAssociatedStylesheets failed", ioe); - 0571

- 0572
} - 0573

- 0574
return _stylesheetPIHandler.getAssociatedStylesheet(); - 0575

- 0576
} - 0577

- 0578
- 0579

- 0580

- 0581

- 0582

- 0583

- 0584
- 0585
public Transformer newTransformer() - 0586
throws TransformerConfigurationException - 0587
{ - 0588
TransformerImpl result = new TransformerImpl(new Properties(), - 0589
_indentNumber, this); - 0590
if (_uriResolver != null) { - 0591
result.setURIResolver(_uriResolver); - 0592
} - 0593
- 0594
if (_isSecureProcessing) { - 0595
result.setSecureProcessing(true); - 0596
} - 0597
return result; - 0598
} - 0599

- 0600
- 0601

- 0602

- 0603

- 0604

- 0605

- 0606

- 0607

- 0608

- 0609

- 0610
public Transformer newTransformer(Source source) throws - 0611
TransformerConfigurationException - 0612
{ - 0613
final Templates templates = newTemplates(source); - 0614
final Transformer transformer = templates.newTransformer(); - 0615
if (_uriResolver != null) { - 0616
transformer.setURIResolver(_uriResolver); - 0617
} - 0618
return(transformer); - 0619
} - 0620

- 0621
- 0622

- 0623

- 0624
private void passWarningsToListener(Vector messages) - 0625
throws TransformerException - 0626
{ - 0627
if (_errorListener == null || messages == null) { - 0628
return; - 0629
} - 0630
- 0631
final int count = messages.size(); - 0632
for (int pos = 0; pos < count; pos++) { - 0633
ErrorMsg msg = (ErrorMsg)messages.elementAt(pos); - 0634
- 0635
if (msg.isWarningError()) - 0636
_errorListener.error( - 0637
new TransformerConfigurationException(msg.toString())); - 0638
else - 0639
_errorListener.warning( - 0640
new TransformerConfigurationException(msg.toString())); - 0641
} - 0642
} - 0643

- 0644
- 0645

- 0646

- 0647
private void passErrorsToListener(Vector messages) { - 0648
try { - 0649
if (_errorListener == null || messages == null) { - 0650
return; - 0651
} - 0652
- 0653
final int count = messages.size(); - 0654
for (int pos = 0; pos < count; pos++) { - 0655
String message = messages.elementAt(pos).toString(); - 0656
_errorListener.error(new TransformerException(message)); - 0657
} - 0658
} - 0659
catch (TransformerException e) { - 0660
- 0661
} - 0662
} - 0663

- 0664
- 0665

- 0666

- 0667

- 0668

- 0669

- 0670

- 0671

- 0672

- 0673
public Templates newTemplates(Source source) - 0674
throws TransformerConfigurationException - 0675
{ - 0676
- 0677
- 0678
- 0679
if (_useClasspath) { - 0680
String transletName = getTransletBaseName(source); - 0681
- 0682
if (_packageName != null) - 0683
transletName = _packageName + "." + transletName; - 0684
- 0685
try { - 0686
final Class clazz = ObjectFactory.findProviderClass( - 0687
transletName, ObjectFactory.findClassLoader(), true); - 0688
resetTransientAttributes(); - 0689
- 0690
return new TemplatesImpl(new Class[]{clazz}, transletName, null, _indentNumber, this); - 0691
} - 0692
catch (ClassNotFoundException cnfe) { - 0693
ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName); - 0694
throw new TransformerConfigurationException(err.toString()); - 0695
} - 0696
catch (Exception e) { - 0697
ErrorMsg err = new ErrorMsg( - 0698
new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY) - 0699
+ e.getMessage()); - 0700
throw new TransformerConfigurationException(err.toString()); - 0701
} - 0702
} - 0703
- 0704
- 0705
- 0706
if (_autoTranslet) { - 0707
byte[][] bytecodes = null; - 0708
String transletClassName = getTransletBaseName(source); - 0709
- 0710
if (_packageName != null) - 0711
transletClassName = _packageName + "." + transletClassName; - 0712
- 0713
if (_jarFileName != null) - 0714
bytecodes = getBytecodesFromJar(source, transletClassName); - 0715
else - 0716
bytecodes = getBytecodesFromClasses(source, transletClassName); - 0717
- 0718
if (bytecodes != null) { - 0719
if (_debug) { - 0720
if (_jarFileName != null) - 0721
System.err.println(new ErrorMsg( - 0722
ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName)); - 0723
else - 0724
System.err.println(new ErrorMsg( - 0725
ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName)); - 0726
} - 0727

- 0728
- 0729
- 0730
resetTransientAttributes(); - 0731
- 0732
return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this); - 0733
} - 0734
} - 0735
- 0736
- 0737
final XSLTC xsltc = new XSLTC(); - 0738
if (_debug) xsltc.setDebug(true); - 0739
if (_enableInlining) xsltc.setTemplateInlining(true); - 0740
if (_isSecureProcessing) xsltc.setSecureProcessing(true); - 0741
xsltc.init(); - 0742

- 0743
- 0744
if (_uriResolver != null) { - 0745
xsltc.setSourceLoader(this); - 0746
} - 0747

- 0748
- 0749
- 0750
if ((_piParams != null) && (_piParams.get(source) != null)) { - 0751
- 0752
PIParamWrapper p = (PIParamWrapper)_piParams.get(source); - 0753
- 0754
if (p != null) { - 0755
xsltc.setPIParameters(p._media, p._title, p._charset); - 0756
} - 0757
} - 0758

- 0759
- 0760
int outputType = XSLTC.BYTEARRAY_OUTPUT; - 0761
if (_generateTranslet || _autoTranslet) { - 0762
- 0763
xsltc.setClassName(getTransletBaseName(source)); - 0764
- 0765
if (_destinationDirectory != null) - 0766
xsltc.setDestDirectory(_destinationDirectory); - 0767
else { - 0768
String xslName = getStylesheetFileName(source); - 0769
if (xslName != null) { - 0770
File xslFile = new File(xslName); - 0771
String xslDir = xslFile.getParent(); - 0772
- 0773
if (xslDir != null) - 0774
xsltc.setDestDirectory(xslDir); - 0775
} - 0776
} - 0777
- 0778
if (_packageName != null) - 0779
xsltc.setPackageName(_packageName); - 0780
- 0781
if (_jarFileName != null) { - 0782
xsltc.setJarFileName(_jarFileName); - 0783
outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT; - 0784
} - 0785
else - 0786
outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT; - 0787
} - 0788

- 0789
- 0790
final InputSource input = Util.getInputSource(xsltc, source); - 0791
byte[][] bytecodes = xsltc.compile(null, input, outputType); - 0792
final String transletName = xsltc.getClassName(); - 0793

- 0794
- 0795
if ((_generateTranslet || _autoTranslet) - 0796
&& bytecodes != null && _jarFileName != null) { - 0797
try { - 0798
xsltc.outputToJar(); - 0799
} - 0800
catch (java.io.IOException e) { } - 0801
} - 0802

- 0803
- 0804
- 0805
resetTransientAttributes(); - 0806
