FileDocCategorySizeDatePackage
XMLEncoderFactory.javaAPI DocApache Axis 1.44860Sat Apr 22 18:57:28 BST 2006org.apache.axis.components.encoding

XMLEncoderFactory

public class XMLEncoderFactory extends Object
Factory for XMLEncoder
author
Jens Schumann
author
Davanum Srinivas

Fields Summary
protected static Log
log
public static final String
ENCODING_UTF_8
public static final String
ENCODING_UTF_16
public static final String
DEFAULT_ENCODING
private static Map
encoderMap
private static final String
PLUGABLE_PROVIDER_FILENAME
Constructors Summary
Methods Summary
public static XMLEncodergetDefaultEncoder()
Returns the default encoder

return


     
        encoderMap.put(ENCODING_UTF_8, new UTF8Encoder());
        encoderMap.put(ENCODING_UTF_16, new UTF16Encoder());
        encoderMap.put(ENCODING_UTF_8.toLowerCase(), new UTF8Encoder());
        encoderMap.put(ENCODING_UTF_16.toLowerCase(), new UTF16Encoder());
        try {
            loadPluggableEncoders();
        } catch (Throwable t){
            String msg=t + JavaUtils.LS + JavaUtils.stackToString(t);
            log.info(Messages.getMessage("exception01",msg));
        }
    
        try {
            return getEncoder(DEFAULT_ENCODING);
        } catch (UnsupportedEncodingException e) {
            //  as far I know J++ VMs will throw this exception
            throw new IllegalStateException(Messages.getMessage("unsupportedDefaultEncoding00", DEFAULT_ENCODING));
        }
    
public static XMLEncodergetEncoder(java.lang.String encoding)
Returns the requested encoder

param
encoding
return
throws
UnsupportedEncodingException

        XMLEncoder encoder = (XMLEncoder) encoderMap.get(encoding);
        if (encoder == null) {
            encoder = new DefaultXMLEncoder(encoding);
            encoderMap.put(encoding, encoder);
        }
        return encoder;
    
private static voidloadPluggableEncoders()
Look for file META-INF/services/org.apache.axis.components.encoding.XMLEncoder in all the JARS, get the classes listed in those files and add them to encoders list if they are valid encoders. Here is how the scheme would work. A company providing a new encoder will jar up their encoder related classes in a JAR file. The following file containing the name of the new encoder class is also made part of this JAR file. META-INF/services/org.apache.axis.components.encoding.XMLEncoder By making this JAR part of the webapp, the new encoder will be automatically discovered.

        ClassLoader clzLoader = XMLEncoder.class.getClassLoader();
        ClassLoaders loaders = new ClassLoaders();
        loaders.put(clzLoader);
        DiscoverServiceNames dsn = new DiscoverServiceNames(loaders);
        ResourceNameIterator iter = dsn.findResourceNames(PLUGABLE_PROVIDER_FILENAME);
        while (iter.hasNext()) {
            String className = iter.nextResourceName();
            try {
                Object o = Class.forName(className).newInstance();
                if (o instanceof XMLEncoder) {
                    XMLEncoder encoder = (XMLEncoder) o;
                    encoderMap.put(encoder.getEncoding(), encoder);
                    encoderMap.put(encoder.getEncoding().toLowerCase(), encoder);
                }
            } catch (Exception e) {
                String msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
                log.info(Messages.getMessage("exception01", msg));
                continue;
            }
        }