01: /*
02: * Copyright 1999-2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.jasig.portal.serialize;
18:
19: import java.io.OutputStream;
20: import java.io.Writer;
21: import java.io.UnsupportedEncodingException;
22: import org.apache.xerces.dom.DOMMessageFormatter;
23:
24: /**
25: * Default serializer factory can construct serializers for the three
26: * markup serializers (XML, HTML, XHTML ).
27: *
28: *
29: * @version $Revision: 36559 $ $Date: 2006-04-28 11:38:13 -0700 (Fri, 28 Apr 2006) $
30: * @author <a href="mailto:Scott_Boag/CAM/Lotus@lotus.com">Scott Boag</a>
31: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
32: */
33: final class SerializerFactoryImpl extends SerializerFactory {
34:
35: private String _method;
36:
37: SerializerFactoryImpl(String method) {
38: _method = method;
39: if (!_method.equals(Method.XML) && !_method.equals(Method.HTML)
40: && !_method.equals(Method.XHTML)
41: && !_method.equals(Method.TEXT)) {
42: String msg = DOMMessageFormatter.formatMessage(
43: DOMMessageFormatter.SERIALIZER_DOMAIN,
44: "MethodNotSupported", new Object[] { method });
45: throw new IllegalArgumentException(msg);
46: }
47: }
48:
49: public Serializer makeSerializer(OutputFormat format) {
50: Serializer serializer;
51:
52: serializer = getSerializer(format);
53: serializer.setOutputFormat(format);
54: return serializer;
55: }
56:
57: public Serializer makeSerializer(Writer writer, OutputFormat format) {
58: Serializer serializer;
59:
60: serializer = getSerializer(format);
61: serializer.setOutputCharStream(writer);
62: return serializer;
63: }
64:
65: public Serializer makeSerializer(OutputStream output,
66: OutputFormat format) throws UnsupportedEncodingException {
67: Serializer serializer;
68:
69: serializer = getSerializer(format);
70: serializer.setOutputByteStream(output);
71: return serializer;
72: }
73:
74: private Serializer getSerializer(OutputFormat format) {
75: if (_method.equals(Method.XML)) {
76: return new XMLSerializer(format);
77: } else if (_method.equals(Method.HTML)) {
78: return new HTMLSerializer(format);
79: } else if (_method.equals(Method.XHTML)) {
80: return new XHTMLSerializer(format);
81: } else if (_method.equals(Method.TEXT)) {
82: return new TextSerializer();
83: } else {
84: String msg = DOMMessageFormatter.formatMessage(
85: DOMMessageFormatter.SERIALIZER_DOMAIN,
86: "MethodNotSupported", new Object[] { _method });
87: throw new IllegalStateException(msg);
88: }
89: }
90:
91: protected String getSupportedMethod() {
92: return _method;
93: }
94:
95: }
|