1 package net.sf.saxon.event; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.charcode.CharacterSet; 5 import net.sf.saxon.charcode.CharacterSetFactory; 6 import net.sf.saxon.charcode.PluggableCharacterSet; 7 import net.sf.saxon.charcode.UnicodeCharacterSet; 8 import net.sf.saxon.om.NamePool; 9 import net.sf.saxon.trans.DynamicError; 10 import net.sf.saxon.trans.XPathException; 11 import org.xml.sax.ContentHandler ; 12 13 import javax.xml.transform.OutputKeys ; 14 import javax.xml.transform.Result ; 15 import javax.xml.transform.TransformerException ; 16 import javax.xml.transform.stream.StreamResult ; 17 import java.io.*; 18 import java.net.URI ; 19 import java.net.URISyntaxException ; 20 import java.util.Properties ; 21 22 23 37 38 public abstract class Emitter implements Result , Receiver 39 { 40 protected PipelineConfiguration pipelineConfig; 41 protected NamePool namePool; 42 protected String systemId; 43 protected StreamResult streamResult; 44 protected Writer writer; 45 protected OutputStream outputStream; 46 protected Properties outputProperties; 47 protected CharacterSet characterSet = null; 48 49 52 53 public void setPipelineConfiguration(PipelineConfiguration config) { 54 this.pipelineConfig = config; 55 this.namePool = config.getConfiguration().getNamePool(); 56 } 57 58 61 62 public PipelineConfiguration getPipelineConfiguration() { 63 return pipelineConfig; 64 } 65 66 69 70 public Configuration getConfiguration() { 71 return pipelineConfig.getConfiguration(); 72 } 73 74 77 78 public void setSystemId(String systemId) { 79 this.systemId = systemId; 80 } 81 82 85 86 public String getSystemId() { 87 return systemId; 88 } 89 90 93 94 public void setOutputProperties(Properties details) throws XPathException { 95 if (characterSet==null) { 96 characterSet = CharacterSetFactory.getCharacterSet(details, getPipelineConfiguration().getController()); 97 } 98 outputProperties = details; 99 } 100 101 104 105 public Properties getOutputProperties() { 106 return outputProperties; 107 } 108 109 112 113 public void setStreamResult(StreamResult result) throws XPathException { 114 this.streamResult = result; 115 } 116 117 120 121 protected void makeWriter() throws XPathException { 122 if (writer != null) { 123 return; 124 } 125 if (streamResult == null) { 126 throw new IllegalStateException ("Emitter must have either a Writer or a StreamResult to write to"); 127 } 128 writer = streamResult.getWriter(); 129 if (writer == null) { 130 OutputStream os = streamResult.getOutputStream(); 131 if (os != null) { 132 setOutputStream(os); 133 } 134 } 135 if (writer == null) { 136 String uri = streamResult.getSystemId(); 137 try { 138 File file = new File(new URI (uri)); 139 setOutputStream(new FileOutputStream(file)); 140 streamResult.setOutputStream(outputStream); 143 } catch (FileNotFoundException fnf) { 144 throw new DynamicError(fnf); 145 } catch (URISyntaxException use) { 146 throw new DynamicError(use); 147 } 148 } 149 } 150 155 156 public boolean usesWriter() { 157 return true; 158 } 159 160 163 164 public void setWriter(Writer writer) { 165 this.writer = writer; 166 167 171 if (writer instanceof OutputStreamWriter && outputProperties != null) { 172 String enc = ((OutputStreamWriter)writer).getEncoding(); 173 outputProperties.put(OutputKeys.ENCODING, enc); 174 } 175 } 176 177 180 181 public Writer getWriter() { 182 return writer; 183 } 184 185 188 189 public void setOutputStream(OutputStream stream) throws XPathException { 190 this.outputStream = stream; 191 192 197 if (usesWriter()) { 198 199 201 String encoding = outputProperties.getProperty(OutputKeys.ENCODING); 202 if (encoding==null) encoding = "UTF8"; 203 if (encoding.equalsIgnoreCase("UTF-8")) encoding = "UTF8"; 204 206 if (characterSet instanceof PluggableCharacterSet) { 207 encoding = ((PluggableCharacterSet)characterSet).getEncodingName(); 208 } 209 210 while (true) { 211 try { 212 String javaEncoding = encoding; 213 if (encoding.equalsIgnoreCase("iso-646") || encoding.equalsIgnoreCase("iso646")) { 214 javaEncoding = "US-ASCII"; 215 } 216 writer = new BufferedWriter( 217 new OutputStreamWriter( 218 outputStream, javaEncoding)); 219 break; 220 } catch (Exception err) { 221 if (encoding.equalsIgnoreCase("UTF8")) { 222 throw new DynamicError("Failed to create a UTF8 output writer"); 223 } 224 DynamicError de = new DynamicError("Encoding " + encoding + " is not supported: using UTF8"); 225 de.setErrorCode("SESU0007"); 226 try { 227 getPipelineConfiguration().getErrorListener().error(de); 228 } catch (TransformerException e) { 229 throw DynamicError.makeDynamicError(e); 230 } 231 encoding = "UTF8"; 232 characterSet = UnicodeCharacterSet.getInstance(); 233 outputProperties.put(OutputKeys.ENCODING, "UTF-8"); 234 } 235 } 236 } 237 238 } 239 240 243 244 public OutputStream getOutputStream() { 245 return outputStream; 246 } 247 248 252 253 public void setUnparsedEntity(String name, String uri, String publicId) throws XPathException {} 254 255 258 259 public static Receiver makeEmitter (String className, Controller controller) throws XPathException 260 { 261 Object handler; 262 try { 263 handler = controller.getConfiguration().getInstance(className, controller.getClassLoader()); 264 } catch (XPathException e) { 265 throw new DynamicError("Cannot load user-supplied output method " + className); 266 } 267 268 if (handler instanceof Receiver) { 269 return (Receiver)handler; 270 } else if (handler instanceof ContentHandler ) { 271 ContentHandlerProxy emitter = new ContentHandlerProxy(); 272 emitter.setUnderlyingContentHandler((ContentHandler )handler); 273 return emitter; 274 } else { 275 throw new DynamicError("Failed to load " + className + 276 ": it is neither a Receiver nor a SAX2 ContentHandler"); 277 } 278 279 } 280 281 284 297 } 298 299 | Popular Tags |