| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.StaticProperty; |
| 12 |
import net.sf.saxon.expr.StringLiteral; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.lib.Feature; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.om.NameChecker; |
| 17 |
import net.sf.saxon.om.Sequence; |
| 18 |
import net.sf.saxon.om.StandardNames; |
| 19 |
import net.sf.saxon.om.StructuredQName; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.value.BooleanValue; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class ElementAvailable extends SystemFunction { |
| 30 |
|
| 31 |
public static boolean isXslt20Instruction(int fp) { |
| 32 |
switch (fp) { |
| 33 |
case StandardNames.XSL_ANALYZE_STRING: |
| 34 |
case StandardNames.XSL_APPLY_IMPORTS: |
| 35 |
case StandardNames.XSL_APPLY_TEMPLATES: |
| 36 |
case StandardNames.XSL_ATTRIBUTE: |
| 37 |
case StandardNames.XSL_CALL_TEMPLATE: |
| 38 |
case StandardNames.XSL_CHOOSE: |
| 39 |
case StandardNames.XSL_COMMENT: |
| 40 |
case StandardNames.XSL_COPY: |
| 41 |
case StandardNames.XSL_COPY_OF: |
| 42 |
case StandardNames.XSL_DOCUMENT: |
| 43 |
case StandardNames.XSL_ELEMENT: |
| 44 |
case StandardNames.XSL_FALLBACK: |
| 45 |
case StandardNames.XSL_FOR_EACH: |
| 46 |
case StandardNames.XSL_FOR_EACH_GROUP: |
| 47 |
case StandardNames.XSL_IF: |
| 48 |
case StandardNames.XSL_MESSAGE: |
| 49 |
case StandardNames.XSL_NAMESPACE: |
| 50 |
case StandardNames.XSL_NEXT_MATCH: |
| 51 |
case StandardNames.XSL_PERFORM_SORT: |
| 52 |
case StandardNames.XSL_PROCESSING_INSTRUCTION: |
| 53 |
case StandardNames.XSL_RESULT_DOCUMENT: |
| 54 |
case StandardNames.XSL_SEQUENCE: |
| 55 |
case StandardNames.XSL_TEXT: |
| 56 |
case StandardNames.XSL_VALUE_OF: |
| 57 |
case StandardNames.XSL_VARIABLE: |
| 58 |
return true; |
| 59 |
default: |
| 60 |
return false; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public static boolean isXslt30Element(int fp) { |
| 65 |
switch (fp) { |
| 66 |
case StandardNames.XSL_ACCEPT: |
| 67 |
case StandardNames.XSL_ACCUMULATOR: |
| 68 |
case StandardNames.XSL_ACCUMULATOR_RULE: |
| 69 |
case StandardNames.XSL_ANALYZE_STRING: |
| 70 |
case StandardNames.XSL_APPLY_IMPORTS: |
| 71 |
case StandardNames.XSL_APPLY_TEMPLATES: |
| 72 |
case StandardNames.XSL_ASSERT: |
| 73 |
case StandardNames.XSL_ATTRIBUTE: |
| 74 |
case StandardNames.XSL_ATTRIBUTE_SET: |
| 75 |
case StandardNames.XSL_BREAK: |
| 76 |
case StandardNames.XSL_CALL_TEMPLATE: |
| 77 |
case StandardNames.XSL_CATCH: |
| 78 |
case StandardNames.XSL_CHARACTER_MAP: |
| 79 |
case StandardNames.XSL_CHOOSE: |
| 80 |
case StandardNames.XSL_COMMENT: |
| 81 |
case StandardNames.XSL_CONTEXT_ITEM: |
| 82 |
case StandardNames.XSL_COPY: |
| 83 |
case StandardNames.XSL_COPY_OF: |
| 84 |
case StandardNames.XSL_DECIMAL_FORMAT: |
| 85 |
case StandardNames.XSL_DOCUMENT: |
| 86 |
case StandardNames.XSL_ELEMENT: |
| 87 |
case StandardNames.XSL_EVALUATE: |
| 88 |
case StandardNames.XSL_EXPOSE: |
| 89 |
case StandardNames.XSL_FALLBACK: |
| 90 |
case StandardNames.XSL_FOR_EACH: |
| 91 |
case StandardNames.XSL_FOR_EACH_GROUP: |
| 92 |
case StandardNames.XSL_FORK: |
| 93 |
case StandardNames.XSL_FUNCTION: |
| 94 |
case StandardNames.XSL_GLOBAL_CONTEXT_ITEM: |
| 95 |
case StandardNames.XSL_IF: |
| 96 |
case StandardNames.XSL_IMPORT: |
| 97 |
case StandardNames.XSL_IMPORT_SCHEMA: |
| 98 |
case StandardNames.XSL_INCLUDE: |
| 99 |
case StandardNames.XSL_ITERATE: |
| 100 |
case StandardNames.XSL_KEY: |
| 101 |
case StandardNames.XSL_MAP: |
| 102 |
case StandardNames.XSL_MAP_ENTRY: |
| 103 |
case StandardNames.XSL_MATCHING_SUBSTRING: |
| 104 |
case StandardNames.XSL_MERGE: |
| 105 |
case StandardNames.XSL_MERGE_ACTION: |
| 106 |
case StandardNames.XSL_MERGE_KEY: |
| 107 |
case StandardNames.XSL_MERGE_SOURCE: |
| 108 |
case StandardNames.XSL_MESSAGE: |
| 109 |
case StandardNames.XSL_MODE: |
| 110 |
case StandardNames.XSL_NAMESPACE: |
| 111 |
case StandardNames.XSL_NAMESPACE_ALIAS: |
| 112 |
case StandardNames.XSL_NEXT_ITERATION: |
| 113 |
case StandardNames.XSL_NEXT_MATCH: |
| 114 |
case StandardNames.XSL_NON_MATCHING_SUBSTRING: |
| 115 |
case StandardNames.XSL_NUMBER: |
| 116 |
case StandardNames.XSL_ON_COMPLETION: |
| 117 |
case StandardNames.XSL_ON_EMPTY: |
| 118 |
case StandardNames.XSL_ON_NON_EMPTY: |
| 119 |
case StandardNames.XSL_OTHERWISE: |
| 120 |
case StandardNames.XSL_OUTPUT: |
| 121 |
case StandardNames.XSL_OUTPUT_CHARACTER: |
| 122 |
case StandardNames.XSL_OVERRIDE: |
| 123 |
case StandardNames.XSL_PACKAGE: |
| 124 |
case StandardNames.XSL_PARAM: |
| 125 |
case StandardNames.XSL_PERFORM_SORT: |
| 126 |
case StandardNames.XSL_PRESERVE_SPACE: |
| 127 |
case StandardNames.XSL_PROCESSING_INSTRUCTION: |
| 128 |
case StandardNames.XSL_RESULT_DOCUMENT: |
| 129 |
case StandardNames.XSL_SEQUENCE: |
| 130 |
case StandardNames.XSL_SORT: |
| 131 |
case StandardNames.XSL_SOURCE_DOCUMENT: |
| 132 |
case StandardNames.XSL_STRIP_SPACE: |
| 133 |
case StandardNames.XSL_STYLESHEET: |
| 134 |
case StandardNames.XSL_TEMPLATE: |
| 135 |
case StandardNames.XSL_TEXT: |
| 136 |
case StandardNames.XSL_TRANSFORM: |
| 137 |
case StandardNames.XSL_TRY: |
| 138 |
case StandardNames.XSL_USE_PACKAGE: |
| 139 |
case StandardNames.XSL_VALUE_OF: |
| 140 |
case StandardNames.XSL_VARIABLE: |
| 141 |
case StandardNames.XSL_WHEN: |
| 142 |
case StandardNames.XSL_WITH_PARAM: |
| 143 |
case StandardNames.XSL_WHERE_POPULATED: |
| 144 |
return true; |
| 145 |
default: |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public static boolean isSaxonJSElement(int fp) { |
| 151 |
switch (fp) { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
case StandardNames.XSL_ANALYZE_STRING: |
| 156 |
case StandardNames.XSL_APPLY_IMPORTS: |
| 157 |
case StandardNames.XSL_APPLY_TEMPLATES: |
| 158 |
case StandardNames.XSL_ASSERT: |
| 159 |
case StandardNames.XSL_ATTRIBUTE: |
| 160 |
case StandardNames.XSL_ATTRIBUTE_SET: |
| 161 |
|
| 162 |
case StandardNames.XSL_CALL_TEMPLATE: |
| 163 |
case StandardNames.XSL_CATCH: |
| 164 |
|
| 165 |
case StandardNames.XSL_CHOOSE: |
| 166 |
case StandardNames.XSL_COMMENT: |
| 167 |
|
| 168 |
case StandardNames.XSL_COPY: |
| 169 |
case StandardNames.XSL_COPY_OF: |
| 170 |
case StandardNames.XSL_DECIMAL_FORMAT: |
| 171 |
case StandardNames.XSL_DOCUMENT: |
| 172 |
case StandardNames.XSL_ELEMENT: |
| 173 |
|
| 174 |
|
| 175 |
case StandardNames.XSL_FALLBACK: |
| 176 |
case StandardNames.XSL_FOR_EACH: |
| 177 |
case StandardNames.XSL_FOR_EACH_GROUP: |
| 178 |
|
| 179 |
case StandardNames.XSL_FUNCTION: |
| 180 |
|
| 181 |
case StandardNames.XSL_IF: |
| 182 |
case StandardNames.XSL_IMPORT: |
| 183 |
|
| 184 |
case StandardNames.XSL_INCLUDE: |
| 185 |
|
| 186 |
case StandardNames.XSL_KEY: |
| 187 |
case StandardNames.XSL_MAP: |
| 188 |
case StandardNames.XSL_MAP_ENTRY: |
| 189 |
case StandardNames.XSL_MATCHING_SUBSTRING: |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
case StandardNames.XSL_MESSAGE: |
| 195 |
case StandardNames.XSL_MODE: |
| 196 |
case StandardNames.XSL_NAMESPACE: |
| 197 |
case StandardNames.XSL_NAMESPACE_ALIAS: |
| 198 |
|
| 199 |
case StandardNames.XSL_NEXT_MATCH: |
| 200 |
case StandardNames.XSL_NON_MATCHING_SUBSTRING: |
| 201 |
case StandardNames.XSL_NUMBER: |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
case StandardNames.XSL_OTHERWISE: |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
case StandardNames.XSL_PARAM: |
| 211 |
case StandardNames.XSL_PERFORM_SORT: |
| 212 |
case StandardNames.XSL_PRESERVE_SPACE: |
| 213 |
case StandardNames.XSL_PROCESSING_INSTRUCTION: |
| 214 |
case StandardNames.XSL_RESULT_DOCUMENT: |
| 215 |
case StandardNames.XSL_SEQUENCE: |
| 216 |
case StandardNames.XSL_SORT: |
| 217 |
|
| 218 |
case StandardNames.XSL_STRIP_SPACE: |
| 219 |
case StandardNames.XSL_STYLESHEET: |
| 220 |
case StandardNames.XSL_TEMPLATE: |
| 221 |
case StandardNames.XSL_TEXT: |
| 222 |
case StandardNames.XSL_TRANSFORM: |
| 223 |
case StandardNames.XSL_TRY: |
| 224 |
|
| 225 |
case StandardNames.XSL_VALUE_OF: |
| 226 |
case StandardNames.XSL_VARIABLE: |
| 227 |
case StandardNames.XSL_WHEN: |
| 228 |
case StandardNames.XSL_WITH_PARAM: |
| 229 |
|
| 230 |
return true; |
| 231 |
default: |
| 232 |
return false; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
@Override |
| 244 |
public int getSpecialProperties(Expression[] arguments) { |
| 245 |
try { |
| 246 |
if (arguments[0] instanceof StringLiteral) { |
| 247 |
String arg = ((StringLiteral) arguments[0]).getStringValue(); |
| 248 |
StructuredQName elem = getElementName(arg); |
| 249 |
if (elem.hasURI(NamespaceConstant.XSLT) && elem.getLocalPart().equals("evaluate")) { |
| 250 |
return super.getSpecialProperties(arguments) | StaticProperty.DEPENDS_ON_RUNTIME_ENVIRONMENT; |
| 251 |
} |
| 252 |
} |
| 253 |
} catch (XPathException e) { |
| 254 |
|
| 255 |
} |
| 256 |
return super.getSpecialProperties(arguments); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private boolean isElementAvailable(String lexicalName, String edition, XPathContext context) throws XPathException { |
| 274 |
|
| 275 |
StructuredQName qName = getElementName(lexicalName); |
| 276 |
|
| 277 |
if (qName.hasURI(NamespaceConstant.XSLT)) { |
| 278 |
int fp = context.getConfiguration().getNamePool().getFingerprint(NamespaceConstant.XSLT, qName.getLocalPart()); |
| 279 |
boolean known = isXslt30Element(fp); |
| 280 |
if ("JS".equals(edition)) { |
| 281 |
known = known && isSaxonJSElement(fp); |
| 282 |
} |
| 283 |
if (fp == StandardNames.XSL_EVALUATE) { |
| 284 |
known = known && !context.getConfiguration().getBooleanProperty(Feature.DISABLE_XSL_EVALUATE); |
| 285 |
} |
| 286 |
return known; |
| 287 |
} else { |
| 288 |
return context.getConfiguration().isExtensionElementAvailable(qName); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
private StructuredQName getElementName(String lexicalName) throws XPathException { |
| 293 |
try { |
| 294 |
if (lexicalName.indexOf(':') < 0 && NameChecker.isValidNCName(lexicalName)) { |
| 295 |
String uri = getRetainedStaticContext().getURIForPrefix("", true); |
| 296 |
return new StructuredQName("", uri, lexicalName); |
| 297 |
} else { |
| 298 |
return StructuredQName.fromLexicalQName(lexicalName, false, |
| 299 |
true, getRetainedStaticContext()); |
| 300 |
} |
| 301 |
} catch (XPathException e) { |
| 302 |
XPathException err = new XPathException("Invalid element name passed to element-available(): " + e.getMessage()); |
| 303 |
err.setErrorCode("XTDE1440"); |
| 304 |
throw err; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 318 |
String lexicalQName = arguments[0].head().getStringValue(); |
| 319 |
boolean b = isElementAvailable(lexicalQName, getRetainedStaticContext().getPackageData().getTargetEdition(), context); |
| 320 |
return BooleanValue.get(b); |
| 321 |
} |
| 322 |
} |
| 323 |
|