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: ResultTreeType.java,v 1.21 2004/02/16 22:26:44 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
  20. import com.sun.org.apache.bcel.internal.generic.ALOAD;
  21. import com.sun.org.apache.bcel.internal.generic.ASTORE;
  22. import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
  23. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  24. import com.sun.org.apache.bcel.internal.generic.GETFIELD;
  25. import com.sun.org.apache.bcel.internal.generic.IFEQ;
  26. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  27. import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
  28. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  29. import com.sun.org.apache.bcel.internal.generic.Instruction;
  30. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  31. import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
  32. import com.sun.org.apache.bcel.internal.generic.NEW;
  33. import com.sun.org.apache.bcel.internal.generic.PUSH;
  34. import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  35. import com.sun.org.apache.xalan.internal.xsltc.compiler.FlowList;
  36. /**
  37. * @author Jacek Ambroziak
  38. * @author Santiago Pericas-Geertsen
  39. * @author Morten Jorgensen
  40. */
  41. public final class ResultTreeType extends Type {
  42. private final String _methodName;
  43. protected ResultTreeType() {
  44. _methodName = null;
  45. }
  46. public ResultTreeType(String methodName) {
  47. _methodName = methodName;
  48. }
  49. public String toString() {
  50. return "result-tree";
  51. }
  52. public boolean identicalTo(Type other) {
  53. return (other instanceof ResultTreeType);
  54. }
  55. public String toSignature() {
  56. return DOM_INTF_SIG;
  57. }
  58. public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
  59. return Util.getJCRefType(toSignature());
  60. }
  61. public String getMethodName() {
  62. return _methodName;
  63. }
  64. public boolean implementedAsMethod() {
  65. return _methodName != null;
  66. }
  67. /**
  68. * Translates a result tree to object of internal type <code>type</code>.
  69. * The translation to int is undefined since result trees
  70. * are always converted to reals in arithmetic expressions.
  71. *
  72. * @param classGen A BCEL class generator
  73. * @param methodGen A BCEL method generator
  74. * @param type An instance of the type to translate the result tree to
  75. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  76. */
  77. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  78. Type type) {
  79. if (type == Type.String) {
  80. translateTo(classGen, methodGen, (StringType)type);
  81. }
  82. else if (type == Type.Boolean) {
  83. translateTo(classGen, methodGen, (BooleanType)type);
  84. }
  85. else if (type == Type.Real) {
  86. translateTo(classGen, methodGen, (RealType)type);
  87. }
  88. else if (type == Type.NodeSet) {
  89. translateTo(classGen, methodGen, (NodeSetType)type);
  90. }
  91. else if (type == Type.Reference) {
  92. translateTo(classGen, methodGen, (ReferenceType)type);
  93. }
  94. else if (type == Type.Object) {
  95. translateTo(classGen, methodGen, (ObjectType) type);
  96. }
  97. else {
  98. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  99. toString(), type.toString());
  100. classGen.getParser().reportError(Constants.FATAL, err);
  101. }
  102. }
  103. /**
  104. * Expects an result tree on the stack and pushes a boolean.
  105. * Translates a result tree to a boolean by first converting it to string.
  106. *
  107. * @param classGen A BCEL class generator
  108. * @param methodGen A BCEL method generator
  109. * @param type An instance of BooleanType (any)
  110. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  111. */
  112. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  113. BooleanType type) {
  114. // A result tree is always 'true' when converted to a boolean value,
  115. // since the tree always has at least one node (the root).
  116. final ConstantPoolGen cpg = classGen.getConstantPool();
  117. final InstructionList il = methodGen.getInstructionList();
  118. il.append(POP); // don't need the DOM reference
  119. il.append(ICONST_1); // push 'true' on the stack
  120. }
  121. /**
  122. * Expects an result tree on the stack and pushes a string.
  123. *
  124. * @param classGen A BCEL class generator
  125. * @param methodGen A BCEL method generator
  126. * @param type An instance of StringType (any)
  127. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  128. */
  129. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  130. StringType type) {
  131. final ConstantPoolGen cpg = classGen.getConstantPool();
  132. final InstructionList il = methodGen.getInstructionList();
  133. if (_methodName == null) {
  134. int index = cpg.addInterfaceMethodref(DOM_INTF,
  135. "getStringValue",
  136. "()"+STRING_SIG);
  137. il.append(new INVOKEINTERFACE(index, 1));
  138. }
  139. else {
  140. final String className = classGen.getClassName();
  141. final int current = methodGen.getLocalIndex("current");
  142. // Push required parameters
  143. il.append(classGen.loadTranslet());
  144. if (classGen.isExternal()) {
  145. il.append(new CHECKCAST(cpg.addClass(className)));
  146. }
  147. il.append(DUP);
  148. il.append(new GETFIELD(cpg.addFieldref(className, "_dom",
  149. DOM_INTF_SIG)));
  150. // Create a new instance of a StringValueHandler
  151. int index = cpg.addMethodref(STRING_VALUE_HANDLER, "<init>", "()V");
  152. il.append(new NEW(cpg.addClass(STRING_VALUE_HANDLER)));
  153. il.append(DUP);
  154. il.append(DUP);
  155. il.append(new INVOKESPECIAL(index));
  156. // Store new Handler into a local variable
  157. final LocalVariableGen handler =
  158. methodGen.addLocalVariable("rt_to_string_handler",
  159. Util.getJCRefType(STRING_VALUE_HANDLER_SIG),
  160. null, null);
  161. il.append(new ASTORE(handler.getIndex()));
  162. // Call the method that implements this result tree
  163. index = cpg.addMethodref(className, _methodName,
  164. "("+DOM_INTF_SIG+TRANSLET_OUTPUT_SIG+")V");
  165. il.append(new INVOKEVIRTUAL(index));
  166. // Restore new handler and call getValue()
  167. il.append(new ALOAD(handler.getIndex()));
  168. index = cpg.addMethodref(STRING_VALUE_HANDLER,
  169. "getValue",
  170. "()" + STRING_SIG);
  171. il.append(new INVOKEVIRTUAL(index));
  172. }
  173. }
  174. /**
  175. * Expects an result tree on the stack and pushes a real.
  176. * Translates a result tree into a real by first converting it to string.
  177. *
  178. * @param classGen A BCEL class generator
  179. * @param methodGen A BCEL method generator
  180. * @param type An instance of RealType (any)
  181. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  182. */
  183. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  184. RealType type) {
  185. translateTo(classGen, methodGen, Type.String);
  186. Type.String.translateTo(classGen, methodGen, Type.Real);
  187. }
  188. /**
  189. * Expects a result tree on the stack and pushes a boxed result tree.
  190. * Result trees are already boxed so the translation is just a NOP.
  191. *
  192. * @param classGen A BCEL class generator
  193. * @param methodGen A BCEL method generator
  194. * @param type An instance of ReferenceType (any)
  195. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  196. */
  197. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  198. ReferenceType type) {
  199. final ConstantPoolGen cpg = classGen.getConstantPool();
  200. final InstructionList il = methodGen.getInstructionList();
  201. if (_methodName == null) {
  202. il.append(NOP);
  203. }
  204. else {
  205. LocalVariableGen domBuilder, newDom;
  206. final String className = classGen.getClassName();
  207. final int current = methodGen.getLocalIndex("current");
  208. // Push required parameters
  209. il.append(classGen.loadTranslet());
  210. if (classGen.isExternal()) {
  211. il.append(new CHECKCAST(cpg.addClass(className)));
  212. }
  213. il.append(methodGen.loadDOM());
  214. // Create new instance of DOM class (with RTF_INITIAL_SIZE nodes)
  215. il.append(methodGen.loadDOM());
  216. int index = cpg.addInterfaceMethodref(DOM_INTF,
  217. "getResultTreeFrag",
  218. "(IZ)" + DOM_INTF_SIG);
  219. il.append(new PUSH(cpg, RTF_INITIAL_SIZE));
  220. il.append(new PUSH(cpg, false));
  221. il.append(new INVOKEINTERFACE(index,3));
  222. il.append(DUP);
  223. // Store new DOM into a local variable
  224. newDom = methodGen.addLocalVariable("rt_to_reference_dom",
  225. Util.getJCRefType(DOM_INTF_SIG),
  226. null, null);
  227. il.append(new CHECKCAST(cpg.addClass(DOM_INTF_SIG)));
  228. il.append(new ASTORE(newDom.getIndex()));
  229. // Overwrite old handler with DOM handler
  230. index = cpg.addInterfaceMethodref(DOM_INTF,
  231. "getOutputDomBuilder",
  232. "()" + TRANSLET_OUTPUT_SIG);
  233. il.append(new INVOKEINTERFACE(index,1));
  234. //index = cpg.addMethodref(DOM_IMPL,
  235. // "getOutputDomBuilder",
  236. // "()" + TRANSLET_OUTPUT_SIG);
  237. //il.append(new INVOKEVIRTUAL(index));
  238. il.append(DUP);
  239. il.append(DUP);
  240. // Store DOM handler in a local in order to call endDocument()
  241. domBuilder =
  242. methodGen.addLocalVariable("rt_to_reference_handler",
  243. Util.getJCRefType(TRANSLET_OUTPUT_SIG),
  244. null, null);
  245. il.append(new ASTORE(domBuilder.getIndex()));
  246. // Call startDocument on the new handler
  247. index = cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
  248. "startDocument", "()V");
  249. il.append(new INVOKEINTERFACE(index, 1));
  250. // Call the method that implements this result tree
  251. index = cpg.addMethodref(className,
  252. _methodName,
  253. "("
  254. + DOM_INTF_SIG
  255. + TRANSLET_OUTPUT_SIG
  256. +")V");
  257. il.append(new INVOKEVIRTUAL(index));
  258. // Call endDocument on the DOM handler
  259. il.append(new ALOAD(domBuilder.getIndex()));
  260. index = cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
  261. "endDocument", "()V");
  262. il.append(new INVOKEINTERFACE(index, 1));
  263. // Push the new DOM on the stack
  264. il.append(new ALOAD(newDom.getIndex()));
  265. }
  266. }
  267. /**
  268. * Expects a result tree on the stack and pushes a node-set (iterator).
  269. * Note that the produced iterator is an iterator for the DOM that
  270. * contains the result tree, and not the DOM that is currently in use.
  271. * This conversion here will therefore not directly work with elements
  272. * such as <xsl:apply-templates> and <xsl:for-each> without the DOM
  273. * parameter/variable being updates as well.
  274. *
  275. * @param classGen A BCEL class generator
  276. * @param methodGen A BCEL method generator
  277. * @param type An instance of NodeSetType (any)
  278. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  279. */
  280. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  281. NodeSetType type) {
  282. final ConstantPoolGen cpg = classGen.getConstantPool();
  283. final InstructionList il = methodGen.getInstructionList();
  284. // Put an extra copy of the result tree (DOM) on the stack
  285. il.append(DUP);
  286. // DOM adapters containing a result tree are not initialised with
  287. // translet-type to DOM-type mapping. This must be done now for
  288. // XPath expressions and patterns to work for the iterator we create.
  289. il.append(classGen.loadTranslet()); // get names array
  290. il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
  291. NAMES_INDEX,
  292. NAMES_INDEX_SIG)));
  293. il.append(classGen.loadTranslet()); // get uris array
  294. il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
  295. URIS_INDEX,
  296. URIS_INDEX_SIG)));
  297. il.append(classGen.loadTranslet()); // get types array
  298. il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
  299. TYPES_INDEX,
  300. TYPES_INDEX_SIG)));
  301. il.append(classGen.loadTranslet()); // get namespaces array
  302. il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
  303. NAMESPACE_INDEX,
  304. NAMESPACE_INDEX_SIG)));
  305. // Pass the type mappings to the DOM adapter
  306. final int mapping = cpg.addInterfaceMethodref(DOM_INTF,
  307. "setupMapping",
  308. "(["+STRING_SIG+
  309. "["+STRING_SIG+
  310. "[I" +
  311. "["+STRING_SIG+")V");
  312. il.append(new INVOKEINTERFACE(mapping, 5));
  313. il.append(DUP);
  314. // Create an iterator for the root node of the DOM adapter
  315. final int iter = cpg.addInterfaceMethodref(DOM_INTF,
  316. "getIterator",
  317. "()"+NODE_ITERATOR_SIG);
  318. il.append(new INVOKEINTERFACE(iter, 1));
  319. }
  320. /**
  321. * Subsume result tree into ObjectType.
  322. *
  323. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  324. */
  325. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  326. ObjectType type) {
  327. methodGen.getInstructionList().append(NOP);
  328. }
  329. /**
  330. * Translates a result tree into a non-synthesized boolean.
  331. * It does not push a 0 or a 1 but instead returns branchhandle list
  332. * to be appended to the false list.
  333. *
  334. * @param classGen A BCEL class generator
  335. * @param methodGen A BCEL method generator
  336. * @param type An instance of BooleanType (any)
  337. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateToDesynthesized
  338. */
  339. public FlowList translateToDesynthesized(ClassGenerator classGen,
  340. MethodGenerator methodGen,
  341. BooleanType type) {
  342. final InstructionList il = methodGen.getInstructionList();
  343. translateTo(classGen, methodGen, Type.Boolean);
  344. return new FlowList(il.append(new IFEQ(null)));
  345. }
  346. /**
  347. * Translates a result tree to a Java type denoted by <code>clazz</code>.
  348. * Expects a result tree on the stack and pushes an object
  349. * of the appropriate type after coercion. Result trees are translated
  350. * to W3C Node or W3C NodeList and the translation is done
  351. * via node-set type.
  352. *
  353. * @param classGen A BCEL class generator
  354. * @param methodGen A BCEL method generator
  355. * @param type An reference to the Class to translate to
  356. * @see com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type#translateTo
  357. */
  358. public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
  359. Class clazz) {
  360. final String className = clazz.getName();
  361. final ConstantPoolGen cpg = classGen.getConstantPool();
  362. final InstructionList il = methodGen.getInstructionList();
  363. if (className.equals("org.w3c.dom.Node")) {
  364. translateTo(classGen, methodGen, Type.NodeSet);
  365. int index = cpg.addInterfaceMethodref(DOM_INTF,
  366. MAKE_NODE,
  367. MAKE_NODE_SIG2);
  368. il.append(new INVOKEINTERFACE(index, 2));
  369. }
  370. else if (className.equals("org.w3c.dom.NodeList")) {
  371. translateTo(classGen, methodGen, Type.NodeSet);
  372. int index = cpg.addInterfaceMethodref(DOM_INTF,
  373. MAKE_NODE_LIST,
  374. MAKE_NODE_LIST_SIG2);
  375. il.append(new INVOKEINTERFACE(index, 2));
  376. }
  377. else if (className.equals("java.lang.Object")) {
  378. il.append(NOP);
  379. }
  380. else if (className.equals("java.lang.String")) {
  381. translateTo(classGen, methodGen, Type.String);
  382. }
  383. else {
  384. ErrorMsg err = new ErrorMsg(ErrorMsg.DATA_CONVERSION_ERR,
  385. toString(), className);
  386. classGen.getParser().reportError(Constants.FATAL, err);
  387. }
  388. }
  389. /**
  390. * Translates an object of this type to its boxed representation.
  391. */
  392. public void translateBox(ClassGenerator classGen,
  393. MethodGenerator methodGen) {
  394. translateTo(classGen, methodGen, Type.Reference);
  395. }
  396. /**
  397. * Translates an object of this type to its unboxed representation.
  398. */
  399. public void translateUnBox(ClassGenerator classGen,
  400. MethodGenerator methodGen) {
  401. methodGen.getInstructionList().append(NOP);
  402. }
  403. /**
  404. * Returns the class name of an internal type's external representation.
  405. */
  406. public String getClassName() {
  407. return(DOM_INTF);
  408. }
  409. public Instruction LOAD(int slot) {
  410. return new ALOAD(slot);
  411. }
  412. public Instruction STORE(int slot) {
  413. return new ASTORE(slot);
  414. }
  415. }