1 16 19 package org.apache.xalan.transformer; 20 21 import java.io.OutputStream ; 22 import java.io.Writer ; 23 import java.util.Properties ; 24 25 import javax.xml.transform.OutputKeys ; 26 import javax.xml.transform.TransformerException ; 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 ; 34 35 39 public class SerializerSwitcher 40 { 41 42 52 public static void switchSerializerIfHTML( 53 TransformerImpl transformer, String ns, String localName) 54 throws TransformerException 55 { 56 57 if (null == transformer) 58 return; 59 60 if (((null == ns) || (ns.length() == 0)) 61 && localName.equalsIgnoreCase("html")) 62 { 63 if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD)) 67 return; 68 69 Properties prevProperties = transformer.getOutputFormat().getProperties(); 72 73 OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML); 77 78 htmlOutputProperties.copyFrom(prevProperties, true); 79 Properties htmlProperties = htmlOutputProperties.getProperties(); 80 81 try 82 { 83 Serializer oldSerializer = null; 85 86 if (null != oldSerializer) 87 { 88 Serializer serializer = 89 SerializerFactory.getSerializer(htmlProperties); 90 91 Writer writer = oldSerializer.getWriter(); 92 93 if (null != writer) 94 serializer.setWriter(writer); 95 else 96 { 97 OutputStream os = oldSerializer.getOutputStream(); 98 99 if (null != os) 100 serializer.setOutputStream(os); 101 } 102 103 105 ContentHandler ch = serializer.asContentHandler(); 106 107 transformer.setContentHandler(ch); 108 } 109 } 110 catch (java.io.IOException e) 111 { 112 throw new TransformerException (e); 113 } 114 } 115 } 116 117 129 private static String getOutputPropertyNoDefault(String qnameString, Properties props) 130 throws IllegalArgumentException 131 { 132 String value = (String )props.get(qnameString); 133 134 return value; 135 } 136 137 147 public static Serializer switchSerializerIfHTML( 148 String ns, String localName, Properties props, Serializer oldSerializer) 149 throws TransformerException 150 { 151 Serializer newSerializer = oldSerializer; 152 153 if (((null == ns) || (ns.length() == 0)) 154 && localName.equalsIgnoreCase("html")) 155 { 156 if (null != getOutputPropertyNoDefault(OutputKeys.METHOD, props)) 160 return newSerializer; 161 162 Properties prevProperties = props; 165 166 OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML); 170 171 htmlOutputProperties.copyFrom(prevProperties, true); 172 Properties htmlProperties = htmlOutputProperties.getProperties(); 173 174 { 176 if (null != oldSerializer) 177 { 178 Serializer serializer = 179 SerializerFactory.getSerializer(htmlProperties); 180 181 Writer writer = oldSerializer.getWriter(); 182 183 if (null != writer) 184 serializer.setWriter(writer); 185 else 186 { 187 OutputStream os = serializer.getOutputStream(); 188 189 if (null != os) 190 serializer.setOutputStream(os); 191 } 192 newSerializer = serializer; 193 } 194 } 195 } 200 return newSerializer; 201 } 202 203 } 204 | Popular Tags |