📄 i18nbundleimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.components.xmlbundle.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.TreeMap;
import javax.swing.ImageIcon;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.avalon.framework.logger.Logger;
import org.rapla.components.util.IOUtil;
import org.rapla.components.xmlbundle.I18nBundle;
import org.rapla.components.xmlbundle.LocaleChangeEvent;
import org.rapla.components.xmlbundle.LocaleChangeListener;
import org.rapla.components.xmlbundle.LocaleSelector;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
/** The default implementation of the xmlbundle component allows reading from
a compiled ResourceBundle as well as directly from the source-xml-file.
<p>
Sample Configuration 1: (Resources are loaded from the compiled ResourceBundles)
<pre>
<resource-bundle id="org.rapla.RaplaResources"/>
</pre>
</p>
<p>
Sample Configuration 2: (Resources will be loaded directly from the resource-file)
<pre>
<resource-bundle id="org.rapla.plugin.periodwizard.WizardResources">
<file>/home/christopher/Rapla/src/org/rapla/periodwizard/WizardResources.xml</file>
</resource-bundle>
</pre>
</p>
<p>
This class looks for a LocaleSelector on the context and registers itself as
a LocaleChangeListener and switches to the new Locale on a LocaleChangeEvent.
</p>
@see TranslationParser
@see LocaleSelector
*/
public class I18nBundleImpl implements I18nBundle, LocaleChangeListener, Disposable
{
String className;
String dictionaryFile;
Locale locale;
Logger logger = null;
I18nBundle m_parentBundle;
LocaleSelectorImpl m_localeSelector;
Map stringCache = Collections.synchronizedMap( new TreeMap() );
Map iconCache = Collections.synchronizedMap( new TreeMap() );
ResourceBundle resourceBundle;
RaplaDictionary dict;
String parentId = null;
/**
* @throws RaplaException when the resource-file is missing or can't be accessed
or can't be parsed
*/
public I18nBundleImpl( RaplaContext serviceManager, Configuration config, Logger logger ) throws RaplaException
{
enableLogging( logger );
dictionaryFile = config.getChild( "file" ).getValue( null );
try
{
if ( dictionaryFile == null )
{
className = config.getChild( "classname" ).getValue( null );
if ( className == null )
className = config.getAttribute( "id" );
else
className = className.trim();
}
if ( dictionaryFile != null )
{
try
{
TranslationParser parser = new TranslationParser();
String path = new File( dictionaryFile ).getCanonicalPath();
getLogger().info( "getting lanaguageResources from " + path );
dict = parser.parse( new FileInputStream( new File( dictionaryFile ) ) );
}
finally
{
}
}
}
catch ( Exception ex )
{
throw new RaplaException( ex );
}
m_localeSelector = ( (LocaleSelectorImpl) serviceManager.lookup( LocaleSelector.ROLE ) );
if ( m_localeSelector != null )
{
m_localeSelector.addLocaleChangeListenerFirst( this );
setLocale( m_localeSelector.getLocale() );
}
else
{
setLocale( Locale.getDefault() );
}
try
{
parentId = lookup( TranslationParser.PARENT_BUNDLE_IDENTIFIER );
}
catch ( MissingResourceException ex )
{
}
if ( parentId != null )
{
m_parentBundle = (I18nBundle) serviceManager.lookup( I18nBundle.ROLE + "/" + parentId );
}
}
public String getParentId()
{
return parentId;
}
public static Configuration createConfig( String resourceFile )
{
DefaultConfiguration config = new DefaultConfiguration( "component", "auto-configuration of " + resourceFile );
config.setAttribute( "id", resourceFile.toString() );
return config;
}
/*
private void init(I18nBundle parentBundle,LocaleSelector ) {
if (m_localeSelector != null) {
m_localeSelector.addLocaleChangeListenerFirst(this);
setLocale(m_localeSelector.getLocale());
} else {
setLocale(Locale.getDefault());
}
}
*/
public void dispose()
{
if ( m_localeSelector != null )
m_localeSelector.removeLocaleChangeListener( this );
}
public void localeChanged( LocaleChangeEvent evt )
{
try
{
setLocale( evt.getLocale() );
}
catch ( Exception ex )
{
getLogger().error( "Can't set new locale " + evt.getLocale(), ex );
}
}
public void enableLogging( Logger logger )
{
this.logger = logger;
}
protected Logger getLogger()
{
return logger;
}
public String format( String key, Object obj1 )
{
Object[] array1 = new Object[1];
array1[0] = obj1;
return format( key, array1 );
}
public String format( String key, Object obj1, Object obj2 )
{
Object[] array2 = new Object[2];
array2[0] = obj1;
array2[1] = obj2;
return format( key, array2 );
}
public String format( String key, Object[] obj )
{
MessageFormat msg = new MessageFormat( getString( key ) );
return msg.format( obj );
}
private final byte[] loadResource( String fileName ) throws IOException
{
return IOUtil.readBytes( getResourceFromFile( fileName ) );
}
private URL getResourceFromFile( String fileName ) throws IOException
{
URL resource = null;
String base;
if ( dict == null )
{
base = resourceBundle.getClass().getName();
resource = resourceBundle.getClass().getResource( fileName );
}
else
{
base = ( new File( dictionaryFile ) ).getParent();
if ( getLogger().isDebugEnabled() )
getLogger().debug( "Looking for resourcefile " + fileName + " in directory " + base );
File resourceFile = new File( base, fileName );
if ( resourceFile.exists() )
resource = resourceFile.toURI().toURL();
}
if ( resource == null )
throw new IOException( "File '"
+ fileName
+ "' not found. "
+ " in bundle "
+ className
+ " It must be in the same location as '"
+ base
+ "'" );
return resource;
}
public URL getResource( String key ) throws MissingResourceException
{
String resourceFile;
try
{
resourceFile = lookup( key );
}
catch ( MissingResourceException ex )
{
if ( m_parentBundle != null )
return m_parentBundle.getResource( key );
throw ex;
}
try
{
return getResourceFromFile( resourceFile );
}
catch ( Exception ex )
{
String message = "Resourcefile " + resourceFile + " not found: " + ex.getMessage();
getLogger().error( message );
throw new MissingResourceException( message, className, key );
}
}
public ImageIcon getIcon( String key ) throws MissingResourceException
{
String iconfile;
try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -