1 package net.sf.saxon; 2 import net.sf.saxon.event.Builder; 3 import net.sf.saxon.event.Receiver; 4 import net.sf.saxon.event.ReceivingContentHandler; 5 import net.sf.saxon.om.DocumentInfo; 6 import net.sf.saxon.trans.XPathException; 7 import org.xml.sax.SAXException ; 8 9 import javax.xml.transform.Result ; 10 import javax.xml.transform.Transformer ; 11 import javax.xml.transform.TransformerException ; 12 import javax.xml.transform.sax.TransformerHandler ; 13 14 15 22 23 public class TransformerHandlerImpl extends ReceivingContentHandler implements TransformerHandler { 24 25 27 Controller controller; 28 Builder builder; 29 Result result; 30 String systemId; 31 boolean started = false; 32 33 38 39 protected TransformerHandlerImpl(Controller controller) { 40 this.controller = controller; 41 setPipelineConfiguration(controller.makePipelineConfiguration()); 42 builder = controller.makeBuilder(); 43 builder.setPipelineConfiguration(getPipelineConfiguration()); 44 Receiver stripper = controller.makeStripper(builder); 45 if (controller.getExecutable().stripsInputTypeAnnotations()) { 46 stripper = controller.getConfiguration().getAnnotationStripper(stripper); 47 } 48 this.setReceiver(stripper); 49 } 50 51 58 59 public void startDocument () throws SAXException { 60 if (started) { 61 throw new UnsupportedOperationException ( 62 "The TransformerHandler is not serially reusable. The startDocument() method must be called once only."); 63 } 64 started = true; 65 super.startDocument(); 66 } 67 68 71 72 public Transformer getTransformer() { 73 return controller; 74 } 75 76 79 80 public void setSystemId(String url) { 81 systemId = url; 82 builder.setSystemId(url); 83 } 84 85 88 89 public String getSystemId() { 90 return systemId; 91 } 92 93 96 97 public void setResult(Result result) { 98 if (result==null) { 99 throw new IllegalArgumentException ("Result must not be null"); 100 } 101 this.result = result; 102 } 103 104 107 108 public Result getResult() { 109 return result; 110 } 111 112 116 117 public void endDocument() throws SAXException { 118 super.endDocument(); 119 DocumentInfo doc = (DocumentInfo)builder.getCurrentRoot(); 120 if (doc==null) { 121 throw new SAXException ("No source document has been built"); 122 } 123 try { 125 controller.transformDocument(doc, result); 126 } catch (TransformerException err) { 127 if (err instanceof XPathException && !((XPathException)err).hasBeenReported()) { 128 try { 129 controller.getErrorListener().fatalError(err); 130 } catch (TransformerException e) { 131 } 133 } 134 throw new SAXException (err); 135 } 136 } 137 138 139 140 141 } 142 143 | Popular Tags |