1 16 package org.apache.axis.components.encoding; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.axis.utils.JavaUtils; 20 import org.apache.axis.utils.Messages; 21 import org.apache.commons.discovery.ResourceNameIterator; 22 import org.apache.commons.discovery.resource.ClassLoaders; 23 import org.apache.commons.discovery.resource.names.DiscoverServiceNames; 24 import org.apache.commons.logging.Log; 25 26 import java.io.UnsupportedEncodingException ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 36 public class XMLEncoderFactory { 37 protected static Log log = LogFactory.getLog(XMLEncoderFactory.class.getName()); 38 39 public static final String ENCODING_UTF_8 = "UTF-8"; 40 public static final String ENCODING_UTF_16 = "UTF-16"; 41 public static final String DEFAULT_ENCODING = ENCODING_UTF_8; 42 43 private static Map encoderMap = new HashMap (); 44 private static final String PLUGABLE_PROVIDER_FILENAME = "org.apache.axis.components.encoding.XMLEncoder"; 45 46 static { 47 encoderMap.put(ENCODING_UTF_8, new UTF8Encoder()); 48 encoderMap.put(ENCODING_UTF_16, new UTF16Encoder()); 49 encoderMap.put(ENCODING_UTF_8.toLowerCase(), new UTF8Encoder()); 50 encoderMap.put(ENCODING_UTF_16.toLowerCase(), new UTF16Encoder()); 51 try { 52 loadPluggableEncoders(); 53 } catch (Throwable t){ 54 String msg=t + JavaUtils.LS + JavaUtils.stackToString(t); 55 log.info(Messages.getMessage("exception01",msg)); 56 } 57 } 58 59 63 public static XMLEncoder getDefaultEncoder() { 64 try { 65 return getEncoder(DEFAULT_ENCODING); 66 } catch (UnsupportedEncodingException e) { 67 throw new IllegalStateException (Messages.getMessage("unsupportedDefaultEncoding00", DEFAULT_ENCODING)); 69 } 70 } 71 72 78 public static XMLEncoder getEncoder(String encoding) throws UnsupportedEncodingException { 79 XMLEncoder encoder = (XMLEncoder) encoderMap.get(encoding); 80 if (encoder == null) { 81 encoder = new DefaultXMLEncoder(encoding); 82 encoderMap.put(encoding, encoder); 83 } 84 return encoder; 85 } 86 87 103 private static void loadPluggableEncoders() { 104 ClassLoader clzLoader = XMLEncoder.class.getClassLoader(); 105 ClassLoaders loaders = new ClassLoaders(); 106 loaders.put(clzLoader); 107 DiscoverServiceNames dsn = new DiscoverServiceNames(loaders); 108 ResourceNameIterator iter = dsn.findResourceNames(PLUGABLE_PROVIDER_FILENAME); 109 while (iter.hasNext()) { 110 String className = iter.nextResourceName(); 111 try { 112 Object o = Class.forName(className).newInstance(); 113 if (o instanceof XMLEncoder) { 114 XMLEncoder encoder = (XMLEncoder) o; 115 encoderMap.put(encoder.getEncoding(), encoder); 116 encoderMap.put(encoder.getEncoding().toLowerCase(), encoder); 117 } 118 } catch (Exception e) { 119 String msg = e + JavaUtils.LS + JavaUtils.stackToString(e); 120 log.info(Messages.getMessage("exception01", msg)); 121 continue; 122 } 123 } 124 } 125 } | Popular Tags |