/** * Generate Javascript code to test whether an item conforms to this item type * * @param knownToBe a type that this item is known to conform to * @param targetVersion * @return a Javascript instruction or sequence of instructions, which can be used as the body * of a Javascript function, and which returns a boolean indication whether the value of the * variable "item" is an instance of this item type. * @throws XPathException if JS code cannot be generated for this item type, for example because * the test is schema-aware. */ @Override public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { if (this == ANY_MAP_TYPE) { return "return SaxonJS.U.isMap(item)"; } FastStringBuffer fsb = new FastStringBuffer(256); fsb.append("function k(item) {" + keyType.generateJavaScriptItemTypeTest(BuiltInAtomicType.ANY_ATOMIC, targetVersion) + "};"); fsb.append("function v(item) {" + valueType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};"); int card = valueType.getCardinality(); fsb.append(Cardinality.generateJavaScriptChecker(card)); if (targetVersion == 1) { fsb.append("return SaxonJS.U.isMap(item) && item.conforms(k, v, c);"); } else { fsb.append("return SaxonJS.U.isConstrainedMap(item, k, v, c);"); } return fsb.toString(); } }
/** * Generate Javascript code to test whether an item conforms to this item type * * @param knownToBe a type that this item is known to conform to * @param targetVersion * @return a Javascript instruction or sequence of instructions, which can be used as the body * of a Javascript function, and which returns a boolean indication whether the value of the * variable "item" is an instance of this item type. * @throws XPathException if JS code cannot be generated for this item type, for example because * the test is schema-aware. */ @Override public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { if (this == ANY_MAP_TYPE) { return "return SaxonJS.U.isMap(item)"; } FastStringBuffer fsb = new FastStringBuffer(256); fsb.append("function k(item) {" + keyType.generateJavaScriptItemTypeTest(BuiltInAtomicType.ANY_ATOMIC, targetVersion) + "};"); fsb.append("function v(item) {" + valueType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};"); int card = valueType.getCardinality(); fsb.append(Cardinality.generateJavaScriptChecker(card)); if (targetVersion == 1) { fsb.append("return SaxonJS.U.isMap(item) && item.conforms(k, v, c);"); } else { fsb.append("return SaxonJS.U.isConstrainedMap(item, k, v, c);"); } return fsb.toString(); } }