org.apache.turbine.services.mimetype.util
Class CharSetMap

java.lang.Object
  |
  +--org.apache.turbine.services.mimetype.util.CharSetMap

public class CharSetMap
extends java.lang.Object

This class maintains a set of mappers defining mappings between locales and the corresponding charsets. The mappings are defined as properties between locale and charset names. The definitions can be listed in property files located in user's home directory, Java home directory or the current class jar. In addition, this class maintains static default mappings and constructors support application specific mappings.

Version:
$Id$
Author:
Ilkka Priha

Field Summary
static java.lang.String CHARSET_RESOURCE
          The name for charset mapper resources.
private static java.util.HashMap commonMapper
          A common charset mapper for languages.
static java.lang.String DEFAULT_CHARSET
          The default charset when nothing else is applicable.
private static int MAP_CACHE
          Priorities of available mappers.
private static int MAP_COM
           
private static int MAP_HOME
           
private static int MAP_JAR
           
private static int MAP_PROG
           
private static int MAP_SYS
           
private  java.util.Map[] mappers
          An array of available charset mappers.
 
Constructor Summary
CharSetMap()
          Constructs a new charset map with default mappers.
CharSetMap(java.io.File file)
          Contructs a charset map read from a property file.
CharSetMap(java.io.InputStream input)
          Contructs a charset map read from a stream.
CharSetMap(java.util.Properties props)
          Contructs a charset map from properties.
CharSetMap(java.lang.String path)
          Contructs a charset map read from a property file path.
 
Method Summary
(package private) static void ()
           
 java.lang.String getCharSet(java.util.Locale locale)
          Gets the charset for a locale.
 java.lang.String getCharSet(java.util.Locale locale, java.lang.String variant)
          Gets the charset for a locale with a variant.
 java.lang.String getCharSet(java.lang.String key)
          Gets the charset for a specified key.
 java.lang.String getCharSet(java.lang.String key, java.lang.String def)
          Gets the charset for a specified key.
protected static java.util.Map loadFile(java.io.File file)
          Loads mappings from a file.
protected static java.util.Map loadPath(java.lang.String path)
          Loads mappings from a file path.
protected static java.util.Map loadResource(java.lang.String name)
          Loads mappings from a resource.
protected static java.util.Map loadStream(java.io.InputStream input)
          Loads mappings from a stream.
private  java.lang.String searchCharSet(java.lang.String key)
          Searches for a charset for a specified key.
private  java.lang.String searchCharSet(java.lang.String[] items)
          Searches for a charset for a specified locale.
private  java.lang.String searchCharSet(java.lang.String[] items, java.lang.StringBuffer base, int count)
          Searches recursively for a charset for a specified locale.
 void setCharSet(java.lang.String key, java.lang.String charset)
          Sets a locale-charset mapping.
protected  void setCommonCharSet(java.lang.String key, java.lang.String charset)
          Sets a common locale-charset mapping.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final java.lang.String DEFAULT_CHARSET
The default charset when nothing else is applicable.

CHARSET_RESOURCE

public static final java.lang.String CHARSET_RESOURCE
The name for charset mapper resources.

MAP_CACHE

private static final int MAP_CACHE
Priorities of available mappers.

MAP_PROG

private static final int MAP_PROG

MAP_HOME

private static final int MAP_HOME

MAP_SYS

private static final int MAP_SYS

MAP_JAR

private static final int MAP_JAR

MAP_COM

private static final int MAP_COM

commonMapper

private static java.util.HashMap commonMapper
A common charset mapper for languages.

mappers

private java.util.Map[] mappers
An array of available charset mappers.
Constructor Detail

CharSetMap

public CharSetMap()
Constructs a new charset map with default mappers.

CharSetMap

public CharSetMap(java.util.Properties props)
Contructs a charset map from properties.
Parameters:
props - charset mapping propeties.

CharSetMap

public CharSetMap(java.io.InputStream input)
           throws java.io.IOException
