01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.Expression;
04: import net.sf.saxon.expr.StaticContext;
05: import net.sf.saxon.expr.XPathContext;
06: import net.sf.saxon.om.DocumentInfo;
07: import net.sf.saxon.om.Item;
08: import net.sf.saxon.om.NodeInfo;
09: import net.sf.saxon.trans.XPathException;
10: import net.sf.saxon.value.StringValue;
11: import net.sf.saxon.type.Type;
12:
13: /**
14: * Implements the unparsed-entity-uri() function defined in XSLT 1.0
15: * and the unparsed-entity-public-id() function defined in XSLT 2.0
16: */
17:
18: public class UnparsedEntity extends SystemFunction implements
19: XSLTFunction {
20:
21: public static int URI = 0;
22: public static int PUBLIC_ID = 1;
23:
24: /**
25: * Simplify: add a second implicit argument, the context document
26: */
27:
28: public Expression simplify(StaticContext env) throws XPathException {
29: UnparsedEntity f = (UnparsedEntity) super .simplify(env);
30: f.addContextDocumentArgument(1,
31: (operation == URI ? "unparsed-entity-uri_9999_"
32: : "unparsed-entity-public-id_9999_"));
33: return f;
34: }
35:
36: /**
37: * preEvaluate: this method suppresses compile-time evaluation by doing nothing
38: */
39:
40: public Expression preEvaluate(StaticContext env) {
41: return this ;
42: }
43:
44: /**
45: * Evaluate the expression
46: */
47:
48: public Item evaluateItem(XPathContext context)
49: throws XPathException {
50: String arg0 = argument[0].evaluateItem(context)
51: .getStringValue();
52: NodeInfo doc = (NodeInfo) argument[1].evaluateItem(context);
53: if (doc.getNodeKind() != Type.DOCUMENT) {
54: String code = (operation == URI ? "XTDE1370" : "XTDE1380");
55: dynamicError(
56: "In function "
57: + getDisplayName(context.getNamePool())
58: + ", the context node must be in a tree whose root is a document node",
59: code, context);
60: }
61: String[] ids = ((DocumentInfo) doc).getUnparsedEntity(arg0);
62: if (ids == null)
63: return StringValue.EMPTY_STRING;
64: return new StringValue(ids[operation]);
65: }
66:
67: }
68:
69: //
70: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
71: // you may not use this file except in compliance with the License. You may obtain a copy of the
72: // License at http://www.mozilla.org/MPL/
73: //
74: // Software distributed under the License is distributed on an "AS IS" basis,
75: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
76: // See the License for the specific language governing rights and limitations under the License.
77: //
78: // The Original Code is: all this file.
79: //
80: // The Initial Developer of the Original Code is Michael H. Kay.
81: //
82: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
83: //
84: // Contributor(s): none.
85: //
|