Debian Codegraph @

net/sf/saxon/serialize/CharacterMap.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.om.StructuredQName;
11 import net.sf.saxon.serialize.charcode.UTF16CharacterSet;
12 import net.sf.saxon.trace.ExpressionPresenter;
13 import net.sf.saxon.tree.tiny.CompressedWhitespace;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.value.Whitespace;
16 import net.sf.saxon.z.IntHashMap;
17 import net.sf.saxon.z.IntIterator;
18
19 /**
20 * This class defines a character map, that is, a mapping from characters to strings used by the serializer
21 * when mapping individual characters in the output.
22 */
23 public class CharacterMap {
24
25 private StructuredQName name;
26 private IntHashMap<String> charMap;
27 private int min = Integer.MAX_VALUE; // the lowest mapped character
28 private int max = 0; // the highest mapped character
29 private boolean mapsWhitespace = false;
30
31 /**
32 * Create a CharacterMap from a raw map of integers to strings
33 *
34 * @param map the mapping of integer Unicode character codes to strings. This must not be subsequently changed.
35 */
36
37 public CharacterMap(StructuredQName name, IntHashMap<String> map) {
38 this.name = name;
39 this.charMap = map;
40 init();
41 }
42
43 /**
44 * Create a CharacterMap that combines a set of existing character maps.
45 *
46 * @param list the list of existing character maps. If the same character
47 * is mapped by more than one map in the list, the last mapping takes
48 * precedence
49 */
50
51 public CharacterMap(Iterable<CharacterMap> list) {
52 charMap = new IntHashMap<String>(64);
53 for (CharacterMap map : list) {
54 IntIterator keys = map.charMap.keyIterator();
55 while (keys.hasNext()) {
56 int next = keys.next();
57 charMap.put(next, map.charMap.get(next));
58 }
59 }
60 init();
61 }
62
63 private void init() {
64 IntIterator keys = charMap.keyIterator();
65 while (keys.hasNext()) {
66 int next = keys.next();
67 if (next < min) {
68 min = next;
69 }
70 if (next > max) {
71 max = next;
72 }
73 if (!mapsWhitespace && Whitespace.isWhitespace(next)) {
74 mapsWhitespace = true;
75 }
76 }
77 if (min > 0xD800) {
78 // if all the mapped characters are above the BMP, we need to check
79 // surrogates
80 min = 0xD800;
81 }
82 }
83
84 /**
85 * Get the name of the character map
86 * @return the name given in the stylesheet if this is an un-expanded character map, or null if
87 * this is an expanded map (a map combined with the character maps that it references)
88 */
89
90 public StructuredQName getName() {
91 return name;
92 }
93
94 /**
95 * Expand all the characters in a string using this character mapping
96 *
97 * @param in the input string to be mapped
98 * @param insertNulls true if null (0) characters are to be inserted before
99 * and after replacement characters. This is done to signal
100 * that output escaping of these characters is disabled. The flag is set to true when writing
101 * XML or HTML, but to false when writing TEXT.
102 */
103
104 /*@NotNull*/
105 public CharSequence map(CharSequence in, boolean insertNulls) {
106
107 if (!mapsWhitespace && in instanceof CompressedWhitespace) {
108 return in;
109 }
110
111 // First scan the string to see if there are any possible mapped
112 // characters; if not, don't bother creating the new buffer
113
114 boolean move = false;
115 for (int i = 0; i < in.length(); ) {
116 char c = in.charAt(i++);
117 if (c >= min && c <= max) {
118 move = true;
119 break;
120 }
121 }
122 if (!move) {
123 return in;
124 }
125
126 FastStringBuffer buffer = new FastStringBuffer(in.length() * 2);
127 int i = 0;
128 while (i < in.length()) {
129 char c = in.charAt(i++);
130 if (c >= min && c <= max) {
131 if (UTF16CharacterSet.isHighSurrogate(c)) {
132 // assume the string is properly formed
133 char d = in.charAt(i++);
134 int s = UTF16CharacterSet.combinePair(c, d);
135 String rep = charMap.get(s);
136 if (rep == null) {
137 buffer.append(c);
138 buffer.append(d);
139 } else {
140 if (insertNulls) {
141 buffer.append((char) 0);
142 buffer.append(rep);
143 buffer.append((char) 0);
144 } else {
145 buffer.append(rep);
146 }
147 }
148 } else {
149 String rep = charMap.get(c);
150 if (rep == null) {
151 buffer.append(c);
152 } else {
153 if (insertNulls) {
154 buffer.append((char) 0);
155 buffer.append(rep);
156 buffer.append((char) 0);
157 } else {
158 buffer.append(rep);
159 }
160 }
161 }
162 } else {
163 buffer.append(c);
164 }
165 }
166 return buffer;
167 }
168
169 /**
170 * Write the character map to an export file
171 * @param out the output destination
172 */
173
174 public void export(ExpressionPresenter out) {
175 out.startElement("charMap");
176 out.emitAttribute("name", name);
177 for (IntIterator iter = charMap.keyIterator(); iter.hasNext();) {
178 int c = iter.next();
179 String s = charMap.get(c);
180 out.startElement("m");
181 out.emitAttribute("c", c+"");
182 out.emitAttribute("s", s);
183 out.endElement();
184 }
185 out.endElement();
186 }
187
188
189 }
190