| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.ProxyReceiver; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.event.ReceiverOptions; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.om.NodeName; |
| 15 |
import net.sf.saxon.serialize.codenorm.Normalizer; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.type.SimpleType; |
| 19 |
import net.sf.saxon.value.Whitespace; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class UnicodeNormalizer extends ProxyReceiver { |
| 28 |
|
| 29 |
private Normalizer normalizer; |
| 30 |
|
| 31 |
public UnicodeNormalizer(String form, Receiver next) throws XPathException { |
| 32 |
super(next); |
| 33 |
byte fb; |
| 34 |
switch (form) { |
| 35 |
case "NFC": |
| 36 |
fb = Normalizer.C; |
| 37 |
break; |
| 38 |
case "NFD": |
| 39 |
fb = Normalizer.D; |
| 40 |
break; |
| 41 |
case "NFKC": |
| 42 |
fb = Normalizer.KC; |
| 43 |
break; |
| 44 |
case "NFKD": |
| 45 |
fb = Normalizer.KD; |
| 46 |
break; |
| 47 |
default: |
| 48 |
XPathException err = new XPathException("Unknown normalization form " + form); |
| 49 |
err.setErrorCode("SESU0011"); |
| 50 |
throw err; |
| 51 |
} |
| 52 |
normalizer = new Normalizer(fb, getConfiguration()); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public Normalizer getNormalizer() { |
| 61 |
return normalizer; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) |
| 69 |
throws XPathException { |
| 70 |
nextReceiver.attribute(nameCode, typeCode, |
| 71 |
normalize(value, (properties & ReceiverOptions.USE_NULL_MARKERS) != 0), locationId, properties); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public void characters( CharSequence chars, Location locationId, int properties) throws XPathException { |
| 79 |
if (Whitespace.isWhite(chars)) { |
| 80 |
nextReceiver.characters(chars, locationId, properties); |
| 81 |
} else { |
| 82 |
nextReceiver.characters(normalize(chars, (properties & ReceiverOptions.USE_NULL_MARKERS) != 0), locationId, properties); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private CharSequence normalize(CharSequence in, boolean containsNullMarkers) { |
| 87 |
if (containsNullMarkers) { |
| 88 |
FastStringBuffer out = new FastStringBuffer(in.length()); |
| 89 |
String s = in.toString(); |
| 90 |
int start = 0; |
| 91 |
int nextNull = s.indexOf((char)0); |
| 92 |
while (nextNull >= 0) { |
| 93 |
out.append(normalizer.normalize(s.substring(start, nextNull))); |
| 94 |
out.append((char) 0); |
| 95 |
start = nextNull + 1; |
| 96 |
nextNull = s.indexOf((char) 0, start); |
| 97 |
out.append(s.substring(start, nextNull)); |
| 98 |
out.append((char) 0); |
| 99 |
start = nextNull + 1; |
| 100 |
nextNull = s.indexOf((char) 0, start); |
| 101 |
} |
| 102 |
out.append(normalizer.normalize(s.substring(start))); |
| 103 |
return out.condense(); |
| 104 |
} else { |
| 105 |
return normalizer.normalize(in); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|