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 23 24 public class Error extends SystemFunction { 25 26 29 30 public Expression simplify(StaticContext env) throws StaticError { 31 return this; 32 } 33 34 37 38 public Expression preEvaluate(StaticContext env) { 39 return this; 40 } 41 42 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 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 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 | Popular Tags |