KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > common > Util


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.common;
40
41 import javax.xml.stream.XMLStreamConstants;
42 import javax.xml.stream.XMLStreamReader;
43 import javax.xml.stream.XMLEventReader;
44 import javax.xml.stream.XMLInputFactory;
45 import javax.xml.stream.XMLStreamException;
46 import javax.xml.namespace.QName JavaDoc;
47
48 /** <p>Sample utitliy.</p>
49  * This is an utility class used for samples in this package.
50  */

51 public class Util {
52     
53     /** Creates a new instance of Util */
54     public Util() {
55     }
56     
57     /** Get event type in string format
58      *
59      * @param eventType event type
60      * @return String
61      */

62     public final static String JavaDoc getEventTypeString(int eventType) {
63         switch (eventType){
64             case XMLStreamConstants.START_ELEMENT:
65                 return "START_ELEMENT";
66             case XMLStreamConstants.END_ELEMENT:
67                 return "END_ELEMENT";
68             case XMLStreamConstants.PROCESSING_INSTRUCTION:
69                 return "PROCESSING_INSTRUCTION";
70             case XMLStreamConstants.CHARACTERS:
71                 return "CHARACTERS";
72             case XMLStreamConstants.COMMENT:
73                 return "COMMENT";
74             case XMLStreamConstants.START_DOCUMENT:
75                 return "START_DOCUMENT";
76             case XMLStreamConstants.END_DOCUMENT:
77                 return "END_DOCUMENT";
78             case XMLStreamConstants.ENTITY_REFERENCE:
79                 return "ENTITY_REFERENCE";
80             case XMLStreamConstants.ATTRIBUTE:
81                 return "ATTRIBUTE";
82             case XMLStreamConstants.DTD:
83                 return "DTD";
84             case XMLStreamConstants.CDATA:
85                 return "CDATA";
86         }
87         return "UNKNOWN_EVENT_TYPE";
88     }
89     
90     /** Print out event type
91      *
92      * @param eventType event type
93      */

94     public static void printEventType(int eventType) {
95         System.out.print("EVENT TYPE("+eventType+"):");
96         System.out.println(getEventTypeString(eventType));
97     }
98     
99     /** Print out element name
100      *
101      * @param xmlr Stream reader
102      * @param eventType event type
103      */

104     public static void printName(XMLStreamReader xmlr,int eventType){
105         if(xmlr.hasName()){
106             System.out.println("HAS NAME: " + xmlr.getLocalName());
107         } else {
108             System.out.println("HAS NO NAME");
109         }
110     }
111     
112     /** Print out text
113      *
114      * @param xmlr Stream reader
115      */

116     public static void printText(XMLStreamReader xmlr){
117         if(xmlr.hasText()){
118             System.out.println("HAS TEXT: " + xmlr.getText());
119         } else {
120             System.out.println("HAS NO TEXT");
121         }
122     }
123     
124     /** Print out processing instructions
125      *
126      * @param xmlr Stream reader
127      */

128     public static void printPIData(XMLStreamReader xmlr){
129         if (xmlr.getEventType() == XMLStreamConstants.PROCESSING_INSTRUCTION){
130             System.out.println(" PI target = " + xmlr.getPITarget() ) ;
131             System.out.println(" PI Data = " + xmlr.getPIData() ) ;
132         }
133     }
134     
135     /** Print out element attributes
136      *
137      * @param xmlr Stream reader
138      */

139     public static void printAttributes(XMLStreamReader xmlr){
140         if(xmlr.getAttributeCount() > 0){
141             System.out.println("\nHAS ATTRIBUTES: ");
142             int count = xmlr.getAttributeCount() ;
143             for(int i = 0 ; i < count ; i++) {
144                 
145                 QName JavaDoc name = xmlr.getAttributeName(i) ;
146                 String JavaDoc namespace = xmlr.getAttributeNamespace(i) ;
147                 String JavaDoc type = xmlr.getAttributeType(i) ;
148                 String JavaDoc prefix = xmlr.getAttributePrefix(i) ;
149                 String JavaDoc value = xmlr.getAttributeValue(i) ;
150                 
151                 System.out.println("ATTRIBUTE-PREFIX: " + prefix );
152                 System.out.println("ATTRIBUTE-NAMESP: " + namespace );
153                 System.out.println("ATTRIBUTE-NAME: " + name.toString() );
154                 System.out.println("ATTRIBUTE-VALUE: " + value );
155                 System.out.println("ATTRIBUTE-TYPE: " + type );
156                 System.out.println();
157                 
158             }
159             
160         } else {
161             System.out.println("HAS NO ATTRIBUTES");
162         }
163     }
164         
165 }
166
Popular Tags