KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > stax > EventReader


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package samples.stax;
40 /*
41  * Demonstrate using StAX event API
42  *
43  */

44
45 import java.io.File JavaDoc;
46 import java.io.FileInputStream JavaDoc;
47 import java.io.BufferedInputStream JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.FileReader JavaDoc;
50 import javax.xml.stream.*;
51 import javax.xml.stream.events.* ;
52 import javax.xml.namespace.QName JavaDoc;
53 import javax.xml.stream.XMLStreamReader;
54 import javax.xml.stream.XMLEventReader;
55 import javax.xml.stream.XMLStreamException;
56
57 import com.sun.xml.fastinfoset.stax.StAXDocumentParser;
58 import com.sun.xml.fastinfoset.stax.StAXEventReader;
59 import samples.common.Util;
60
61
62 /** <p>This is a sample that demonstrates the use of FI StAX Event Iterator API.</p>
63  * The sample starts by creating an input stream from the input FI document. The input stream is used
64  * to instantiate a stream reader as does in the StreamReader sample. An event reader (StAXEventReader)
65  * is created using the stream reader isntance. With the event reader, the program iterates through all
66  * of the events in the input FI document and displays event types and content.
67  */

68 public class EventReader{
69     private File JavaDoc input;
70     InputStream JavaDoc document = null;
71     XMLStreamReader streamReader = null;
72     
73     /** Creates a new instance of EventReader */
74     public EventReader() {
75     }
76      /** Starts the sample. The sample takes a FI document filename that will
77       * be read using StAXEventReader.
78      *
79      * @param args a FI document.
80      */

81     public static void main(String JavaDoc[] args) {
82         if (args.length < 1 || args.length > 2) {
83             displayUsageAndExit();
84         }
85         EventReader eventReader = new EventReader();
86         try {
87             eventReader.readFIDoc(args[0]);
88         } catch (Exception JavaDoc e) {
89             e.printStackTrace();
90         }
91     }
92     
93      /** Reads a FI document using fastinfoset.StAXEventReader and displays all of the events in the document.
94      *
95      * @param filename FI document name.
96      */

97     public void readFIDoc(String JavaDoc filename) throws Exception JavaDoc {
98         input = new File JavaDoc(filename);
99         document= new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(input));
100
101         XMLStreamReader streamReader = new StAXDocumentParser(document);
102         XMLEventReader r = new StAXEventReader(streamReader);
103         int count = 0;
104         System.out.println("Reading "+ input.getName() + ": \n");
105         while(r.hasNext()) {
106             count++;
107             XMLEvent e = r.nextEvent();
108             System.out.println(count + ": " + Util.getEventTypeString(e.getEventType()) + " " + e.toString());
109         }
110     }
111     
112     private static void displayUsageAndExit() {
113         System.err.println("Usage: ant EventReader or samples.stax.EventReader FI_file");
114         System.exit(1);
115     }
116         
117 }
118
Popular Tags