KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xpath > XTreeReader


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23
24 package org.xquark.xpath;
25
26 import java.util.Iterator JavaDoc;
27
28 import org.xml.sax.*;
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30 import org.xquark.util.DefaultXMLReader;
31 import org.xquark.util.SAXConstants;
32
33 /**
34  * XTree SAX2 reader producing an XML document or fragment conform to the
35  * collection element of the XMLDBC/Metadata schema.
36  * <p>Special Features:</p>
37  * <code>sax/features/fragment</code> disables SAX
38  * startDocument() and endDocument() events triggering,
39  * <code>sax/features/trace</code> calls the custom
40  * method toXML(ContentHandler) on XTreeNode implementation to add trace
41  * information.
42  */

43 public class XTreeReader extends DefaultXMLReader implements SAXConstants
44 {
45     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
46     private static final String JavaDoc RCSName = "$Name: $";
47
48     public static final String JavaDoc SOURCE_METADATA_NAMESPACE =
49         "http://www.xquark.org/XMLDBC/Metadata";
50     public static final String JavaDoc SOURCE_METADATA_PREFIX = "xm";
51     public static final String JavaDoc NS_ATTR = "ns";
52     public static final String JavaDoc NAME_ATTR = "name";
53     public static final String JavaDoc TYPE_ATTR = "type";
54     public static final String JavaDoc ATTRIBUTE_TYPE_VALUE = "attribute";
55     public static final String JavaDoc ELEMENT_TYPE_VALUE = "element";
56     public static final String JavaDoc STEP_ELEMENT_NAME = "step";
57     public static final String JavaDoc COLLECTION_ELEMENT_NAME = "collection";
58
59     private XTree tree;
60     private boolean fragment = false;
61     private boolean trace = false;
62     private AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
63
64     /**
65      * Constructor.
66      * @param tree the XTree from which SAX events will be generated
67      */

68     public XTreeReader(XTree tree)
69     {
70         this.tree = tree;
71     }
72
73     /**
74      * @see org.xml.sax.XMLReader#getFeature(String)
75      */

76     public boolean getFeature(String JavaDoc name)
77         throws SAXNotRecognizedException, SAXNotSupportedException
78     {
79         if (name.equals(FRAGMENT_FEATURE))
80             return fragment;
81         else if (name.equals(TRACE_FEATURE))
82             return trace;
83         else
84             return super.getFeature(name);
85     }
86
87     /**
88      * @see org.xml.sax.XMLReader#setFeature(String, boolean)
89      */

90     public void setFeature(String JavaDoc name, boolean value)
91         throws SAXNotRecognizedException, SAXNotSupportedException
92     {
93         if (name.equals(FRAGMENT_FEATURE))
94             fragment = value;
95         else if (name.equals(TRACE_FEATURE))
96             trace = value;
97         else
98             super.setFeature(name, value);
99     }
100
101     /**
102      * @see org.xml.sax.XMLReader#setProperty(String, Object)
103      */

104     public void setProperty(String JavaDoc name, Object JavaDoc value)
105         throws SAXNotRecognizedException, SAXNotSupportedException
106     {
107         if (name.equals(SAXConstants.SAX_LEXICAL_PROPERTY)) // ignored
108
return;
109         else
110             super.setProperty(name, value);
111     }
112
113     /**
114      * Triggers SAX events generation.
115      * @param input is ignored.
116      */

117     public void parse(InputSource input)
118         throws SAXException, java.io.IOException JavaDoc
119     {
120         parse("");
121     }
122
123     /**
124      * Triggers SAX events generation.
125      * @param collectionName is passed as a root attribute during parsing.
126      */

127     public void parse(String JavaDoc collectionName) throws SAXException, java.io.IOException JavaDoc
128     {
129         if (!fragment)
130             contentHandler.startDocument();
131
132         contentHandler.startPrefixMapping(
133             SOURCE_METADATA_PREFIX,
134             SOURCE_METADATA_NAMESPACE);
135
136         if (collectionName != null)
137             atts.addAttribute("", NAME_ATTR, "", "CDATA", collectionName);
138         contentHandler.startElement(
139             SOURCE_METADATA_NAMESPACE,
140             COLLECTION_ELEMENT_NAME,
141             "",
142             atts);
143         atts.clear();
144
145         XTreeNode root = tree.getRoot();
146         if (root.getChildren() != null)
147         {
148             Iterator JavaDoc it = root.getChildren().iterator();
149             while (it.hasNext())
150             {
151                 browseBranch((XTreeNode) it.next());
152             }
153         }
154         contentHandler.endElement(
155             SOURCE_METADATA_NAMESPACE,
156             COLLECTION_ELEMENT_NAME,
157             "");
158         // contentHandler.endPrefixMapping(WRAPPERMETADATA_PREFIX, WRAPPERMETADATA_NAMESPACE);
159

160         contentHandler.endPrefixMapping(SOURCE_METADATA_PREFIX);
161
162         if (!fragment)
163             contentHandler.endDocument();
164     }
165
166     private void browseBranch(XTreeNode node)
167         throws SAXException, java.io.IOException JavaDoc
168     {
169         if (node.getNamespace() != null)
170             atts.addAttribute("", NS_ATTR, "", "CDATA", node.getNamespace());
171
172         atts.addAttribute("", NAME_ATTR, "", "CDATA", node.getLocalName());
173
174         if (node.getType() == NodeKind.ATTRIBUTE)
175             atts.addAttribute("", TYPE_ATTR, "", "CDATA", ATTRIBUTE_TYPE_VALUE);
176         // else : element is default
177

178         contentHandler.startElement(
179             SOURCE_METADATA_NAMESPACE,
180             STEP_ELEMENT_NAME,
181             "",
182             atts);
183         atts.clear();
184
185         if (trace)
186             node.toXML(contentHandler);
187
188         // browse children
189
if ((node.getType() == NodeKind.ELEMENT) && (node.getChildren() != null))
190         {
191             Iterator JavaDoc it = node.getChildren().iterator();
192             while (it.hasNext())
193             {
194                 browseBranch((XTreeNode) it.next());
195             }
196         }
197         contentHandler.endElement(
198             SOURCE_METADATA_NAMESPACE,
199             STEP_ELEMENT_NAME,
200             "");
201     }
202
203 }
204
Popular Tags