19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file 49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information 59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file 69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License. 89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at 99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software 139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and 169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License. 179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: DTMNodeIterator.java 468653 2006-10-28 07:07:05Z minchau $ 209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xml.dtm.ref; 229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM; 249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMDOMException; 259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMIterator; 269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.w3c.dom.DOMException; 289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.w3c.dom.Node; 299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.w3c.dom.traversal.NodeFilter; 309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/** 329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>DTMNodeIterator</code> gives us an implementation of the 339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * DTMNodeIterator which returns DOM nodes. 349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Please note that this is not necessarily equivlaent to a DOM 369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeIterator operating over the same document. In particular: 379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <ul> 389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>If there are several Text nodes in logical succession (ie, 409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * across CDATASection and EntityReference boundaries), we will return 419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * only the first; the caller is responsible for stepping through 429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * them. 439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (%REVIEW% Provide a convenience routine here to assist, pending 449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * proposed DOM Level 3 getAdjacentText() operation?) </li> 459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>Since the whole XPath/XSLT architecture assumes that the source 479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * document is not altered while we're working with it, we do not 489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * promise to implement the DOM NodeIterator's "maintain current 499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * position" response to document mutation. </li> 509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>Since our design for XPath NodeIterators builds a stateful 529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * filter directly into the traversal object, getNodeFilter() is not 539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * supported.</li> 549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </ul> 569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p>State: In progress!!</p> 589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class DTMNodeIterator implements org.w3c.dom.traversal.NodeIterator 609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{ 619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson private DTMIterator dtm_iter; 629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson private boolean valid=true; 639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson //================================================================ 659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Methods unique to this class 669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Public constructor: Wrap a DTMNodeIterator around an existing 689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * and preconfigured DTMIterator 699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public DTMNodeIterator(DTMIterator dtmIterator) 719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson try 739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson dtm_iter=(DTMIterator)dtmIterator.clone(); 759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson catch(CloneNotSupportedException cnse) 779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new org.apache.xml.utils.WrappedRuntimeException(cnse); 799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Access the wrapped DTMIterator. I'm not sure whether anyone will 839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * need this or not, but let's write it and think about it. 849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public DTMIterator getDTMIterator() 869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return dtm_iter; 889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson //================================================================ 929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // org.w3c.dom.traversal.NodeFilter API follows 939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Detaches the NodeIterator from the set which it iterated over, 959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * releasing any computational resources and placing the iterator in 969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the INVALID state. 979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void detach() 999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Theoretically, we could release dtm_iter at this point. But 1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // some of the operations may still want to consult it even though 1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // navigation is now invalid. 1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson valid=false; 1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** The value of this flag determines whether the children 1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of entity reference nodes are visible to the iterator. 1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return false, always (the DTM model flattens entity references) 1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean getExpandEntityReferences() 1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return false; 1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Return a handle to the filter used to screen nodes. 1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This is ill-defined in Xalan's usage of Nodeiterator, where we have 1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * built stateful XPath-based filtering directly into the traversal 1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * object. We could return something which supports the NodeFilter interface 1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * and allows querying whether a given node would be permitted if it appeared 1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * as our next node, but in the current implementation that would be very 1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * complex -- and just isn't all that useful. 1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws DOMException -- NOT_SUPPORTED_ERROR because I can't think 1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of anything more useful to do in this case 1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public NodeFilter getFilter() 1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new DTMDOMException(DOMException.NOT_SUPPORTED_ERR); 1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** @return The root node of the NodeIterator, as specified 1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * when it was created. 1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public Node getRoot() 1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int handle=dtm_iter.getRoot(); 1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return dtm_iter.getDTM(handle).getNode(handle); 1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Return a mask describing which node types are presented via the 1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * iterator. 1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson **/ 1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int getWhatToShow() 1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return dtm_iter.getWhatToShow(); 1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** @return the next node in the set and advance the position of the 1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * iterator in the set. 1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws DOMException - INVALID_STATE_ERR Raised if this method is 1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * called after the detach method was invoked. 1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public Node nextNode() throws DOMException 1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if(!valid) 1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new DTMDOMException(DOMException.INVALID_STATE_ERR); 1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int handle=dtm_iter.nextNode(); 1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (handle==DTM.NULL) 1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return null; 1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return dtm_iter.getDTM(handle).getNode(handle); 1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** @return the next previous in the set and advance the position of the 1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * iterator in the set. 1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws DOMException - INVALID_STATE_ERR Raised if this method is 1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * called after the detach method was invoked. 1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */ 1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public Node previousNode() 1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if(!valid) 1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new DTMDOMException(DOMException.INVALID_STATE_ERR); 1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int handle=dtm_iter.previousNode(); 1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (handle==DTM.NULL) 1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return null; 1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return dtm_iter.getDTM(handle).getNode(handle); 1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} 187