1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: XNodeSetForDOM.java 469368 2006-10-31 04:41:36Z minchau $
20 */
21package org.apache.xpath.objects;
22
23import org.apache.xml.dtm.DTMManager;
24import org.apache.xpath.NodeSetDTM;
25import org.apache.xpath.XPathContext;
26
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29import org.w3c.dom.traversal.NodeIterator;
30
31/**
32 * This class overrides the XNodeSet#object() method to provide the original
33 * Node object, NodeList object, or NodeIterator.
34 */
35public class XNodeSetForDOM extends XNodeSet
36{
37    static final long serialVersionUID = -8396190713754624640L;
38  Object m_origObj;
39
40  public XNodeSetForDOM(Node node, DTMManager dtmMgr)
41  {
42    m_dtmMgr = dtmMgr;
43    m_origObj = node;
44    int dtmHandle = dtmMgr.getDTMHandleFromNode(node);
45    setObject(new NodeSetDTM(dtmMgr));
46    ((NodeSetDTM) m_obj).addNode(dtmHandle);
47  }
48
49  /**
50   * Construct a XNodeSet object.
51   *
52   * @param val Value of the XNodeSet object
53   */
54  public XNodeSetForDOM(XNodeSet val)
55  {
56  	super(val);
57  	if(val instanceof XNodeSetForDOM)
58    	m_origObj = ((XNodeSetForDOM)val).m_origObj;
59  }
60
61  public XNodeSetForDOM(NodeList nodeList, XPathContext xctxt)
62  {
63    m_dtmMgr = xctxt.getDTMManager();
64    m_origObj = nodeList;
65
66    // JKESS 20020514: Longer-term solution is to force
67    // folks to request length through an accessor, so we can defer this
68    // retrieval... but that requires an API change.
69    // m_obj=new org.apache.xpath.NodeSetDTM(nodeList, xctxt);
70    org.apache.xpath.NodeSetDTM nsdtm=new org.apache.xpath.NodeSetDTM(nodeList, xctxt);
71    m_last=nsdtm.getLength();
72    setObject(nsdtm);
73  }
74
75  public XNodeSetForDOM(NodeIterator nodeIter, XPathContext xctxt)
76  {
77    m_dtmMgr = xctxt.getDTMManager();
78    m_origObj = nodeIter;
79
80    // JKESS 20020514: Longer-term solution is to force
81    // folks to request length through an accessor, so we can defer this
82    // retrieval... but that requires an API change.
83    // m_obj = new org.apache.xpath.NodeSetDTM(nodeIter, xctxt);
84    org.apache.xpath.NodeSetDTM nsdtm=new org.apache.xpath.NodeSetDTM(nodeIter, xctxt);
85    m_last=nsdtm.getLength();
86    setObject(nsdtm);
87  }
88
89  /**
90   * Return the original DOM object that the user passed in.  For use primarily
91   * by the extension mechanism.
92   *
93   * @return The object that this class wraps
94   */
95  public Object object()
96  {
97    return m_origObj;
98  }
99
100  /**
101   * Cast result object to a nodelist. Always issues an error.
102   *
103   * @return null
104   *
105   * @throws javax.xml.transform.TransformerException
106   */
107  public NodeIterator nodeset() throws javax.xml.transform.TransformerException
108  {
109    return (m_origObj instanceof NodeIterator)
110                   ? (NodeIterator)m_origObj : super.nodeset();
111  }
112
113  /**
114   * Cast result object to a nodelist. Always issues an error.
115   *
116   * @return null
117   *
118   * @throws javax.xml.transform.TransformerException
119   */
120  public NodeList nodelist() throws javax.xml.transform.TransformerException
121  {
122    return (m_origObj instanceof NodeList)
123                   ? (NodeList)m_origObj : super.nodelist();
124  }
125
126
127
128}
129