1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: TemplatesImpl.java,v 1.34 2004/02/23 10:29:36 aruny Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.trax;
  20. import java.io.IOException;
  21. import java.io.ObjectInputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.io.Serializable;
  24. import java.util.Properties;
  25. import java.security.AccessController;
  26. import java.security.PrivilegedAction;
  27. import javax.xml.transform.Templates;
  28. import javax.xml.transform.Transformer;
  29. import javax.xml.transform.TransformerConfigurationException;
  30. import javax.xml.transform.URIResolver;
  31. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  32. import com.sun.org.apache.xalan.internal.xsltc.Translet;
  33. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  34. import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet;
  35. import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
  36. /**
  37. * @author Morten Jorgensen
  38. * @author G. Todd Millerj
  39. * @author Jochen Cordes <Jochen.Cordes@t-online.de>
  40. * @author Santiago Pericas-Geertsen
  41. */
  42. public final class TemplatesImpl implements Templates, Serializable {
  43. /**
  44. * Name of the superclass of all translets. This is needed to
  45. * determine which, among all classes comprising a translet,
  46. * is the main one.
  47. */
  48. private static String ABSTRACT_TRANSLET
  49. = "com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet";
  50. /**
  51. * Name of the main class or default name if unknown.
  52. */
  53. private String _name = null;
  54. /**
  55. * Contains the actual class definition for the translet class and
  56. * any auxiliary classes.
  57. */
  58. private byte[][] _bytecodes = null;
  59. /**
  60. * Contains the translet class definition(s). These are created when
  61. * this Templates is created or when it is read back from disk.
  62. */
  63. private Class[] _class = null;
  64. /**
  65. * The index of the main translet class in the arrays _class[] and
  66. * _bytecodes.
  67. */
  68. private int _transletIndex = -1;
  69. /**
  70. * Contains the list of auxiliary class definitions.
  71. */
  72. private Hashtable _auxClasses = null;
  73. /**
  74. * Output properties of this translet.
  75. */
  76. private Properties _outputProperties;
  77. /**
  78. * Number of spaces to add for output indentation.
  79. */
  80. private int _indentNumber;
  81. /**
  82. * This URIResolver is passed to all Transformers.
  83. * Declaring it transient to fix bug 22438
  84. */
  85. private transient URIResolver _uriResolver = null;
  86. /**
  87. * Cache the DTM for the stylesheet in a thread local variable,
  88. * which is used by the document('') function.
  89. * Use ThreadLocal because a DTM cannot be shared between
  90. * multiple threads.
  91. * Declaring it transient to fix bug 22438
  92. */
  93. private transient ThreadLocal _sdom = new ThreadLocal();
  94. /**
  95. * A reference to the transformer factory that this templates
  96. * object belongs to.
  97. */
  98. private transient TransformerFactoryImpl _tfactory = null;
  99. static final class TransletClassLoader extends ClassLoader {
  100. TransletClassLoader(ClassLoader parent) {
  101. super(parent);
  102. }
  103. /**
  104. * Access to final protected superclass member from outer class.
  105. */
  106. Class defineClass(final byte[] b) {
  107. return defineClass(null, b, 0, b.length);
  108. }
  109. }
  110. /**
  111. * Create an XSLTC template object from the bytecodes.
  112. * The bytecodes for the translet and auxiliary classes, plus the name of
  113. * the main translet class, must be supplied.
  114. */
  115. protected TemplatesImpl(byte[][] bytecodes, String transletName,
  116. Properties outputProperties, int indentNumber,
  117. TransformerFactoryImpl tfactory)
  118. {
  119. _bytecodes = bytecodes;
  120. _name = transletName;
  121. _outputProperties = outputProperties;
  122. _indentNumber = indentNumber;
  123. _tfactory = tfactory;
  124. }
  125. /**
  126. * Create an XSLTC template object from the translet class definition(s).
  127. */
  128. protected TemplatesImpl(Class[] transletClasses, String transletName,
  129. Properties outputProperties, int indentNumber,
  130. TransformerFactoryImpl tfactory)
  131. {
  132. _class = transletClasses;
  133. _name = transletName;
  134. _transletIndex = 0;
  135. _outputProperties = outputProperties;
  136. _indentNumber = indentNumber;
  137. _tfactory = tfactory;
  138. }
  139. /**
  140. * Need for de-serialization, see readObject().
  141. */
  142. public TemplatesImpl() { }
  143. /**
  144. * Overrides the default readObject implementation since we decided
  145. * it would be cleaner not to serialize the entire tranformer
  146. * factory. [ ref bugzilla 12317 ]
  147. * We need to check if the user defined class for URIResolver also
  148. * implemented Serializable
  149. * if yes then we need to deserialize the URIResolver
  150. * Fix for bugzilla bug 22438
  151. */
  152. private void readObject(ObjectInputStream is)
  153. throws IOException, ClassNotFoundException
  154. {
  155. is.defaultReadObject();
  156. if (is.readBoolean()) {
  157. _uriResolver = (URIResolver) is.readObject();
  158. }
  159. _tfactory = new TransformerFactoryImpl();
  160. }
  161. /**
  162. * This is to fix bugzilla bug 22438
  163. * If the user defined class implements URIResolver and Serializable
  164. * then we want it to get serialized
  165. */
  166. private void writeObject(ObjectOutputStream os)
  167. throws IOException, ClassNotFoundException {
  168. os.defaultWriteObject();
  169. if (_uriResolver instanceof Serializable) {
  170. os.writeBoolean(true);
  171. os.writeObject((Serializable) _uriResolver);
  172. }
  173. else {
  174. os.writeBoolean(false);
  175. }
  176. }
  177. /**
  178. * Store URIResolver needed for Transformers.
  179. */
  180. public synchronized void setURIResolver(URIResolver resolver) {
  181. _uriResolver = resolver;
  182. }
  183. /**
  184. * The TransformerFactory must pass us the translet bytecodes using this
  185. * method before we can create any translet instances
  186. */
  187. protected synchronized void setTransletBytecodes(byte[][] bytecodes) {
  188. _bytecodes = bytecodes;
  189. }
  190. /**
  191. * Returns the translet bytecodes stored in this template
  192. */
  193. public synchronized byte[][] getTransletBytecodes() {
  194. return _bytecodes;
  195. }
  196. /**
  197. * Returns the translet bytecodes stored in this template
  198. */
  199. public synchronized Class[] getTransletClasses() {
  200. try {
  201. if (_class == null) defineTransletClasses();
  202. }
  203. catch (TransformerConfigurationException e) {
  204. // Falls through
  205. }
  206. return _class;
  207. }
  208. /**
  209. * Returns the index of the main class in array of bytecodes
  210. */
  211. public synchronized int getTransletIndex() {
  212. try {
  213. if (_class == null) defineTransletClasses();
  214. }
  215. catch (TransformerConfigurationException e) {
  216. // Falls through
  217. }
  218. return _transletIndex;
  219. }
  220. /**
  221. * The TransformerFactory should call this method to set the translet name
  222. */
  223. protected synchronized void setTransletName(String name) {
  224. _name = name;
  225. }
  226. /**
  227. * Returns the name of the main translet class stored in this template
  228. */
  229. protected synchronized String getTransletName() {
  230. return _name;
  231. }
  232. /**
  233. * Defines the translet class and auxiliary classes.
  234. * Returns a reference to the Class object that defines the main class
  235. */
  236. private void defineTransletClasses()
  237. throws TransformerConfigurationException {
  238. if (_bytecodes == null) {
  239. ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR);
  240. throw new TransformerConfigurationException(err.toString());
  241. }
  242. TransletClassLoader loader = (TransletClassLoader)
  243. AccessController.doPrivileged(new PrivilegedAction() {
  244. public Object run() {
  245. return new TransletClassLoader(ObjectFactory.findClassLoader());
  246. }
  247. });
  248. try {
  249. final int classCount = _bytecodes.length;
  250. _class = new Class[classCount];
  251. if (classCount > 1) {
  252. _auxClasses = new Hashtable();
  253. }
  254. for (int i = 0; i < classCount; i++) {
  255. _class[i] = loader.defineClass(_bytecodes[i]);
  256. final Class superClass = _class[i].getSuperclass();
  257. // Check if this is the main class
  258. if (superClass.getName().equals(ABSTRACT_TRANSLET)) {
  259. _transletIndex = i;
  260. }
  261. else {
  262. _auxClasses.put(_class[i].getName(), _class[i]);
  263. }
  264. }
  265. if (_transletIndex < 0) {
  266. ErrorMsg err= new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name);
  267. throw new TransformerConfigurationException(err.toString());
  268. }
  269. }
  270. catch (ClassFormatError e) {
  271. ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name);
  272. throw new TransformerConfigurationException(err.toString());
  273. }
  274. catch (LinkageError e) {
  275. ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
  276. throw new TransformerConfigurationException(err.toString());
  277. }
  278. }
  279. /**
  280. * This method generates an instance of the translet class that is
  281. * wrapped inside this Template. The translet instance will later
  282. * be wrapped inside a Transformer object.
  283. */
  284. private Translet getTransletInstance()
  285. throws TransformerConfigurationException {
  286. try {
  287. if (_name == null) return null;
  288. if (_class == null) defineTransletClasses();
  289. // The translet needs to keep a reference to all its auxiliary
  290. // class to prevent the GC from collecting them
  291. AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance();
  292. translet.postInitialization();
  293. translet.setTemplates(this);
  294. if (_auxClasses != null) {
  295. translet.setAuxiliaryClasses(_auxClasses);
  296. }
  297. return translet;
  298. }
  299. catch (InstantiationException e) {
  300. ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
  301. throw new TransformerConfigurationException(err.toString());
  302. }
  303. catch (IllegalAccessException e) {
  304. ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
  305. throw new TransformerConfigurationException(err.toString());
  306. }
  307. }
  308. /**
  309. * Implements JAXP's Templates.newTransformer()
  310. *
  311. * @throws TransformerConfigurationException
  312. */
  313. public synchronized Transformer newTransformer()
  314. throws TransformerConfigurationException
  315. {
  316. TransformerImpl transformer;
  317. transformer = new TransformerImpl(getTransletInstance(), _outputProperties,
  318. _indentNumber, _tfactory);
  319. if (_uriResolver != null) {
  320. transformer.setURIResolver(_uriResolver);
  321. }
  322. return transformer;
  323. }
  324. /**
  325. * Implements JAXP's Templates.getOutputProperties(). We need to
  326. * instanciate a translet to get the output settings, so
  327. * we might as well just instanciate a Transformer and use its
  328. * implementation of this method.
  329. */
  330. public synchronized Properties getOutputProperties() {
  331. try {
  332. return newTransformer().getOutputProperties();
  333. }
  334. catch (TransformerConfigurationException e) {
  335. return null;
  336. }
  337. }
  338. /**
  339. * Return the thread local copy of the stylesheet DOM.
  340. */
  341. public DOM getStylesheetDOM() {
  342. return (DOM)_sdom.get();
  343. }
  344. /**
  345. * Set the thread local copy of the stylesheet DOM.
  346. */
  347. public void setStylesheetDOM(DOM sdom) {
  348. _sdom.set(sdom);
  349. }
  350. }