KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > SerializerSwitcher


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: SerializerSwitcher.java,v 1.14 2004/02/16 20:41:29 minchau Exp $
18  */

19 package org.apache.xalan.transformer;
20
21 import java.io.OutputStream JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import javax.xml.transform.OutputKeys JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27
28 import org.apache.xml.serializer.Serializer;
29 import org.apache.xml.serializer.SerializerFactory;
30 import org.apache.xml.serializer.Method;
31 import org.apache.xalan.templates.OutputProperties;
32
33 import org.xml.sax.ContentHandler JavaDoc;
34
35 /**
36  * This is a helper class that decides if Xalan needs to switch
37  * serializers, based on the first output element.
38  */

39 public class SerializerSwitcher
40 {
41
42   /**
43    * Switch to HTML serializer if element is HTML
44    *
45    *
46    * @param transformer Non-null transformer instance
47    * @param ns Namespace URI of the element
48    * @param localName Local part of name of element
49    *
50    * @throws TransformerException
51    */

52   public static void switchSerializerIfHTML(
53           TransformerImpl transformer, String JavaDoc ns, String JavaDoc localName)
54             throws TransformerException JavaDoc
55   {
56
57     if (null == transformer)
58       return;
59
60     if (((null == ns) || (ns.length() == 0))
61             && localName.equalsIgnoreCase("html"))
62     {
63       // System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+
64
// transformer.getOutputPropertyNoDefault(OutputKeys.METHOD));
65
// Access at level of hashtable to see if the method has been set.
66
if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD))
67         return;
68
69       // Getting the output properties this way won't cause a clone of
70
// the properties.
71
Properties JavaDoc prevProperties = transformer.getOutputFormat().getProperties();
72       
73       // We have to make sure we get an output properties with the proper
74
// defaults for the HTML method. The easiest way to do this is to
75
// have the OutputProperties class do it.
76
OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
77
78       htmlOutputProperties.copyFrom(prevProperties, true);
79       Properties JavaDoc htmlProperties = htmlOutputProperties.getProperties();
80
81       try
82       {
83 // Serializer oldSerializer = transformer.getSerializer();
84
Serializer oldSerializer = null;
85
86         if (null != oldSerializer)
87         {
88           Serializer serializer =
89             SerializerFactory.getSerializer(htmlProperties);
90
91           Writer JavaDoc writer = oldSerializer.getWriter();
92
93           if (null != writer)
94             serializer.setWriter(writer);
95           else
96           {
97             OutputStream JavaDoc os = oldSerializer.getOutputStream();
98
99             if (null != os)
100               serializer.setOutputStream(os);
101           }
102
103 // transformer.setSerializer(serializer);
104

105           ContentHandler JavaDoc ch = serializer.asContentHandler();
106
107           transformer.setContentHandler(ch);
108         }
109       }
110       catch (java.io.IOException JavaDoc e)
111       {
112         throw new TransformerException JavaDoc(e);
113       }
114     }
115   }
116   
117   /**
118    * Get the value of a property, without using the default properties. This
119    * can be used to test if a property has been explicitly set by the stylesheet
120    * or user.
121    *
122    * @param name The property name, which is a fully-qualified URI.
123    *
124    * @return The value of the property, or null if not found.
125    *
126    * @throws IllegalArgumentException If the property is not supported,
127    * and is not namespaced.
128    */

129   private static String JavaDoc getOutputPropertyNoDefault(String JavaDoc qnameString, Properties JavaDoc props)
130     throws IllegalArgumentException JavaDoc
131   {
132     String JavaDoc value = (String JavaDoc)props.get(qnameString);
133     
134     return value;
135   }
136   
137   /**
138    * Switch to HTML serializer if element is HTML
139    *
140    *
141    * @param ns Namespace URI of the element
142    * @param localName Local part of name of element
143    *
144    * @throws TransformerException
145    * @return new contentHandler.
146    */

147   public static Serializer switchSerializerIfHTML(
148           String JavaDoc ns, String JavaDoc localName, Properties JavaDoc props, Serializer oldSerializer)
149             throws TransformerException JavaDoc
150   {
151     Serializer newSerializer = oldSerializer;
152
153     if (((null == ns) || (ns.length() == 0))
154             && localName.equalsIgnoreCase("html"))
155     {
156       // System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+
157
// transformer.getOutputPropertyNoDefault(OutputKeys.METHOD));
158
// Access at level of hashtable to see if the method has been set.
159
if (null != getOutputPropertyNoDefault(OutputKeys.METHOD, props))
160         return newSerializer;
161
162       // Getting the output properties this way won't cause a clone of
163
// the properties.
164
Properties JavaDoc prevProperties = props;
165       
166       // We have to make sure we get an output properties with the proper
167
// defaults for the HTML method. The easiest way to do this is to
168
// have the OutputProperties class do it.
169
OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
170
171       htmlOutputProperties.copyFrom(prevProperties, true);
172       Properties JavaDoc htmlProperties = htmlOutputProperties.getProperties();
173
174 // try
175
{
176         if (null != oldSerializer)
177         {
178           Serializer serializer =
179             SerializerFactory.getSerializer(htmlProperties);
180
181           Writer JavaDoc writer = oldSerializer.getWriter();
182
183           if (null != writer)
184             serializer.setWriter(writer);
185           else
186           {
187             OutputStream JavaDoc os = serializer.getOutputStream();
188
189             if (null != os)
190               serializer.setOutputStream(os);
191           }
192           newSerializer = serializer;
193         }
194       }
195 // catch (java.io.IOException e)
196
// {
197
// throw new TransformerException(e);
198
// }
199
}
200     return newSerializer;
201   }
202   
203 }
204
Popular Tags