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: ChildTestIterator.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xpath.axes;
022    
023    import org.apache.xml.dtm.Axis;
024    import org.apache.xml.dtm.DTM;
025    import org.apache.xml.dtm.DTMAxisTraverser;
026    import org.apache.xml.dtm.DTMIterator;
027    import org.apache.xpath.compiler.Compiler;
028    
029    /**
030     * This class implements an optimized iterator for
031     * children patterns that have a node test, and possibly a predicate.
032     * @see org.apache.xpath.axes.BasicTestIterator
033     * @xsl.usage advanced
034     */
035    public class ChildTestIterator extends BasicTestIterator
036    {
037        static final long serialVersionUID = -7936835957960705722L;
038      /** The traverser to use to navigate over the descendants. */
039      transient protected DTMAxisTraverser m_traverser;
040      
041      /** The extended type ID, not set until setRoot. */
042    //  protected int m_extendedTypeID;
043    
044    
045      /**
046       * Create a ChildTestIterator object.
047       *
048       * @param compiler A reference to the Compiler that contains the op map.
049       * @param opPos The position within the op map, which contains the
050       * location path expression for this itterator.
051       *
052       * @throws javax.xml.transform.TransformerException
053       */
054      ChildTestIterator(Compiler compiler, int opPos, int analysis)
055              throws javax.xml.transform.TransformerException
056      {
057        super(compiler, opPos, analysis);
058      }
059      
060      /**
061       * Create a ChildTestIterator object.
062       *
063       * @param traverser Traverser that tells how the KeyIterator is to be handled.
064       *
065       * @throws javax.xml.transform.TransformerException
066       */
067      public ChildTestIterator(DTMAxisTraverser traverser)
068      {
069    
070        super(null);
071    
072        m_traverser = traverser;
073      }
074    
075      /**
076       * Get the next node via getNextXXX.  Bottlenecked for derived class override.
077       * @return The next node on the axis, or DTM.NULL.
078       */
079      protected int getNextNode()
080      {                     
081        if(true /* 0 == m_extendedTypeID */)
082        {
083          m_lastFetched = (DTM.NULL == m_lastFetched)
084                       ? m_traverser.first(m_context)
085                       : m_traverser.next(m_context, m_lastFetched);
086        }
087    //    else
088    //    {
089    //      m_lastFetched = (DTM.NULL == m_lastFetched)
090    //                   ? m_traverser.first(m_context, m_extendedTypeID)
091    //                   : m_traverser.next(m_context, m_lastFetched, 
092    //                                      m_extendedTypeID);
093    //    }
094    
095        return m_lastFetched;
096      }
097    
098      
099      /**
100       *  Get a cloned Iterator that is reset to the beginning
101       *  of the query.
102       * 
103       *  @return A cloned NodeIterator set of the start of the query.
104       * 
105       *  @throws CloneNotSupportedException
106       */
107      public DTMIterator cloneWithReset() throws CloneNotSupportedException
108      {
109    
110        ChildTestIterator clone = (ChildTestIterator) super.cloneWithReset();
111        clone.m_traverser = m_traverser;
112    
113        return clone;
114      }
115      
116    
117      /**
118       * Initialize the context values for this expression
119       * after it is cloned.
120       *
121       * @param context The XPath runtime context for this
122       * transformation.
123       */
124      public void setRoot(int context, Object environment)
125      {
126        super.setRoot(context, environment);
127        m_traverser = m_cdtm.getAxisTraverser(Axis.CHILD);
128        
129    //    String localName = getLocalName();
130    //    String namespace = getNamespace();
131    //    int what = m_whatToShow;
132    //    // System.out.println("what: ");
133    //    // NodeTest.debugWhatToShow(what);
134    //    if(DTMFilter.SHOW_ALL == what ||
135    //       ((DTMFilter.SHOW_ELEMENT & what) == 0)
136    //       || localName == NodeTest.WILD
137    //       || namespace == NodeTest.WILD)
138    //    {
139    //      m_extendedTypeID = 0;
140    //    }
141    //    else
142    //    {
143    //      int type = getNodeTypeTest(what);
144    //      m_extendedTypeID = m_cdtm.getExpandedTypeID(namespace, localName, type);
145    //    }
146        
147      }
148      
149      /**
150       * Returns the axis being iterated, if it is known.
151       * 
152       * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple 
153       * types.
154       */
155      public int getAxis()
156      {
157        return org.apache.xml.dtm.Axis.CHILD;
158      }
159    
160      /**
161       *  Detaches the iterator from the set which it iterated over, releasing
162       * any computational resources and placing the iterator in the INVALID
163       * state. After<code>detach</code> has been invoked, calls to
164       * <code>nextNode</code> or<code>previousNode</code> will raise the
165       * exception INVALID_STATE_ERR.
166       */
167      public void detach()
168      {   
169        if(m_allowDetach)
170        {
171          m_traverser = null;
172          
173          // Always call the superclass detach last!
174          super.detach();
175        }
176      }
177    
178    }