1 16 19 20 package org.apache.xalan.xsltc.runtime.output; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.Writer ; 25 26 import javax.xml.parsers.ParserConfigurationException ; 27 28 import org.apache.xalan.xsltc.trax.SAX2DOM; 29 import org.apache.xml.serializer.ToHTMLSAXHandler; 30 import org.apache.xml.serializer.ToHTMLStream; 31 import org.apache.xml.serializer.ToTextSAXHandler; 32 import org.apache.xml.serializer.ToTextStream; 33 import org.apache.xml.serializer.ToUnknownStream; 34 import org.apache.xml.serializer.ToXMLSAXHandler; 35 import org.apache.xml.serializer.ToXMLStream; 36 import org.apache.xml.serializer.SerializationHandler; 37 import org.w3c.dom.Node ; 38 39 import org.xml.sax.ContentHandler ; 40 import org.xml.sax.ext.LexicalHandler ; 41 42 45 public class TransletOutputHandlerFactory { 46 47 public static final int STREAM = 0; 48 public static final int SAX = 1; 49 public static final int DOM = 2; 50 51 private String _encoding = "utf-8"; 52 private String _method = null; 53 private int _outputType = STREAM; 54 private OutputStream _ostream = System.out; 55 private Writer _writer = null; 56 private Node _node = null; 57 private int _indentNumber = -1; 58 private ContentHandler _handler = null; 59 private LexicalHandler _lexHandler = null; 60 61 static public TransletOutputHandlerFactory newInstance() { 62 return new TransletOutputHandlerFactory(); 63 } 64 65 public void setOutputType(int outputType) { 66 _outputType = outputType; 67 } 68 69 public void setEncoding(String encoding) { 70 if (encoding != null) { 71 _encoding = encoding; 72 } 73 } 74 75 public void setOutputMethod(String method) { 76 _method = method; 77 } 78 79 public void setOutputStream(OutputStream ostream) { 80 _ostream = ostream; 81 } 82 83 public void setWriter(Writer writer) { 84 _writer = writer; 85 } 86 87 public void setHandler(ContentHandler handler) { 88 _handler = handler; 89 } 90 91 public void setLexicalHandler(LexicalHandler lex) { 92 _lexHandler = lex; 93 } 94 95 public void setNode(Node node) { 96 _node = node; 97 } 98 99 public Node getNode() { 100 return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM() 101 : null; 102 } 103 104 public void setIndentNumber(int value) { 105 _indentNumber = value; 106 } 107 108 public SerializationHandler getSerializationHandler() 109 throws IOException , ParserConfigurationException 110 { 111 SerializationHandler result = null; 112 switch (_outputType) 113 { 114 case STREAM : 115 116 if (_method == null) 117 { 118 result = new ToUnknownStream(); 119 } 120 else if (_method.equalsIgnoreCase("xml")) 121 { 122 123 result = new ToXMLStream(); 124 125 } 126 else if (_method.equalsIgnoreCase("html")) 127 { 128 129 result = new ToHTMLStream(); 130 131 } 132 else if (_method.equalsIgnoreCase("text")) 133 { 134 135 result = new ToTextStream(); 136 137 } 138 139 if (result != null && _indentNumber >= 0) 140 { 141 result.setIndentAmount(_indentNumber); 142 } 143 144 result.setEncoding(_encoding); 145 146 if (_writer != null) 147 { 148 result.setWriter(_writer); 149 } 150 else 151 { 152 result.setOutputStream(_ostream); 153 } 154 return result; 155 156 case DOM : 157 _handler = (_node != null) ? new SAX2DOM(_node) : new SAX2DOM(); 158 _lexHandler = (LexicalHandler ) _handler; 159 case SAX : 161 if (_method == null) 162 { 163 _method = "xml"; } 165 166 if (_method.equalsIgnoreCase("xml")) 167 { 168 169 if (_lexHandler == null) 170 { 171 result = new ToXMLSAXHandler(_handler, _encoding); 172 } 173 else 174 { 175 result = 176 new ToXMLSAXHandler( 177 _handler, 178 _lexHandler, 179 _encoding); 180 } 181 182 } 183 else if (_method.equalsIgnoreCase("html")) 184 { 185 186 if (_lexHandler == null) 187 { 188 result = new ToHTMLSAXHandler(_handler, _encoding); 189 } 190 else 191 { 192 result = 193 new ToHTMLSAXHandler( 194 _handler, 195 _lexHandler, 196 _encoding); 197 } 198 199 } 200 else if (_method.equalsIgnoreCase("text")) 201 { 202 203 if (_lexHandler == null) 204 { 205 result = new ToTextSAXHandler(_handler, _encoding); 206 } 207 else 208 { 209 result = 210 new ToTextSAXHandler( 211 _handler, 212 _lexHandler, 213 _encoding); 214 } 215 216 } 217 return result; 218 } 219 return null; 220 } 221 222 } 223 | Popular Tags |