Debian Codegraph @

net/sf/saxon/style/XSLElement.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.style;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.Literal;
12 import net.sf.saxon.expr.StringLiteral;
13 import net.sf.saxon.expr.instruct.*;
14 import net.sf.saxon.lib.StandardURIChecker;
15 import net.sf.saxon.lib.Validation;
16 import net.sf.saxon.om.*;
17 import net.sf.saxon.trans.Err;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.SchemaType;
20 import net.sf.saxon.value.Whitespace;
21
22
23 /**
24 * An xsl:element element in the stylesheet. <br>
25 */
26
27 public class XSLElement extends StyleElement {
28
29 /*@Nullable*/ private Expression elementName;
30 private Expression namespace = null;
31 private String use;
32 private StructuredQName[] attributeSets = null;
33 private int validation;
34 private SchemaType schemaType = null;
35 private boolean inheritNamespaces = true;
36
37 /**
38 * Determine whether this node is an instruction.
39 *
40 * @return true - it is an instruction
41 */
42
43 public boolean isInstruction() {
44 return true;
45 }
46
47 /**
48 * Determine whether this type of element is allowed to contain a template-body
49 *
50 * @return true: yes, it may contain a template-body
51 */
52
53 public boolean mayContainSequenceConstructor() {
54 return true;
55 }
56
57 public void prepareAttributes() throws XPathException {
58
59 AttributeCollection atts = getAttributeList();
60
61 String nameAtt = null;
62 String namespaceAtt = null;
63 String validationAtt = null;
64 String typeAtt = null;
65 String inheritAtt = null;
66
67 for (int a = 0; a < atts.getLength(); a++) {
68 String f = atts.getQName(a);
69 if (f.equals("name")) {
70 nameAtt = Whitespace.trim(atts.getValue(a));
71 elementName = makeAttributeValueTemplate(nameAtt, a);
72 } else if (f.equals("namespace")) {
73 namespaceAtt = atts.getValue(a);
74 namespace = makeAttributeValueTemplate(namespaceAtt, a);
75 } else if (f.equals("validation")) {
76 validationAtt = Whitespace.trim(atts.getValue(a));
77 } else if (f.equals("type")) {
78 typeAtt = Whitespace.trim(atts.getValue(a));
79 } else if (f.equals("inherit-namespaces")) {
80 inheritAtt = Whitespace.trim(atts.getValue(a));
81 } else if (f.equals("use-attribute-sets")) {
82 use = atts.getValue(a);
83 } else {
84 checkUnknownAttribute(atts.getNodeName(a));
85 }
86 }
87
88 if (nameAtt == null) {
89 reportAbsence("name");
90 } else {
91 if (elementName instanceof StringLiteral) {
92 if (!NameChecker.isQName(((StringLiteral) elementName).getStringValue())) {
93 compileError("Element name " +
94 Err.wrap(((StringLiteral) elementName).getStringValue()) +
95 " is not a valid QName", "XTDE0820");
96 // to prevent duplicate error messages:
97 elementName = new StringLiteral("saxon-error-element");
98 }
99 }
100 }
101
102 if (namespaceAtt != null) {
103 if (namespace instanceof StringLiteral) {
104 if (!StandardURIChecker.getInstance().isValidURI(((StringLiteral) namespace).getStringValue())) {
105 compileError("The value of the namespace attribute must be a valid URI", "XTDE0835");
106 }
107 }
108 }
109
110 if (validationAtt != null) {
111 validation = validateValidationAttribute(validationAtt);
112 } else {
113 validation = getDefaultValidation();
114 }
115
116 if (typeAtt != null) {
117 if (!isSchemaAware()) {
118 compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660");
119 }
120 schemaType = getSchemaType(typeAtt);
121 validation = Validation.BY_TYPE;
122 }
123
124 if (typeAtt != null && validationAtt != null) {
125 compileError("The @validation and @type attributes are mutually exclusive", "XTSE1505");
126 }
127
128 if (inheritAtt != null) {
129 inheritNamespaces = processBooleanAttribute("inherit-namespaces", inheritAtt);
130 }
131
132 }
133
134 public void validate(ComponentDeclaration decl) throws XPathException {
135 if (use != null) {
136 // get the names of referenced attribute sets
137 attributeSets = getUsedAttributeSets(use);
138 }
139
140 elementName = typeCheck("name", elementName);
141 namespace = typeCheck("namespace", namespace);
142 }
143
144 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
145
146 // deal specially with the case where the element name is known statically
147
148 if (elementName instanceof StringLiteral) {
149 CharSequence qName = ((StringLiteral) elementName).getStringValue();
150
151 String[] parts;
152 try {
153 parts = NameChecker.getQNameParts(qName);
154 } catch (QNameException e) {
155 compileErrorInAttribute("Invalid element name: " + qName, "XTDE0820", "name");
156 return null;
157 }
158
159 String nsuri = null;
160 if (namespace instanceof StringLiteral) {
161 nsuri = ((StringLiteral) namespace).getStringValue();
162 if (nsuri.isEmpty()) {
163 parts[0] = "";
164 }
165 } else if (namespace == null) {
166 nsuri = getURIForPrefix(parts[0], true);
167 if (nsuri == null) {
168 undeclaredNamespaceError(parts[0], "XTDE0830", "name");
169 }
170 }
171 if (nsuri != null) {
172 // Local name and namespace are both known statically: generate a FixedElement instruction
173 FingerprintedQName qn = new FingerprintedQName(parts[0], nsuri, parts[1]);
174 qn.obtainFingerprint(getNamePool());
175 FixedElement inst = new FixedElement(qn,
176 NamespaceBinding.EMPTY_ARRAY,
177 inheritNamespaces,
178 true, schemaType,
179 validation);
180 inst.setLocation(allocateLocation());
181 return compileContentExpression(exec, decl, inst);
182 }
183 }
184
185 ComputedElement inst = new ComputedElement(elementName,
186 namespace,
187 schemaType,
188 validation,
189 inheritNamespaces,
190 false);
191
192 inst.setLocation(allocateLocation());
193 return compileContentExpression(exec, decl, inst);
194 }
195
196 private Expression compileContentExpression(Compilation exec, ComponentDeclaration decl, ElementCreator inst) throws XPathException {
197 Expression content = compileSequenceConstructor(exec, decl, true);
198
199 if (attributeSets != null) {
200 Expression use = UseAttributeSet.makeUseAttributeSets(attributeSets, this);
201 if (content == null) {
202 content = use;
203 } else {
204 content = Block.makeBlock(use, content);
205 content.setLocation(allocateLocation());
206 }
207 }
208 if (content == null) {
209 content = Literal.makeEmptySequence();
210 }
211 inst.setContentExpression(content);
212 return inst;
213 }
214
215
216 }
217