Debian Codegraph @

net/sf/saxon/serialize/UnicodeNormalizer.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.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 * UnicodeNormalizer: This ProxyReceiver performs unicode normalization on the contents
23 * of attribute and text nodes.
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 * Get the underlying normalizer
57 * @return the underlying Normalizer
58 */
59
60 public Normalizer getNormalizer() {
61 return normalizer;
62 }
63
64 /**
65 * Output an attribute
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 * Output character data
76 */
77
78 public void characters(/*@NotNull*/ 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