Debian Codegraph @

net/sf/saxon/expr/instruct/FixedAttribute.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.expr.*;
12 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
13 import net.sf.saxon.expr.parser.ExpressionTool;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RebindingMap;
16 import net.sf.saxon.functions.NormalizeSpace_1;
17 import net.sf.saxon.functions.SystemFunction;
18 import net.sf.saxon.lib.ConversionRules;
19 import net.sf.saxon.lib.Validation;
20 import net.sf.saxon.om.NodeInfo;
21 import net.sf.saxon.om.NodeName;
22 import net.sf.saxon.om.StandardNames;
23 import net.sf.saxon.trace.ExpressionPresenter;
24 import net.sf.saxon.trans.Err;
25 import net.sf.saxon.trans.XPathException;
26 import net.sf.saxon.tree.util.Orphan;
27 import net.sf.saxon.type.*;
28
29 /**
30 * An instruction derived from an xsl:attribute element in stylesheet, or from
31 * an attribute constructor in XQuery. This version deals only with attributes
32 * whose name is known at compile time. It is also used for attributes of
33 * literal result elements. The value of the attribute is in general computed
34 * at run-time.
35 */
36
37 public final class FixedAttribute extends AttributeCreator {
38
39 private NodeName nodeName;
40
41 /**
42 * Construct an Attribute instruction
43 *
44 * @param nodeName Represents the attribute name
45 * @param validationAction the validation required, for example strict or lax
46 * @param schemaType the schema type against which validation is required, null if not applicable
47 * of the instruction - zero if the attribute was not present
48 */
49
50 public FixedAttribute(NodeName nodeName,
51 int validationAction,
52 SimpleType schemaType) {
53 this.nodeName = nodeName;
54 setSchemaType(schemaType);
55 setValidationAction(validationAction);
56 setOptions(0);
57 }
58
59 /**
60 * Get the name of this instruction (return 'xsl:attribute')
61 */
62
63 public int getInstructionNameCode() {
64 return StandardNames.XSL_ATTRIBUTE;
65 }
66
67 /**
68 * Get a name identifying the kind of expression, in terms meaningful to a user.
69 *
70 * @return a name identifying the kind of expression, in terms meaningful to a user.
71 * The name will always be in the form of a lexical XML QName, and should match the name used
72 * in explain() output displaying the expression.
73 */
74 @Override
75 public String getExpressionName() {
76 return "att";
77 }
78
79 public NodeName getAttributeName() {
80 return nodeName;
81 }
82
83 public void localTypeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
84 // If attribute name is xml:id, add whitespace normalization
85 if (nodeName.equals(StandardNames.XML_ID_NAME) && !getSelect().isCallOn(NormalizeSpace_1.class)) {
86 Expression select = SystemFunction.makeCall(
87 "normalize-space", getRetainedStaticContext(), getSelect());
88 setSelect(select);
89 }
90
91 Configuration config = visitor.getConfiguration();
92 final ConversionRules rules = config.getConversionRules();
93 SimpleType schemaType = getSchemaType();
94 String errorCode = "XTTE1540";
95 if (schemaType == null) {
96 int validation = getValidationAction();
97 if (validation == Validation.STRICT) {
98 SchemaDeclaration decl = config.getAttributeDeclaration(nodeName.getStructuredQName());
99 if (decl == null) {
100 XPathException se = new XPathException(
101 "Strict validation fails: there is no global attribute declaration for " +
102 nodeName.getDisplayName());
103 se.setErrorCode("XTTE1510");
104 se.setLocation(getLocation());
105 throw se;
106 }
107 schemaType = (SimpleType) decl.getType();
108 errorCode = "XTTE1510";
109 } else if (validation == Validation.LAX) {
110 SchemaDeclaration decl = config.getAttributeDeclaration(nodeName.getStructuredQName());
111 if (decl != null) {
112 schemaType = (SimpleType) decl.getType();
113 errorCode = "XTTE1515";
114 } else {
115 visitor.getStaticContext().issueWarning(
116 "Lax validation has no effect: there is no global attribute declaration for " +
117 nodeName.getDisplayName(), getLocation());
118 }
119 }
120 }
121
122 // Attempt early validation if possible
123 if (Literal.isAtomic(getSelect()) && schemaType != null && !schemaType.isNamespaceSensitive()) {
124 CharSequence value = ((Literal) getSelect()).getValue().getStringValueCS();
125 ValidationFailure err = schemaType.validateContent(
126 value, DummyNamespaceResolver.getInstance(), rules);
127 if (err != null) {
128 XPathException se = new XPathException("Attribute value " + Err.wrap(value, Err.VALUE) +
129 " does not the match the required type " +
130 schemaType.getDescription() + ". " +
131 err.getMessage());
132 se.setErrorCode(errorCode);
133 throw se;
134 }
135 }
136
137 // If value is fixed, test whether there are any special characters that might need to be
138 // escaped when the time comes for serialization
139 if (getSelect() instanceof StringLiteral) {
140 boolean special = false;
141 CharSequence val = ((StringLiteral) getSelect()).getStringValue();
142 for (int k = 0; k < val.length(); k++) {
143 char c = val.charAt(k);
144 if ((int) c < 33 || (int) c > 126 ||
145 c == '<' || c == '>' || c == '&' || c == '\"' || c == '\'') {
146 special = true;
147 break;
148 }
149 }
150 if (!special) {
151 setNoSpecialChars();
152 }
153 }
154 }
155
156 /**
157 * Get the name pool name code of the attribute to be constructed
158 *
159 * @return the attribute's name code
160 */
161
162 public int getAttributeFingerprint() {
163 return nodeName.getFingerprint();
164 }
165
166
167 public int getCardinality() {
168 return StaticProperty.EXACTLY_ONE;
169 }
170
171 /**
172 * Copy an expression. This makes a deep copy.
173 *
174 * @return the copy of the original expression
175 * @param rebindings a mutable list of (old binding, new binding) pairs
176 * that is used to update the bindings held in any
177 * local variable references that are copied.
178 */
179
180 /*@NotNull*/
181 public Expression copy(RebindingMap rebindings) {
182 FixedAttribute exp = new FixedAttribute(nodeName, getValidationAction(), getSchemaType());
183 ExpressionTool.copyLocationInfo(this, exp);
184 exp.setSelect(getSelect().copy(rebindings));
185 exp.setInstruction(isInstruction());
186 return exp;
187 }
188
189 public NodeName evaluateNodeName(XPathContext context) {
190 return nodeName;
191 }
192
193 /**
194 * Check that any elements and attributes constructed or returned by this expression are acceptable
195 * in the content model of a given complex type. It's always OK to say yes, since the check will be
196 * repeated at run-time. The process of checking element and attribute constructors against the content
197 * model of a complex type also registers the type of content expected of those constructors, so the
198 * static validation can continue recursively.
199 */
200
201 public void checkPermittedContents(SchemaType parentType, boolean whole) throws XPathException {
202 int fp = nodeName.getFingerprint();
203 if (fp == StandardNames.XSI_TYPE ||
204 fp == StandardNames.XSI_SCHEMA_LOCATION ||
205 fp == StandardNames.XSI_NIL ||
206 fp == StandardNames.XSI_NO_NAMESPACE_SCHEMA_LOCATION) {
207 return;
208 }
209 if (parentType instanceof SimpleType) {
210 XPathException err = new XPathException("Attribute " + nodeName.getDisplayName() +
211 " is not permitted in the content model of the simple type " + parentType.getDescription());
212 err.setIsTypeError(true);
213 err.setLocation(getLocation());
214 if (getPackageData().getHostLanguage() == Configuration.XSLT) {
215 err.setErrorCode("XTTE1510");
216 } else {
217 err.setErrorCode("XQDY0027");
218 }
219 throw err;
220 }
221 SchemaType type;
222 try {
223 type = ((ComplexType) parentType).getAttributeUseType(nodeName.getStructuredQName());
224 } catch (SchemaException e) {
225 throw new XPathException(e);
226 }
227 if (type == null) {
228 XPathException err = new XPathException("Attribute " + nodeName.getDisplayName() +
229 " is not permitted in the content model of the complex type " + parentType.getDescription());
230 err.setIsTypeError(true);
231 err.setLocation(getLocation());
232 if (getPackageData().getHostLanguage() == Configuration.XSLT) {
233 err.setErrorCode("XTTE1510");
234 } else {
235 err.setErrorCode("XQDY0027");
236 }
237 throw err;
238 }
239
240 try {
241 // When select is a SimpleContentConstructor, this does nothing
242 getSelect().checkPermittedContents(type, true);
243 } catch (XPathException e) {
244 e.maybeSetLocation(getLocation());
245 throw e;
246 }
247 }
248
249
250 public NodeInfo evaluateItem(XPathContext context) throws XPathException {
251 Orphan o = (Orphan) super.evaluateItem(context);
252 assert o != null;
253 validateOrphanAttribute(o, context);
254 return o;
255 }
256
257
258 /**
259 * Diagnostic print of expression structure. The abstract expression tree
260 * is written to the supplied output destination.
261 */
262
263 public void export(ExpressionPresenter out) throws XPathException {
264 out.startElement("att", this);
265 out.emitAttribute("name", nodeName.getDisplayName());
266 if (!nodeName.getStructuredQName().hasURI("")) {
267 out.emitAttribute("nsuri", nodeName.getStructuredQName().getURI());
268 }
269 if (getValidationAction() != Validation.SKIP && getValidationAction() != Validation.BY_TYPE) {
270 out.emitAttribute("validation", Validation.toString(getValidationAction()));
271 }
272 if (getSchemaType() != null) {
273 out.emitAttribute("type", getSchemaType().getStructuredQName());
274 }
275 String flags = "";
276 if (isLocal()) {
277 flags += "l";
278 }
279 if (!flags.isEmpty()) {
280 out.emitAttribute("flags", flags);
281 }
282 getSelect().export(out);
283 out.endElement();
284 }
285
286 /**
287 * Produce a short string identifying the expression for use in error messages
288 *
289 * @return a short string, sufficient to identify the expression
290 */
291 @Override
292 public String toShortString() {
293 return "attr{" + nodeName.getDisplayName() + "=...}";
294 }
295 }
296