Debian Codegraph @

net/sf/saxon/serialize/SerializationParamsHandler.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.serialize;
9
10 import net.sf.saxon.expr.instruct.ResultDocument;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.lib.NamespaceConstant;
13 import net.sf.saxon.lib.SaxonOutputKeys;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.pattern.NodeKindTest;
16 import net.sf.saxon.regex.UnicodeString;
17 import net.sf.saxon.trans.Err;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.AxisIterator;
20 import net.sf.saxon.tree.util.Navigator;
21 import net.sf.saxon.type.Type;
22 import net.sf.saxon.z.IntHashMap;
23
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Properties;
27 import java.util.Set;
28
29 /**
30 * This class handles a set of serialization parameters provided in the form of an XDM instance
31 * as specified in the Serialization 3.0 definition, section 3.1
32 */
33 public class SerializationParamsHandler {
34
35 public static final String NAMESPACE = NamespaceConstant.OUTPUT;
36 Properties properties;
37 CharacterMap characterMap;
38 Location locator;
39
40 public SerializationParamsHandler() {}
41
42 public SerializationParamsHandler(Properties props) {
43 this.properties = props;
44 }
45
46 /**
47 * Set the location of the instruction to be used for error message reporting
48 * @param locator the location for error reporting
49 */
50
51 public void setLocator(Location locator) {
52 this.locator = locator;
53 }
54
55 /**
56 * Set the serialization parameters in the form of an XDM instance
57 *
58 * @param node either the serialization-parameters element node, or a document node having
59 * this element as its only child
60 * @throws XPathException if incorrect serialization parameters are found
61 */
62
63 public void setSerializationParams(NodeInfo node) throws XPathException {
64 if (properties == null) {
65 properties = new Properties();
66 }
67 if (node.getNodeKind() == Type.DOCUMENT) {
68 node = Navigator.getOutermostElement(node.getTreeInfo());
69 }
70 if (node.getNodeKind() != Type.ELEMENT) {
71 throw new XPathException("Serialization params: node must be a document or element node");
72 }
73 if (!node.getLocalPart().equals("serialization-parameters")) {
74 throw new XPathException("Serialization params: element name must be 'serialization-parameters");
75 }
76 if (!node.getURI().equals(NAMESPACE)) {
77 throw new XPathException("Serialization params: element namespace must be " + NAMESPACE);
78 }
79 restrictAttributes(node);
80 Set<NodeName> nodeNames = new HashSet<>();
81 AxisIterator kids = node.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
82 NodeInfo child;
83 while ((child = kids.next()) != null) {
84 if (!nodeNames.add(NameOfNode.makeName(child))) {
85 throw new XPathException("Duplicated serialization parameter " + child.getDisplayName(), "SEPM0019");
86 }
87 String lname = child.getLocalPart();
88 String uri = child.getURI();
89 if (uri.isEmpty()) {
90 throw new XPathException("Serialization parameter " + lname + " is in no namespace", "SEPM0017");
91 }
92 if (NamespaceConstant.OUTPUT.equals(uri)) {
93 uri = "";
94 }
95 if ("".equals(uri) && lname.equals("use-character-maps")) {
96 restrictAttributes(child);
97 AxisIterator gKids = child.iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT);
98 NodeInfo gChild;
99 IntHashMap<String> map = new IntHashMap<>();
100 while ((gChild = gKids.next()) != null) {
101 restrictAttributes(gChild, "character", "map-string");
102 if (!(gChild.getURI().equals(NAMESPACE) && gChild.getLocalPart().equals("character-map"))) {
103 if (gChild.getURI().equals(NAMESPACE) || gChild.getURI().isEmpty()) {
104 throw new XPathException("Invalid child of use-character-maps: " + gChild.getDisplayName(), "SEPM0017");
105 }
106 }
107 String ch = getAttribute(gChild, "character");
108 String str = getAttribute(gChild, "map-string");
109 UnicodeString chValue = UnicodeString.makeUnicodeString(ch);
110
111 if (chValue.uLength() != 1) {
112 throw new XPathException("In the serialization parameters, the value of @character in the character map " +
113 "must be a single Unicode character", "SEPM0017");
114 }
115 int code = chValue.uCharAt(0);
116 String prev = map.put(code, str);
117 if (prev != null) {
118 throw new XPathException("In the serialization parameters, the character map contains two entries for the character \\u" +
119 Integer.toHexString(65536+code).substring(1), "SEPM0018");
120 }
121 }
122 characterMap = new CharacterMap(NameOfNode.makeName(node).getStructuredQName(), map);
123 } else {
124 restrictAttributes(child, "value");
125 String value = getAttribute(child, "value");
126 try {
127 ResultDocument.setSerializationProperty(properties, uri, lname, value,
128 new InscopeNamespaceResolver(child), false, node.getConfiguration());
129 } catch (XPathException err) {
130 if ("XQST0109".equals(err.getErrorCodeLocalPart()) || "SEPM0016".equals(err.getErrorCodeLocalPart())) {
131 if ("".equals(uri)) {
132 XPathException e2 = new XPathException("Unknown serialization parameter " +
133 Err.depict(child), "SEPM0017");
134 e2.setLocator(locator);
135 throw e2;
136 }
137 // Unknown serialization parameter - no action, ignore the error
138 } else {
139 throw err;
140 }
141 }
142 }
143
144 }
145 }
146
147 /**
148 * Check that no no-namespace attributes are present on an element, unless they have a specified local name
149 * @param element the element to be checked
150 * @param allowedNames a list of permitted local names of attributes, in sorted order
151 * @throws XPathException if there is a no-namespace attribute present whose name is not the name supplied
152 */
153
154 private static void restrictAttributes(NodeInfo element, String... allowedNames) throws XPathException {
155 element.iterateAxis(AxisInfo.ATTRIBUTE).forEachOrFail(att -> {
156 if ("".equals(att.getURI()) && Arrays.binarySearch(allowedNames, att.getLocalPart()) < 0) {
157 throw new XPathException("In serialization parameters, attribute @" + att.getLocalPart() +
158 " must not appear on element " + element.getDisplayName(), "SEPM0017");
159 }
160 });
161 }
162
163 /**
164 * Check that a given attribute is present on an element, and return its value
165 *
166 * @param element the element to be checked
167 * @param localName the required attribute
168 * @return the value of the attribute
169 * @throws XPathException if the attribute does not exist
170 */
171
172 private static String getAttribute(NodeInfo element, String localName) throws XPathException {
173 String value = element.getAttributeValue("", localName);
174 if (value == null) {
175 throw new XPathException("In serialization parameters, attribute @" + localName +
176 " is missing on element " + element.getDisplayName());
177 }
178 return value;
179 }
180
181 /**
182 * Get the output properties represented by this serialization parameter document
183 * @return the output properties obtained by parsing and validating this serialization
184 * parameter document
185 */
186
187 public SerializationProperties getSerializationProperties() {
188 CharacterMapIndex index = new CharacterMapIndex();
189 if (characterMap != null) {
190 index.putCharacterMap(new StructuredQName("", "", "charMap"), characterMap);
191 properties.put(SaxonOutputKeys.USE_CHARACTER_MAPS, "charMap");
192 }
193 return new SerializationProperties(properties, index);
194 }
195
196 public CharacterMap getCharacterMap() {
197 return characterMap;
198 }
199 }
200