1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.platform.dom.traversal;
16
17 import org.htmlunit.html.DomNode;
18 import org.w3c.dom.DOMException;
19 import org.w3c.dom.Node;
20 import org.w3c.dom.traversal.NodeFilter;
21 import org.w3c.dom.traversal.TreeWalker;
22
23 /**
24 * An implementation of {@link TreeWalker} backed by {@link org.htmlunit.html.HtmlDomTreeWalker}.
25 *
26 * @see <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html">
27 * DOM-Level-2-Traversal-Range</a>
28 * @author Ronald Brill
29 */
30 public class DomTreeWalker implements TreeWalker {
31
32 private final org.htmlunit.html.HtmlDomTreeWalker domTreeWalker_;
33
34 /**
35 * Creates an instance.
36 *
37 * @param root The root node of the TreeWalker. Must not be
38 * {@code null}.
39 * @param whatToShow Flag specifying which types of nodes appear in the
40 * logical view of the TreeWalker. See {@link NodeFilter} for the
41 * set of possible Show_ values.
42 * @param filter The {@link NodeFilter} to be used with this TreeWalker,
43 * or {@code null} to indicate no filter.
44 * @param expandEntityReferences If false, the contents of
45 * EntityReference nodes are not present in the logical view.
46 * @throws DOMException on attempt to create a TreeWalker with a root that
47 * is {@code null}.
48 */
49
50 public DomTreeWalker(final DomNode root, final int whatToShow, final NodeFilter filter,
51 final boolean expandEntityReferences) throws DOMException {
52 domTreeWalker_ = new org.htmlunit.html.HtmlDomTreeWalker(
53 root, whatToShow, filter, expandEntityReferences);
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 @Override
60 public DomNode getRoot() {
61 return domTreeWalker_.getRoot();
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 @Override
68 public int getWhatToShow() {
69 return domTreeWalker_.getWhatToShow();
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 public NodeFilter getFilter() {
77 return domTreeWalker_.getFilter();
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 @Override
84 public boolean getExpandEntityReferences() {
85 return domTreeWalker_.getExpandEntityReferences();
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public DomNode getCurrentNode() {
93 return domTreeWalker_.getCurrentNode();
94 }
95
96 /**
97 * {@inheritDoc}
98 */
99 @Override
100 public void setCurrentNode(final Node currentNode) throws DOMException {
101 domTreeWalker_.setCurrentNode(currentNode);
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 public DomNode nextNode() {
109 return domTreeWalker_.nextNode();
110 }
111
112 /**
113 * {@inheritDoc}
114 */
115 @Override
116 public DomNode nextSibling() {
117 return domTreeWalker_.nextSibling();
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public DomNode parentNode() {
125 return domTreeWalker_.parentNode();
126 }
127
128 /**
129 * {@inheritDoc}
130 */
131 @Override
132 public DomNode previousSibling() {
133 return domTreeWalker_.previousSibling();
134 }
135
136 /**
137 * {@inheritDoc}
138 */
139 @Override
140 public DomNode lastChild() {
141 return domTreeWalker_.lastChild();
142 }
143
144 /**
145 * {@inheritDoc}
146 */
147 @Override
148 public DomNode previousNode() {
149 return domTreeWalker_.previousNode();
150 }
151
152 /**
153 * {@inheritDoc}
154 */
155 @Override
156 public DomNode firstChild() {
157 return domTreeWalker_.firstChild();
158 }
159 }