View Javadoc
1   /**
2    * This file Copyright (c) 2009-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * A wrapper around java.text.Normalizer
46   *
47   * <strong>note:</strong> If needed, one can use their own implementation,
48   * by setting the info.magnolia.cms.util.UnicodeNormalizer$Normalizer system property.
49   *
50   * @see java.text.Normalizer
51   * @see <a href="http://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms">http://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms</a> for more information.
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       * Normalizes the given String to the NFC form.
73       */
74      public static String normalizeNFC(String in) {
75          if (in == null) {
76              return null;
77          }
78          return normalizer.normalizeNFC(in);
79          /* if you're in dire need to debug:
80           try {
81              log.debug("not normalized: " + Arrays.toString(in.getBytes("UTF-8")) + " (" + in + ")");
82              String out = normalizer.normalizeNFC(in);
83              log.debug("    normalized: " + Arrays.toString(out.getBytes("UTF-8")) + " (" + out + ")");
84              return out;
85          } catch (UnsupportedEncodingException e) {
86              // do nothing
87          }
88          return in;
89          */
90      }
91  
92      /**
93       * Used to normalize a String.
94       */
95      public interface Normalizer {
96          String normalizeNFC(String in);
97      }
98  
99      /**
100      * Java 6 Normalizer wrapper.
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      * Returns the original value unchanged.
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      * Tries to load the normalizer dynamically and respects the property {@link SystemProperty#MAGNOLIA_UTF8_ENABLED}.
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 }