View Javadoc

1   /**
2    * This file Copyright (c) 2009-2014 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   * @author gjoseph
53   * @version $Revision: $ ($Author: $)
54   */
55  public class UnicodeNormalizer {
56      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(UnicodeNormalizer.class);
57  
58      private static final String JAVA6_NORMALIZER_CLASS = "java.text.Normalizer";
59      private static final String JAVA6_FORMPARAM_CLASS = "java.text.Normalizer$Form";
60  
61      private static final Normalizer normalizer = Components.getSingleton(Normalizer.class);
62  
63      public static String[] normalizeNFC(String[] values) {
64          if (values == null) {
65              return null;
66          }
67          for (int i = 0; i < values.length; i++) {
68              values[i] = normalizeNFC(values[i]);
69          }
70          return values;
71      }
72  
73      /**
74       * Normalizes the given String to the NFC form.
75       */
76      public static String normalizeNFC(String in) {
77          if (in == null) {
78              return null;
79          }
80          return normalizer.normalizeNFC(in);
81          /* if you're in dire need to debug:
82           try {
83              log.debug("not normalized: " + Arrays.toString(in.getBytes("UTF-8")) + " (" + in + ")");
84              String out = normalizer.normalizeNFC(in);
85              log.debug("    normalized: " + Arrays.toString(out.getBytes("UTF-8")) + " (" + out + ")");
86              return out;
87          } catch (UnsupportedEncodingException e) {
88              // do nothing
89          }
90          return in;
91          */
92      }
93  
94      /**
95       * Used to normalize a String.
96       */
97      public interface Normalizer {
98          String normalizeNFC(String in);
99      }
100 
101     /**
102      * Java 6 Normalizer wrapper.
103      */
104     @Singleton
105     public static final class Java6Normalizer implements Normalizer {
106         private final Method normalize;
107         private final Object nfc;
108 
109         public Java6Normalizer() {
110             try {
111                 final Class<?> normalizer = Class.forName(JAVA6_NORMALIZER_CLASS);
112                 final Class<?> form = Class.forName(JAVA6_FORMPARAM_CLASS);
113                 normalize = normalizer.getMethod("normalize", CharSequence.class, form);
114                 nfc = form.getField("NFC").get(null);
115             } catch (ClassNotFoundException e) {
116                 throw new RuntimeException(e);
117             } catch (IllegalAccessException e) {
118                 throw new RuntimeException(e);
119             } catch (NoSuchFieldException e) {
120                 throw new RuntimeException(e);
121             } catch (NoSuchMethodException e) {
122                 throw new RuntimeException(e);
123             }
124 
125         }
126 
127         @Override
128         public String normalizeNFC(String in) {
129             try {
130                 return (String) normalize.invoke(null, in, nfc);
131             } catch (IllegalAccessException e) {
132                 throw new RuntimeException(e);
133             } catch (InvocationTargetException e) {
134                 throw new RuntimeException(e);
135             }
136         }
137     }
138 
139     /**
140      * Returns the original value unchanged.
141      */
142     @Singleton
143     public static final class NonNormalizer implements UnicodeNormalizer.Normalizer {
144         @Override
145         public String normalizeNFC(String in) {
146             return in;
147         }
148     }
149 
150     /**
151      * Tries to load the normalizer dynamically and respects the property {@link SystemProperty#MAGNOLIA_UTF8_ENABLED}.
152      */
153     @Singleton
154     public static final class AutoDetectNormalizer implements Normalizer {
155         private final Normalizer delegate;
156 
157         public AutoDetectNormalizer() {
158             Normalizer candidate;
159             if (SystemProperty.getBooleanProperty(SystemProperty.MAGNOLIA_UTF8_ENABLED)) {
160                 candidate = new Java6Normalizer();
161                 
162             } else {
163                 candidate = new NonNormalizer();
164             }
165             this.delegate = candidate;
166         }
167 
168         @Override
169         public String normalizeNFC(String in) {
170             return delegate.normalizeNFC(in);
171         }
172     }
173 
174 }