View Javadoc
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.javascript.host.dom;
16  
17  import org.htmlunit.html.DomElement;
18  import org.htmlunit.javascript.HtmlUnitScriptable;
19  import org.htmlunit.javascript.configuration.JsxClass;
20  import org.htmlunit.javascript.configuration.JsxFunction;
21  import org.htmlunit.util.XmlUtils;
22  import org.htmlunit.xpath.xml.utils.PrefixResolver;
23  
24  /**
25   * A JavaScript object for {@code XPathNSResolver}.
26   *
27   * @author Ahmed Ashour
28   * @author Chuck Dumont
29   * @author Ronald Brill
30   */
31  @JsxClass(className = "NativeXPathNSResolver", isJSObject = false)
32  public class XPathNSResolver extends HtmlUnitScriptable implements PrefixResolver {
33  
34      private Node element_;
35  
36      /**
37       * Sets the element to start lookup from.
38       * @param element {@link org.htmlunit.javascript.host.html.HTMLElement}
39       *        or {@link org.htmlunit.javascript.host.Element} to start lookup from
40       */
41      public void setElement(final Node element) {
42          element_ = element;
43      }
44  
45      /**
46       * Look up the namespace URI associated to the given namespace prefix.
47       * @param prefix the prefix to look for
48       * @return the associated namespace URI or null if none is found
49       */
50      @JsxFunction
51      public String lookupNamespaceURI(final String prefix) {
52          return XmlUtils.lookupNamespaceURI((DomElement) element_.getDomNodeOrDie(), prefix);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public String getNamespaceForPrefix(final String prefix) {
60          return lookupNamespaceURI(prefix);
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public String getNamespaceForPrefix(final String prefix, final org.w3c.dom.Node context) {
68          throw new UnsupportedOperationException();
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public boolean handlesNullPrefixes() {
76          return false;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public Object getDefaultValue(final Class<?> hint) {
84          return element_.getDefaultValue(hint);
85      }
86  }