| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2013 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.expr.instruct; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.trace.ExpressionPresenter; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.type.AnyItemType; |
| 14 | import net.sf.saxon.type.ItemType; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * Information about the requirements placed by a query or stylesheet on the global |
| 21 | * context item: whether it is mandatory or optional, what its type must be, whether |
| 22 | * it is streamable, whether it has a default value. |
| 23 | * |
| 24 | * In XSLT, if more than one module specifies a global context item type, they must be the same. |
| 25 | * In XQuery, several modules can specify different required types, and the actual context item |
| 26 | * must satisfy them all. |
| 27 | */ |
| 28 | public class GlobalContextRequirement { |
| 29 | |
| 30 | private boolean mayBeOmitted = true; |
| 31 | private boolean absentFocus; |
| 32 | private boolean external; // XQuery only |
| 33 | private List<ItemType> requiredItemTypes = new ArrayList<>(); |
| 34 | private Expression defaultValue = null; // Used in XQuery only |
| 35 | |
| 36 | /** |
| 37 | * Get the required item type of the context item. If several required item types have been registered |
| 38 | * (which only happens in XQuery with multiple modules) then this returns the first. |
| 39 | * @return The first registered required item type |
| 40 | */ |
| 41 | |
| 42 | public ItemType getRequiredItemType() { |
| 43 | if (requiredItemTypes.isEmpty()) { |
| 44 | return AnyItemType.getInstance(); |
| 45 | } else { |
| 46 | return requiredItemTypes.get(0); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get all the required item types. In XSLT there can only be one, but in XQuery there can be several, |
| 52 | * one for each module (the actual context item must satisfy them all) |
| 53 | * @return the list of required item types |
| 54 | */ |
| 55 | |
| 56 | public List<ItemType> getRequiredItemTypes() { |
| 57 | return requiredItemTypes; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Specify the required item type of the context item |
| 62 | * @param requiredItemType the required item type |
| 63 | */ |
| 64 | |
| 65 | public void addRequiredItemType(ItemType requiredItemType) { |
| 66 | requiredItemTypes.add(requiredItemType); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the expression that supplies the default value of the context item, if any. This |
| 71 | * is used only in XQuery. |
| 72 | * @return the expression used to compute the value of the context item, in the absence |
| 73 | * of an externally-supplied value |
| 74 | */ |
| 75 | |
| 76 | public Expression getDefaultValue() { |
| 77 | return defaultValue; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Set the expression used to compute the default value of the global context item |
| 82 | * @param defaultValue the expression used to compute the default value. |
| 83 | */ |
| 84 | |
| 85 | public void setDefaultValue(Expression defaultValue) { |
| 86 | this.defaultValue = defaultValue; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Export the global context item declaration to an SEF export file |
| 91 | * @param out the export destination |
| 92 | * @throws XPathException if things go wrong |
| 93 | */ |
| 94 | |
| 95 | public void export(ExpressionPresenter out) throws XPathException { |
| 96 | out.startElement("glob"); |
| 97 | String use; |
| 98 | if (isMayBeOmitted()) { |
| 99 | if (isAbsentFocus()) { |
| 100 | use = "pro"; |
| 101 | } else { |
| 102 | use = "opt"; |
| 103 | } |
| 104 | } else { |
| 105 | use = "req"; |
| 106 | } |
| 107 | out.emitAttribute("use", use); |
| 108 | if (!getRequiredItemType().equals(AnyItemType.getInstance())) { |
| 109 | out.emitAttribute("type", getRequiredItemType().toExportString()); |
| 110 | } |
| 111 | if ("JS".equals(((ExpressionPresenter.ExportOptions) out.getOptions()).target)) { |
| 112 | int targetVersion = ((ExpressionPresenter.ExportOptions) out.getOptions()).targetVersion; |
| 113 | out.emitAttribute("jsTest", getRequiredItemType().generateJavaScriptItemTypeTest( |
| 114 | AnyItemType.getInstance(), targetVersion)); |
| 115 | |
| 116 | } |
| 117 | out.endElement(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Say whether the global context item for a query or stylesheet will be absent. This is relevant |
| 122 | * only for XSLT. |
| 123 | * @param absent true if there will be no context item (externally or internally supplied). |
| 124 | */ |
| 125 | |
| 126 | public void setAbsentFocus(boolean absent) { |
| 127 | this.absentFocus = absent; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Ask whether the global context item for a query or stylesheet will be absent. This is relevant |
| 132 | * only for XSLT. |
| 133 | * |
| 134 | * @return true if there will be no context item (externally or internally supplied). |
| 135 | */ |
| 136 | |
| 137 | public boolean isAbsentFocus() { |
| 138 | return absentFocus; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Say whether it is OK for the application to supply no context item |
| 143 | * @param mayOmit true if it is OK for the application to supply to context item |
| 144 | */ |
| 145 | |
| 146 | public void setMayBeOmitted(boolean mayOmit) { |
| 147 | this.mayBeOmitted = mayOmit; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Ask whether it is OK for the application to supply no context item |
| 152 | * @return true if it is OK for the application to supply to context item |
| 153 | */ |
| 154 | |
| 155 | public boolean isMayBeOmitted() { |
| 156 | return mayBeOmitted; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Say whether (in XQuery) the global context item is declared as external |
| 161 | * @param external true if the global context item is declared as external |
| 162 | */ |
| 163 | |
| 164 | public void setExternal(boolean external) { |
| 165 | this.external = external; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Ask whether (in XQuery) the global context item is declared as external |
| 170 | * @return true if the global context item is declared as external |
| 171 | */ |
| 172 | |
| 173 | public boolean isExternal() { |
| 174 | return external; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Copyright (c) 2018 Saxonica Limited |