1 package net.sf.saxon.event; 2 3 import net.sf.saxon.Controller; 4 import net.sf.saxon.om.ExternalObjectModel; 5 import net.sf.saxon.trans.DynamicError; 6 import net.sf.saxon.trans.XPathException; 7 8 import javax.xml.transform.OutputKeys ; 9 import javax.xml.transform.Result ; 10 import javax.xml.transform.sax.SAXResult ; 11 import javax.xml.transform.stream.StreamResult ; 12 import java.util.List ; 13 import java.util.Properties ; 14 15 20 21 public class ResultWrapper { 22 23 private ResultWrapper() { 25 } 26 27 30 31 public static Receiver getReceiver(Result result, 32 PipelineConfiguration pipe, 33 Properties props) 34 throws XPathException { 35 if (result instanceof Emitter) { 36 ((Emitter)result).setOutputProperties(props); 37 return (Emitter)result; 38 } else if (result instanceof Receiver) { 39 Receiver builder = (Receiver)result; 40 builder.setSystemId(result.getSystemId()); 41 builder.setPipelineConfiguration(pipe); 42 builder.open(); 43 builder.startDocument(0); 44 return builder; 45 } else if (result instanceof SAXResult ) { 46 ContentHandlerProxy proxy = new ContentHandlerProxy(); 47 proxy.setUnderlyingContentHandler(((SAXResult )result).getHandler()); 48 proxy.setPipelineConfiguration(pipe); 49 return proxy; 50 } else if (result instanceof StreamResult ) { 51 52 58 Receiver target; 59 Emitter emitter; 60 61 CharacterMapExpander characterMapExpander = null; 62 String useMaps = props.getProperty(SaxonOutputKeys.USE_CHARACTER_MAPS); 63 if (useMaps != null) { 64 Controller controller = pipe.getController(); 65 if (controller == null) { 66 throw new DynamicError("Cannot use character maps in an environment with no Controller"); 67 } 68 characterMapExpander = controller.makeCharacterMapExpander(useMaps); 69 } 70 71 UnicodeNormalizer normalizer = null; 72 String normForm = props.getProperty(SaxonOutputKeys.NORMALIZATION_FORM); 73 if (normForm != null && !normForm.equals("none")) { 74 normalizer = new UnicodeNormalizer(normForm); 75 } 76 77 String method = props.getProperty(OutputKeys.METHOD); 78 if (method==null) { 79 target = new UncommittedSerializer(result, props); 80 target.setPipelineConfiguration(pipe); 81 return target; 82 83 } else if ("html".equals(method)) { 84 emitter = new HTMLEmitter(); 85 emitter.setPipelineConfiguration(pipe); 86 target = createHTMLSerializer(emitter, props, pipe, characterMapExpander, normalizer); 87 88 } else if ("xml".equals(method)) { 89 emitter = new XMLEmitter(); 90 emitter.setPipelineConfiguration(pipe); 91 target = createXMLSerializer(emitter, props, pipe, normalizer, characterMapExpander); 92 93 } else if ("xhtml".equals(method)) { 94 emitter = new XHTMLEmitter(); 95 emitter.setPipelineConfiguration(pipe); 96 target = createXHTMLSerializer(emitter, props, pipe, normalizer, characterMapExpander); 97 98 } else if ("text".equals(method)) { 99 emitter = new TEXTEmitter(); 100 emitter.setPipelineConfiguration(pipe); 101 target = createTextSerializer(emitter, characterMapExpander, normalizer); 102 103 } else { 104 Receiver userReceiver; 105 userReceiver = pipe.getConfiguration().getOutputMethod(method); 107 if (userReceiver == null) { 108 int brace = method.indexOf('}'); 109 String localName = method.substring(brace+1); 110 int colon = localName.indexOf(':'); 111 localName = localName.substring(colon+1); 112 userReceiver = Emitter.makeEmitter(localName, pipe.getController()); 113 } 114 userReceiver.setPipelineConfiguration(pipe); 115 target = userReceiver; 116 if (userReceiver instanceof Emitter) { 117 emitter = (Emitter)userReceiver; 118 } else { 119 return userReceiver; 120 } 121 } 122 emitter.setOutputProperties(props); 123 StreamResult sr = (StreamResult )result; 124 emitter.setStreamResult(sr); 125 return target; 126 127 } else { 128 List externalObjectModels = pipe.getConfiguration().getExternalObjectModels(); 130 for (int m=0; m<externalObjectModels.size(); m++) { 131 ExternalObjectModel model = (ExternalObjectModel)externalObjectModels.get(m); 132 Receiver builder = model.getDocumentBuilder(result); 133 if (builder != null) { 134 builder.setSystemId(result.getSystemId()); 135 builder.setPipelineConfiguration(pipe); 136 return builder; 137 } 138 } 139 } 140 141 throw new IllegalArgumentException ("Unknown type of result: " + result.getClass()); 142 } 143 144 private static Receiver createUncommittedSerializer( 145 Emitter emitter, CharacterMapExpander characterMapExpander, UnicodeNormalizer normalizer) { 146 Receiver target; 147 target = emitter; 148 if (characterMapExpander != null) { 149 characterMapExpander.setUnderlyingReceiver(target); 150 target = characterMapExpander; 151 } 152 if (normalizer != null) { 153 normalizer.setUnderlyingReceiver(target); 154 target = normalizer; 155 } 156 return target; 157 } 158 159 private static Receiver createHTMLSerializer( 160 Emitter emitter, Properties props, PipelineConfiguration pipe, CharacterMapExpander characterMapExpander, UnicodeNormalizer normalizer) throws XPathException { 161 Receiver target; 162 target = emitter; 163 if (!"no".equals(props.getProperty(OutputKeys.INDENT))) { 164 HTMLIndenter in = new HTMLIndenter(); 165 in.setUnderlyingReceiver(target); 166 in.setPipelineConfiguration(pipe); 167 in.setOutputProperties(props); 168 target=in; 169 } 170 if (!"no".equals(props.getProperty(SaxonOutputKeys.INCLUDE_CONTENT_TYPE))) { 171 MetaTagAdjuster in = new MetaTagAdjuster(); 172 in.setUnderlyingReceiver(target); 173 in.setPipelineConfiguration(pipe); 174 in.setIsXHTML(false); 175 in.setOutputProperties(props); 176 target=in; 177 } 178 if (characterMapExpander != null) { 179 characterMapExpander.setUnderlyingReceiver(target); 180 target = characterMapExpander; 181 target.setPipelineConfiguration(pipe); 182 } 183 if (normalizer != null) { 184 normalizer.setUnderlyingReceiver(target); 185 target = normalizer; 186 target.setPipelineConfiguration(pipe); 187 } 188 if (!"no".equals(props.getProperty(SaxonOutputKeys.ESCAPE_URI_ATTRIBUTES))) { 189 HTMLURIEscaper escaper = new HTMLURIEscaper(); 190 escaper.setUnderlyingReceiver(target); 191 target = escaper; 192 target.setPipelineConfiguration(pipe); 193 } 194 return target; 195 } 196 197 private static Receiver createTextSerializer( 198 Emitter emitter, CharacterMapExpander characterMapExpander, UnicodeNormalizer normalizer) { 199 Receiver target; 200 target = emitter; 201 if (characterMapExpander != null) { 202 characterMapExpander.setUnderlyingReceiver(target); 203 characterMapExpander.setUseNullMarkers(false); 204 target = characterMapExpander; 205 } 206 if (normalizer != null) { 207 normalizer.setUnderlyingReceiver(target); 208 target = normalizer; 209 } 210 return target; 211 } 212 213 private static Receiver createXHTMLSerializer( 214 Emitter emitter, Properties props, PipelineConfiguration pipe, UnicodeNormalizer normalizer, CharacterMapExpander characterMapExpander) throws XPathException { 215 Receiver target; 216 target = emitter; 217 if (!"no".equals(props.getProperty(OutputKeys.INDENT))) { 218 XHTMLIndenter in = new XHTMLIndenter(); 219 in.setUnderlyingReceiver(target); 220 in.setPipelineConfiguration(pipe); 221 in.setOutputProperties(props); 222 target=in; 223 } 224 if (!"no".equals(props.getProperty(SaxonOutputKeys.INCLUDE_CONTENT_TYPE))) { 225 MetaTagAdjuster in = new MetaTagAdjuster(); 226 in.setUnderlyingReceiver(target); 227 in.setPipelineConfiguration(pipe); 228 in.setIsXHTML(true); 229 in.setOutputProperties(props); 230 target=in; 231 } 232 String cdataElements = props.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); 233 if (cdataElements!=null && cdataElements.length()>0) { 234 CDATAFilter filter = new CDATAFilter(); 235 filter.setUnderlyingReceiver(target); 236 filter.setPipelineConfiguration(pipe); 237 filter.setOutputProperties(props); 238 target = filter; 239 } 240 if (normalizer != null) { 241 normalizer.setUnderlyingReceiver(target); 242 normalizer.setPipelineConfiguration(pipe); 243 target = normalizer; 244 } 245 if (characterMapExpander != null) { 246 characterMapExpander.setUnderlyingReceiver(target); 247 characterMapExpander.setPipelineConfiguration(pipe); 248 target = characterMapExpander; 249 } 250 if (!"no".equals(props.getProperty(SaxonOutputKeys.ESCAPE_URI_ATTRIBUTES))) { 251 HTMLURIEscaper escaper = new HTMLURIEscaper(); 252 escaper.setUnderlyingReceiver(target); 253 escaper.setPipelineConfiguration(pipe); 254 target = escaper; 255 } 256 return target; 257 } 258 259 private static Receiver createXMLSerializer( 260 Emitter emitter, Properties props, PipelineConfiguration pipe, UnicodeNormalizer normalizer, CharacterMapExpander characterMapExpander) throws XPathException { 261 Receiver target; 262 target = emitter; 263 if ("yes".equals(props.getProperty(OutputKeys.INDENT))) { 264 XMLIndenter in = new XMLIndenter(); 265 in.setUnderlyingReceiver(target); 266 in.setPipelineConfiguration(pipe); 267 in.setOutputProperties(props); 268 target=in; 269 } 270 String cdataElements = props.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); 271 if (cdataElements!=null && cdataElements.length()>0) { 272 CDATAFilter filter = new CDATAFilter(); 273 filter.setUnderlyingReceiver(target); 274 filter.setPipelineConfiguration(pipe); 275 filter.setOutputProperties(props); 276 target = filter; 277 } 278 if (normalizer != null) { 279 normalizer.setUnderlyingReceiver(target); 280 target = normalizer; 281 } 282 if (characterMapExpander != null) { 283 characterMapExpander.setUnderlyingReceiver(target); 284 target = characterMapExpander; 285 } 286 return target; 287 } 288 } 289 290 | Popular Tags |