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