KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > SerializerFactory


1 /*
2  * Copyright 1999-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 /*
17  * $Id: SerializerFactory.java,v 1.6 2004/02/23 10:29:37 aruny Exp $
18  */

19 package com.sun.org.apache.xml.internal.serializer;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.xml.transform.OutputKeys JavaDoc;
25
26 import com.sun.org.apache.xml.internal.res.XMLErrorResources;
27 import com.sun.org.apache.xml.internal.res.XMLMessages;
28 import org.xml.sax.ContentHandler JavaDoc;
29
30 /**
31  * Factory for creating serializers.
32  */

33 public abstract class SerializerFactory
34 {
35   /**
36    * Associates output methods to default output formats.
37    */

38   private static Hashtable JavaDoc m_formats = new Hashtable JavaDoc();
39
40   /**
41    * Returns a serializer for the specified output method.
42    * If no implementation exists that supports the specified output method
43    * an exception of some type will be thrown.
44    * For a list of the default output methods see {@link Method}.
45    *
46    * @param format The output format, minimally the "method" property must be set.
47    * @return A suitable serializer.
48    * @throws IllegalArgumentException if method is
49    * null or an appropriate serializer can't be found
50    * @throws Exception if the class for the serializer is found but does not
51    * implement ContentHandler.
52    * @throws WrappedRuntimeException if an exception is thrown while trying to find serializer
53    */

54   public static Serializer getSerializer(Properties JavaDoc format)
55   {
56       Serializer ser;
57
58       try
59       {
60         String JavaDoc method = format.getProperty(OutputKeys.METHOD);
61
62         if (method == null)
63           throw new IllegalArgumentException JavaDoc(
64             "The output format has a null method name");
65
66         String JavaDoc className =
67             format.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER);
68
69
70         if (null == className)
71         {
72             // Missing Content Handler property, load default using OutputPropertiesFactory
73
Properties JavaDoc methodDefaults =
74                 OutputPropertiesFactory.getDefaultMethodProperties(method);
75             className =
76             methodDefaults.getProperty(OutputPropertiesFactory.S_KEY_CONTENT_HANDLER);
77                 if (null == className)
78                 throw new IllegalArgumentException JavaDoc(
79                     "The output format must have a '"
80                     + OutputPropertiesFactory.S_KEY_CONTENT_HANDLER + "' property!");
81         }
82
83
84
85         ClassLoader JavaDoc loader = ObjectFactory.findClassLoader();
86
87         Class JavaDoc cls = ObjectFactory.findProviderClass(className, loader, true);
88
89         // _serializers.put(method, cls);
90

91         Object JavaDoc obj = cls.newInstance();
92
93         if (obj instanceof SerializationHandler)
94         {
95               // this is one of the supplied serializers
96
ser = (Serializer) cls.newInstance();
97             ser.setOutputFormat(format);
98         }
99         else
100         {
101               /*
102                * This must be a user defined Serializer.
103                * It had better implement ContentHandler.
104                */

105                if (obj instanceof ContentHandler JavaDoc)
106                {
107
108                   /*
109                    * The user defined serializer defines ContentHandler,
110                    * but we need to wrap it with ToXMLSAXHandler which
111                    * will collect SAX-like events and emit true
112                    * SAX ContentHandler events to the users handler.
113                    */

114                   className = SerializerConstants.DEFAULT_SAX_SERIALIZER;
115                   cls = ObjectFactory.findProviderClass(className, loader, true);
116                   SerializationHandler sh =
117                       (SerializationHandler) cls.newInstance();
118                   sh.setContentHandler( (ContentHandler JavaDoc) obj);
119                   sh.setOutputFormat(format);
120
121                   ser = sh;
122                }
123                else
124                {
125                   // user defined serializer does not implement
126
// ContentHandler, ... very bad
127
throw new Exception JavaDoc(
128                        XMLMessages.createXMLMessage(
129                            XMLErrorResources.ER_SERIALIZER_NOT_CONTENTHANDLER,
130                                new Object JavaDoc[] { className}));
131                }
132
133         }
134       }
135       catch (Exception JavaDoc e)
136       {
137         throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
138       }
139
140       // If we make it to here ser is not null.
141
return ser;
142   }
143 }
144
Popular Tags