001: package net.sf.saxon.event;
002:
003: import net.sf.saxon.Controller;
004: import net.sf.saxon.om.DocumentInfo;
005: import net.sf.saxon.trans.DynamicError;
006: import net.sf.saxon.trans.XPathException;
007:
008: import javax.xml.transform.Result;
009: import javax.xml.transform.Transformer;
010: import javax.xml.transform.TransformerException;
011:
012: /**
013: * <b>TransformerReceiver</b> is similar in concept to the JAXP TransformerHandler,
014: * except that it implements Saxon's Receiver interface rather than the standard
015: * SAX2 interface. This means that it allows nodes with type annotations to be
016: * passed down a pipeline from one transformation to another.
017: */
018:
019: public class TransformerReceiver extends ProxyReceiver {
020:
021: Controller controller;
022: Builder builder;
023: Result result;
024: String systemId;
025:
026: /**
027: * Create a TransformerHandlerImpl and initialise variables.
028: */
029:
030: public TransformerReceiver(Controller controller) {
031: this .controller = controller;
032: }
033:
034: /**
035: * Start of event stream
036: */
037:
038: public void open() throws XPathException {
039: setPipelineConfiguration(controller.makePipelineConfiguration());
040: builder = controller.makeBuilder();
041: builder.setPipelineConfiguration(getPipelineConfiguration());
042: builder.setSystemId(systemId);
043: Receiver stripper = controller.makeStripper(builder);
044: if (controller.getExecutable().stripsInputTypeAnnotations()) {
045: stripper = controller.getConfiguration()
046: .getAnnotationStripper(stripper);
047: }
048: this .setUnderlyingReceiver(stripper);
049: super .open();
050: }
051:
052: /**
053: * Get the Transformer used for this transformation
054: */
055:
056: public Transformer getTransformer() {
057: return controller;
058: }
059:
060: /**
061: * Set the SystemId of the document
062: */
063:
064: public void setSystemId(String url) {
065: systemId = url;
066: }
067:
068: /**
069: * Get the systemId of the document
070: */
071:
072: public String getSystemId() {
073: return systemId;
074: }
075:
076: /**
077: * Notify the start of an element
078: * @param nameCode integer code identifying the name of the element within the name pool.
079: * @param typeCode integer code identifying the element's type within the name pool.
080: * @param properties bit-significant properties of the element node.
081: */
082:
083: public void startElement(int nameCode, int typeCode,
084: int locationId, int properties) throws XPathException {
085: super .startElement(nameCode, typeCode, locationId, properties);
086: }
087:
088: /**
089: * Set the output destination of the transformation
090: */
091:
092: public void setResult(Result result) {
093: if (result == null) {
094: throw new IllegalArgumentException(
095: "Result must not be null");
096: }
097: this .result = result;
098: }
099:
100: /**
101: * Get the output destination of the transformation
102: */
103:
104: public Result getResult() {
105: return result;
106: }
107:
108: /**
109: * Override the behaviour of endDocument() in ReceivingContentHandler, so that it fires off
110: * the transformation of the constructed document
111: */
112:
113: public void close() throws XPathException {
114: super .close();
115: DocumentInfo doc = (DocumentInfo) builder.getCurrentRoot();
116: if (doc == null) {
117: throw new DynamicError("No source document has been built");
118: }
119: //doc.getNamePool().allocateDocumentNumber(doc);
120: try {
121: controller.transformDocument(doc, result);
122: } catch (TransformerException e) {
123: throw DynamicError.makeDynamicError(e);
124: }
125: }
126:
127: }
128:
129: //
130: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
131: // you may not use this file except in compliance with the License. You may obtain a copy of the
132: // License at http://www.mozilla.org/MPL/
133: //
134: // Software distributed under the License is distributed on an "AS IS" basis,
135: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
136: // See the License for the specific language governing rights and limitations under the License.
137: //
138: // The Original Code is: all this file.
139: //
140: // The Initial Developer of the Original Code is Michael H. Kay.
141: //
142: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143: //
144: // Contributor(s): None
145: //
|