Debian Codegraph @

net/sf/saxon/style/XSLCharacterMap.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.z.IntHashMap;
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.trans.Err;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.value.Whitespace;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.StringTokenizer;
20
21 /**
22 * An xsl:character-map declaration in the stylesheet. <br>
23 */
24
25 public class XSLCharacterMap extends StyleElement {
26
27 /*@Nullable*/ String use;
28 // the value of the use-character-maps attribute, as supplied
29
30 List<XSLCharacterMap> characterMapElements = null;
31 // list of XSLCharacterMap objects referenced by this one
32
33 boolean validated = false;
34 // set to true once validate() has been called
35
36 boolean redundant = false;
37 // set to true if another character-map overrrides this one
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 /**
52 * Get the fingerprint of the name of this character map
53 *
54 * @return the fingerprint value
55 */
56
57 public StructuredQName getCharacterMapName() {
58 StructuredQName name = getObjectName();
59 if (name == null) {
60 try {
61 return makeQName(getAttributeValue("", "name"));
62 } catch (Exception err) {
63 // the error will be reported later
64 return new StructuredQName("", "", "unnamedCharacterMap_" + hashCode());
65 }
66 }
67 return name;
68 }
69
70 /**
71 * Test whether this character map is redundant (because another with the
72 * same name has higher import precedence). Note that a character map is not
73 * considered redundant simply because it is not referenced in an xsl:output
74 * declaration; we allow character-maps to be selected at run-time using the
75 * setOutputProperty() API.
76 *
77 * @return true if this character map is redundant
78 */
79
80 public boolean isRedundant() {
81 return redundant;
82 }
83
84 /**
85 * Validate the attributes on this instruction
86 *
87 * @throws XPathException
88 */
89
90 public void prepareAttributes() throws XPathException {
91
92 String name = null;
93 use = null;
94
95 AttributeCollection atts = getAttributeList();
96
97 for (int a = 0; a < atts.getLength(); a++) {
98 String f = atts.getQName(a);
99 if (f.equals("name")) {
100 name = Whitespace.trim(atts.getValue(a));
101 } else if (f.equals("use-character-maps")) {
102 use = atts.getValue(a);
103 } else {
104 checkUnknownAttribute(atts.getNodeName(a));
105 }
106 }
107
108 if (name == null) {
109 reportAbsence("name");
110 name = "unnamedCharacterMap_" + hashCode();
111 }
112
113 try {
114 setObjectName(makeQName(name));
115 } catch (XPathException err) {
116 compileError(err.getMessage(), "XTSE0020");
117 name = "unnamedCharacterMap_" + hashCode();
118 setObjectName(new StructuredQName("", "", name));
119 }
120
121 }
122
123 public void validate(ComponentDeclaration decl) throws XPathException {
124
125 if (validated) {
126 return;
127 }
128
129 // check that this is a top-level declaration
130
131 checkTopLevel("XTSE0010", false);
132
133 // check that the only children are xsl:output-character elements
134
135 iterateAxis(AxisInfo.CHILD).forEachOrFail(child -> {
136 if (!(child instanceof XSLOutputCharacter)) {
137 compileError("Only xsl:output-character is allowed within xsl:character-map", "XTSE0010");
138 }
139 });
140
141 // check that there isn't another character-map with the same name and import
142 // precedence
143
144 PrincipalStylesheetModule psm = getPrincipalStylesheetModule();
145 ComponentDeclaration other = psm.getCharacterMap(getObjectName());
146 if (other.getSourceElement() != this) {
147 if (decl.getPrecedence() == other.getPrecedence()) {
148 compileError("There are two character-maps with the same name and import precedence", "XTSE1580");
149 } else if (decl.getPrecedence() < other.getPrecedence()) {
150 redundant = true;
151 }
152 }
153
154 // validate the use-character-maps attribute
155
156 if (use != null) {
157
158 // identify any character maps that this one refers to
159
160 characterMapElements = new ArrayList<XSLCharacterMap>(5);
161 StringTokenizer st = new StringTokenizer(use, " \t\n\r", false);
162
163 while (st.hasMoreTokens()) {
164 String displayname = st.nextToken();
165 try {
166 String[] parts = NameChecker.getQNameParts(displayname);
167 String uri = getURIForPrefix(parts[0], false);
168 if (uri == null) {
169 compileError("Undeclared namespace prefix " + Err.wrap(parts[0])
170 + " in character map name", "XTSE0280");
171 }
172 StructuredQName qn = new StructuredQName(parts[0], uri, parts[1]);
173 ComponentDeclaration charMapDecl = psm.getCharacterMap(qn);
174 if (charMapDecl == null) {
175 compileError("No character-map named '" + displayname + "' has been defined", "XTSE1590");
176 } else {
177 XSLCharacterMap ref = (XSLCharacterMap) charMapDecl.getSourceElement();
178 characterMapElements.add(ref);
179 }
180 } catch (QNameException err) {
181 compileError("Invalid character-map name. " + err.getMessage(), "XTSE1590");
182 }
183 }
184
185 // check for circularity
186
187 for (Object characterMapElement : characterMapElements) {
188 ((XSLCharacterMap) characterMapElement).checkCircularity(this);
189 }
190 }
191
192 validated = true;
193 }
194
195 /**
196 * Check for circularity: specifically, check that this attribute set does not contain
197 * a direct or indirect reference to the one supplied as a parameter
198 *
199 * @param origin the start point of the search
200 * @throws net.sf.saxon.trans.XPathException
201 * if an error is detected
202 */
203
204 private void checkCircularity(XSLCharacterMap origin) throws XPathException {
205 if (this == origin) {
206 compileError("The definition of the character map is circular", "XTSE1600");
207 characterMapElements = null; // for error recovery
208 } else {
209 if (!validated) {
210 // if this attribute set isn't validated yet, we don't check it.
211 // The circularity will be detected when the last attribute set in the cycle
212 // gets validated
213 return;
214 }
215 if (characterMapElements != null) {
216 for (Object characterMapElement : characterMapElements) {
217 ((XSLCharacterMap) characterMapElement).checkCircularity(origin);
218 }
219 }
220 }
221 }
222
223 /**
224 * Assemble all the mappings defined by this character map, adding them to a
225 * HashMap that maps integer codepoints to strings
226 *
227 * @param map an IntHash map populated with the character mappings
228 */
229
230 public void assemble(IntHashMap<String> map) {
231 if (characterMapElements != null) {
232 for (XSLCharacterMap charmap : characterMapElements) {
233 charmap.assemble(map);
234 }
235 }
236 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
237 while (true) {
238 Item child = kids.next();
239 if (child == null) {
240 return;
241 }
242 XSLOutputCharacter oc = (XSLOutputCharacter) child;
243 map.put(oc.getCodePoint(), oc.getReplacementString());
244 }
245 }
246
247 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException {
248 // no action
249 }
250
251 }
252