Debian Codegraph @

net/sf/saxon/functions/TypeAvailable.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.Configuration;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.om.Sequence;
16 import net.sf.saxon.om.StructuredQName;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.JavaExternalObjectType;
19 import net.sf.saxon.type.SchemaType;
20 import net.sf.saxon.value.BooleanValue;
21
22 /**
23 * This class supports the XSLT fn:type-available() function.
24 */
25
26 public class TypeAvailable extends SystemFunction {
27
28 private boolean typeAvailable(String lexicalName, Configuration config) throws XPathException {
29 StructuredQName qName;
30 try {
31 if (lexicalName.indexOf(':') < 0 && !lexicalName.startsWith("Q{")) {
32 String uri = getRetainedStaticContext().getURIForPrefix("", true);
33 qName = new StructuredQName("", uri, lexicalName);
34 } else {
35 qName = StructuredQName.fromLexicalQName(lexicalName,
36 false, true,
37 getRetainedStaticContext());
38 }
39 } catch (XPathException e) {
40 e.setErrorCode("XTDE1428");
41 throw e;
42 }
43
44 String uri = qName.getURI();
45 if (uri.equals(NamespaceConstant.JAVA_TYPE)) {
46 try {
47 String className = JavaExternalObjectType.localNameToClassName(qName.getLocalPart());
48 config.getClass(className, false, null);
49 return true;
50 } catch (XPathException err) {
51 return false;
52 }
53 } else {
54 SchemaType type = config.getSchemaType(qName);
55 return type != null;
56 }
57 }
58
59 /**
60 * Evaluate the expression
61 *
62 * @param context the dynamic evaluation context
63 * @param arguments the values of the arguments, supplied as SequenceIterators
64 * @return the result of the evaluation, in the form of a SequenceIterator
65 * @throws net.sf.saxon.trans.XPathException
66 * if a dynamic error occurs during the evaluation of the expression
67 */
68 public BooleanValue call(XPathContext context, Sequence[] arguments /*@NotNull*/) throws XPathException {
69 String lexicalQName = arguments[0].head().getStringValue();
70 return BooleanValue.get(typeAvailable(lexicalQName, context.getConfiguration()));
71 }
72
73 /**
74 * Make an expression that either calls this function, or that is equivalent to a call
75 * on this function
76 *
77 * @param arguments the supplied arguments to the function call
78 * @return either a function call on this function, or an expression that delivers
79 * the same result
80 */
81 @Override
82 public Expression makeFunctionCall(Expression[] arguments) {
83 try {
84 if (arguments[0] instanceof Literal) {
85 boolean b = typeAvailable(
86 ((Literal)arguments[0]).getValue().getStringValue(),
87 getRetainedStaticContext().getConfiguration());
88 return Literal.makeLiteral(BooleanValue.get(b));
89 }
90 } catch (XPathException e) {
91 // fall through
92 }
93 return super.makeFunctionCall(arguments);
94 }
95 }
96