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