Contructs a charset map read from a stream.
Parameters:
input - an input stream.
Throws:
java.io.IOException - for an incorrect stream.

CharSetMap

public CharSetMap(java.io.File file)
           throws java.io.IOException
Contructs a charset map read from a property file.
Parameters:
file - a property file.
Throws:
java.io.IOException - for an incorrect property file.

CharSetMap

public CharSetMap(java.lang.String path)
           throws java.io.IOException
Contructs a charset map read from a property file path.
Parameters:
path - a property file path.
Throws:
java.io.IOException - for an incorrect property file.
Method Detail

static void ()

loadStream

protected static java.util.Map loadStream(java.io.InputStream input)
                                   throws java.io.IOException
Loads mappings from a stream.
Parameters:
input - an input stream.
Returns:
the mappings.
Throws:
java.io.IOException - for an incorrect stream.

loadFile

protected static java.util.Map loadFile(java.io.File file)
                                 throws java.io.IOException
Loads mappings from a file.
Parameters:
file - a file.
Returns:
the mappings.
Throws:
java.io.IOException - for an incorrect file.

loadPath

protected static java.util.Map loadPath(java.lang.String path)
                                 throws java.io.IOException
Loads mappings from a file path.
Parameters:
path - a file path.
Returns:
the mappings.
Throws:
java.io.IOException - for an incorrect file.

loadResource

protected static java.util.Map loadResource(java.lang.String name)
Loads mappings from a resource.
Parameters:
name - a resource name.
Returns:
the mappings.

setCharSet

public void setCharSet(java.lang.String key,
                       java.lang.String charset)
Sets a locale-charset mapping.
Parameters:
key - the key for the charset.
charset - the corresponding charset.

getCharSet

public java.lang.String getCharSet(java.util.Locale locale)
Gets the charset for a locale. First a locale specific charset is searched for, then a country specific one and lastly a language specific one. If none is found, the default charset is returned.
Parameters:
locale - the locale.
Returns:
the charset.

getCharSet

public java.lang.String getCharSet(java.util.Locale locale,
                                   java.lang.String variant)
Gets the charset for a locale with a variant. The search is performed in the following order: "lang"_"country"_"variant"="charset", _"counry"_"variant"="charset", "lang"__"variant"="charset", __"variant"="charset", "lang"_"country"="charset", _"country"="charset", "lang"="charset". If nothing of the above is found, the default charset is returned.
Parameters:
locale - the locale.
variant - a variant field.
Returns:
the charset.

getCharSet

public java.lang.String getCharSet(java.lang.String key)
Gets the charset for a specified key.
Parameters:
key - the key for the charset.
Returns:
the found charset or the default one.

getCharSet

public java.lang.String getCharSet(java.lang.String key,
                                   java.lang.String def)
Gets the charset for a specified key.
Parameters:
key - the key for the charset.
def - the default charset if none is found.
Returns:
the found charset or the given default.

searchCharSet

private java.lang.String searchCharSet(java.lang.String[] items)
Searches for a charset for a specified locale.
Parameters:
items - an array of locale items.
Returns:
the found charset or an empty string.

searchCharSet

private java.lang.String searchCharSet(java.lang.String[] items,
                                       java.lang.StringBuffer base,
                                       int count)
Searches recursively for a charset for a specified locale.
Parameters:
items - an array of locale items.
base - a buffer of base items.
count - the number of items to go through.
Returns:
the found charset or an empty string.

searchCharSet

private java.lang.String searchCharSet(java.lang.String key)
Searches for a charset for a specified key.
Parameters:
key - the key for the charset.
Returns:
the found charset or an empty string.

setCommonCharSet

protected void setCommonCharSet(java.lang.String key,
                                java.lang.String charset)
Sets a common locale-charset mapping.
Parameters:
key - the key for the charset.
charset - the corresponding charset.


Copyright © 1999-2001 Apache Software Foundation. All Rights Reserved.