Debian Codegraph @

net/sf/saxon/expr/instruct/AttributeCreator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 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.Configuration;
11 import net.sf.saxon.Controller;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.event.ReceiverOptions;
14 import net.sf.saxon.expr.StaticProperty;
15 import net.sf.saxon.expr.XPathContext;
16 import net.sf.saxon.lib.ConversionRules;
17 import net.sf.saxon.lib.Validation;
18 import net.sf.saxon.om.NameOfNode;
19 import net.sf.saxon.om.NodeName;
20 import net.sf.saxon.om.StandardNames;
21 import net.sf.saxon.pattern.NodeKindTest;
22 import net.sf.saxon.trans.Err;
23 import net.sf.saxon.trans.XPathException;
24 import net.sf.saxon.tree.util.Orphan;
25 import net.sf.saxon.type.*;
26 import net.sf.saxon.value.Whitespace;
27
28 /**
29 * Abstract class for fixed and computed attribute constructor expressions
30 */
31
32 public abstract class AttributeCreator extends SimpleNodeConstructor implements ValidatingInstruction {
33
34 //Null implies untyped (no validation required)
35 /*@Nullable*/ SimpleType schemaType = null;
36 private int validationAction;
37 private int options;
38 private boolean isInstruction;
39
40 /**
41 * Say whether this attribute creator originates as an XSLT instruction
42 * @param inst true if this is an xsl:attribute instruction
43 */
44
45 public void setInstruction(boolean inst) {
46 isInstruction = inst;
47 }
48
49 /**
50 * Ask whether this expression is an instruction. In XSLT streamability analysis this
51 * is used to distinguish constructs corresponding to XSLT instructions from other constructs.
52 *
53 * @return true if this construct originates as an XSLT instruction
54 */
55
56 @Override
57 public boolean isInstruction() {
58 return isInstruction;
59 }
60
61 /**
62 * Set the required schema type of the attribute
63 *
64 * @param type the required schema type, if validation against a specific type is required,
65 * or null if no validation is required
66 */
67
68 public void setSchemaType(/*@Nullable*/ SimpleType type) {
69 schemaType = type;
70 }
71
72 /**
73 * Return the required schema type of the attribute
74 *
75 * @return if validation against a schema type was requested, return the schema type (always a simple type).
76 * Otherwise, if validation against a specific type was not requested, return null
77 */
78
79 /*@Nullable*/
80 public SimpleType getSchemaType() {
81 return schemaType;
82 }
83
84 /**
85 * Set the validation action required
86 *
87 * @param action the validation action required, for example strict or lax
88 */
89
90 public void setValidationAction(int action) {
91 validationAction = action;
92 }
93
94 /**
95 * Get the validation action requested
96 *
97 * @return the validation action, for example strict or lax
98 */
99
100 public int getValidationAction() {
101 return validationAction;
102 }
103
104 /**
105 * Set the options to be used on the attribute event
106 *
107 * @param options Options to be used. The only option currently defined is
108 * {@link ReceiverOptions#REJECT_DUPLICATES}, which controls whether or not it is an error
109 * to create two attributes with the same name for the same element. (This is an error in XQuery
110 * but not in XSLT).
111 */
112
113 public void setOptions(int options) {
114 this.options = options;
115 }
116
117 /**
118 * Indicate that two attributes with the same name are not acceptable.
119 * (This option is set in XQuery, but not in XSLT)
120 */
121
122 public void setRejectDuplicates() {
123 options |= ReceiverOptions.REJECT_DUPLICATES;
124 }
125
126 /**
127 * Indicate that the attribute value contains no special characters that
128 * might need escaping
129 */
130
131 public void setNoSpecialChars() {
132 options |= ReceiverOptions.NO_SPECIAL_CHARS;
133 }
134
135 /**
136 * Get the options to be used on the attribute event
137 *
138 * @return the option flags to be used
139 */
140
141 public int getOptions() {
142 return options;
143 }
144
145
146 /**
147 * Get the static properties of this expression (other than its type). The result is
148 * bit-signficant. These properties are used for optimizations. In general, if
149 * property bit is set, it is true, but if it is unset, the value is unknown.
150 *
151 * @return a set of flags indicating static properties of this expression
152 */
153 @Override
154 public int computeSpecialProperties() {
155 int p = super.computeSpecialProperties();
156 if (getValidationAction() == Validation.SKIP) {
157 p |= StaticProperty.ALL_NODES_UNTYPED;
158 }
159 return p;
160 }
161
162 /**
163 * Get the static type of this expression
164 *
165 * @return the static type of the item returned by this expression
166 */
167
168 /*@NotNull*/
169 public ItemType getItemType() {
170 return NodeKindTest.ATTRIBUTE;
171 }
172
173 /**
174 * Process the value of the node, to create the new node.
175 *
176 * @param value the string value of the new node
177 * @param context the dynamic evaluation context
178 * @throws XPathException if a dynamic error occurs
179 */
180
181 public void processValue(CharSequence value, XPathContext context) throws XPathException {
182 NodeName attName = evaluateNodeName(context);
183 Receiver out = context.getReceiver();
184 int opt = getOptions();
185 SimpleType ann;
186
187 // we may need to change the namespace prefix if the one we chose is
188 // already in use with a different namespace URI: this is done behind the scenes
189 // by the ComplexContentOutputter
190
191 SimpleType schemaType = getSchemaType();
192 int validationAction = getValidationAction();
193 if (schemaType != null) {
194 ann = schemaType;
195 // test whether the value actually conforms to the given type
196 ValidationFailure err = schemaType.validateContent(
197 value, DummyNamespaceResolver.getInstance(), context.getConfiguration().getConversionRules());
198 if (err != null) {
199 ValidationFailure ve = new ValidationFailure(
200 "Attribute value " + Err.wrap(value, Err.VALUE) +
201 " does not match the required type " +
202 schemaType.getDescription() + ". " +
203 err.getMessage());
204 ve.setSchemaType(schemaType);
205 ve.setErrorCode("XTTE1540");
206 throw ve.makeException();
207 }
208 } else if (validationAction == Validation.STRICT ||
209 validationAction == Validation.LAX) {
210 try {
211 Configuration config = context.getConfiguration();
212 ann = config.validateAttribute(attName.getStructuredQName(), value, validationAction);
213 } catch (ValidationException e) {
214 XPathException err = XPathException.makeXPathException(e);
215 err.maybeSetErrorCode(validationAction == Validation.STRICT ? "XTTE1510" : "XTTE1515");
216 err.setXPathContext(context);
217 err.maybeSetLocation(getLocation());
218 err.setIsTypeError(true);
219 throw err;
220 }
221 } else {
222 ann = BuiltInAtomicType.UNTYPED_ATOMIC;
223 }
224 if (attName.equals(StandardNames.XML_ID_NAME)) {
225 value = Whitespace.collapseWhitespace(value);
226 }
227
228 try {
229 out.attribute(attName, ann, value, getLocation(), opt);
230 } catch (XPathException err) {
231 throw dynamicError(getLocation(), err, context);
232 }
233
234 }
235
236 /**
237 * Validate a newly-constructed parentless attribute using the type and validation attributes. Note,
238 * this is XSLT-only: XQuery does not allow validation of individual attributes
239 *
240 * @param orphan the new attribute node
241 * @param context the dynamic evaluation context
242 * @throws XPathException if validation fails
243 */
244
245 protected void validateOrphanAttribute(Orphan orphan, XPathContext context) throws XPathException {
246 ConversionRules rules = context.getConfiguration().getConversionRules();
247 SimpleType schemaType = getSchemaType();
248 int validationAction = getValidationAction();
249 if (schemaType != null) {
250 ValidationFailure err = schemaType.validateContent(
251 orphan.getStringValueCS(), DummyNamespaceResolver.getInstance(), rules);
252 if (err != null) {
253 err.setMessage("Attribute value " + Err.wrap(orphan.getStringValueCS(), Err.VALUE) +
254 " does not the match the required type " +
255 schemaType.getDescription() + ". " +
256 err.getMessage());
257 err.setErrorCode("XTTE1555");
258 err.setLocator(getLocation());
259 throw err.makeException();
260 }
261 orphan.setTypeAnnotation(schemaType);
262 if (schemaType.isNamespaceSensitive()) {
263 throw new XPathException("Cannot validate a parentless attribute whose content is namespace-sensitive", "XTTE1545");
264 }
265 } else if (validationAction == Validation.STRICT || validationAction == Validation.LAX) {
266 try {
267 final Controller controller = context.getController();
268 assert controller != null;
269 SimpleType ann = controller.getConfiguration().validateAttribute(
270 NameOfNode.makeName(orphan).getStructuredQName(), orphan.getStringValueCS(), validationAction);
271 orphan.setTypeAnnotation(ann);
272 } catch (ValidationException e) {
273 XPathException err = XPathException.makeXPathException(e);
274 err.setErrorCodeQName(e.getErrorCodeQName());
275 err.setXPathContext(context);
276 err.setLocation(getLocation());
277 err.setIsTypeError(true);
278 throw err;
279 }
280 }
281 }
282 }
283