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;
16
17 import org.htmlunit.corejs.javascript.Context;
18 import org.htmlunit.corejs.javascript.NativeFunction;
19 import org.htmlunit.corejs.javascript.Scriptable;
20 import org.htmlunit.xpath.xml.utils.PrefixResolver;
21
22 /**
23 * A special {@link PrefixResolver} for {@link NativeFunction}s.
24 *
25 * @author Chuck Dumont
26 */
27 public class NativeFunctionPrefixResolver implements PrefixResolver {
28
29 private final NativeFunction resolverFn_;
30 private final Scriptable scope_;
31
32 /**
33 * Constructor.
34 *
35 * @param resolverFn the {@link NativeFunction} this resolver is for
36 * @param scope the scope
37 */
38 public NativeFunctionPrefixResolver(final NativeFunction resolverFn, final Scriptable scope) {
39 resolverFn_ = resolverFn;
40 scope_ = scope;
41 }
42
43 /**
44 * {@inheritDoc}
45 */
46 @Override
47 public String getNamespaceForPrefix(final String prefix) {
48 final Object result = Context.call(null, resolverFn_, scope_, null, new Object[]{prefix});
49 return result == null ? null : result.toString();
50 }
51
52 /**
53 * {@inheritDoc}
54 */
55 @Override
56 public String getNamespaceForPrefix(final String prefix, final org.w3c.dom.Node node) {
57 throw new UnsupportedOperationException();
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 @Override
64 public boolean handlesNullPrefixes() {
65 return false;
66 }
67 }