KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Xerces01Parser


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.tester;
19
20
21 import java.net.URL JavaDoc;
22 import org.xml.sax.AttributeList JavaDoc;
23 import org.xml.sax.HandlerBase JavaDoc;
24 import org.xml.sax.Parser JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.SAXParseException JavaDoc;
27 import org.xml.sax.helpers.ParserFactory JavaDoc;
28
29
30 /**
31  * SAX parser (based on SAXCount) that exercises the Xerces parser within the
32  * environment of a web application.
33  *
34  * @author Amy Roh
35  * @author Craig McClanahan
36  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
37  */

38
39 public class Xerces01Parser extends HandlerBase JavaDoc {
40
41
42     // ----------------------------------------------------- Instance Variables
43

44
45     /**
46      * The number of XML attributes we have encountered.
47      */

48     protected int attributes = 0;
49
50
51     /**
52      * The number of characters we have encountered.
53      */

54     protected int characters = 0;
55
56
57     /**
58      * The number of XML elements we have encountered.
59      */

60     protected int elements = 0;
61
62
63     /**
64      * The amount of ignorable whitespace we have encountered.
65      */

66     protected int whitespace = 0;
67
68
69
70     // --------------------------------------------------------- Public Methods
71

72
73     /**
74      * Execute the requested parse.
75      *
76      * @param url The URL of the XML resource to be parsed
77      *
78      * @exception Exception if any processing exception occurs
79      */

80     public void parse(URL JavaDoc url) throws Exception JavaDoc {
81
82         // Construct a parser for our use
83
Parser JavaDoc parser =
84             ParserFactory.makeParser("org.apache.xerces.parsers.SAXParser");
85         parser.setDocumentHandler(this);
86         parser.setErrorHandler(this);
87
88         // Perform the requested parse
89
long before = System.currentTimeMillis();
90         parser.parse(url.toString());
91         long after = System.currentTimeMillis();
92
93         // Log the results
94
StaticLogger.write("Parsing time = " + (after - before) +
95                            " milliseconds");
96         StaticLogger.write("Processed " + elements + " elements");
97         StaticLogger.write("Processed " + attributes + " attributes");
98         StaticLogger.write("Processed " + characters + " characters");
99         StaticLogger.write("Processed " + whitespace + " whitespaces");
100
101     }
102
103
104     // ------------------------------------------------------ SAX Error Methods
105

106
107     /**
108      * Receive notification of a parser error.
109      *
110      * @param e The parser exception being reported
111      *
112      * @exception SAXException if a parsing error occurs
113      */

114     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
115
116         StaticLogger.write("[Error] " +
117                            getLocationString(e) + ": " +
118                            e.getMessage());
119
120     }
121
122
123     /**
124      * Receive notification of a fatal error.
125      *
126      * @param e The parser exception being reported
127      *
128      * @exception SAXException if a parsing error occurs
129      */

130     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
131
132         StaticLogger.write("[Fatal] " +
133                            getLocationString(e) + ": " +
134                            e.getMessage());
135
136     }
137
138
139     /**
140      * Receive notification of a parser warning.
141      *
142      * @param e The parser exception being reported
143      *
144      * @exception SAXException if a parsing error occurs
145      */

146     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
147
148         StaticLogger.write("[Warning] " +
149                            getLocationString(e) + ": " +
150                            e.getMessage());
151
152     }
153
154
155     /**
156      * Return the location at which this exception occurred.
157      *
158      * @param e The SAXParseException we are reporting on
159      */

160     private String JavaDoc getLocationString(SAXParseException JavaDoc e) {
161
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
163         String JavaDoc systemId = e.getSystemId();
164         if (systemId != null) {
165             int index = systemId.lastIndexOf('/');
166             if (index != -1)
167                 systemId = systemId.substring(index + 1);
168             sb.append(systemId);
169         }
170         sb.append(':');
171         sb.append(e.getLineNumber());
172         sb.append(':');
173         sb.append(e.getColumnNumber());
174         return (sb.toString());
175
176     }
177
178
179     // ------------------------------------------------------ SAX Event Methods
180

181
182     /**
183      * Character data event handler.
184      *
185      * @param ch Character array containing the characters
186      * @param start Starting position in the array
187      * @param length Number of characters to process
188      *
189      * @exception SAXException if a parsing error occurs
190      */

191     public void characters(char ch[], int start, int length)
192         throws SAXException JavaDoc {
193
194         characters += length;
195
196     }
197
198
199     /**
200      * Ignorable whitespace event handler.
201      *
202      * @param ch Character array containing the characters
203      * @param start Starting position in the array
204      * @param length Number of characters to process
205      *
206      * @exception SAXException if a parsing error occurs
207      */

208     public void ignorableWhitespace(char ch[], int start, int length)
209         throws SAXException JavaDoc {
210
211         whitespace += length;
212
213     }
214
215
216     /**
217      * Start of element event handler.
218      *
219      * @param name The element type name
220      * @param attrs The specified or defaulted attributes
221      *
222      * @exception SAXException if a parsing error occurs
223      */

224     public void startElement(String JavaDoc name, AttributeList JavaDoc attrs) {
225
226         elements++;
227         if (attrs != null)
228             attributes += attrs.getLength();
229
230     }
231
232
233
234 }
235
Popular Tags