KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > stax > StreamReader


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 import java.io.PrintWriter JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.InputStream JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.BufferedInputStream JavaDoc;
46 import java.io.FileNotFoundException JavaDoc;
47 import javax.xml.stream.XMLStreamReader;
48 import javax.xml.stream.XMLEventReader;
49 import javax.xml.stream.XMLStreamException;
50
51 import com.sun.xml.fastinfoset.stax.StAXDocumentParser;
52 import samples.common.Util;
53
54 /** <p>This is a sample that demonstrates the use of FI StAX Cursor API.</p>
55  * The sample starts by creating an input stream from the input FI document. The input stream is
56  * then used to instantiating a new instance of FI StAX stream reader "StAXDocumentParser". The
57  * rest of the code simply uses the StreamReader to loop through
58  * the document and displays event types, names and etc.
59  */

60 public class StreamReader {
61     
62     /** Creates a new instance of Cursor */
63     public StreamReader() {
64     }
65      /** Starts the sample. The sample takes a FI document that will
66      * be read using StAXDocumentParser in the fastinfoset package.
67      *
68      * @param args a FI document.
69      */

70     public static void main(String JavaDoc[] args) {
71         if (args.length < 1 || args.length > 1) {
72             displayUsageAndExit();
73         }
74         StreamReader streamReader = new StreamReader();
75         streamReader.parse(args[0]);
76     }
77     
78      /** Reads a FI document using StAXDocumentParser and displays event types and contents.
79      *
80      * @param filename FI document name.
81      */

82     public void parse(String JavaDoc filename) {
83         File JavaDoc input = null;
84         InputStream JavaDoc document = null;
85         XMLStreamReader streamReader = null;
86         try{
87             input = new File JavaDoc(filename);
88             document= new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(filename));
89             
90             streamReader = new StAXDocumentParser(document);
91                         
92         } catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }
95         
96         System.out.println("Reading "+ input.getName() + ": \n");
97         long starttime = System.currentTimeMillis() ;
98         try{
99             int eventType = streamReader.getEventType();
100
101             while(streamReader.hasNext()){
102                 eventType = streamReader.next();
103                 Util.printEventType(eventType);
104                 Util.printName(streamReader,eventType);
105                 Util.printText(streamReader);
106                 if(streamReader.isStartElement()){
107                     Util.printAttributes(streamReader);
108                 }
109                 Util.printPIData(streamReader);
110                 System.out.println("-----------------------------");
111             }
112         }catch(Exception JavaDoc ex){
113             ex.printStackTrace();
114         }
115         long endtime = System.currentTimeMillis();
116         System.out.println(" Parsing Time = " + (endtime - starttime) );
117                 
118     }
119
120     private static void displayUsageAndExit() {
121         System.err.println("Usage: ant StreamReader or samples.stax.StreamReader FI_file");
122         System.exit(1);
123     }
124         
125 }
126
Popular Tags