01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.XPathContext;
04: import net.sf.saxon.expr.Expression;
05: import net.sf.saxon.expr.StaticContext;
06: import net.sf.saxon.om.Item;
07: import net.sf.saxon.om.QNameException;
08: import net.sf.saxon.om.NameChecker;
09: import net.sf.saxon.trans.XPathException;
10: import net.sf.saxon.trans.DynamicError;
11: import net.sf.saxon.value.AtomicValue;
12: import net.sf.saxon.value.QNameValue;
13:
14: /**
15: * This class supports the fn:QName() function (previously named fn:expanded-QName())
16: */
17:
18: public class QNameFn extends SystemFunction {
19:
20: /**
21: * Pre-evaluate a function at compile time. Functions that do not allow
22: * pre-evaluation, or that need access to context information, can override this method.
23: */
24:
25: public Expression preEvaluate(StaticContext env)
26: throws XPathException {
27: try {
28: final Item item1 = argument[1].evaluateItem(env
29: .makeEarlyEvaluationContext());
30: final String lex = item1.getStringValue();
31: final Item item0 = argument[0].evaluateItem(env
32: .makeEarlyEvaluationContext());
33: String uri;
34: if (item0 == null) {
35: uri = "";
36: } else {
37: uri = item0.getStringValue();
38: }
39: final NameChecker checker = env.getConfiguration()
40: .getNameChecker();
41: final String[] parts = checker.getQNameParts(lex);
42: return new QNameValue(parts[0], uri, parts[1], checker);
43: } catch (QNameException e) {
44: DynamicError err = new DynamicError(e.getMessage(), this );
45: err.setErrorCode("FOCA0002");
46: err.setLocator(this );
47: throw err;
48: }
49: }
50:
51: /**
52: * Evaluate the expression
53: */
54:
55: public Item evaluateItem(XPathContext context)
56: throws XPathException {
57: AtomicValue arg0 = (AtomicValue) argument[0]
58: .evaluateItem(context);
59:
60: String uri;
61: if (arg0 == null) {
62: uri = null;
63: } else {
64: uri = arg0.getStringValue();
65: }
66:
67: try {
68: final String lex = argument[1].evaluateItem(context)
69: .getStringValue();
70: final NameChecker checker = context.getConfiguration()
71: .getNameChecker();
72: final String[] parts = checker.getQNameParts(lex);
73: return new QNameValue(parts[0], uri, parts[1], checker);
74: } catch (QNameException e) {
75: dynamicError(e.getMessage(), "FOCA0002", context);
76: return null;
77: }
78: }
79:
80: }
81:
82: //
83: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
84: // you may not use this file except in compliance with the License. You may obtain a copy of the
85: // License at http://www.mozilla.org/MPL/
86: //
87: // Software distributed under the License is distributed on an "AS IS" basis,
88: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
89: // See the License for the specific language governing rights and limitations under the License.
90: //
91: // The Original Code is: all this file.
92: //
93: // The Initial Developer of the Original Code is Michael H. Kay
94: //
95: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
96: //
97: // Contributor(s): none.
98: //
|