pax_global_header 0000666 0000000 0000000 00000000064 11574511370 0014516 g ustar 00root root 0000000 0000000 52 comment=59ef44d2c18f04c6c5f35a4d61cfde73e9f74d6b
localizer-1.13/ 0000775 0000000 0000000 00000000000 11574511370 0013426 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/ 0000775 0000000 0000000 00000000000 11574511370 0014174 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/pom.xml 0000664 0000000 0000000 00000001262 11574511370 0015512 0 ustar 00root root 0000000 0000000 4.0.0org.jvnet.localizerlocalizer-parent1.13../pom.xmllocalizerLocalizerjunitjunit4.0test
localizer-1.13/lib/src/ 0000775 0000000 0000000 00000000000 11574511370 0014763 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/main/ 0000775 0000000 0000000 00000000000 11574511370 0015707 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/main/java/ 0000775 0000000 0000000 00000000000 11574511370 0016630 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/main/java/org/ 0000775 0000000 0000000 00000000000 11574511370 0017417 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/main/java/org/jvnet/ 0000775 0000000 0000000 00000000000 11574511370 0020545 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/main/java/org/jvnet/localizer/ 0000775 0000000 0000000 00000000000 11574511370 0022531 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/main/java/org/jvnet/localizer/LocaleProvider.java 0000664 0000000 0000000 00000005073 11574511370 0026313 0 ustar 00root root 0000000 0000000 /*
* The MIT License
*
* Copyright (c) 2007-, the localizer project contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.localizer;
import java.util.Locale;
/**
* Determines the locale, normally from the context.
*
*
* For example, in webapps, you might use the current request's Accept-Language
* header, or maybe it's just an invocation to {@link Locale#getDefault()}.
*
*
* A single instance of {@link LocaleProvider} is maintained in this class
* for the use by {@link ResourceBundleHolder}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class LocaleProvider {
/**
* Determines the locale to be used.
*
* @return
* must not be null.
*/
public abstract Locale get();
public static void setProvider(LocaleProvider p) {
if(p==null) throw new IllegalArgumentException();
theInstance = p;
}
/**
* Gets the currently installed system-wide {@link LocaleProvider}.
* @return
* always non-null.
*/
public static LocaleProvider getProvider() {
return theInstance;
}
/**
* Short for {@code getProvider().get()}
*/
public static Locale getLocale() {
return theInstance.get();
}
/**
* {@link LocaleProvider} that uses {@link Locale#getDefault()}.
*/
public static final LocaleProvider DEFAULT = new LocaleProvider() {
public Locale get() {
return Locale.getDefault();
}
};
private static volatile LocaleProvider theInstance = DEFAULT;
}
localizer-1.13/lib/src/main/java/org/jvnet/localizer/Localizable.java 0000664 0000000 0000000 00000005434 11574511370 0025623 0 ustar 00root root 0000000 0000000 /*
* The MIT License
*
* Copyright (c) 2007-, the localizer project contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.localizer;
import java.util.Locale;
import java.text.MessageFormat;
import java.io.Serializable;
import java.util.MissingResourceException;
import static java.util.Arrays.asList;
/**
* Captures the localizable string. Can be converted to a string just by
* supplying a {@link Locale}.
*
* @author Kohsuke Kawaguchi
*/
public class Localizable implements Serializable {
private final ResourceBundleHolder holder;
private final String key;
private final Serializable[] args;
public Localizable(ResourceBundleHolder holder, String key, Object... args) {
this.holder = holder;
this.key = key;
this.args = new Serializable[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof Serializable) {
this.args[i] = (Serializable)args[i];
} else {
// MessageFormat only supports formats of "number", "date", "time" and "choice"
// All of which will be formatting objects that must be Serializable
// Anything else will just have it's toString() method invoked
// by MessageFormat, so we'll just call toString up front.
this.args[i] = args[i].toString();
}
}
}
public String toString(Locale locale) {
try {
return MessageFormat.format(holder.get(locale).getString(key),args);
} catch (MissingResourceException e) {
throw new RuntimeException("Failed to localize key="+key+",args="+ asList(args),e);
}
}
public String toString() {
return toString(LocaleProvider.getLocale());
}
}
localizer-1.13/lib/src/main/java/org/jvnet/localizer/ResourceBundleHolder.java 0000664 0000000 0000000 00000013713 11574511370 0027460 0 ustar 00root root 0000000 0000000 /*
* The MIT License
*
* Copyright (c) 2007-, the localizer project contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.localizer;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.ObjectStreamException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.lang.ref.WeakReference;
/**
* Maintains {@link ResourceBundle}s per locale.
*
* @author Kohsuke Kawaguchi
*/
public final class ResourceBundleHolder implements Serializable {
/** Need to cache, but not tie up a classloader refernce in cases of unloading */
private static final Map> cache =
new WeakHashMap> ();
/**
* Gets a {@link ResourceBundleHolder} for the given class,
* by utilizing a cache if possible.
*/
public synchronized static ResourceBundleHolder get(Class clazz) {
WeakReference entry = cache.get(clazz);
if (entry != null) {
ResourceBundleHolder rbh = entry.get();
if (rbh != null) return rbh;
}
ResourceBundleHolder rbh = new ResourceBundleHolder(clazz);
cache.put(clazz, new WeakReference(rbh));
return rbh;
}
private transient final Map bundles = new ConcurrentHashMap();
public final Class owner;
/**
* {@link Locale} object that corresponds to the base bundle.
*/
private static final Locale ROOT = new Locale("");
/**
* @param owner
* The name of the generated resource bundle class.
* @deprecated
* Use {@link #get(Class)}
*/
public ResourceBundleHolder(Class owner) {
this.owner = owner;
}
/**
* Work around deserialization issues.
*/
private Object readResolve() throws ObjectStreamException {
return get(owner);
}
/**
* Loads {@link ResourceBundle} for the locale.
*/
public ResourceBundle get(Locale locale) {
ResourceBundle rb = bundles.get(locale);
if(rb!=null) return rb;
// try to update the map
synchronized(this) {
rb = bundles.get(locale);
if(rb!=null) return rb;
// turns out this is totally unsable because the getBundle method
// always checks Locale.getDefault() and that wins over the bundle for the root locale.
// bundles.put(locale, rb=ResourceBundle.getBundle(owner.getName(),locale,owner.getClassLoader()));
Locale next = getBaseLocale(locale);
String s = locale.toString();
URL res = owner.getResource(owner.getSimpleName()+(s.length()>0?'_'+s:"")+".properties");
if(res!=null) {
// found property file for this locale.
try {
InputStream is = res.openStream();
ResourceBundleImpl bundle = new ResourceBundleImpl(is);
is.close();
rb = bundle;
if(next!=null)
bundle.setParent(get(next));
bundles.put(locale,bundle);
} catch (IOException e) {
MissingResourceException x = new MissingResourceException("Unable to load resource " + res, owner.getName(), null);
x.initCause(e);
throw x;
}
} else {
if(next!=null)
// no matching resource, so just use the locale for the base
bundles.put(locale,rb=get(next));
else
throw new MissingResourceException(
"No resource was found for "+owner.getName(),owner.getName(),null);
}
}
return rb;
}
@Override
public String toString() {
return getClass().getName()+"["+owner.getName()+"]";
}
static class ResourceBundleImpl extends PropertyResourceBundle {
ResourceBundleImpl(InputStream stream) throws IOException {
super(stream);
}
protected void setParent(ResourceBundle parent) {
super.setParent(parent);
}
}
/**
* Returns the locale to fall back to.
*/
private Locale getBaseLocale(Locale l) {
if (l.getVariant().length() > 0)
return new Locale(l.getLanguage(), l.getCountry());
if (l.getCountry().length() > 0)
return new Locale(l.getLanguage());
if (l.getLanguage().length()>0)
return ROOT;
return null;
}
/**
* Formats a resource specified by the given key by using the default locale
*/
public String format(String key, Object... args) {
return MessageFormat.format(get(LocaleProvider.getLocale()).getString(key),args);
}
}
localizer-1.13/lib/src/test/ 0000775 0000000 0000000 00000000000 11574511370 0015742 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/test/java/ 0000775 0000000 0000000 00000000000 11574511370 0016663 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/test/java/foo/ 0000775 0000000 0000000 00000000000 11574511370 0017446 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/test/java/foo/LocaleTest.java 0000664 0000000 0000000 00000001335 11574511370 0022352 0 ustar 00root root 0000000 0000000 package foo;
import junit.framework.TestCase;
import java.util.Locale;
import org.jvnet.localizer.ResourceBundleHolder;
/**
* @author Kohsuke Kawaguchi
*/
public class LocaleTest extends TestCase {
public void test1() {
Locale.setDefault(Locale.GERMANY);
routineCheck();
}
public void test2() {
Locale.setDefault(Locale.ENGLISH);
routineCheck();
}
private void routineCheck() {
ResourceBundleHolder h = new ResourceBundleHolder(LocaleTest.class);
assertEquals("base",h.get(Locale.ENGLISH).getString("abc"));
assertEquals("german",h.get(Locale.GERMANY).getString("abc"));
assertEquals("german",h.get(Locale.GERMAN).getString("abc"));
}
}
localizer-1.13/lib/src/test/resources/ 0000775 0000000 0000000 00000000000 11574511370 0017754 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/test/resources/foo/ 0000775 0000000 0000000 00000000000 11574511370 0020537 5 ustar 00root root 0000000 0000000 localizer-1.13/lib/src/test/resources/foo/LocaleTest.properties 0000664 0000000 0000000 00000000010 11574511370 0024703 0 ustar 00root root 0000000 0000000 abc=base localizer-1.13/lib/src/test/resources/foo/LocaleTest_de.properties 0000664 0000000 0000000 00000000012 11574511370 0025355 0 ustar 00root root 0000000 0000000 abc=german localizer-1.13/maven-plugin/ 0000775 0000000 0000000 00000000000 11574511370 0016030 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/pom.xml 0000664 0000000 0000000 00000003201 11574511370 0017341 0 ustar 00root root 0000000 0000000 4.0.0org.jvnet.localizerlocalizer-parent1.13../pom.xmlmaven-localizer-pluginmaven-pluginLocalizer generatororg.apache.mavenmaven-plugin-api2.0.1org.apache.mavenmaven-archiver2.0.1org.codehaus.plexusplexus-utils1.0.4org.apache.mavenmaven-artifact2.0.1org.apache.antant1.7.0com.sun.codemodelcodemodel2.1${project.groupId}localizer${project.version}
localizer-1.13/maven-plugin/src/ 0000775 0000000 0000000 00000000000 11574511370 0016617 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/src/main/ 0000775 0000000 0000000 00000000000 11574511370 0017543 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/src/main/java/ 0000775 0000000 0000000 00000000000 11574511370 0020464 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/src/main/java/org/ 0000775 0000000 0000000 00000000000 11574511370 0021253 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/src/main/java/org/jvnet/ 0000775 0000000 0000000 00000000000 11574511370 0022401 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/src/main/java/org/jvnet/localizer/ 0000775 0000000 0000000 00000000000 11574511370 0024365 5 ustar 00root root 0000000 0000000 localizer-1.13/maven-plugin/src/main/java/org/jvnet/localizer/Generator.java 0000664 0000000 0000000 00000015460 11574511370 0027164 0 ustar 00root root 0000000 0000000 /*
* The MIT License
*
* Copyright (c) 2007-, the localizer project contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jvnet.localizer;
import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JVar;
import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.writer.FileCodeWriter;
import org.apache.tools.ant.DirectoryScanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
/**
* @author Kohsuke Kawaguchi
*/
public class Generator {
private final JCodeModel cm = new JCodeModel();
private final File outputDirectory;
private final Reporter reporter;
public Generator(File outputDirectory, Reporter reporter) {
this.outputDirectory = outputDirectory;
this.reporter = reporter;
}
public void generate(File baseDir, DirectoryScanner ds) throws IOException {
for( String relPath : ds.getIncludedFiles() ) {
File f = new File(baseDir,relPath);
if(!f.getName().endsWith(".properties") || f.getName().contains("_"))
continue;
try {
generate(f,relPath);
} catch (IOException e) {
IOException x = new IOException("Failed to generate a class from " + f);
x.initCause(e);
throw x;
}
}
}
public void generate(File propertyFile, String relPath) throws IOException {
String className = toClassName(relPath);
// up to date check
File sourceFile = new File(outputDirectory,className.replace('.','/')+".java");
if(sourceFile.exists() && sourceFile.lastModified()>propertyFile.lastModified()) {
reporter.debug(sourceFile+" is up to date");
return;
}
// go generate one
Properties props = new Properties();
FileInputStream in = new FileInputStream(propertyFile);
try {
props.load(in);
} catch (IOException e) {
in.close();
}
try {
JDefinedClass c = cm._class(className);
c.annotate(SuppressWarnings.class).paramArray("value").param("").param("PMD");
// [RESULT]
// private static final ResourceBundleHolder holder = BundleHolder.get(Messages.class);
JVar holder = c.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, ResourceBundleHolder.class, "holder",
cm.ref(ResourceBundleHolder.class).staticInvoke("get").arg(c.dotclass()) );
for (Entry