1 package net.sf.saxon.event; 2 import net.sf.saxon.Controller; 3 import net.sf.saxon.om.DocumentInfo; 4 import net.sf.saxon.trans.DynamicError; 5 import net.sf.saxon.trans.XPathException; 6 7 import javax.xml.transform.Result ; 8 import javax.xml.transform.Transformer ; 9 import javax.xml.transform.TransformerException ; 10 11 12 18 19 public class TransformerReceiver extends ProxyReceiver { 20 21 Controller controller; 22 Builder builder; 23 Result result; 24 String systemId; 25 26 29 30 public TransformerReceiver(Controller controller) { 31 this.controller = controller; 32 } 33 34 37 38 public void open() throws XPathException { 39 setPipelineConfiguration(controller.makePipelineConfiguration()); 40 builder = controller.makeBuilder(); 41 builder.setPipelineConfiguration(getPipelineConfiguration()); 42 builder.setSystemId(systemId); 43 Receiver stripper = controller.makeStripper(builder); 44 if (controller.getExecutable().stripsInputTypeAnnotations()) { 45 stripper = controller.getConfiguration().getAnnotationStripper(stripper); 46 } 47 this.setUnderlyingReceiver(stripper); 48 super.open(); 49 } 50 51 54 55 public Transformer getTransformer() { 56 return controller; 57 } 58 59 62 63 public void setSystemId(String url) { 64 systemId = url; 65 } 66 67 70 71 public String getSystemId() { 72 return systemId; 73 } 74 75 81 82 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 83 super.startElement(nameCode, typeCode, locationId, properties); 84 } 85 86 89 90 public void setResult(Result result) { 91 if (result==null) { 92 throw new IllegalArgumentException ("Result must not be null"); 93 } 94 this.result = result; 95 } 96 97 100 101 public Result getResult() { 102 return result; 103 } 104 105 109 110 public void close() throws XPathException { 111 super.close(); 112 DocumentInfo doc = (DocumentInfo)builder.getCurrentRoot(); 113 if (doc==null) { 114 throw new DynamicError("No source document has been built"); 115 } 116 try { 118 controller.transformDocument(doc, result); 119 } catch (TransformerException e) { 120 throw DynamicError.makeDynamicError(e); 121 } 122 } 123 124 125 126 127 } 128 129 | Popular Tags |