KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > TreeDriver


1
2 package com.icl.saxon;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.output.Outputter;
5 import com.icl.saxon.output.GeneralOutputter;
6 import org.xml.sax.*;
7 import org.w3c.dom.Document JavaDoc;
8
9
10 import java.util.Properties JavaDoc;
11
12 import javax.xml.transform.TransformerException JavaDoc;
13 import javax.xml.transform.sax.SAXResult JavaDoc;
14
15
16 /**
17 * TreeDriver.java: (pseudo-)SAX driver for Saxon trees.<BR>
18 * Subclasses DOMDriver for the case where the tree is a Saxon tree (a DocumentInfo)
19 * This class simulates the action of a SAX Parser, taking an already-constructed
20 * DOM Document and walking around it in a depth-first traversal,
21 * calling a SAX-compliant ContentHandler to process the children as it does so.
22 */

23
24 public class TreeDriver extends DOMDriver
25 {
26
27     private Outputter outputter;
28   
29     /**
30     * Set the DOM Document that will be walked
31     */

32
33     public void setDocument(Document JavaDoc doc) {
34         root = doc;
35         if (!(doc instanceof DocumentInfo)) {
36             throw new IllegalArgumentException JavaDoc("TreeDriver can only be used with a Saxon tree");
37         }
38     }
39
40     /**
41     * Set the DOM Document that will be walked
42     */

43
44     //public void setDocumentInfo(DocumentInfo doc) {
45
// root = doc;
46
//}
47

48     /**
49     * Walk a document (traversing the nodes depth first)
50     * @param doc The (DOM) Document object to walk.
51     * @exception SAXException On any error in the document
52     */

53
54     public void parse() throws SAXException
55     {
56         if (root==null) {
57             throw new SAXException("TreeDriver: no start node defined");
58         }
59         if (contentHandler==null) {
60             throw new SAXException("DOMDriver: no content handler defined");
61         }
62         contentHandler.setDocumentLocator(this);
63         DocumentInfo doc = (DocumentInfo)root;
64         try {
65             GeneralOutputter outputter = new GeneralOutputter(doc.getNamePool());
66             SAXResult JavaDoc result = new SAXResult JavaDoc(contentHandler);
67             result.setSystemId(systemId);
68             outputter.setOutputDestination(new Properties JavaDoc(), result);
69             doc.copy(outputter);
70             outputter.close();
71         } catch (TransformerException JavaDoc err) {
72             throw new SAXException(err);
73         }
74     }
75
76 }
77 //
78
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
79
// you may not use this file except in compliance with the License. You may obtain a copy of the
80
// License at http://www.mozilla.org/MPL/
81
//
82
// Software distributed under the License is distributed on an "AS IS" basis,
83
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
84
// See the License for the specific language governing rights and limitations under the License.
85
//
86
// The Original Code is: all this file.
87
//
88
// The Initial Developer of the Original Code is
89
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
90
//
91
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
92
//
93
// Contributor(s): none.
94
//
95
Popular Tags