KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > encoding > XMLEncoderFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Factory for XMLEncoder
32  *
33  * @author <a HREF="mailto:jens@void.fm">Jens Schumann</a>
34  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
35  */

36 public class XMLEncoderFactory {
37     protected static Log log = LogFactory.getLog(XMLEncoderFactory.class.getName());
38     
39     public static final String JavaDoc ENCODING_UTF_8 = "UTF-8";
40     public static final String JavaDoc ENCODING_UTF_16 = "UTF-16";
41     public static final String JavaDoc DEFAULT_ENCODING = ENCODING_UTF_8;
42     
43     private static Map JavaDoc encoderMap = new HashMap JavaDoc();
44     private static final String JavaDoc 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 JavaDoc t){
54             String JavaDoc msg=t + JavaUtils.LS + JavaUtils.stackToString(t);
55             log.info(Messages.getMessage("exception01",msg));
56         }
57     }
58
59     /**
60      * Returns the default encoder
61      * @return
62      */

63     public static XMLEncoder getDefaultEncoder() {
64         try {
65             return getEncoder(DEFAULT_ENCODING);
66         } catch (UnsupportedEncodingException JavaDoc e) {
67             // as far I know J++ VMs will throw this exception
68
throw new IllegalStateException JavaDoc(Messages.getMessage("unsupportedDefaultEncoding00", DEFAULT_ENCODING));
69         }
70     }
71
72     /**
73      * Returns the requested encoder
74      * @param encoding
75      * @return
76      * @throws UnsupportedEncodingException
77      */

78     public static XMLEncoder getEncoder(String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
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     /**
88      Look for file META-INF/services/org.apache.axis.components.encoding.XMLEncoder
89      in all the JARS, get the classes listed in those files and add them to
90      encoders list if they are valid encoders.
91
92      Here is how the scheme would work.
93
94      A company providing a new encoder will jar up their encoder related
95      classes in a JAR file. The following file containing the name of the new
96      encoder class is also made part of this JAR file.
97
98      META-INF/services/org.apache.axis.components.encoding.XMLEncoder
99
100      By making this JAR part of the webapp, the new encoder will be
101      automatically discovered.
102      */

103     private static void loadPluggableEncoders() {
104         ClassLoader JavaDoc 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 JavaDoc className = iter.nextResourceName();
111             try {
112                 Object JavaDoc 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 JavaDoc e) {
119                 String JavaDoc msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
120                 log.info(Messages.getMessage("exception01", msg));
121                 continue;
122             }
123         }
124     }
125 }
Popular Tags