1. /*
  2. * Copyright 1999-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: XObjectFactory.java,v 1.5 2004/02/17 04:34:38 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.objects;
  20. import com.sun.org.apache.xml.internal.dtm.Axis;
  21. import com.sun.org.apache.xml.internal.dtm.DTM;
  22. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  23. import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  24. import com.sun.org.apache.xpath.internal.XPathContext;
  25. import com.sun.org.apache.xpath.internal.axes.OneStepIterator;
  26. public class XObjectFactory
  27. {
  28. /**
  29. * Create the right XObject based on the type of the object passed. This
  30. * function can not make an XObject that exposes DOM Nodes, NodeLists, and
  31. * NodeIterators to the XSLT stylesheet as node-sets.
  32. *
  33. * @param val The java object which this object will wrap.
  34. *
  35. * @return the right XObject based on the type of the object passed.
  36. */
  37. static public XObject create(Object val)
  38. {
  39. XObject result;
  40. if (val instanceof XObject)
  41. {
  42. result = (XObject) val;
  43. }
  44. else if (val instanceof String)
  45. {
  46. result = new XString((String) val);
  47. }
  48. else if (val instanceof Boolean)
  49. {
  50. result = new XBoolean((Boolean)val);
  51. }
  52. else if (val instanceof Double)
  53. {
  54. result = new XNumber(((Double) val));
  55. }
  56. else
  57. {
  58. result = new XObject(val);
  59. }
  60. return result;
  61. }
  62. /**
  63. * Create the right XObject based on the type of the object passed.
  64. * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and
  65. * NodeIterators to the XSLT stylesheet as node-sets.
  66. *
  67. * @param val The java object which this object will wrap.
  68. * @param xctxt The XPath context.
  69. *
  70. * @return the right XObject based on the type of the object passed.
  71. */
  72. static public XObject create(Object val, XPathContext xctxt)
  73. {
  74. XObject result;
  75. if (val instanceof XObject)
  76. {
  77. result = (XObject) val;
  78. }
  79. else if (val instanceof String)
  80. {
  81. result = new XString((String) val);
  82. }
  83. else if (val instanceof Boolean)
  84. {
  85. result = new XBoolean((Boolean)val);
  86. }
  87. else if (val instanceof Number)
  88. {
  89. result = new XNumber(((Number) val));
  90. }
  91. else if (val instanceof DTM)
  92. {
  93. DTM dtm = (DTM)val;
  94. try
  95. {
  96. int dtmRoot = dtm.getDocument();
  97. DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
  98. iter.setStartNode(dtmRoot);
  99. DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
  100. iterator.setRoot(dtmRoot, xctxt);
  101. result = new XNodeSet(iterator);
  102. }
  103. catch(Exception ex)
  104. {
  105. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex);
  106. }
  107. }
  108. else if (val instanceof DTMAxisIterator)
  109. {
  110. DTMAxisIterator iter = (DTMAxisIterator)val;
  111. try
  112. {
  113. DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
  114. iterator.setRoot(iter.getStartNode(), xctxt);
  115. result = new XNodeSet(iterator);
  116. }
  117. catch(Exception ex)
  118. {
  119. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex);
  120. }
  121. }
  122. else if (val instanceof DTMIterator)
  123. {
  124. result = new XNodeSet((DTMIterator) val);
  125. }
  126. // This next three instanceofs are a little worrysome, since a NodeList
  127. // might also implement a Node!
  128. else if (val instanceof org.w3c.dom.Node)
  129. {
  130. result = new XNodeSetForDOM((org.w3c.dom.Node)val, xctxt);
  131. }
  132. // This must come after org.w3c.dom.Node, since many Node implementations
  133. // also implement NodeList.
  134. else if (val instanceof org.w3c.dom.NodeList)
  135. {
  136. result = new XNodeSetForDOM((org.w3c.dom.NodeList)val, xctxt);
  137. }
  138. else if (val instanceof org.w3c.dom.traversal.NodeIterator)
  139. {
  140. result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator)val, xctxt);
  141. }
  142. else
  143. {
  144. result = new XObject(val);
  145. }
  146. return result;
  147. }
  148. }