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: XNodeSetForDOM.java,v 1.6 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.DTMManager;
  21. import com.sun.org.apache.xpath.internal.NodeSetDTM;
  22. import com.sun.org.apache.xpath.internal.XPathContext;
  23. import org.w3c.dom.Node;
  24. import org.w3c.dom.NodeList;
  25. import org.w3c.dom.traversal.NodeIterator;
  26. /**
  27. * This class overrides the XNodeSet#object() method to provide the original
  28. * Node object, NodeList object, or NodeIterator.
  29. */
  30. public class XNodeSetForDOM extends XNodeSet
  31. {
  32. Object m_origObj;
  33. public XNodeSetForDOM(Node node, DTMManager dtmMgr)
  34. {
  35. m_dtmMgr = dtmMgr;
  36. m_origObj = node;
  37. int dtmHandle = dtmMgr.getDTMHandleFromNode(node);
  38. m_obj = new NodeSetDTM(dtmMgr);
  39. ((NodeSetDTM) m_obj).addNode(dtmHandle);
  40. }
  41. /**
  42. * Construct a XNodeSet object.
  43. *
  44. * @param val Value of the XNodeSet object
  45. */
  46. public XNodeSetForDOM(XNodeSet val)
  47. {
  48. super(val);
  49. if(val instanceof XNodeSetForDOM)
  50. m_origObj = ((XNodeSetForDOM)val).m_origObj;
  51. }
  52. public XNodeSetForDOM(NodeList nodeList, XPathContext xctxt)
  53. {
  54. m_dtmMgr = xctxt.getDTMManager();
  55. m_origObj = nodeList;
  56. // JKESS 20020514: Longer-term solution is to force
  57. // folks to request length through an accessor, so we can defer this
  58. // retrieval... but that requires an API change.
  59. // m_obj=new com.sun.org.apache.xpath.internal.NodeSetDTM(nodeList, xctxt);
  60. com.sun.org.apache.xpath.internal.NodeSetDTM nsdtm=new com.sun.org.apache.xpath.internal.NodeSetDTM(nodeList, xctxt);
  61. m_last=nsdtm.getLength();
  62. m_obj = nsdtm;
  63. }
  64. public XNodeSetForDOM(NodeIterator nodeIter, XPathContext xctxt)
  65. {
  66. m_dtmMgr = xctxt.getDTMManager();
  67. m_origObj = nodeIter;
  68. // JKESS 20020514: Longer-term solution is to force
  69. // folks to request length through an accessor, so we can defer this
  70. // retrieval... but that requires an API change.
  71. // m_obj = new com.sun.org.apache.xpath.internal.NodeSetDTM(nodeIter, xctxt);
  72. com.sun.org.apache.xpath.internal.NodeSetDTM nsdtm=new com.sun.org.apache.xpath.internal.NodeSetDTM(nodeIter, xctxt);
  73. m_last=nsdtm.getLength();
  74. m_obj = nsdtm;
  75. }
  76. /**
  77. * Return the original DOM object that the user passed in. For use primarily
  78. * by the extension mechanism.
  79. *
  80. * @return The object that this class wraps
  81. */
  82. public Object object()
  83. {
  84. return m_origObj;
  85. }
  86. /**
  87. * Cast result object to a nodelist. Always issues an error.
  88. *
  89. * @return null
  90. *
  91. * @throws javax.xml.transform.TransformerException
  92. */
  93. public NodeIterator nodeset() throws javax.xml.transform.TransformerException
  94. {
  95. return (m_origObj instanceof NodeIterator)
  96. ? (NodeIterator)m_origObj : super.nodeset();
  97. }
  98. /**
  99. * Cast result object to a nodelist. Always issues an error.
  100. *
  101. * @return null
  102. *
  103. * @throws javax.xml.transform.TransformerException
  104. */
  105. public NodeList nodelist() throws javax.xml.transform.TransformerException
  106. {
  107. return (m_origObj instanceof NodeList)
  108. ? (NodeList)m_origObj : super.nodelist();
  109. }
  110. }