feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.istack.internal.localization;
|
||||
|
||||
/**
|
||||
* Localizable message.
|
||||
*
|
||||
* @author WS Development Team
|
||||
*/
|
||||
public interface Localizable {
|
||||
/**
|
||||
* Gets the key in the resource bundle.
|
||||
*
|
||||
* @return
|
||||
* if this method returns {@link #NOT_LOCALIZABLE},
|
||||
* that means the message is not localizable, and
|
||||
* the first item of {@link #getArguments()} array
|
||||
* holds a String.
|
||||
*/
|
||||
public String getKey();
|
||||
|
||||
/**
|
||||
* Returns the arguments for message formatting.
|
||||
*
|
||||
* @return
|
||||
* can be an array of length 0 but never be null.
|
||||
*/
|
||||
public Object[] getArguments();
|
||||
public String getResourceBundleName();
|
||||
|
||||
|
||||
/**
|
||||
* Special constant that represents a message that
|
||||
* is not localizable.
|
||||
*
|
||||
* <p>
|
||||
* Use of "new" is to create an unique instance.
|
||||
*/
|
||||
public static final String NOT_LOCALIZABLE = "\u0000";
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.istack.internal.localization;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author WS Development Team
|
||||
*/
|
||||
public final class LocalizableMessage implements Localizable {
|
||||
|
||||
private final String _bundlename;
|
||||
private final String _key;
|
||||
private final Object[] _args;
|
||||
|
||||
public LocalizableMessage(String bundlename, String key, Object... args) {
|
||||
_bundlename = bundlename;
|
||||
_key = key;
|
||||
if(args==null)
|
||||
args = new Object[0];
|
||||
_args = args;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return _key;
|
||||
}
|
||||
|
||||
public Object[] getArguments() {
|
||||
return Arrays.copyOf(_args, _args.length);
|
||||
}
|
||||
|
||||
public String getResourceBundleName() {
|
||||
return _bundlename;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.istack.internal.localization;
|
||||
|
||||
/**
|
||||
* @author WS Development Team
|
||||
*/
|
||||
public class LocalizableMessageFactory {
|
||||
|
||||
private final String _bundlename;
|
||||
|
||||
public LocalizableMessageFactory(String bundlename) {
|
||||
_bundlename = bundlename;
|
||||
}
|
||||
|
||||
public Localizable getMessage(String key, Object... args) {
|
||||
return new LocalizableMessage(_bundlename, key, args);
|
||||
}
|
||||
|
||||
}
|
||||
155
jdkSrc/jdk8/com/sun/istack/internal/localization/Localizer.java
Normal file
155
jdkSrc/jdk8/com/sun/istack/internal/localization/Localizer.java
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.istack.internal.localization;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
* Localizes the {@link Localizable} into a message
|
||||
* by using a configured {@link Locale}.
|
||||
*
|
||||
* @author WS Development Team
|
||||
*/
|
||||
public class Localizer {
|
||||
|
||||
private final Locale _locale;
|
||||
private final HashMap _resourceBundles;
|
||||
|
||||
public Localizer() {
|
||||
this(Locale.getDefault());
|
||||
}
|
||||
|
||||
public Localizer(Locale l) {
|
||||
_locale = l;
|
||||
_resourceBundles = new HashMap();
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return _locale;
|
||||
}
|
||||
|
||||
public String localize(Localizable l) {
|
||||
String key = l.getKey();
|
||||
if (key == Localizable.NOT_LOCALIZABLE) {
|
||||
// this message is not localizable
|
||||
return (String) l.getArguments()[0];
|
||||
}
|
||||
String bundlename = l.getResourceBundleName();
|
||||
|
||||
try {
|
||||
ResourceBundle bundle =
|
||||
(ResourceBundle) _resourceBundles.get(bundlename);
|
||||
|
||||
if (bundle == null) {
|
||||
try {
|
||||
bundle = ResourceBundle.getBundle(bundlename, _locale);
|
||||
} catch (MissingResourceException e) {
|
||||
// work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
|
||||
// all files with an extension different from .class (hence all the .properties files)
|
||||
// get copied to the top level directory instead of being in the package where they
|
||||
// are defined
|
||||
// so, since we can't find the bundle under its proper name, we look for it under
|
||||
// the top-level package
|
||||
|
||||
int i = bundlename.lastIndexOf('.');
|
||||
if (i != -1) {
|
||||
String alternateBundleName =
|
||||
bundlename.substring(i + 1);
|
||||
try {
|
||||
bundle =
|
||||
ResourceBundle.getBundle(
|
||||
alternateBundleName,
|
||||
_locale);
|
||||
} catch (MissingResourceException e2) {
|
||||
//try context classloader
|
||||
try {
|
||||
bundle = ResourceBundle.getBundle(bundlename, _locale, Thread.currentThread().getContextClassLoader());
|
||||
} catch (MissingResourceException e3) {
|
||||
// give up
|
||||
return getDefaultMessage(l);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_resourceBundles.put(bundlename, bundle);
|
||||
}
|
||||
|
||||
if (bundle == null) {
|
||||
return getDefaultMessage(l);
|
||||
}
|
||||
|
||||
if (key == null)
|
||||
key = "undefined";
|
||||
|
||||
String msg;
|
||||
try {
|
||||
msg = bundle.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
// notice that this may throw a MissingResourceException of its own (caught below)
|
||||
msg = bundle.getString("undefined");
|
||||
}
|
||||
|
||||
// localize all arguments to the given localizable object
|
||||
Object[] args = l.getArguments();
|
||||
for (int i = 0; i < args.length; ++i) {
|
||||
if (args[i] instanceof Localizable)
|
||||
args[i] = localize((Localizable) args[i]);
|
||||
}
|
||||
|
||||
String message = MessageFormat.format(msg, args);
|
||||
return message;
|
||||
|
||||
} catch (MissingResourceException e) {
|
||||
return getDefaultMessage(l);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getDefaultMessage(Localizable l) {
|
||||
String key = l.getKey();
|
||||
Object[] args = l.getArguments();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("[failed to localize] ");
|
||||
sb.append(key);
|
||||
if (args != null) {
|
||||
sb.append('(');
|
||||
for (int i = 0; i < args.length; ++i) {
|
||||
if (i != 0)
|
||||
sb.append(", ");
|
||||
sb.append(String.valueOf(args[i]));
|
||||
}
|
||||
sb.append(')');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.istack.internal.localization;
|
||||
|
||||
/**
|
||||
* {@link Localizable} that wraps a non-localizable string.
|
||||
*
|
||||
* @author WS Development Team
|
||||
*/
|
||||
public final class NullLocalizable implements Localizable {
|
||||
private final String msg;
|
||||
|
||||
public NullLocalizable(String msg) {
|
||||
if(msg==null)
|
||||
throw new IllegalArgumentException();
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return Localizable.NOT_LOCALIZABLE;
|
||||
}
|
||||
public Object[] getArguments() {
|
||||
return new Object[]{msg};
|
||||
}
|
||||
public String getResourceBundleName() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user