01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.XPathContext;
04: import net.sf.saxon.om.*;
05: import net.sf.saxon.trans.XPathException;
06: import net.sf.saxon.value.AtomicValue;
07: import net.sf.saxon.value.QNameValue;
08:
09: /**
10: * This class supports the resolve-QName function in XPath 2.0
11: */
12:
13: public class ResolveQName extends SystemFunction {
14:
15: /**
16: * Evaluate the expression
17: */
18:
19: public Item evaluateItem(XPathContext context)
20: throws XPathException {
21: AtomicValue arg0 = (AtomicValue) argument[0]
22: .evaluateItem(context);
23: if (arg0 == null) {
24: return null;
25: }
26:
27: CharSequence qname = arg0.getStringValueCS();
28: String[] parts;
29: final NameChecker checker = context.getConfiguration()
30: .getNameChecker();
31: try {
32:
33: parts = checker.getQNameParts(qname);
34: } catch (QNameException err) {
35: dynamicError(err.getMessage(), "FOCA0002", context);
36: return null;
37: }
38:
39: NodeInfo element = (NodeInfo) argument[1].evaluateItem(context);
40: SequenceIterator nsNodes = element.iterateAxis(Axis.NAMESPACE);
41:
42: while (true) {
43: NodeInfo namespace = (NodeInfo) nsNodes.next();
44: if (namespace == null) {
45: break;
46: }
47: String prefix = namespace.getLocalPart();
48: if (prefix.equals(parts[0])) {
49: return new QNameValue(prefix, namespace
50: .getStringValue(), parts[1], checker);
51: }
52: }
53:
54: if (parts[0].equals("")) {
55: return new QNameValue("", null, parts[1], checker);
56: }
57:
58: dynamicError("Namespace prefix '" + parts[0]
59: + "' is not in scope for the selected element",
60: "FONS0004", context);
61: return null;
62: }
63:
64: }
65:
66: //
67: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
68: // you may not use this file except in compliance with the License. You may obtain a copy of the
69: // License at http://www.mozilla.org/MPL/
70: //
71: // Software distributed under the License is distributed on an "AS IS" basis,
72: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
73: // See the License for the specific language governing rights and limitations under the License.
74: //
75: // The Original Code is: all this file.
76: //
77: // The Initial Developer of the Original Code is Michael H. Kay
78: //
79: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
80: //
81: // Contributor(s): none.
82: //
|