Debian Codegraph @

net/sf/saxon/functions/UnparsedEntity.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.Callable;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.Type;
17 import net.sf.saxon.value.AnyURIValue;
18 import net.sf.saxon.value.StringValue;
19
20 /**
21 * Implements the unparsed-entity-uri() function defined in XSLT 1.0
22 * and the unparsed-entity-public-id() function defined in XSLT 2.0
23 */
24
25
26 public abstract class UnparsedEntity extends SystemFunction implements Callable {
27
28 public static int URI = 0;
29 public static int PUBLIC_ID = 1;
30
31 public abstract int getOp();
32
33 /**
34 * Evaluate the expression
35 *
36 * @param context the dynamic evaluation context
37 * @param arguments the values of the arguments, supplied as SequenceIterators
38 * @return the result of the evaluation, in the form of a SequenceIterator
39 * @throws net.sf.saxon.trans.XPathException
40 * if a dynamic error occurs during the evaluation of the expression
41 */
42 public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
43 int operation = getOp();
44 String arg0 = arguments[0].head().getStringValue();
45 NodeInfo doc = null;
46 if (getArity() == 1) {
47 Item it = context.getContextItem();
48 if (it instanceof NodeInfo) {
49 doc = ((NodeInfo)it).getRoot();
50 }
51 if (doc == null || doc.getNodeKind() != Type.DOCUMENT) {
52 String code = operation == URI ? "XTDE1370" : "XTDE1380";
53 throw new XPathException("In function " + getFunctionName().getDisplayName() +
54 ", the context item must be a node in a tree whose root is a document node", code, context);
55 }
56 } else {
57 doc = (NodeInfo) arguments[1].head();
58 if (doc != null) {
59 doc = doc.getRoot();
60 }
61 if (doc == null || doc.getNodeKind() != Type.DOCUMENT) {
62 String code = operation == URI ? "XTDE1370" : "XTDE1380";
63 throw new XPathException("In function " + getFunctionName().getDisplayName() +
64 ", the second argument must be a document node", code, context);
65 }
66 }
67 String[] ids = doc.getTreeInfo().getUnparsedEntity(arg0);
68 String result = ids == null ? "" : ids[operation];
69 return operation == URI ? new AnyURIValue(result) : new StringValue(result);
70 }
71
72 public static class UnparsedEntityUri extends UnparsedEntity {
73 public int getOp() {
74 return URI;
75 }
76 }
77
78 public static class UnparsedEntityPublicId extends UnparsedEntity {
79 public int getOp() {
80 return PUBLIC_ID;
81 }
82 }
83 }
84