KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.xerces.dom.DOMMessageFormatter;
25
26 /**
27  * Default serializer factory can construct serializers for the three
28  * markup serializers (XML, HTML, XHTML ).
29  *
30  *
31  * @version $Revision: 1.8 $ $Date: 2004/02/24 23:34:03 $
32  * @author <a HREF="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
33  * @author <a HREF="mailto:arkin@intalio.com">Assaf Arkin</a>
34  */

35 final class SerializerFactoryImpl
36     extends SerializerFactory
37 {
38
39
40     private String JavaDoc _method;
41     
42     
43     SerializerFactoryImpl( String JavaDoc method )
44     {
45         _method = method;
46         if ( ! _method.equals( Method.XML ) &&
47              ! _method.equals( Method.HTML ) &&
48              ! _method.equals( Method.XHTML ) &&
49              ! _method.equals( Method.TEXT ) ) {
50             String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "MethodNotSupported", new Object JavaDoc[]{method});
51             throw new IllegalArgumentException JavaDoc(msg);
52         }
53     }
54
55
56     public Serializer makeSerializer( OutputFormat format )
57     {
58         Serializer serializer;
59         
60         serializer = getSerializer( format );
61         serializer.setOutputFormat( format );
62         return serializer;
63     }
64     
65     
66     
67     public Serializer makeSerializer( Writer JavaDoc writer,
68                                       OutputFormat format )
69     {
70         Serializer serializer;
71         
72         serializer = getSerializer( format );
73         serializer.setOutputCharStream( writer );
74         return serializer;
75     }
76     
77     
78     public Serializer makeSerializer( OutputStream JavaDoc output,
79                                       OutputFormat format )
80         throws UnsupportedEncodingException JavaDoc
81     {
82         Serializer serializer;
83         
84         serializer = getSerializer( format );
85         serializer.setOutputByteStream( output );
86         return serializer;
87     }
88     
89     
90     private Serializer getSerializer( OutputFormat format )
91     {
92         if ( _method.equals( Method.XML ) ) {
93             return new XMLSerializer( format );
94         } else if ( _method.equals( Method.HTML ) ) {
95             return new HTMLSerializer( format );
96         } else if ( _method.equals( Method.XHTML ) ) {
97             return new XHTMLSerializer( format );
98         } else if ( _method.equals( Method.TEXT ) ) {
99             return new TextSerializer();
100         } else {
101             String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "MethodNotSupported", new Object JavaDoc[]{_method});
102             throw new IllegalStateException JavaDoc(msg);
103         }
104     }
105     
106     
107     protected String JavaDoc getSupportedMethod()
108     {
109         return _method;
110     }
111
112
113 }
114
115
Popular Tags