1 16 19 20 package org.apache.xalan.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 30 import javax.xml.transform.Templates ; 31 import javax.xml.transform.Transformer ; 32 import javax.xml.transform.TransformerConfigurationException ; 33 import javax.xml.transform.URIResolver ; 34 35 import org.apache.xalan.xsltc.DOM; 36 import org.apache.xalan.xsltc.Translet; 37 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 38 import org.apache.xalan.xsltc.runtime.AbstractTranslet; 39 import org.apache.xalan.xsltc.runtime.Hashtable; 40 41 47 public final class TemplatesImpl implements Templates , Serializable { 48 49 54 private static String ABSTRACT_TRANSLET 55 = "org.apache.xalan.xsltc.runtime.AbstractTranslet"; 56 57 60 private String _name = null; 61 62 66 private byte[][] _bytecodes = null; 67 68 72 private Class [] _class = null; 73 74 78 private int _transletIndex = -1; 79 80 83 private Hashtable _auxClasses = null; 84 85 88 private Properties _outputProperties; 89 90 93 private int _indentNumber; 94 95 99 private transient URIResolver _uriResolver = null; 100 101 108 private transient ThreadLocal _sdom = new ThreadLocal (); 109 110 114 private transient TransformerFactoryImpl _tfactory = null; 115 116 static final class TransletClassLoader extends ClassLoader { 117 TransletClassLoader(ClassLoader parent) { 118 super(parent); 119 } 120 121 124 Class defineClass(final byte[] b) { 125 return defineClass(null, b, 0, b.length); 126 } 127 } 128 129 130 135 protected TemplatesImpl(byte[][] bytecodes, String transletName, 136 Properties outputProperties, int indentNumber, 137 TransformerFactoryImpl tfactory) 138 { 139 _bytecodes = bytecodes; 140 _name = transletName; 141 _outputProperties = outputProperties; 142 _indentNumber = indentNumber; 143 _tfactory = tfactory; 144 } 145 146 149 protected TemplatesImpl(Class [] transletClasses, String transletName, 150 Properties outputProperties, int indentNumber, 151 TransformerFactoryImpl tfactory) 152 { 153 _class = transletClasses; 154 _name = transletName; 155 _transletIndex = 0; 156 _outputProperties = outputProperties; 157 _indentNumber = indentNumber; 158 _tfactory = tfactory; 159 } 160 161 162 165 public TemplatesImpl() { } 166 167 176 private void readObject(ObjectInputStream is) 177 throws IOException , ClassNotFoundException 178 { 179 is.defaultReadObject(); 180 if (is.readBoolean()) { 181 _uriResolver = (URIResolver ) is.readObject(); 182 } 183 184 _tfactory = new TransformerFactoryImpl(); 185 } 186 187 188 193 private void writeObject(ObjectOutputStream os) 194 throws IOException , ClassNotFoundException { 195 os.defaultWriteObject(); 196 if (_uriResolver instanceof Serializable ) { 197 os.writeBoolean(true); 198 os.writeObject((Serializable ) _uriResolver); 199 } 200 else { 201 os.writeBoolean(false); 202 } 203 } 204 205 206 209 public synchronized void setURIResolver(URIResolver resolver) { 210 _uriResolver = resolver; 211 } 212 213 217 protected synchronized void setTransletBytecodes(byte[][] bytecodes) { 218 _bytecodes = bytecodes; 219 } 220 221 224 public synchronized byte[][] getTransletBytecodes() { 225 return _bytecodes; 226 } 227 228 231 public synchronized Class [] getTransletClasses() { 232 try { 233 if (_class == null) defineTransletClasses(); 234 } 235 catch (TransformerConfigurationException e) { 236 } 238 return _class; 239 } 240 241 244 public synchronized int getTransletIndex() { 245 try { 246 if (_class == null) defineTransletClasses(); 247 } 248 catch (TransformerConfigurationException e) { 249 } 251 return _transletIndex; 252 } 253 254 257 protected synchronized void setTransletName(String name) { 258 _name = name; 259 } 260 261 264 protected synchronized String getTransletName() { 265 return _name; 266 } 267 268 272 private void defineTransletClasses() 273 throws TransformerConfigurationException { 274 275 if (_bytecodes == null) { 276 ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR); 277 throw new TransformerConfigurationException (err.toString()); 278 } 279 280 TransletClassLoader loader = (TransletClassLoader) 281 AccessController.doPrivileged(new PrivilegedAction () { 282 public Object run() { 283 return new TransletClassLoader(ObjectFactory.findClassLoader()); 284 } 285 }); 286 287 try { 288 final int classCount = _bytecodes.length; 289 _class = new Class [classCount]; 290 291 if (classCount > 1) { 292 _auxClasses = new Hashtable(); 293 } 294 295 for (int i = 0; i < classCount; i++) { 296 _class[i] = loader.defineClass(_bytecodes[i]); 297 final Class superClass = _class[i].getSuperclass(); 298 299 if (superClass.getName().equals(ABSTRACT_TRANSLET)) { 301 _transletIndex = i; 302 } 303 else { 304 _auxClasses.put(_class[i].getName(), _class[i]); 305 } 306 } 307 308 if (_transletIndex < 0) { 309 ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name); 310 throw new TransformerConfigurationException (err.toString()); 311 } 312 } 313 catch (ClassFormatError e) { 314 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name); 315 throw new TransformerConfigurationException (err.toString()); 316 } 317 catch (LinkageError e) { 318 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); 319 throw new TransformerConfigurationException (err.toString()); 320 } 321 } 322 323 328 private Translet getTransletInstance() 329 throws TransformerConfigurationException { 330 try { 331 if (_name == null) return null; 332 333 if (_class == null) defineTransletClasses(); 334 335 AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance(); 338 translet.postInitialization(); 339 translet.setTemplates(this); 340 if (_auxClasses != null) { 341 translet.setAuxiliaryClasses(_auxClasses); 342 } 343 344 return translet; 345 } 346 catch (InstantiationException e) { 347 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); 348 throw new TransformerConfigurationException (err.toString()); 349 } 350 catch (IllegalAccessException e) { 351 ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name); 352 throw new TransformerConfigurationException (err.toString()); 353 } 354 } 355 356 361 public synchronized Transformer newTransformer() 362 throws TransformerConfigurationException 363 { 364 TransformerImpl transformer; 365 366 transformer = new TransformerImpl(getTransletInstance(), _outputProperties, 367 _indentNumber, _tfactory); 368 369 if (_uriResolver != null) { 370 transformer.setURIResolver(_uriResolver); 371 } 372 return transformer; 373 } 374 375 381 public synchronized Properties getOutputProperties() { 382 try { 383 return newTransformer().getOutputProperties(); 384 } 385 catch (TransformerConfigurationException e) { 386 return null; 387 } 388 } 389 390 393 public DOM getStylesheetDOM() { 394 return (DOM)_sdom.get(); 395 } 396 397 400 public void setStylesheetDOM(DOM sdom) { 401 _sdom.set(sdom); 402 } 403 } 404 | Popular Tags |