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  import javax.inject.Singleton;
42  
43  /**
44   * A wrapper around java.text.Normalizer
45   *
46   * <strong>note:</strong> If needed, one can use their own implementation, 
47   * by setting the info.magnolia.cms.util.UnicodeNormalizer$Normalizer system property.
48   *
49   * @see java.text.Normalizer
50   * @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.
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       * Normalizes the given String to the NFC form.
72       */
73      public static String normalizeNFC(String in) {
74          if (in == null) {
75              return null;
76          }
77          return normalizer.normalizeNFC(in);
78          /* if you're in dire need to debug:
79           try {
80              log.debug("not normalized: " + Arrays.toString(in.getBytes("UTF-8")) + " (" + in + ")");
81              String out = normalizer.normalizeNFC(in);
82              log.debug("    normalized: " + Arrays.toString(out.getBytes("UTF-8")) + " (" + out + ")");
83              return out;
84          } catch (UnsupportedEncodingException e) {
85              // do nothing
86          }
87          return in;
88          */
89      }
90  
91      /**
92       * Used to normalize a String.
93       */
94      public interface Normalizer {
95          String normalizeNFC(String in);
96      }
97  
98      /**
99       * Java 6 Normalizer wrapper.
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      * Returns the original value unchanged.
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      * Tries to load the normalizer dynamically and respects the property {@link SystemProperty#MAGNOLIA_UTF8_ENABLED}.
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 }