Debian Codegraph @

net/sf/saxon/style/XSLOutput.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.functions.ResolveQName;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.lib.SaxonOutputKeys;
13 import net.sf.saxon.lib.SerializerFactory;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.value.Whitespace;
17
18 import javax.xml.transform.OutputKeys;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Properties;
22 import java.util.StringTokenizer;
23
24 /**
25 * An xsl:output element in the stylesheet.
26 */
27
28 public class XSLOutput extends StyleElement {
29
30 private StructuredQName outputFormatName;
31 /*@Nullable*/ private String method = null;
32 private String outputVersion = null;
33
34 private String useCharacterMaps = null;
35 private Map<String, String> serializationAttributes = new HashMap<String, String>(10);
36
37 private HashMap<String, String> userAttributes = null;
38
39 /**
40 * Ask whether this node is a declaration, that is, a permitted child of xsl:stylesheet
41 * (including xsl:include and xsl:import).
42 *
43 * @return true for this element
44 */
45
46 @Override
47 public boolean isDeclaration() {
48 return true;
49 }
50
51 public void prepareAttributes() throws XPathException {
52 AttributeCollection atts = getAttributeList();
53 String nameAtt = null;
54 for (int a = 0; a < atts.getLength(); a++) {
55 String f = atts.getNodeName(a).getStructuredQName().getClarkName();
56 if (f.equals("name")) {
57 nameAtt = Whitespace.trim(atts.getValue(a));
58 } else if (f.equals("version")) {
59 String outputVersion = Whitespace.trim(atts.getValue(a));
60 serializationAttributes.put(f, outputVersion);
61 } else if (f.equals("use-character-maps")) {
62 useCharacterMaps = atts.getValue(a);
63 } else if (f.equals("parameter-document")) {
64 String val = Whitespace.trim(atts.getValue(a));
65 serializationAttributes.put(f, val);
66 serializationAttributes.put(SaxonOutputKeys.PARAMETER_DOCUMENT_BASE_URI, getBaseURI());
67 } else if (XSLResultDocument.fans.contains(f) && !f.equals("output-version")) {
68 String val = atts.getValue(a);
69 if (!f.equals(SaxonOutputKeys.ITEM_SEPARATOR) && !f.equals(SaxonOutputKeys.NEWLINE)) {
70 val = Whitespace.trim(val);
71 }
72 serializationAttributes.put(f, val);
73 } else {
74 String attributeURI = atts.getURI(a);
75 if ("".equals(attributeURI) ||
76 NamespaceConstant.XSLT.equals(attributeURI) ||
77 NamespaceConstant.SAXON.equals(attributeURI)) {
78 checkUnknownAttribute(atts.getNodeName(a));
79 } else {
80 String name = '{' + attributeURI + '}' + atts.getLocalName(a);
81 if (userAttributes == null) {
82 userAttributes = new HashMap<String, String>(5);
83 }
84 userAttributes.put(name, atts.getValue(a));
85 }
86 }
87 }
88 if (nameAtt != null) {
89 try {
90 outputFormatName = makeQName(nameAtt);
91 } catch (XPathException err) {
92 compileError(err.getMessage(), "XTSE1570");
93 }
94 }
95 }
96
97 /**
98 * Get the name of the xsl:output declaration
99 *
100 * @return the name, as a structured QName; or null for an unnamed output declaration
101 */
102
103 public StructuredQName getFormatQName() {
104 return outputFormatName;
105 }
106
107 public void validate(ComponentDeclaration decl) throws XPathException {
108 checkTopLevel("XTSE0010", false);
109 checkEmpty();
110 }
111
112 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) {
113 // no action
114 }
115
116 /**
117 * Process the [xsl:]version attribute if there is one
118 *
119 * @param ns the namespace URI of the attribute required, either the XSLT namespace or ""
120 */
121 @Override
122 protected void processVersionAttribute(String ns) {
123 version = ((StyleElement)getParent()).getEffectiveVersion();
124 }
125
126 /**
127 * Validate the properties,
128 * and return the values as additions to a supplied Properties object.
129 *
130 * @param details the Properties object to be populated with property values
131 * @param precedences a HashMap to be populated with information about the precedence
132 * of the property values: the key is the property name as a Clark name, the value
133 * is a boxed integer giving the property's import precedence
134 * @param thisPrecedence the precedence of thi instruction
135 * @throws net.sf.saxon.trans.XPathException
136 * if an error is found
137 */
138
139 protected void gatherOutputProperties(Properties details, HashMap<String, Integer> precedences, int thisPrecedence)
140 throws XPathException {
141 SerializerFactory sf = getConfiguration().getSerializerFactory();
142 if (method != null) {
143 if ("xml".equals(method) || "html".equals(method) ||
144 "text".equals(method) || "xhtml".equals(method) ||
145 "json".equals(method) || "adaptive".equals(method)) {
146 checkAndPut(sf, OutputKeys.METHOD, method, details, precedences, thisPrecedence);
147 //details.put(OutputKeys.METHOD, method);
148 } else {
149 String[] parts;
150 try {
151 parts = NameChecker.getQNameParts(method);
152 String prefix = parts[0];
153 if (prefix.isEmpty()) {
154 compileError("method must be xml, html, xhtml, text, json, adaptive, or a prefixed name", "XTSE1570");
155 } else {
156 String uri = getURIForPrefix(prefix, false);
157 if (uri == null) {
158 undeclaredNamespaceError(prefix, "XTSE0280", "method");
159 }
160 checkAndPut(sf, OutputKeys.METHOD, '{' + uri + '}' + parts[1], details, precedences, thisPrecedence);
161 //details.put(OutputKeys.METHOD, '{' + uri + '}' + parts[1] );
162 }
163 } catch (QNameException e) {
164 compileError("Invalid method name. " + e.getMessage(), "XTSE1570");
165 }
166 }
167 }
168
169 for (Map.Entry<String, String> entry : serializationAttributes.entrySet()) {
170 checkAndPut(sf, entry.getKey(), entry.getValue(), details, precedences, thisPrecedence);
171 }
172
173 if (serializationAttributes.containsKey(SaxonOutputKeys.NEXT_IN_CHAIN)) {
174 checkAndPut(sf, SaxonOutputKeys.NEXT_IN_CHAIN_BASE_URI, getSystemId(), details, precedences, thisPrecedence);
175 }
176
177 if (useCharacterMaps != null) {
178 String s = prepareCharacterMaps(this, useCharacterMaps, details);
179 details.setProperty(SaxonOutputKeys.USE_CHARACTER_MAPS, s);
180 }
181
182 // deal with user-defined attributes
183
184 if (userAttributes != null) {
185 for (Map.Entry<String, String> e : userAttributes.entrySet()) {
186 details.setProperty(e.getKey(), e.getValue());
187 }
188 }
189
190 }
191
192 /**
193 * Add an output property to the list of properties after checking that it is consistent
194 * with other properties
195 *
196 * @param property the name of the property in Clark format
197 * @param value the value of the property
198 * @param props the list of properties to be updated
199 * @param precedences the import precedence of each property
200 * @param thisPrecedence the import precedence of the declaration containing this value
201 * @throws net.sf.saxon.trans.XPathException
202 * if an error occurs
203 */
204
205 private void checkAndPut(SerializerFactory sf, String property, String value, Properties props,
206 HashMap<String, Integer> precedences, int thisPrecedence) {
207 try {
208 if (isListOfNames(property)) {
209 boolean useDefaultNS = !property.equals(SaxonOutputKeys.ATTRIBUTE_ORDER);
210 boolean allowStar = property.equals(SaxonOutputKeys.ATTRIBUTE_ORDER);
211 value = SaxonOutputKeys.parseListOfNodeNames(
212 value, this, useDefaultNS, false, allowStar, "XTSE0280");
213 }
214 if (isQName(property) && value.contains(":")) {
215 value = ResolveQName.resolveQName(value, this).getClarkName();
216 }
217 value = sf.checkOutputProperty(property, value);
218 } catch (XPathException err) {
219 String code = property.equals("method") ? "XTSE1570" : "XTSE0020";
220 if (property.contains("{")) {
221 compileError(err.getMessage(), code);
222 } else {
223 compileErrorInAttribute(err.getMessage(), code, property);
224 }
225 return;
226 }
227
228 String old = props.getProperty(property);
229 if (old == null) {
230 props.setProperty(property, value);
231 precedences.put(property, thisPrecedence);
232 } else if (old.equals(value)) {
233 // do nothing
234 } else if (isListOfNames(property)) {
235 props.setProperty(property, old + " " + value);
236 precedences.put(property, thisPrecedence);
237 } else {
238 Integer oldPrec = precedences.get(property);
239 if (oldPrec == null) {
240 return; // shouldn't happen but ignore it
241 }
242 if (oldPrec > thisPrecedence) {
243 // ignore this value, the other has higher precedence
244 } else if (oldPrec == thisPrecedence) {
245 compileError("Conflicting values for output property " + property, "XTSE1560");
246 } else {
247 // this has higher precedence: can't happen
248 throw new IllegalStateException("Output properties must be processed in decreasing precedence order");
249 }
250 }
251 }
252
253 private static boolean isListOfNames(String property) {
254 return property.equals(OutputKeys.CDATA_SECTION_ELEMENTS) ||
255 property.equals(SaxonOutputKeys.SUPPRESS_INDENTATION) ||
256 property.equals(SaxonOutputKeys.ATTRIBUTE_ORDER) ||
257 property.equals(SaxonOutputKeys.DOUBLE_SPACE);
258 }
259
260 private static boolean isQName(String property) {
261 return property.equals(OutputKeys.METHOD) ||
262 property.equals(SaxonOutputKeys.JSON_NODE_OUTPUT_METHOD);
263 }
264
265 /**
266 * Process the use-character-maps attribute
267 *
268 * @param element the stylesheet element on which the use-character-maps attribute appears
269 * @param useCharacterMaps the value of the use-character-maps attribute
270 * @param details The existing output properties
271 * @return the augmented value of the use-character-maps attribute in Clark notation
272 * @throws XPathException if the value is invalid
273 */
274 public static String prepareCharacterMaps(StyleElement element,
275 String useCharacterMaps,
276 Properties details)
277 throws XPathException {
278 PrincipalStylesheetModule psm = element.getPrincipalStylesheetModule();
279 String existing = details.getProperty(SaxonOutputKeys.USE_CHARACTER_MAPS);
280 if (existing == null) {
281 existing = "";
282 }
283 String s = "";
284 StringTokenizer st = new StringTokenizer(useCharacterMaps, " \t\n\r", false);
285 while (st.hasMoreTokens()) {
286 String displayname = st.nextToken();
287 StructuredQName qName = element.makeQName(displayname);
288 ComponentDeclaration decl = psm.getCharacterMap(qName);
289 if (decl == null) {
290 element.compileErrorInAttribute(
291 "No character-map named '" + displayname + "' has been defined", "XTSE1590", "use-character-maps");
292 }
293 s += " " + qName.getClarkName();
294 }
295 existing = s + existing;
296 return existing;
297 }
298
299 }
300