001: package net.sf.saxon.functions;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.expr.ExpressionLocation;
005: import net.sf.saxon.expr.StaticContext;
006: import net.sf.saxon.expr.XPathContext;
007: import net.sf.saxon.om.Item;
008: import net.sf.saxon.om.NamespaceConstant;
009: import net.sf.saxon.om.NodeInfo;
010: import net.sf.saxon.sxpath.XPathEvaluator;
011: import net.sf.saxon.sxpath.XPathExpression;
012: import net.sf.saxon.trans.DynamicError;
013: import net.sf.saxon.trans.StaticError;
014: import net.sf.saxon.trans.XPathException;
015: import net.sf.saxon.type.Type;
016: import net.sf.saxon.value.QNameValue;
017: import net.sf.saxon.value.SequenceExtent;
018: import net.sf.saxon.value.SingletonNode;
019: import net.sf.saxon.value.Value;
020:
021: /**
022: * Implement XPath function fn:error()
023: */
024:
025: public class Error extends SystemFunction {
026:
027: /**
028: * Simplify and validate.
029: */
030:
031: public Expression simplify(StaticContext env) throws StaticError {
032: return this ;
033: }
034:
035: /**
036: * preEvaluate: this method suppresses compile-time evaluation by doing nothing
037: */
038:
039: public Expression preEvaluate(StaticContext env) {
040: return this ;
041: }
042:
043: /**
044: * Evaluation of the expression always throws an error
045: */
046:
047: public Item evaluateItem(XPathContext context)
048: throws XPathException {
049: QNameValue qname = null;
050: if (argument.length > 0) {
051: qname = (QNameValue) argument[0].evaluateItem(context);
052: }
053: if (qname == null) {
054: qname = new QNameValue("err", NamespaceConstant.ERR,
055: (argument.length == 1 ? "FOTY0004" : "FOER0000"),
056: context.getConfiguration().getNameChecker());
057: }
058: String description = null;
059: if (argument.length > 1) {
060: description = argument[1].evaluateItem(context)
061: .getStringValue();
062: } else {
063: description = "Error signalled by application call on error()";
064: }
065: DynamicError e = new DynamicError(description);
066: e.setErrorCode(qname.getNamespaceURI(), qname.getLocalName());
067: e.setXPathContext(context);
068: if (argument.length > 2) {
069: Value errorObject = SequenceExtent.makeSequenceExtent(
070: argument[2].iterate(context)).reduce();
071: if (errorObject instanceof SingletonNode) {
072: NodeInfo root = ((SingletonNode) errorObject).getNode();
073: if (root.getNodeKind() == Type.DOCUMENT) {
074: XPathEvaluator xpath = new XPathEvaluator();
075: XPathExpression exp = xpath
076: .createExpression("/error/@module");
077: NodeInfo moduleAtt = (NodeInfo) exp
078: .evaluateSingle(root);
079: String module = (moduleAtt == null ? null
080: : moduleAtt.getStringValue());
081: exp = xpath.createExpression("/error/@line");
082: NodeInfo lineAtt = (NodeInfo) exp
083: .evaluateSingle(root);
084: int line = (lineAtt == null ? -1 : Integer
085: .parseInt(lineAtt.getStringValue()));
086: exp = xpath.createExpression("/error/@column");
087: NodeInfo columnAtt = (NodeInfo) exp
088: .evaluateSingle(root);
089: int column = (columnAtt == null ? -1 : Integer
090: .parseInt(lineAtt.getStringValue()));
091: ExpressionLocation locator = new ExpressionLocation();
092: locator.setSystemId(module);
093: locator.setLineNumber(line);
094: locator.setColumnNumber(column);
095: e.setLocator(locator);
096: }
097: }
098: e.setErrorObject(errorObject);
099: }
100: throw e;
101: }
102:
103: }
104:
105: //
106: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
107: // you may not use this file except in compliance with the License. You may obtain a copy of the
108: // License at http://www.mozilla.org/MPL/
109: //
110: // Software distributed under the License is distributed on an "AS IS" basis,
111: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
112: // See the License for the specific language governing rights and limitations under the License.
113: //
114: // The Original Code is: all this file.
115: //
116: // The Initial Developer of the Original Code is Michael H. Kay.
117: //
118: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
119: //
120: // Contributor(s): none.
121: //
|