/** * Generate Javascript code to test whether an item conforms to this item type * @param knownToBe a type that the item is known to conform to, without further testing * @param targetVersion the Saxon-JS version being targeted * @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. * */ @Override public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { String elTest = "function e(item) {" + elementTest.generateJavaScriptItemTypeTest(NodeKindTest.ELEMENT, targetVersion) + "};"; if (targetVersion == 1) { return elTest + "return SaxonJS.U.isNode(item) && (item.nodeType===9 || item.nodeType===11) && " + "SaxonJS.U.Axis.child(item).filter(e).next();"; } else { return elTest + "return SaxonJS.U.isConstrainedDocumentNode(item, e);"; } }
/** * Generate Javascript code to test whether an item conforms to this item type * @param knownToBe a type that the item is known to conform to, without further testing * @param targetVersion the Saxon-JS version being targeted * @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. * */ @Override public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { String elTest = "function e(item) {" + elementTest.generateJavaScriptItemTypeTest(NodeKindTest.ELEMENT, targetVersion) + "};"; if (targetVersion == 1) { return elTest + "return SaxonJS.U.isNode(item) && (item.nodeType===9 || item.nodeType===11) && " + "SaxonJS.U.Axis.child(item).filter(e).next();"; } else { return elTest + "return SaxonJS.U.isConstrainedDocumentNode(item, e);"; } }
/** * Generate Javascript code to convert a supplied Javascript value to this item type, * if conversion is possible, or throw an error otherwise. * * @param errorCode the error to be thrown if conversion is not possible * @param targetVersion the version of Saxon-JS being targeted * @return a Javascript instruction or sequence of instructions, which can be used as the body * of a Javascript function, and which returns the result of conversion to this type, or throws * an error if conversion is not possible. The variable "val" will hold the supplied Javascript * value. */ public String generateJavaScriptItemTypeAcceptor(String errorCode, int targetVersion) throws XPathException { return "function test(item) {" + generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};" + "if (test(val)) {return val;} else {throw SaxonJS.XError('Conversion failed', '" + errorCode + "');}"; }
/** * Generate Javascript code to convert a supplied Javascript value to this item type, * if conversion is possible, or throw an error otherwise. * * @param errorCode the error to be thrown if conversion is not possible * @param targetVersion the version of Saxon-JS being targeted * @return a Javascript instruction or sequence of instructions, which can be used as the body * of a Javascript function, and which returns the result of conversion to this type, or throws * an error if conversion is not possible. The variable "val" will hold the supplied Javascript * value. */ public String generateJavaScriptItemTypeAcceptor(String errorCode, int targetVersion) throws XPathException { return "function test(item) {" + generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};" + "if (test(val)) {return val;} else {throw SaxonJS.XError('Conversion failed', '" + errorCode + "');}"; }
break; fsb.append("function a() {" + nodetest1.generateJavaScriptItemTypeTest(knownToBe, targetVersion) + "};"); fsb.append("function b() {" + nodetest2.generateJavaScriptItemTypeTest(knownToBe, targetVersion) + "};"); fsb.append("return a()" + conjunctor + "b();"); return fsb.toString();
break; fsb.append("function a() {" + nodetest1.generateJavaScriptItemTypeTest(knownToBe, targetVersion) + "};"); fsb.append("function b() {" + nodetest2.generateJavaScriptItemTypeTest(knownToBe, targetVersion) + "};"); fsb.append("return a()" + conjunctor + "b();"); return fsb.toString();
public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.simPos"); presenter.emitAttribute("test", nodeTest.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); } presenter.emitAttribute("pos", position + ""); presenter.endElement(); }
public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.simPos"); presenter.emitAttribute("test", nodeTest.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); } presenter.emitAttribute("pos", position + ""); presenter.endElement(); }
public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.nodeTest"); presenter.emitAttribute("test", nodeTest.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; try { presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } presenter.endElement(); }
public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.nodeTest"); presenter.emitAttribute("test", nodeTest.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; try { presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } presenter.endElement(); }
public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.genPos"); presenter.emitAttribute("type", nodeTest.toString()); if (!usesPosition) { // flag is this way around for backwards compatibility with 9.8 presenter.emitAttribute("flags", "P"); } if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { try { int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } positionExpr.export(presenter); presenter.endElement(); }
/** * Generate Javascript code to test whether an item conforms to this item type * * @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. * @param knownToBe An item type that the supplied item is known to conform to; the generated code * can assume that the item is an instance of this type. * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2. */ @Override public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { if (kind == Type.ELEMENT) { if (schemaType == Untyped.getInstance() || schemaType == AnyType.getInstance()) { return NodeKindTest.makeNodeKindTest(getNodeKind()).generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion); } else if (schemaType == BuiltInAtomicType.UNTYPED_ATOMIC || schemaType == BuiltInAtomicType.ANY_ATOMIC) { return "return false;"; } } else if (kind == Type.ATTRIBUTE) { if (schemaType == Untyped.getInstance() || schemaType == AnyType.getInstance() || schemaType == BuiltInAtomicType.UNTYPED_ATOMIC || schemaType == BuiltInAtomicType.ANY_ATOMIC) { return NodeKindTest.makeNodeKindTest(getNodeKind()).generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion); } } throw new XPathException("Cannot generate JS code to test type annotations", SaxonErrorCode.SXJS0001); }
public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.genPos"); presenter.emitAttribute("type", nodeTest.toString()); if (!usesPosition) { // flag is this way around for backwards compatibility with 9.8 presenter.emitAttribute("flags", "P"); } if ("JS".equals(((ExpressionPresenter.ExportOptions) presenter.getOptions()).target)) { try { int targetVersion = ((ExpressionPresenter.ExportOptions) presenter.getOptions()).targetVersion; presenter.emitAttribute("jsTest", nodeTest.generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } positionExpr.export(presenter); presenter.endElement(); }
/** * Generate Javascript code to test whether an item conforms to this item type * * @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. * @param knownToBe An item type that the supplied item is known to conform to; the generated code * can assume that the item is an instance of this type. * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2. */ @Override public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException { if (kind == Type.ELEMENT) { if (schemaType == Untyped.getInstance() || schemaType == AnyType.getInstance()) { return NodeKindTest.makeNodeKindTest(getNodeKind()).generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion); } else if (schemaType == BuiltInAtomicType.UNTYPED_ATOMIC || schemaType == BuiltInAtomicType.ANY_ATOMIC) { return "return false;"; } } else if (kind == Type.ATTRIBUTE) { if (schemaType == Untyped.getInstance() || schemaType == AnyType.getInstance() || schemaType == BuiltInAtomicType.UNTYPED_ATOMIC || schemaType == BuiltInAtomicType.ANY_ATOMIC) { return NodeKindTest.makeNodeKindTest(getNodeKind()).generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion); } } throw new XPathException("Cannot generate JS code to test type annotations", SaxonErrorCode.SXJS0001); }
/** * Diagnostic print of expression structure. The abstract expression tree * is written to the supplied output destination. */ public void export(ExpressionPresenter destination) throws XPathException { destination.startElement("axis", this); destination.emitAttribute("name", AxisInfo.axisName[axis]); destination.emitAttribute("nodeTest", test == null ? "node()" : test.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) destination.getOptions()).target)) { NodeTest known = AnyNodeTest.getInstance(); if (axis == AxisInfo.ATTRIBUTE) { known = NodeKindTest.makeNodeKindTest(Type.ATTRIBUTE); } else if (axis == AxisInfo.NAMESPACE) { known = NodeKindTest.makeNodeKindTest(Type.NAMESPACE); } int targetVersion = ((ExpressionPresenter.ExportOptions) destination.getOptions()).targetVersion; try { destination.emitAttribute("jsTest", test == null ? "return true;" : test.generateJavaScriptItemTypeTest(known, targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } destination.endElement(); }
/** * Diagnostic print of expression structure. The abstract expression tree * is written to the supplied output destination. */ public void export(ExpressionPresenter destination) throws XPathException { destination.startElement("axis", this); destination.emitAttribute("name", AxisInfo.axisName[axis]); destination.emitAttribute("nodeTest", test == null ? "node()" : test.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) destination.getOptions()).target)) { NodeTest known = AnyNodeTest.getInstance(); if (axis == AxisInfo.ATTRIBUTE) { known = NodeKindTest.makeNodeKindTest(Type.ATTRIBUTE); } else if (axis == AxisInfo.NAMESPACE) { known = NodeKindTest.makeNodeKindTest(Type.NAMESPACE); } int targetVersion = ((ExpressionPresenter.ExportOptions) destination.getOptions()).targetVersion; try { destination.emitAttribute("jsTest", test == null ? "return true;" : test.generateJavaScriptItemTypeTest(known, targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } destination.endElement(); }