| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public BooleanValue call(XPathContext context, Sequence[] arguments ) throws XPathException { |
| 69 |
String lexicalQName = arguments[0].head().getStringValue(); |
| 70 |
return BooleanValue.get(typeAvailable(lexicalQName, context.getConfiguration())); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 92 |
} |
| 93 |
return super.makeFunctionCall(arguments); |
| 94 |
} |
| 95 |
} |
| 96 |
|