1 package com.icl.saxon; 2 import com.icl.saxon.om.NamePool; 3 import com.icl.saxon.output.Outputter; 4 import com.icl.saxon.output.GeneralOutputter; 5 import com.icl.saxon.output.Emitter; 6 7 import org.xml.sax.*; 8 import org.xml.sax.ext.LexicalHandler ; 9 10 import javax.xml.transform.*; 11 import javax.xml.transform.stream.*; 12 import javax.xml.transform.sax.*; 13 import javax.xml.transform.dom.*; 14 15 import java.util.Properties ; 16 17 class IdentityTransformer extends Controller { 18 19 protected IdentityTransformer(TransformerFactoryImpl factory) { 20 super(factory); 21 } 22 23 26 27 public void transform(Source source, Result result) 28 throws TransformerException { 29 SAXSource saxsource = getTransformerFactory().getSAXSource(source, false); 30 XMLReader parser = saxsource.getXMLReader(); 31 32 try { 33 parser.setFeature("http://xml.org/sax/features/namespaces", true); 34 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 35 } catch (SAXNotSupportedException err) { throw new TransformerException( 37 "The SAX2 parser does not recognize a required namespace feature"); 38 } catch (SAXNotRecognizedException err) { 39 throw new TransformerException( 40 "The SAX2 parser does not support a required namespace feature"); 41 } 42 43 if (result instanceof SAXResult) { 44 45 48 ContentHandler ch = ((SAXResult)result).getHandler(); 49 parser.setContentHandler(ch); 50 if (ch instanceof LexicalHandler ) { 51 try { 53 parser.setProperty("http://xml.org/sax/properties/lexical-handler", ch); 54 } catch (SAXNotSupportedException err) { } catch (SAXNotRecognizedException err) { 56 } 57 } 58 try { 59 parser.parse(saxsource.getInputSource()); 60 } catch (Exception err) { 61 throw new TransformerException(err); 62 } 63 64 } else { 65 66 72 NamePool pool = getNamePool(); 73 Properties props = getOutputProperties(); 74 GeneralOutputter out = new GeneralOutputter(pool); 75 out.setOutputDestination(props, result); 76 Emitter emitter = out.getEmitter(); 77 ContentHandler ch = new ContentEmitter(); 78 ((ContentEmitter)ch).setNamePool(pool); 79 ((ContentEmitter)ch).setEmitter(emitter); 80 81 try { 82 parser.setContentHandler(ch); 83 try { 84 parser.setProperty("http://xml.org/sax/properties/lexical-handler", ch); 86 } catch (SAXNotSupportedException err) { } catch (SAXNotRecognizedException err) { 88 } 89 90 parser.parse(saxsource.getInputSource()); 91 } catch (Exception err) { 92 throw new TransformerException(err); 93 } 94 95 out.close(); 96 } 97 } 98 } 99 | Popular Tags |