19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file 49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information 59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file 69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License. 89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at 99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software 139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and 169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License. 179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: SerializerSwitcher.java 468645 2006-10-28 06:57:24Z minchau $ 209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xalan.transformer; 229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.io.OutputStream; 249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.io.Writer; 259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.util.Properties; 269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.OutputKeys; 289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.TransformerException; 299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.serializer.Serializer; 319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.serializer.SerializerFactory; 329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.serializer.Method; 339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xalan.templates.OutputProperties; 349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.xml.sax.ContentHandler; 369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/** 389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This is a helper class that decides if Xalan needs to switch 399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * serializers, based on the first output element. 409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class SerializerSwitcher 429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{ 439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Switch to HTML serializer if element is HTML 469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param transformer Non-null transformer instance 499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ns Namespace URI of the element 509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param localName Local part of name of element 519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws TransformerException 539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public static void switchSerializerIfHTML( 559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson TransformerImpl transformer, String ns, String localName) 569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throws TransformerException 579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null == transformer) 609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return; 619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (((null == ns) || (ns.length() == 0)) 639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson && localName.equalsIgnoreCase("html")) 649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+ 669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // transformer.getOutputPropertyNoDefault(OutputKeys.METHOD)); 679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Access at level of hashtable to see if the method has been set. 689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD)) 699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return; 709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Getting the output properties this way won't cause a clone of 729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // the properties. 739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Properties prevProperties = transformer.getOutputFormat().getProperties(); 749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // We have to make sure we get an output properties with the proper 769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // defaults for the HTML method. The easiest way to do this is to 779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // have the OutputProperties class do it. 789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML); 799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson htmlOutputProperties.copyFrom(prevProperties, true); 819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Properties htmlProperties = htmlOutputProperties.getProperties(); 829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson try 849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// Serializer oldSerializer = transformer.getSerializer(); 869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Serializer oldSerializer = null; 879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != oldSerializer) 899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Serializer serializer = 919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson SerializerFactory.getSerializer(htmlProperties); 929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Writer writer = oldSerializer.getWriter(); 949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != writer) 969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson serializer.setWriter(writer); 979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson OutputStream os = oldSerializer.getOutputStream(); 1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != os) 1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson serializer.setOutputStream(os); 1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// transformer.setSerializer(serializer); 1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ContentHandler ch = serializer.asContentHandler(); 1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson transformer.setContentHandler(ch); 1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson catch (java.io.IOException e) 1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new TransformerException(e); 1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Get the value of a property, without using the default properties. This 1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * can be used to test if a property has been explicitly set by the stylesheet 1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or user. 1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param name The property name, which is a fully-qualified URI. 1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return The value of the property, or null if not found. 1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws IllegalArgumentException If the property is not supported, 1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * and is not namespaced. 1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson private static String getOutputPropertyNoDefault(String qnameString, Properties props) 1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throws IllegalArgumentException 1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String value = (String)props.get(qnameString); 1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return value; 1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Switch to HTML serializer if element is HTML 1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ns Namespace URI of the element 1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param localName Local part of name of element 1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws TransformerException 1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return new contentHandler. 1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public static Serializer switchSerializerIfHTML( 1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String ns, String localName, Properties props, Serializer oldSerializer) 1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throws TransformerException 1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Serializer newSerializer = oldSerializer; 1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (((null == ns) || (ns.length() == 0)) 1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson && localName.equalsIgnoreCase("html")) 1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // System.out.println("transformer.getOutputPropertyNoDefault(OutputKeys.METHOD): "+ 1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // transformer.getOutputPropertyNoDefault(OutputKeys.METHOD)); 1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Access at level of hashtable to see if the method has been set. 1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != getOutputPropertyNoDefault(OutputKeys.METHOD, props)) 1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return newSerializer; 1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Getting the output properties this way won't cause a clone of 1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // the properties. 1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Properties prevProperties = props; 1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // We have to make sure we get an output properties with the proper 1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // defaults for the HTML method. The easiest way to do this is to 1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // have the OutputProperties class do it. 1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML); 1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson htmlOutputProperties.copyFrom(prevProperties, true); 1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Properties htmlProperties = htmlOutputProperties.getProperties(); 1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// try 1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != oldSerializer) 1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Serializer serializer = 1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson SerializerFactory.getSerializer(htmlProperties); 1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Writer writer = oldSerializer.getWriter(); 1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != writer) 1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson serializer.setWriter(writer); 1879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 1889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson OutputStream os = serializer.getOutputStream(); 1909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null != os) 1929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson serializer.setOutputStream(os); 1939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson newSerializer = serializer; 1959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// catch (java.io.IOException e) 1989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// { 1999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// throw new TransformerException(e); 2009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// } 2019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return newSerializer; 2039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} 206