2
* Copyright 2005 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
21
import javax.xml.XMLConstants;
22
import javax.xml.parsers.SAXParser;
23
import javax.xml.parsers.SAXParserFactory;
24
import javax.xml.validation.SchemaFactory;
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;
37
import org.xml.sax.helpers.DefaultHandler;
40
* <p>Demonstrates how to use the datatype interfaces in
41
* <code>org.apache.xerces.xs.datatypes</code>.</p>
43
* @author Ankit Pasricha, IBM
45
* @version $Id: DatatypeInterfaceUsage.java 320381 2005-05-20 22:01:01Z mrglavas $
47
public class DatatypeInterfaceUsage extends DefaultHandler {
49
private PSVIProvider provider;
51
public DatatypeInterfaceUsage(PSVIProvider p) {
58
public static void main(String[] args) throws Exception {
60
if(args.length == 2) {
61
// Use SAX parser and store the PSVIProvider to access PSVI info.
62
SAXParserFactory fact = SAXParserFactory.newInstance();
63
fact.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File(args[0])));
64
SAXParser parser = fact.newSAXParser();
65
PSVIProvider p = (PSVIProvider)parser.getXMLReader();
67
parser.parse(args[1], new DatatypeInterfaceUsage(p));
73
static void printUsage() {
74
System.err.println("USAGE: java java.DatatypeInterfaceUsage <xsd file name (one only)> <xml file name(one only)>");
77
public void endElement(String uri, String localName, String qName) throws SAXException {
78
ElementPSVI psvi = provider.getElementPSVI();
80
//If there was an element content
81
Object value = psvi.getActualNormalizedValue();
82
short type = psvi.getActualNormalizedValueType();
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());
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());
112
case XSConstants.FLOAT_DT:
113
XSFloat f = (XSFloat)value;
114
System.out.println(f.getValue());
117
case XSConstants.DOUBLE_DT:
118
XSDouble d = (XSDouble)value;
119
System.out.println(d.getValue());
122
case XSConstants.HEXBINARY_DT:
123
case XSConstants.BASE64BINARY_DT:
124
ByteList list = (ByteList)value;
125
System.out.println(list.getLength());
128
case XSConstants.LIST_DT:
129
case XSConstants.LISTOFUNION_DT:
130
ObjectList l = (ObjectList)value;
131
System.out.println(l.getLength());
134
case XSConstants.QNAME_DT:
135
XSQName qname = (XSQName)value;
136
System.out.println(qname.getXNIQName());
140
System.out.println("error!!!");
144
super.endElement(uri, localName, qName);