1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.transformer;
  58. import java.io.Writer;
  59. import java.io.OutputStream;
  60. import java.util.Properties;
  61. import org.apache.xalan.templates.StylesheetRoot;
  62. import org.xml.sax.ContentHandler;
  63. import javax.xml.transform.TransformerException;
  64. import javax.xml.transform.OutputKeys;
  65. import org.apache.xalan.serialize.Serializer;
  66. import org.apache.xalan.serialize.SerializerFactory;
  67. import org.apache.xalan.serialize.Method;
  68. import org.apache.xalan.templates.OutputProperties;
  69. /**
  70. * This is a helper class that decides if Xalan needs to switch
  71. * serializers, based on the first output element.
  72. */
  73. public class SerializerSwitcher
  74. {
  75. /**
  76. * Switch to HTML serializer if element is HTML
  77. *
  78. *
  79. * @param transformer Non-null transformer instance
  80. * @param ns Namespace URI of the element
  81. * @param localName Local part of name of element
  82. *
  83. * @throws TransformerException
  84. */
  85. public static void switchSerializerIfHTML(
  86. TransformerImpl transformer, String ns, String localName)
  87. throws TransformerException
  88. {
  89. if (null == transformer)
  90. return;
  91. if (((null == ns) || (ns.length() == 0))
  92. && localName.equalsIgnoreCase("html"))
  93. {
  94. // System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+
  95. // transformer.getOutputPropertyNoDefault(OutputKeys.METHOD));
  96. // Access at level of hashtable to see if the method has been set.
  97. if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD))
  98. return;
  99. // Getting the output properties this way won't cause a clone of
  100. // the properties.
  101. Properties prevProperties = transformer.getOutputFormat().getProperties();
  102. // We have to make sure we get an output properties with the proper
  103. // defaults for the HTML method. The easiest way to do this is to
  104. // have the OutputProperties class do it.
  105. OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
  106. htmlOutputProperties.copyFrom(prevProperties, true);
  107. Properties htmlProperties = htmlOutputProperties.getProperties();
  108. try
  109. {
  110. Serializer oldSerializer = transformer.getSerializer();
  111. if (null != oldSerializer)
  112. {
  113. Serializer serializer =
  114. SerializerFactory.getSerializer(htmlProperties);
  115. Writer writer = oldSerializer.getWriter();
  116. if (null != writer)
  117. serializer.setWriter(writer);
  118. else
  119. {
  120. OutputStream os = oldSerializer.getOutputStream();
  121. if (null != os)
  122. serializer.setOutputStream(os);
  123. }
  124. transformer.setSerializer(serializer);
  125. ContentHandler ch = serializer.asContentHandler();
  126. transformer.setContentHandler(ch);
  127. }
  128. }
  129. catch (java.io.IOException e)
  130. {
  131. throw new TransformerException(e);
  132. }
  133. }
  134. }
  135. /**
  136. * Get the value of a property, without using the default properties. This
  137. * can be used to test if a property has been explicitly set by the stylesheet
  138. * or user.
  139. *
  140. * @param name The property name, which is a fully-qualified URI.
  141. *
  142. * @return The value of the property, or null if not found.
  143. *
  144. * @throws IllegalArgumentException If the property is not supported,
  145. * and is not namespaced.
  146. */
  147. private static String getOutputPropertyNoDefault(String qnameString, Properties props)
  148. throws IllegalArgumentException
  149. {
  150. String value = (String)props.get(qnameString);
  151. return value;
  152. }
  153. /**
  154. * Switch to HTML serializer if element is HTML
  155. *
  156. *
  157. * @param ns Namespace URI of the element
  158. * @param localName Local part of name of element
  159. *
  160. * @throws TransformerException
  161. * @return new contentHandler.
  162. */
  163. public static Serializer switchSerializerIfHTML(
  164. String ns, String localName, Properties props, Serializer oldSerializer)
  165. throws TransformerException
  166. {
  167. Serializer newSerializer = oldSerializer;
  168. if (((null == ns) || (ns.length() == 0))
  169. && localName.equalsIgnoreCase("html"))
  170. {
  171. // System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+
  172. // transformer.getOutputPropertyNoDefault(OutputKeys.METHOD));
  173. // Access at level of hashtable to see if the method has been set.
  174. if (null != getOutputPropertyNoDefault(OutputKeys.METHOD, props))
  175. return newSerializer;
  176. // Getting the output properties this way won't cause a clone of
  177. // the properties.
  178. Properties prevProperties = props;
  179. // We have to make sure we get an output properties with the proper
  180. // defaults for the HTML method. The easiest way to do this is to
  181. // have the OutputProperties class do it.
  182. OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
  183. htmlOutputProperties.copyFrom(prevProperties, true);
  184. Properties htmlProperties = htmlOutputProperties.getProperties();
  185. // try
  186. {
  187. if (null != oldSerializer)
  188. {
  189. Serializer serializer =
  190. SerializerFactory.getSerializer(htmlProperties);
  191. Writer writer = oldSerializer.getWriter();
  192. if (null != writer)
  193. serializer.setWriter(writer);
  194. else
  195. {
  196. OutputStream os = serializer.getOutputStream();
  197. if (null != os)
  198. serializer.setOutputStream(os);
  199. }
  200. newSerializer = serializer;
  201. }
  202. }
  203. // catch (java.io.IOException e)
  204. // {
  205. // throw new TransformerException(e);
  206. // }
  207. }
  208. return newSerializer;
  209. }
  210. }