001: package net.sf.saxon.event;
002:
003: import net.sf.saxon.om.NodeInfo;
004: import net.sf.saxon.trans.XPathException;
005: import net.sf.saxon.type.Type;
006:
007: /**
008: * Sends an entire document to a Receiver.
009: *
010: * @author Ruud Diterwich, integrated by Michael Kay
011: */
012:
013: public class DocumentSender implements SaxonLocator {
014:
015: private NodeInfo top;
016:
017: /**
018: * Create a DocumentSender, which takes an input document tree and generates
019: * a stream of events for a Receiver
020: * @param top the document or element node to be turned into a stream of events
021: */
022:
023: public DocumentSender(NodeInfo top) {
024: this .top = top;
025: int kind = top.getNodeKind();
026: if (kind != Type.DOCUMENT && kind != Type.ELEMENT) {
027: throw new IllegalArgumentException(
028: "DocumentSender can only handle document or element nodes");
029: }
030: }
031:
032: /**
033: * Send the entire document to the receiver
034: */
035:
036: public void send(Receiver receiver) throws XPathException {
037:
038: PipelineConfiguration pipe = receiver
039: .getPipelineConfiguration();
040: if (top.getNamePool() != pipe.getConfiguration().getNamePool()
041: && !(receiver instanceof NamePoolConverter)) {
042: throw new IllegalArgumentException(
043: "DocumentSender source and target must use the same NamePool");
044: }
045:
046: // set system id
047: if (pipe.getLocationProvider() == null) {
048: receiver.setSystemId(top.getSystemId());
049: pipe.setLocationProvider(this );
050: }
051:
052: // start event stream
053: receiver.open();
054:
055: // copy the contents of the document
056: receiver.startDocument(0);
057: top.copy(receiver, NodeInfo.ALL_NAMESPACES, true, 0);
058: receiver.endDocument();
059:
060: // end event stream
061: receiver.close();
062: }
063:
064: // Implement the SAX Locator interface. This is needed to pass the base URI of nodes
065: // to the receiver. We don't attempt to preserve the original base URI of each individual
066: // node as it is copied, only the base URI of the document as a whole.
067:
068: // TODO: this is OK for some applications, but not ideal for others. To pass through the base
069: // URI of individual nodes would make it difficult to re-use the same code as xsl:copy-of, which
070: // does not do this.
071:
072: public int getColumnNumber() {
073: return -1;
074: }
075:
076: public int getLineNumber() {
077: return -1;
078: }
079:
080: public String getPublicId() {
081: return null;
082: }
083:
084: public String getSystemId() {
085: return top.getSystemId();
086: }
087:
088: public String getSystemId(int locationId) {
089: return getSystemId();
090: }
091:
092: public int getLineNumber(int locationId) {
093: return getLineNumber();
094: }
095:
096: }
097: //
098: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
099: // you may not use this file except in compliance with the License. You may obtain a copy of the
100: // License at http://www.mozilla.org/MPL/
101: //
102: // Software distributed under the License is distributed on an "AS IS" basis,
103: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
104: // See the License for the specific language governing rights and limitations under the License.
105: //
106: // The Original Code is: all this file.
107: //
108: // The Initial Developer of the Original Code is Michael H. Kay.
109: //
110: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
111: //
112: // Contributor(s): none.
113: //
|