KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Error


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.ExpressionLocation;
4 import net.sf.saxon.expr.StaticContext;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.om.Item;
7 import net.sf.saxon.om.NamespaceConstant;
8 import net.sf.saxon.om.NodeInfo;
9 import net.sf.saxon.sxpath.XPathEvaluator;
10 import net.sf.saxon.sxpath.XPathExpression;
11 import net.sf.saxon.trans.DynamicError;
12 import net.sf.saxon.trans.StaticError;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.Type;
15 import net.sf.saxon.value.QNameValue;
16 import net.sf.saxon.value.SequenceExtent;
17 import net.sf.saxon.value.SingletonNode;
18 import net.sf.saxon.value.Value;
19
20 /**
21 * Implement XPath function fn:error()
22 */

23
24 public class Error extends SystemFunction {
25
26     /**
27     * Simplify and validate.
28     */

29
30      public Expression simplify(StaticContext env) throws StaticError {
31         return this;
32     }
33
34     /**
35     * preEvaluate: this method suppresses compile-time evaluation by doing nothing
36     */

37
38     public Expression preEvaluate(StaticContext env) {
39         return this;
40     }
41
42     /**
43     * Evaluation of the expression always throws an error
44     */

45
46     public Item evaluateItem(XPathContext context) throws XPathException {
47         QNameValue qname = null;
48         if (argument.length > 0) {
49             qname = (QNameValue)argument[0].evaluateItem(context);
50         }
51         if (qname == null) {
52             qname = new QNameValue("err", NamespaceConstant.ERR, "FOER0000");
53         }
54         String JavaDoc description = null;
55         if (argument.length > 1) {
56             description = argument[1].evaluateItem(context).getStringValue();
57         } else {
58             description = "Error signalled by application call on error()";
59         }
60         DynamicError e = new DynamicError(description);
61         e.setErrorCode(qname.getNamespaceURI(), qname.getLocalName());
62         e.setXPathContext(context);
63         if (argument.length > 2) {
64             Value errorObject = SequenceExtent.makeSequenceExtent(argument[2].iterate(context)).reduce();
65             if (errorObject instanceof SingletonNode) {
66                 NodeInfo root = ((SingletonNode)errorObject).getNode();
67                 if (root.getNodeKind() == Type.DOCUMENT) {
68                     XPathEvaluator xpath = new XPathEvaluator();
69                     XPathExpression exp = xpath.createExpression("/error/@module");
70                     NodeInfo moduleAtt = (NodeInfo)exp.evaluateSingle(root);
71                     String JavaDoc module = (moduleAtt == null ? null : moduleAtt.getStringValue());
72                     exp = xpath.createExpression("/error/@line");
73                     NodeInfo lineAtt = (NodeInfo)exp.evaluateSingle(root);
74                     int line = (lineAtt == null ? -1 : Integer.parseInt(lineAtt.getStringValue()));
75                     exp = xpath.createExpression("/error/@column");
76                     NodeInfo columnAtt = (NodeInfo)exp.evaluateSingle(root);
77                     int column = (columnAtt == null ? -1 : Integer.parseInt(lineAtt.getStringValue()));
78                     ExpressionLocation locator = new ExpressionLocation();
79                     locator.setSystemId(module);
80                     locator.setLineNumber(line);
81                     locator.setColumnNumber(column);
82                     e.setLocator(locator);
83                 }
84             }
85             e.setErrorObject(errorObject);
86         }
87         throw e;
88     }
89
90 }
91
92
93 //
94
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
95
// you may not use this file except in compliance with the License. You may obtain a copy of the
96
// License at http://www.mozilla.org/MPL/
97
//
98
// Software distributed under the License is distributed on an "AS IS" basis,
99
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
100
// See the License for the specific language governing rights and limitations under the License.
101
//
102
// The Original Code is: all this file.
103
//
104
// The Initial Developer of the Original Code is Michael H. Kay.
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
110
Popular Tags