LANG-744 Use IOD for stripAccent init

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1178626 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2011-10-03 23:04:54 +00:00
parent a340f1e7af
commit 5244e89e73
1 changed files with 58 additions and 55 deletions

View File

@ -630,15 +630,15 @@ public class StringUtils {
}
try {
String result = null;
if (java6NormalizeMethod != null) {
if (InitStripAccents.java6NormalizeMethod != null) {
result = removeAccentsJava6(input);
} else if (sunDecomposeMethod != null) {
} else if (InitStripAccents.sunDecomposeMethod != null) {
result = removeAccentsSUN(input);
} else {
throw new UnsupportedOperationException(
"The stripAccents(CharSequence) method requires at least"
+" Java6, but got: "+java6Exception
+"; or a Sun JVM: "+sunException);
+" Java6, but got: "+InitStripAccents.java6Exception
+"; or a Sun JVM: "+InitStripAccents.sunException);
}
// Note that none of the above methods correctly remove ligatures...
return result;
@ -669,12 +669,12 @@ public class StringUtils {
String decomposed = java.text.Normalizer.normalize(CharSequence, Normalizer.Form.NFD);
return java6Pattern.matcher(decomposed).replaceAll("");//$NON-NLS-1$
*/
if (java6NormalizeMethod == null || java6NormalizerFormNFD == null) {
throw new IllegalStateException("java.text.Normalizer is not available", java6Exception);
if (InitStripAccents.java6NormalizeMethod == null || InitStripAccents.java6NormalizerFormNFD == null) {
throw new IllegalStateException("java.text.Normalizer is not available", InitStripAccents.java6Exception);
}
String result;
result = (String) java6NormalizeMethod.invoke(null, new Object[] {text, java6NormalizerFormNFD});
result = java6Pattern.matcher(result).replaceAll("");//$NON-NLS-1$
result = (String) InitStripAccents.java6NormalizeMethod.invoke(null, new Object[] {text, InitStripAccents.java6NormalizerFormNFD});
result = InitStripAccents.java6Pattern.matcher(result).replaceAll("");//$NON-NLS-1$
return result;
}
@ -693,62 +693,65 @@ public class StringUtils {
String decomposed = sun.text.Normalizer.decompose(text, false, 0);
return sunPattern.matcher(decomposed).replaceAll("");//$NON-NLS-1$
*/
if (sunDecomposeMethod == null) {
throw new IllegalStateException("sun.text.Normalizer is not available", sunException);
if (InitStripAccents.sunDecomposeMethod == null) {
throw new IllegalStateException("sun.text.Normalizer is not available", InitStripAccents.sunException);
}
String result;
result = (String) sunDecomposeMethod.invoke(null, new Object[] {text, Boolean.FALSE, Integer.valueOf(0)});
result = sunPattern.matcher(result).replaceAll("");//$NON-NLS-1$
result = (String) InitStripAccents.sunDecomposeMethod.invoke(null, new Object[] {text, Boolean.FALSE, Integer.valueOf(0)});
result = InitStripAccents.sunPattern.matcher(result).replaceAll("");//$NON-NLS-1$
return result;
}
// SUN internal, Java 1.3 -> Java 5
private static final Throwable sunException;
private static final Method sunDecomposeMethod;
private static final Pattern sunPattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");//$NON-NLS-1$
// Java 6+
private static final Throwable java6Exception;
private static final Method java6NormalizeMethod;
private static final Object java6NormalizerFormNFD;
private static final Pattern java6Pattern = sunPattern;
static {
// Set up defaults for final static fields
Object _java6NormalizerFormNFD = null;
Method _java6NormalizeMethod = null;
Method _sunDecomposeMethod = null;
Throwable _java6Exception = null;
Throwable _sunException = null;
try {
// java.text.Normalizer.normalize(CharSequence, Normalizer.Form.NFD);
// Be careful not to get Java 1.3 java.text.Normalizer!
Class<?> normalizerFormClass = Thread.currentThread().getContextClassLoader()
.loadClass("java.text.Normalizer$Form");//$NON-NLS-1$
_java6NormalizerFormNFD = normalizerFormClass.getField("NFD").get(null);//$NON-NLS-1$
Class<?> normalizerClass = Thread.currentThread().getContextClassLoader()
.loadClass("java.text.Normalizer");//$NON-NLS-1$
_java6NormalizeMethod = normalizerClass.getMethod("normalize",//$NON-NLS-1$
new Class[] {CharSequence.class, normalizerFormClass});//$NON-NLS-1$
} catch (Exception e1) {
// Only check for Sun method if Java 6 method is not available
_java6Exception = e1;
// IOD container for stripAccent() initialisation
private static class InitStripAccents {
// SUN internal, Java 1.3 -> Java 5
private static final Throwable sunException;
private static final Method sunDecomposeMethod;
private static final Pattern sunPattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");//$NON-NLS-1$
// Java 6+
private static final Throwable java6Exception;
private static final Method java6NormalizeMethod;
private static final Object java6NormalizerFormNFD;
private static final Pattern java6Pattern = sunPattern;
static {
// Set up defaults for final static fields
Object _java6NormalizerFormNFD = null;
Method _java6NormalizeMethod = null;
Method _sunDecomposeMethod = null;
Throwable _java6Exception = null;
Throwable _sunException = null;
try {
// sun.text.Normalizer.decompose(text, false, 0);
// java.text.Normalizer.normalize(CharSequence, Normalizer.Form.NFD);
// Be careful not to get Java 1.3 java.text.Normalizer!
Class<?> normalizerFormClass = Thread.currentThread().getContextClassLoader()
.loadClass("java.text.Normalizer$Form");//$NON-NLS-1$
_java6NormalizerFormNFD = normalizerFormClass.getField("NFD").get(null);//$NON-NLS-1$
Class<?> normalizerClass = Thread.currentThread().getContextClassLoader()
.loadClass("sun.text.Normalizer");//$NON-NLS-1$
_sunDecomposeMethod = normalizerClass.getMethod("decompose",//$NON-NLS-1$
new Class[] {String.class, Boolean.TYPE, Integer.TYPE});//$NON-NLS-1$
} catch (Exception e2) {
_sunException = e2;
.loadClass("java.text.Normalizer");//$NON-NLS-1$
_java6NormalizeMethod = normalizerClass.getMethod("normalize",//$NON-NLS-1$
new Class[] {CharSequence.class, normalizerFormClass});//$NON-NLS-1$
} catch (Exception e1) {
// Only check for Sun method if Java 6 method is not available
_java6Exception = e1;
try {
// sun.text.Normalizer.decompose(text, false, 0);
Class<?> normalizerClass = Thread.currentThread().getContextClassLoader()
.loadClass("sun.text.Normalizer");//$NON-NLS-1$
_sunDecomposeMethod = normalizerClass.getMethod("decompose",//$NON-NLS-1$
new Class[] {String.class, Boolean.TYPE, Integer.TYPE});//$NON-NLS-1$
} catch (Exception e2) {
_sunException = e2;
}
}
// Set up final static fields
java6Exception = _java6Exception;
java6NormalizerFormNFD = _java6NormalizerFormNFD;
java6NormalizeMethod = _java6NormalizeMethod;
sunException = _sunException;
sunDecomposeMethod = _sunDecomposeMethod;
}
// Set up final static fields
java6Exception = _java6Exception;
java6NormalizerFormNFD = _java6NormalizerFormNFD;
java6NormalizeMethod = _java6NormalizeMethod;
sunException = _sunException;
sunDecomposeMethod = _sunDecomposeMethod;
}
// Equals