KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > simpletype > DatatypeInterfaceUsage


1 /*
2  * Copyright 2005 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 package simpletype;
18
19 import java.io.File JavaDoc;
20
21 import javax.xml.XMLConstants JavaDoc;
22 import javax.xml.parsers.SAXParser JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24 import javax.xml.validation.SchemaFactory JavaDoc;
25
26 import org.apache.xerces.xs.ElementPSVI;
27 import org.apache.xerces.xs.PSVIProvider;
28 import org.apache.xerces.xs.XSConstants;
29 import org.apache.xerces.xs.datatypes.ByteList;
30 import org.apache.xerces.xs.datatypes.ObjectList;
31 import org.apache.xerces.xs.datatypes.XSDateTime;
32 import org.apache.xerces.xs.datatypes.XSDecimal;
33 import org.apache.xerces.xs.datatypes.XSDouble;
34 import org.apache.xerces.xs.datatypes.XSFloat;
35 import org.apache.xerces.xs.datatypes.XSQName;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38
39 /**
40  * <p>Demonstrates how to use the datatype interfaces in
41  * <code>org.apache.xerces.xs.datatypes</code>.</p>
42  *
43  * @author Ankit Pasricha, IBM
44  *
45  * @version $Id: DatatypeInterfaceUsage.java,v 1.3 2005/05/20 22:01:01 mrglavas Exp $
46  */

47 public class DatatypeInterfaceUsage extends DefaultHandler JavaDoc {
48
49     private PSVIProvider provider;
50     
51     public DatatypeInterfaceUsage(PSVIProvider p) {
52         provider = p;
53     }
54     
55     /**
56      * @param args
57      */

58     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
59         
60         if(args.length == 2) {
61             // Use SAX parser and store the PSVIProvider to access PSVI info.
62
SAXParserFactory JavaDoc fact = SAXParserFactory.newInstance();
63             fact.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File JavaDoc(args[0])));
64             SAXParser JavaDoc parser = fact.newSAXParser();
65             PSVIProvider p = (PSVIProvider)parser.getXMLReader();
66             
67             parser.parse(args[1], new DatatypeInterfaceUsage(p));
68         }
69         else
70             printUsage();
71     }
72     
73     static void printUsage() {
74         System.err.println("USAGE: java java.DatatypeInterfaceUsage <xsd file name (one only)> <xml file name(one only)>");
75     }
76
77     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
78         ElementPSVI psvi = provider.getElementPSVI();
79         if(psvi != null) {
80             //If there was an element content
81
Object JavaDoc value = psvi.getActualNormalizedValue();
82             short type = psvi.getActualNormalizedValueType();
83             if(value != null) {
84                 switch(type) {
85                 case XSConstants.INTEGER_DT:
86                 case XSConstants.DECIMAL_DT:
87                 case XSConstants.INT_DT:
88                 case XSConstants.LONG_DT:
89                 case XSConstants.SHORT_DT:
90                 case XSConstants.BYTE_DT:
91                 case XSConstants.UNSIGNEDBYTE_DT:
92                 case XSConstants.UNSIGNEDINT_DT:
93                 case XSConstants.UNSIGNEDLONG_DT:
94                 case XSConstants.UNSIGNEDSHORT_DT:
95                     XSDecimal decimal = (XSDecimal)value;
96                     System.out.println(decimal.getInt());
97                     break;
98                     
99                 case XSConstants.DATE_DT:
100                 case XSConstants.DATETIME_DT:
101                 case XSConstants.GDAY_DT:
102                 case XSConstants.GMONTH_DT:
103                 case XSConstants.GMONTHDAY_DT:
104                 case XSConstants.GYEAR_DT:
105                 case XSConstants.GYEARMONTH_DT:
106                 case XSConstants.DURATION_DT:
107                 case XSConstants.TIME_DT:
108                     XSDateTime dt = (XSDateTime)value;
109                     System.out.println(dt.getDays());
110                     break;
111                     
112                 case XSConstants.FLOAT_DT:
113                     XSFloat f = (XSFloat)value;
114                     System.out.println(f.getValue());
115                     break;
116                     
117                 case XSConstants.DOUBLE_DT:
118                     XSDouble d = (XSDouble)value;
119                     System.out.println(d.getValue());
120                     break;
121                     
122                 case XSConstants.HEXBINARY_DT:
123                 case XSConstants.BASE64BINARY_DT:
124                     ByteList list = (ByteList)value;
125                     System.out.println(list.getLength());
126                     break;
127                     
128                 case XSConstants.LIST_DT:
129                 case XSConstants.LISTOFUNION_DT:
130                     ObjectList l = (ObjectList)value;
131                     System.out.println(l.getLength());
132                     break;
133                     
134                 case XSConstants.QNAME_DT:
135                     XSQName qname = (XSQName)value;
136                     System.out.println(qname.getXNIQName());
137                     break;
138                 
139                 default:
140                     System.out.println("error!!!");
141                 }
142             }
143         }
144         super.endElement(uri, localName, qName);
145     }
146
147 }
148
Popular Tags