| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.jaxp; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.event.Builder; |
| 13 |
import net.sf.saxon.event.PipelineConfiguration; |
| 14 |
import net.sf.saxon.event.Receiver; |
| 15 |
import net.sf.saxon.event.ReceivingContentHandler; |
| 16 |
import net.sf.saxon.lib.ParseOptions; |
| 17 |
import net.sf.saxon.lib.Validation; |
| 18 |
import net.sf.saxon.om.NoElementsSpaceStrippingRule; |
| 19 |
import net.sf.saxon.om.NodeInfo; |
| 20 |
import net.sf.saxon.tree.tiny.TinyBuilder; |
| 21 |
import org.xml.sax.SAXException; |
| 22 |
|
| 23 |
import javax.xml.transform.Result; |
| 24 |
import javax.xml.transform.Transformer; |
| 25 |
import javax.xml.transform.TransformerException; |
| 26 |
import javax.xml.transform.sax.TransformerHandler; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class TransformerHandlerImpl extends ReceivingContentHandler implements TransformerHandler { |
| 39 |
|
| 40 |
TransformerImpl transformer; |
| 41 |
Builder builder; |
| 42 |
Receiver receiver; |
| 43 |
Result result; |
| 44 |
String systemId; |
| 45 |
boolean started = false; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected TransformerHandlerImpl(TransformerImpl transformer) { |
| 56 |
this.transformer = transformer; |
| 57 |
Controller controller = transformer.getUnderlyingXsltTransformer().getUnderlyingController(); |
| 58 |
Configuration config = transformer.getConfiguration(); |
| 59 |
int validation = controller.getSchemaValidationMode(); |
| 60 |
builder = controller.makeBuilder(); |
| 61 |
if (builder instanceof TinyBuilder) { |
| 62 |
((TinyBuilder) builder).setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS); |
| 63 |
} |
| 64 |
PipelineConfiguration pipe = builder.getPipelineConfiguration(); |
| 65 |
ParseOptions options = pipe.getParseOptions(); |
| 66 |
options.setCheckEntityReferences(true); |
| 67 |
setPipelineConfiguration(pipe); |
| 68 |
receiver = controller.makeStripper(builder); |
| 69 |
if (controller.isStylesheetStrippingTypeAnnotations()) { |
| 70 |
receiver = config.getAnnotationStripper(receiver); |
| 71 |
} |
| 72 |
if (validation != Validation.PRESERVE) { |
| 73 |
options.setSchemaValidationMode(validation); |
| 74 |
options.setSpaceStrippingRule(NoElementsSpaceStrippingRule.getInstance()); |
| 75 |
receiver = config.getDocumentValidator(receiver, getSystemId(), options, null); |
| 76 |
} |
| 77 |
setReceiver(receiver); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void startDocument() throws SAXException { |
| 90 |
if (started) { |
| 91 |
throw new UnsupportedOperationException( |
| 92 |
"The TransformerHandler is not serially reusable. The startDocument() method must be called once only."); |
| 93 |
} |
| 94 |
started = true; |
| 95 |
super.startDocument(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public Transformer getTransformer() { |
| 103 |
return transformer; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void setSystemId(String url) { |
| 116 |
systemId = url; |
| 117 |
receiver.setSystemId(url); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public String getSystemId() { |
| 127 |
return systemId; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public void setResult(Result result) { |
| 141 |
if (result == null) { |
| 142 |
throw new IllegalArgumentException("Result must not be null"); |
| 143 |
} |
| 144 |
this.result = result; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public Result getResult() { |
| 154 |
return result; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public void endDocument() throws SAXException { |
| 163 |
super.endDocument(); |
| 164 |
NodeInfo doc = builder.getCurrentRoot(); |
| 165 |
doc.getTreeInfo().setSpaceStrippingRule( |
| 166 |
transformer.getUnderlyingXsltTransformer().getUnderlyingController().getSpaceStrippingRule()); |
| 167 |
builder.reset(); |
| 168 |
if (doc == null) { |
| 169 |
throw new SAXException("No source document has been built"); |
| 170 |
} |
| 171 |
|
| 172 |
try { |
| 173 |
transformer.transform(doc, result); |
| 174 |
} catch (TransformerException err) { |
| 175 |
throw new SAXException(err); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|