KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > DocumentSender


1 package net.sf.saxon.event;
2
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.Type;
6
7 /**
8  * Sends an entire document to a Receiver.
9  *
10  * @author Ruud Diterwich, integrated by Michael Kay
11  */

12
13 public class DocumentSender implements SaxonLocator {
14
15     private NodeInfo top;
16
17     /**
18     * Create a DocumentSender, which takes an input document tree and generates
19     * a stream of events for a Receiver
20     * @param top the document or element node to be turned into a stream of events
21     */

22
23     public DocumentSender(NodeInfo top) {
24         this.top = top;
25         int kind = top.getNodeKind();
26         if (kind != Type.DOCUMENT && kind != Type.ELEMENT) {
27             throw new IllegalArgumentException JavaDoc("DocumentSender can only handle document or element nodes");
28         }
29     }
30
31     /**
32     * Send the entire document to the receiver
33     */

34
35     public void send(Receiver receiver) throws XPathException {
36
37         PipelineConfiguration pipe = receiver.getPipelineConfiguration();
38         if (top.getNamePool() != pipe.getConfiguration().getNamePool()) {
39             throw new IllegalArgumentException JavaDoc("DocumentSender source and target must use the same NamePool");
40         }
41
42         // set system id
43
if (pipe.getLocationProvider() == null) {
44             receiver.setSystemId(top.getSystemId());
45             pipe.setLocationProvider(this);
46         }
47
48         // start event stream
49
receiver.open();
50
51         // copy the contents of the document
52
receiver.startDocument(0);
53         top.copy(receiver, NodeInfo.ALL_NAMESPACES, true, 0);
54         receiver.endDocument();
55
56         // end event stream
57
receiver.close();
58     }
59
60     // Implement the SAX Locator interface. This is needed to pass the base URI of nodes
61
// to the receiver. We don't attempt to preserve the original base URI of each individual
62
// node as it is copied, only the base URI of the document as a whole.
63

64     // TODO: this is OK for some applications, but not ideal for others. To pass through the base
65
// URI of individual nodes would make it difficult to re-use the same code as xsl:copy-of, which
66
// does not do this.
67

68     public int getColumnNumber() {
69         return -1;
70     }
71
72     public int getLineNumber() {
73         return -1;
74     }
75
76     public String JavaDoc getPublicId() {
77         return null;
78     }
79
80     public String JavaDoc getSystemId() {
81         return top.getSystemId();
82     }
83
84     public String JavaDoc getSystemId(int locationId) {
85         return getSystemId();
86     }
87
88     public int getLineNumber(int locationId) {
89         return getLineNumber();
90     }
91
92 }
93 //
94
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
95
// you may not use this file except in compliance with the License. You may obtain a copy of the
96
// License at http://www.mozilla.org/MPL/
97
//
98
// Software distributed under the License is distributed on an "AS IS" basis,
99
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
100
// See the License for the specific language governing rights and limitations under the License.
101
//
102
// The Original Code is: all this file.
103
//
104
// The Initial Developer of the Original Code is Michael H. Kay.
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
Popular Tags