Debian Codegraph @

net/sf/saxon/style/XSLPackage.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.lib.NamespaceConstant;
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.trans.packages.VersionedPackageName;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.tree.linked.NodeImpl;
16 import net.sf.saxon.tree.util.AttributeCollectionImpl;
17 import net.sf.saxon.type.SchemaType;
18 import net.sf.saxon.type.Type;
19 import net.sf.saxon.value.Whitespace;
20
21 /**
22 * Handler for xsl:package elements. Explicit xsl:package elements are not permitted in Saxon-HE, but
23 * implicit packages are created, so the class is present in HE. The top-level module of a stylesheet/package
24 * will always be represented by an XSLPackage object, but if the original name was xsl:stylesheet or xsl:transform
25 * then this original name will be present as the name of the element.
26 */
27 public class XSLPackage extends XSLModuleRoot {
28
29 private String nameAtt = null;
30 private PackageVersion packageVersion = null;
31 private boolean declaredModes = true;
32 private boolean prepared = false;
33
34 /**
35 * Initialise a new ElementImpl with an element name
36 *
37 * @param elemName Integer representing the element name, with namespaces resolved
38 * @param elementType the schema type of the element node
39 * @param atts The attribute list: always null
40 * @param parent The parent node
41 * @param sequenceNumber Integer identifying this element within the document
42 */
43 @Override
44 public void initialise(NodeName elemName, SchemaType elementType, AttributeCollectionImpl atts, NodeInfo parent, int sequenceNumber) {
45 super.initialise(elemName, elementType, atts, parent, sequenceNumber);
46 declaredModes = getLocalPart().equals("package");
47 }
48
49 /**
50 * Get the name of the package (the value of its @name attribute)
51 * @return the name of the package, or null if the @name attribute is omitted
52 */
53
54 public String getName() {
55 if (nameAtt == null) {
56 try {
57 prepareAttributes();
58 } catch (XPathException e) {
59 nameAtt = "default";
60 }
61 }
62 return nameAtt;
63 }
64
65 /**
66 * Get the requested XSLT version (the value of the @version attribute)
67 * @return the value of the @version attribute, times ten as an integer
68 */
69
70 public int getVersion() {
71 if (version == -1) {
72 try {
73 prepareAttributes();
74 } catch (XPathException e) {
75 version = 30;
76 }
77 }
78 return version;
79 }
80
81 public VersionedPackageName getNameAndVersion() {
82 return new VersionedPackageName(getName(), getPackageVersion());
83 }
84
85 /**
86 * Get the package version (the value of the @package-version attribute)
87 * @return the value of the @package-version attribute, defaulting to "1.0"
88 */
89 public PackageVersion getPackageVersion() {
90 if (packageVersion == null) {
91 try {
92 prepareAttributes();
93 } catch (XPathException e) {
94 packageVersion = PackageVersion.ONE;
95 }
96 }
97 return packageVersion;
98 }
99
100
101
102 @Override
103 protected void prepareAttributes() throws XPathException {
104 if (prepared) {
105 // already done
106 return;
107 }
108 prepared = true;
109
110 String inputTypeAnnotationsAtt = null;
111 String packageVersionAtt = null;
112 AttributeCollection atts = getAttributeList();
113
114 for (int a = 0; a < atts.getLength(); a++) {
115 String lexicalName = atts.getQName(a);
116 if (lexicalName.equals("name") && getLocalPart().equals("package")) {
117 nameAtt = Whitespace.trim(atts.getValue(a));
118 } else if (lexicalName.equals("id")) {
119 // no action
120 } else if (lexicalName.equals("version")) {
121 if (version == -1) {
122 processVersionAttribute("");
123 }
124 } else if (lexicalName.equals("package-version") && getLocalPart().equals("package")) {
125 packageVersionAtt = Whitespace.trim(atts.getValue(a));
126
127 } else if (lexicalName.equals("declared-modes") && getLocalPart().equals("package")) {
128 declaredModes = processBooleanAttribute("declared-modes", atts.getValue(a));
129
130 } else if (lexicalName.equals("input-type-annotations")) {
131 inputTypeAnnotationsAtt = atts.getValue(a);
132 } else {
133 checkUnknownAttribute(atts.getNodeName(a));
134 }
135 }
136
137 if (packageVersionAtt == null) {
138 packageVersion = PackageVersion.ONE;
139 } else {
140 try {
141 packageVersion = new PackageVersion(packageVersionAtt);
142 } catch (XPathException ex) {
143 compileErrorInAttribute(ex.getMessage(), ex.getErrorCodeLocalPart(), "package-version");
144 }
145 }
146
147 if (version == -1) {
148 version = 30;
149 reportAbsence("version");
150 }
151 if (inputTypeAnnotationsAtt != null) {
152 switch (inputTypeAnnotationsAtt) {
153 case "strip":
154 //setInputTypeAnnotations(ANNOTATION_STRIP);
155 break;
156 case "preserve":
157 //setInputTypeAnnotations(ANNOTATION_PRESERVE);
158 break;
159 case "unspecified":
160 //
161 break;
162 default:
163 compileError("Invalid value for input-type-annotations attribute. " +
164 "Permitted values are (strip, preserve, unspecified)", "XTSE0020");
165 break;
166 }
167 }
168
169 }
170
171 /**
172 * Ask whether it is required that modes be explicitly declared
173 *
174 * @return true if modes referenced within this package must be explicitly declared
175 */
176 @Override
177 public boolean isDeclaredModes() {
178 if (nameAtt == null) {
179 try {
180 prepareAttributes();
181 } catch (XPathException e) {
182 // it will be reported later
183 nameAtt = null;
184 }
185 }
186 return declaredModes;
187 }
188
189 /**
190 * Recursive walk through the stylesheet to validate all nodes
191 * @param decl not used
192 * @throws XPathException if invalid
193 */
194
195
196 public void validate(ComponentDeclaration decl) throws XPathException {
197
198 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
199 NodeImpl child;
200 while ((child = (NodeImpl)kids.next()) != null) {
201 int fp = child.getFingerprint();
202 if (child.getNodeKind() == Type.TEXT ||
203 (child instanceof StyleElement && ((StyleElement) child).isDeclaration()) ||
204 child instanceof DataElement ) {
205 // all is well
206 } else if (getLocalPart().equals("package") &&
207 (fp == StandardNames.XSL_USE_PACKAGE || fp == StandardNames.XSL_EXPOSE)) {
208 // all is well
209 } else if (!NamespaceConstant.XSLT.equals(child.getURI()) && !"".equals(child.getURI())) {
210 // elements in other namespaces are allowed and ignored
211 } else if (child instanceof AbsentExtensionElement && ((StyleElement) child).forwardsCompatibleModeIsEnabled()) {
212 // this is OK: an unknown XSLT element is allowed in forwards compatibility mode
213 } else if (NamespaceConstant.XSLT.equals(child.getURI())) {
214 if (child instanceof AbsentExtensionElement) {
215 // then the error will be reported later
216 } else {
217 ((StyleElement) child).compileError(
218 "Element " + child.getDisplayName() +
219 " must not appear directly within " + getDisplayName(), "XTSE0010");
220 }
221 } else {
222 ((StyleElement) child).compileError("Element " + child.getDisplayName() +
223 " must not appear directly within " + getDisplayName() +
224 " because it is not in a namespace", "XTSE0130");
225 }
226 }
227
228 if (declaredModes) {
229 String defaultMode = getAttributeValue("default-mode");
230 if (defaultMode != null && getPrincipalStylesheetModule().getRuleManager().obtainMode(getDefaultMode(), false) == null) {
231 compileError("The default mode " + defaultMode + " has not been declared in an xsl:mode declaration", "XTSE3085");
232 }
233 }
234
235 }
236
237
238
239 }