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