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