001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: XNodeSetForDOM.java 469368 2006-10-31 04:41:36Z minchau $
020     */
021    package org.apache.xpath.objects;
022    
023    import org.apache.xml.dtm.DTMManager;
024    import org.apache.xpath.NodeSetDTM;
025    import org.apache.xpath.XPathContext;
026    
027    import org.w3c.dom.Node;
028    import org.w3c.dom.NodeList;
029    import org.w3c.dom.traversal.NodeIterator;
030    
031    /**
032     * This class overrides the XNodeSet#object() method to provide the original 
033     * Node object, NodeList object, or NodeIterator.
034     */
035    public class XNodeSetForDOM extends XNodeSet
036    {
037        static final long serialVersionUID = -8396190713754624640L;
038      Object m_origObj;
039    
040      public XNodeSetForDOM(Node node, DTMManager dtmMgr)
041      {
042        m_dtmMgr = dtmMgr;
043        m_origObj = node;
044        int dtmHandle = dtmMgr.getDTMHandleFromNode(node);
045        setObject(new NodeSetDTM(dtmMgr));
046        ((NodeSetDTM) m_obj).addNode(dtmHandle);
047      }
048      
049      /**
050       * Construct a XNodeSet object.
051       *
052       * @param val Value of the XNodeSet object
053       */
054      public XNodeSetForDOM(XNodeSet val)
055      {
056            super(val);
057            if(val instanceof XNodeSetForDOM)
058            m_origObj = ((XNodeSetForDOM)val).m_origObj;
059      }
060      
061      public XNodeSetForDOM(NodeList nodeList, XPathContext xctxt)
062      {
063        m_dtmMgr = xctxt.getDTMManager();
064        m_origObj = nodeList;
065    
066        // JKESS 20020514: Longer-term solution is to force
067        // folks to request length through an accessor, so we can defer this
068        // retrieval... but that requires an API change.
069        // m_obj=new org.apache.xpath.NodeSetDTM(nodeList, xctxt);
070        org.apache.xpath.NodeSetDTM nsdtm=new org.apache.xpath.NodeSetDTM(nodeList, xctxt);
071        m_last=nsdtm.getLength();
072        setObject(nsdtm);   
073      }
074    
075      public XNodeSetForDOM(NodeIterator nodeIter, XPathContext xctxt)
076      {
077        m_dtmMgr = xctxt.getDTMManager();
078        m_origObj = nodeIter;
079    
080        // JKESS 20020514: Longer-term solution is to force
081        // folks to request length through an accessor, so we can defer this
082        // retrieval... but that requires an API change.
083        // m_obj = new org.apache.xpath.NodeSetDTM(nodeIter, xctxt);
084        org.apache.xpath.NodeSetDTM nsdtm=new org.apache.xpath.NodeSetDTM(nodeIter, xctxt);
085        m_last=nsdtm.getLength();
086        setObject(nsdtm);   
087      }
088      
089      /**
090       * Return the original DOM object that the user passed in.  For use primarily
091       * by the extension mechanism.
092       *
093       * @return The object that this class wraps
094       */
095      public Object object()
096      {
097        return m_origObj;
098      }
099      
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    }