1 16 17 package simpletype; 18 19 import java.io.File ; 20 21 import javax.xml.XMLConstants ; 22 import javax.xml.parsers.SAXParser ; 23 import javax.xml.parsers.SAXParserFactory ; 24 import javax.xml.validation.SchemaFactory ; 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 ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 39 47 public class DatatypeInterfaceUsage extends DefaultHandler { 48 49 private PSVIProvider provider; 50 51 public DatatypeInterfaceUsage(PSVIProvider p) { 52 provider = p; 53 } 54 55 58 public static void main(String [] args) throws Exception { 59 60 if(args.length == 2) { 61 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(); 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 uri, String localName, String qName) throws SAXException { 78 ElementPSVI psvi = provider.getElementPSVI(); 79 if(psvi != null) { 80 Object 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 |