1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.util;
35
36 import info.magnolia.cms.core.SystemProperty;
37 import info.magnolia.objectfactory.Components;
38
39 import java.lang.reflect.InvocationTargetException;
40 import java.lang.reflect.Method;
41 import javax.inject.Singleton;
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class UnicodeNormalizer {
56 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UnicodeNormalizer.class);
57
58 private static final String JAVA6_NORMALIZER_CLASS = "java.text.Normalizer";
59 private static final String JAVA6_FORMPARAM_CLASS = "java.text.Normalizer$Form";
60
61 private static final Normalizer normalizer = Components.getSingleton(Normalizer.class);
62
63 public static String[] normalizeNFC(String[] values) {
64 if (values == null) {
65 return null;
66 }
67 for (int i = 0; i < values.length; i++) {
68 values[i] = normalizeNFC(values[i]);
69 }
70 return values;
71 }
72
73
74
75
76 public static String normalizeNFC(String in) {
77 if (in == null) {
78 return null;
79 }
80 return normalizer.normalizeNFC(in);
81
82
83
84
85
86
87
88
89
90
91
92 }
93
94
95
96
97 public interface Normalizer {
98 String normalizeNFC(String in);
99 }
100
101
102
103
104 @Singleton
105 public static final class Java6Normalizer implements Normalizer {
106 private final Method normalize;
107 private final Object nfc;
108
109 public Java6Normalizer() {
110 try {
111 final Class<?> normalizer = Class.forName(JAVA6_NORMALIZER_CLASS);
112 final Class<?> form = Class.forName(JAVA6_FORMPARAM_CLASS);
113 normalize = normalizer.getMethod("normalize", CharSequence.class, form);
114 nfc = form.getField("NFC").get(null);
115 } catch (ClassNotFoundException e) {
116 throw new RuntimeException(e);
117 } catch (IllegalAccessException e) {
118 throw new RuntimeException(e);
119 } catch (NoSuchFieldException e) {
120 throw new RuntimeException(e);
121 } catch (NoSuchMethodException e) {
122 throw new RuntimeException(e);
123 }
124
125 }
126
127 @Override
128 public String normalizeNFC(String in) {
129 try {
130 return (String) normalize.invoke(null, in, nfc);
131 } catch (IllegalAccessException e) {
132 throw new RuntimeException(e);
133 } catch (InvocationTargetException e) {
134 throw new RuntimeException(e);
135 }
136 }
137 }
138
139
140
141
142 @Singleton
143 public static final class NonNormalizer implements UnicodeNormalizer.Normalizer {
144 @Override
145 public String normalizeNFC(String in) {
146 return in;
147 }
148 }
149
150
151
152
153 @Singleton
154 public static final class AutoDetectNormalizer implements Normalizer {
155 private final Normalizer delegate;
156
157 public AutoDetectNormalizer() {
158 Normalizer candidate;
159 if (SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_UTF8_ENABLED)) {
160 candidate = new Java6Normalizer();
161
162 } else {
163 candidate = new NonNormalizer();
164 }
165 this.delegate = candidate;
166 }
167
168 @Override
169 public String normalizeNFC(String in) {
170 return delegate.normalizeNFC(in);
171 }
172 }
173
174 }