1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.trax; 21 22 import java.io.IOException ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.io.Serializable ; 26 import java.util.Properties ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedAction ; 29 import javax.xml.XMLConstants ; 30 31 import javax.xml.transform.Templates ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerConfigurationException ; 34 import javax.xml.transform.URIResolver ; 35 36 import com.sun.org.apache.xalan.internal.xsltc.DOM; 37 import com.sun.org.apache.xalan.internal.xsltc.Translet; 38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 39 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 40 import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable; 41 42 48 public final class TemplatesImpl implements Templates , Serializable { 49 50 55 private static String ABSTRACT_TRANSLET 56 = "com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet"; 57 58 61 private String _name = null; 62 63 67 private byte[][] _bytecodes = null; 68 69 73 private Class [] _class = null; 74 75 79 private int _transletIndex = -1; 80 81 84 private Hashtable _auxClasses = null; 85 86 89 private Properties _outputProperties; 90 91 94 private int _indentNumber; 95 96 100 private transient URIResolver _uriResolver = null; 101 102 109 private transient ThreadLocal _sdom = new ThreadLocal (); 110 111 115 private transient TransformerFactoryImpl _tfactory = null; 116 117 static final class TransletClassLoader extends ClassLoader { 118 TransletClassLoader(ClassLoader parent) { 119 super(parent); 120 } 121 122 125 Class defineClass(final byte[] b) { 126 return defineClass(null, b, 0, b.length); 127 } 128 } 129 130 131 136 protected TemplatesImpl(byte[][] bytecodes, String transletName, 137 Properties outputProperties, int indentNumber, 138 TransformerFactoryImpl tfactory) 139 { 140 _bytecodes = bytecodes; 141 _name = transletName; 142 _outputProperties = outputProperties; 143 _indentNumber = indentNumber; 144 _tfactory = tfactory; 145 } 146 147 150 protected TemplatesImpl(Class [] transletClasses, String transletName, 151 Properties outputProperties, int indentNumber, 152 TransformerFactoryImpl tfactory) 153 { 154 _class = transletClasses; 155 _name = transletName; 156 _transletIndex = 0; 157 _outputProperties = outputProperties; 158 _indentNumber = indentNumber; 159 _tfactory = tfactory; 160 } 161 162 163 166 public TemplatesImpl() { } 167 168 177 private void readObject(ObjectInputStream is) 178 throws IOException , ClassNotFoundException 179 { 180 is.defaultReadObject(); 181 if (is.readBoolean()) { 182 _uriResolver = (URIResolver ) is.readObject(); 183 } 184 185 _tfactory = new TransformerFactoryImpl(); 186 } 187 188 189 194 private void writeObject(ObjectOutputStream os) 195 throws IOException , ClassNotFoundException { 196 os.defaultWriteObject(); 197 if (_uriResolver instanceof Serializable ) { 198 os.writeBoolean(true); 199 os.writeObject((Serializable ) _uriResolver); 200 } 201 else { 202 os.writeBoolean(false); 203 } 204 } 205 206 207 210 public synchronized void setURIResolver(URIResolver resolver) { 211 _uriResolver = resolver; 212 } 213 214 223 private synchronized void setTransletBytecodes(byte[][] bytecodes) { 224 _bytecodes = bytecodes; 225 } 226 227 235 private synchronized byte[][] getTransletBytecodes() { 236 return _bytecodes; 237 } 238 239 247 private synchronized Class [] getTransletClasses() { 248 try { 249 if (_class == null) defineTransletClasses(); 250 } 251 catch (TransformerConfigurationException e) { 252 } 254 return _class; 255 } 256 257 260 public synchronized int getTransletIndex() { 261 try { 262 if (_class == null) defineTransletClasses(); 263 } 264 catch (TransformerConfigurationException e) { 265 } 267 return _transletIndex; 268 } 269 270 273 protected synchronized void setTransletName(String name) { 274 _name = name; 275 } 276 277 280 protected synchronized String getTransletName() { 281 return _name; 282 } 283 284 288 private void defineTransletClasses() 289 throws TransformerConfigurationException { 290 291 if (_bytecodes == null) { 292 ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR); 293 throw new TransformerConfigurationException (err.toString()); 294 } 295 296 TransletClassLoader loader = (TransletClassLoader) 297 AccessController.doPrivileged(new PrivilegedAction () { 298 public Object run() { 299 return new TransletClassLoader(ObjectFactory.findClassLoader()); 300 } 301 }); 302 303 try { 304 final int classCount = _bytecodes.length; 305 _class = new Class [classCount]; 306 307 if (classCount > 1) { 308 _auxClasses = new Hashtable(); 309 } 310 311 for (int i = 0; i < classCount; i++) { 312 _class[i] = loader.defineClass(_bytecodes[i]); 313 final Class superClass = _class[i].getSuperclass(); 314 315 if (superClass.getName().equals(ABSTRACT_TRANSLET)) { 317 _transletIndex = i; 318 } 319 else { 320 _auxClasses.put(_class[i].getName(), _class[i]); 321 } 322 } 323 324 if (_transletIndex < 0) { 325 ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name); 326 throw new TransformerConfigurationException (err.toString()); 327 } 328 } 329 catch (ClassFormatError e) { 330 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name); 331 throw new TransformerConfigurationException (err.toString()); 332 } 333 catch (LinkageError e) { 334 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); 335 throw new TransformerConfigurationException (err.toString()); 336 } 337 } 338 339 344 private Translet getTransletInstance() 345 throws TransformerConfigurationException { 346 try { 347 if (_name == null) return null; 348 349 if (_class == null) defineTransletClasses(); 350 351 AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance(); 354 translet.postInitialization(); 355 translet.setTemplates(this); 356 if (_auxClasses != null) { 357 translet.setAuxiliaryClasses(_auxClasses); 358 } 359 360 return translet; 361 } 362 catch (InstantiationException e) { 363 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); 364 throw new TransformerConfigurationException (err.toString()); 365 } 366 catch (IllegalAccessException e) { 367 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); 368 throw new TransformerConfigurationException (err.toString()); 369 } 370 } 371 372 377 public synchronized Transformer newTransformer() 378 throws TransformerConfigurationException 379 { 380 TransformerImpl transformer; 381 382 transformer = new TransformerImpl(getTransletInstance(), _outputProperties, 383 _indentNumber, _tfactory); 384 385 if (_uriResolver != null) { 386 transformer.setURIResolver(_uriResolver); 387 } 388 389 if (_tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) { 390 transformer.setSecureProcessing(true); 391 } 392 393 return transformer; 394 } 395 396 402 public synchronized Properties getOutputProperties() { 403 try { 404 return newTransformer().getOutputProperties(); 405 } 406 catch (TransformerConfigurationException e) { 407 return null; 408 } 409 } 410 411 414 public DOM getStylesheetDOM() { 415 return (DOM)_sdom.get(); 416 } 417 418 421 public void setStylesheetDOM(DOM sdom) { 422 _sdom.set(sdom); 423 } 424 } 425 | Popular Tags |