KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > serialize > SerializerFactory


1 /*
2  * Copyright 1999-2002,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
18 package org.apache.xml.serialize;
19
20
21 import java.io.OutputStream JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  *
29  *
30  * @version $Revision: 1.10 $ $Date: 2004/02/24 23:34:03 $
31  * @author <a HREF="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
32  * @author <a HREF="mailto:arkin@intalio.com">Assaf Arkin</a>
33  */

34 public abstract class SerializerFactory
35 {
36
37
38     public static final String JavaDoc FactoriesProperty = "org.apache.xml.serialize.factories";
39
40
41     private static Hashtable JavaDoc _factories = new Hashtable JavaDoc();
42
43
44     static
45     {
46         SerializerFactory factory;
47         String JavaDoc list;
48         StringTokenizer JavaDoc token;
49         String JavaDoc className;
50
51         // The default factories are always registered first,
52
// any factory specified in the properties file and supporting
53
// the same method will override the default factory.
54
factory = new SerializerFactoryImpl( Method.XML );
55         registerSerializerFactory( factory );
56         factory = new SerializerFactoryImpl( Method.HTML );
57         registerSerializerFactory( factory );
58         factory = new SerializerFactoryImpl( Method.XHTML );
59         registerSerializerFactory( factory );
60         factory = new SerializerFactoryImpl( Method.TEXT );
61         registerSerializerFactory( factory );
62
63         list = System.getProperty( FactoriesProperty );
64         if ( list != null ) {
65             token = new StringTokenizer JavaDoc( list, " ;,:" );
66             while ( token.hasMoreTokens() ) {
67                 className = token.nextToken();
68                 try {
69                     factory = (SerializerFactory) ObjectFactory.newInstance( className,
70                         SerializerFactory.class.getClassLoader(), true);
71                     if ( _factories.containsKey( factory.getSupportedMethod() ) )
72                         _factories.put( factory.getSupportedMethod(), factory );
73                 } catch ( Exception JavaDoc except ) { }
74             }
75         }
76     }
77
78
79     /**
80      * Register a serializer factory, keyed by the given
81      * method string.
82      */

83     public static void registerSerializerFactory( SerializerFactory factory )
84     {
85         String JavaDoc method;
86
87         synchronized ( _factories ) {
88             method = factory.getSupportedMethod();
89             _factories.put( method, factory );
90         }
91     }
92
93
94     /**
95      * Register a serializer factory, keyed by the given
96      * method string.
97      */

98     public static SerializerFactory getSerializerFactory( String JavaDoc method )
99     {
100         return (SerializerFactory) _factories.get( method );
101     }
102
103
104     /**
105      * Returns the method supported by this factory and used to register
106      * the factory. This call is required so factories can be added from
107      * a properties file by knowing only the class name. This method is
108      * protected, it is only required by this class but must be implemented
109      * in derived classes.
110      */

111     protected abstract String JavaDoc getSupportedMethod();
112
113
114     /**
115      * Create a new serializer based on the {@link OutputFormat}.
116      * If this method is used to create the serializer, the {@link
117      * Serializer#setOutputByteStream} or {@link Serializer#setOutputCharStream}
118      * methods must be called before serializing a document.
119      */

120     public abstract Serializer makeSerializer(OutputFormat format);
121
122
123     /**
124      * Create a new serializer, based on the {@link OutputFormat} and
125      * using the writer as the output character stream. If this
126      * method is used, the encoding property will be ignored.
127      */

128     public abstract Serializer makeSerializer( Writer JavaDoc writer,
129                                                OutputFormat format );
130
131
132     /**
133      * Create a new serializer, based on the {@link OutputFormat} and
134      * using the output byte stream and the encoding specified in the
135      * output format.
136      *
137      * @throws UnsupportedEncodingException The specified encoding is
138      * not supported
139      */

140     public abstract Serializer makeSerializer( OutputStream JavaDoc output,
141                                                OutputFormat format )
142         throws UnsupportedEncodingException JavaDoc;
143
144
145 }
146
147
148
Popular Tags