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
42 import javax.inject.Singleton;
43
44
45
46
47
48
49
50
51
52
53 public class UnicodeNormalizer {
54 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UnicodeNormalizer.class);
55
56 private static final String JAVA6_NORMALIZER_CLASS = "java.text.Normalizer";
57 private static final String JAVA6_FORMPARAM_CLASS = "java.text.Normalizer$Form";
58
59 private static final Normalizer normalizer = Components.getSingleton(Normalizer.class);
60
61 public static String[] normalizeNFC(String[] values) {
62 if (values == null) {
63 return null;
64 }
65 for (int i = 0; i < values.length; i++) {
66 values[i] = normalizeNFC(values[i]);
67 }
68 return values;
69 }
70
71
72
73
74 public static String normalizeNFC(String in) {
75 if (in == null) {
76 return null;
77 }
78 return normalizer.normalizeNFC(in);
79
80
81
82
83
84
85
86
87
88
89
90 }
91
92
93
94
95 public interface Normalizer {
96 String normalizeNFC(String in);
97 }
98
99
100
101
102 @Singleton
103 public static final class Java6Normalizer implements Normalizer {
104 private final Method normalize;
105 private final Object nfc;
106
107 public Java6Normalizer() {
108 try {
109 final Class<?> normalizer = Class.forName(JAVA6_NORMALIZER_CLASS);
110 final Class<?> form = Class.forName(JAVA6_FORMPARAM_CLASS);
111 normalize = normalizer.getMethod("normalize", CharSequence.class, form);
112 nfc = form.getField("NFC").get(null);
113 } catch (ClassNotFoundException e) {
114 throw new RuntimeException(e);
115 } catch (IllegalAccessException e) {
116 throw new RuntimeException(e);
117 } catch (NoSuchFieldException e) {
118 throw new RuntimeException(e);
119 } catch (NoSuchMethodException e) {
120 throw new RuntimeException(e);
121 }
122
123 }
124
125 @Override
126 public String normalizeNFC(String in) {
127 try {
128 return (String) normalize.invoke(null, in, nfc);
129 } catch (IllegalAccessException e) {
130 throw new RuntimeException(e);
131 } catch (InvocationTargetException e) {
132 throw new RuntimeException(e);
133 }
134 }
135 }
136
137
138
139
140 @Singleton
141 public static final class NonNormalizer implements UnicodeNormalizer.Normalizer {
142 @Override
143 public String normalizeNFC(String in) {
144 return in;
145 }
146 }
147
148
149
150
151 @Singleton
152 public static final class AutoDetectNormalizer implements Normalizer {
153 private final Normalizer delegate;
154
155 public AutoDetectNormalizer() {
156 Normalizer candidate;
157 if (SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_UTF8_ENABLED)) {
158 candidate = new Java6Normalizer();
159
160 } else {
161 candidate = new NonNormalizer();
162 }
163 this.delegate = candidate;
164 }
165
166 @Override
167 public String normalizeNFC(String in) {
168 return delegate.normalizeNFC(in);
169 }
170 }
171
172 }