| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
public class CharacterMap { |
| 24 |
|
| 25 |
private StructuredQName name; |
| 26 |
private IntHashMap<String> charMap; |
| 27 |
private int min = Integer.MAX_VALUE; |
| 28 |
private int max = 0; |
| 29 |
private boolean mapsWhitespace = false; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public CharacterMap(StructuredQName name, IntHashMap<String> map) { |
| 38 |
this.name = name; |
| 39 |
this.charMap = map; |
| 40 |
init(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 79 |
|
| 80 |
min = 0xD800; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public StructuredQName getName() { |
| 91 |
return name; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public CharSequence map(CharSequence in, boolean insertNulls) { |
| 106 |
|
| 107 |
if (!mapsWhitespace && in instanceof CompressedWhitespace) { |
| 108 |
return in; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 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 |
|
| 171 |
|
| 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 |
|