Debian Codegraph @

net/sf/saxon/functions/Error.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.expr.parser.ExplicitLocation;
12 import net.sf.saxon.lib.NamespaceConstant;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.pattern.NameTest;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.AxisIterator;
17 import net.sf.saxon.type.BuiltInAtomicType;
18 import net.sf.saxon.type.Type;
19 import net.sf.saxon.value.QNameValue;
20 import net.sf.saxon.om.ZeroOrOne;
21 import net.sf.saxon.value.StringValue;
22
23 /**
24 * Implement XPath function fn:error()
25 */
26
27 public class Error extends SystemFunction implements Callable {
28
29 @Override
30 public int getSpecialProperties(Expression[] arguments) {
31 return super.getSpecialProperties(arguments) & ~StaticProperty.NO_NODES_NEWLY_CREATED;
32 }
33
34 /**
35 * Determine whether this is a vacuous expression as defined in the XQuery update specification
36 *
37 * @return true if this expression is vacuous
38 */
39
40 public boolean isVacuousExpression() {
41 return true;
42 }
43
44 // /**
45 // * Evaluation of the expression always throws an error
46 // */
47 //
48 // /*@Nullable*/
49 // public Item evaluateItem(XPathContext context) throws XPathException {
50 // int len = getArity();
51 //
52 // switch (len) {
53 // case 0:
54 // return error(context, null, null, null);
55 // case 1:
56 // // Note: error#1 does not allow the first argument to be an empty sequence. So we take
57 // // care to raise XPTY0004 in this case. But we still report a generic error message, rather
58 // // than complaining specifically about the missing error code
59 // QNameValue arg0 = (QNameValue) getArg(0).evaluateItem(context);
60 // if (arg0 == null) {
61 // arg0 = new QNameValue("err", NamespaceConstant.ERR, "XPTY0004");
62 // }
63 // return error(context, arg0, null, null);
64 // case 2:
65 // return error(context, (QNameValue) getArg(0).evaluateItem(context), (StringValue) getArg(1).evaluateItem(context), null);
66 // case 3:
67 // return error(context, (QNameValue) getArg(0).evaluateItem(context), (StringValue) getArg(1).evaluateItem(context), getArg(2).iterate(context));
68 // default:
69 // return null;
70 // }
71 // }
72 //
73
74 // /**
75 // * Evaluate an updating expression, adding the results to a Pending Update List.
76 // * The default implementation of this method, which is used for non-updating expressions,
77 // * throws an UnsupportedOperationException
78 // *
79 // * @param context the XPath dynamic evaluation context
80 // * @param pul the pending update list to which the results should be written
81 // */
82 //
83 // public void evaluatePendingUpdates(XPathContext context, PendingUpdateList pul) throws XPathException {
84 // evaluateItem(context);
85 // }
86
87 public Item error(
88 XPathContext context,
89 /*@Nullable*/ QNameValue errorCode,
90 /*@Nullable*/ StringValue desc,
91 /*@Nullable*/ SequenceIterator errObject) throws XPathException {
92 QNameValue qname = null;
93 if (getArity() > 0) {
94 qname = errorCode;
95 }
96 if (qname == null) {
97 qname = new QNameValue("err", NamespaceConstant.ERR,
98 getArity() == 1 ? "FOTY0004" : "FOER0000",
99 BuiltInAtomicType.QNAME, false);
100 }
101 String description;
102 if (getArity() > 1) {
103 description = desc == null ? "" : desc.getStringValue();
104 } else {
105 description = "Error signalled by application call on error()";
106 }
107 XPathException e = new XPathException(description);
108 e.setErrorCodeQName(qname.getStructuredQName());
109 e.setXPathContext(context);
110 if (getArity() > 2 && errObject != null) {
111 Sequence<?> errorObject = errObject.materialize();
112 if (errorObject instanceof ZeroOrOne) {
113 Item root = ((ZeroOrOne) errorObject).head();
114 if ((root instanceof NodeInfo) && ((NodeInfo) root).getNodeKind() == Type.DOCUMENT) {
115 AxisIterator iter = ((NodeInfo) root).iterateAxis(AxisInfo.CHILD,
116 new NameTest(Type.ELEMENT, "", "error", context.getConfiguration().getNamePool()));
117 NodeInfo errorElement = iter.next();
118 if (errorElement != null) {
119 String module = errorElement.getAttributeValue("", "module");
120 String lineVal = errorElement.getAttributeValue("", "line");
121 int line = lineVal == null ? -1 : Integer.parseInt(lineVal);
122 String columnVal = errorElement.getAttributeValue("", "column");
123 int col = columnVal == null ? -1 : Integer.parseInt(columnVal);
124 ExplicitLocation locator = new ExplicitLocation(module, line, col);
125 e.setLocator(locator);
126 }
127 }
128 }
129 e.setErrorObject(errorObject);
130 }
131 throw e;
132 }
133
134
135 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
136 int len = arguments.length;
137
138 switch (len) {
139 case 0:
140 return error(context, null, null, null);
141 case 1:
142 // Note: in XPath 3.1 first arg may be an empty sequence, and then error code is FOER0000.
143 // Previously in XPath 3.0 error#1 does not allow the first argument to be an empty sequence. So we took
144 // care to raise XPTY0004 in this case. But we still report a generic error message, rather
145 // than complaining specifically about the missing error code
146 QNameValue arg0 = (QNameValue)arguments[0].head();
147 if (arg0 == null) {
148 arg0 = new QNameValue("err", NamespaceConstant.ERR, "FOER0000");
149 }
150 return error(context, arg0, null, null);
151 case 2:
152 return error(context, (QNameValue) arguments[0].head(), (StringValue) arguments[1].head(), null);
153 case 3:
154 return error(context, (QNameValue) arguments[0].head(), (StringValue) arguments[1].head(), arguments[2].iterate());
155 default:
156 return null;
157 }
158 }
159 }
160