KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jaxp > DatatypeAPIUsage


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 jaxp;
18
19 import javax.xml.datatype.DatatypeConfigurationException JavaDoc;
20 import javax.xml.datatype.DatatypeConstants JavaDoc;
21 import javax.xml.datatype.DatatypeFactory JavaDoc;
22 import javax.xml.datatype.Duration JavaDoc;
23 import javax.xml.datatype.XMLGregorianCalendar JavaDoc;
24
25 /**
26  * <p>A sample which demonstrates usage of the JAXP 1.3 Datatype API.</p>
27  *
28  * @version $Id: DatatypeAPIUsage.java,v 1.1 2005/06/01 04:56:47 mrglavas Exp $
29  */

30 public class DatatypeAPIUsage {
31     
32     public static void main (String JavaDoc [] args) {
33         try {
34             DatatypeFactory JavaDoc df = DatatypeFactory.newInstance();
35             // my work number in milliseconds:
36
Duration JavaDoc myPhone = df.newDuration(9054133519l);
37             Duration JavaDoc myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
38             int compareVal = myPhone.compare(myLife);
39             switch (compareVal) {
40                 case DatatypeConstants.LESSER:
41                     System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
42                     break;
43                 case DatatypeConstants.EQUAL:
44                     System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
45                     break;
46                 case DatatypeConstants.GREATER:
47                     System.out.println("There are more milliseconds in my phone number than my lifespan.");
48                     break;
49                 case DatatypeConstants.INDETERMINATE:
50                     System.out.println("The comparison could not be carried out.");
51             }
52             
53             // create a yearMonthDuration
54
Duration JavaDoc ymDuration = df.newDurationYearMonth("P12Y10M");
55             System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());
56             
57             // create a dayTimeDuration (really this time)
58
Duration JavaDoc dtDuration = df.newDurationDayTime("P10DT10H12M0S");
59             System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());
60             
61             // try to fool the factory!
62
try {
63                 ymDuration = df.newDurationYearMonth("P12Y10M1D");
64             }
65             catch(IllegalArgumentException JavaDoc e) {
66                 System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
67             }
68             
69             XMLGregorianCalendar JavaDoc xgc = df.newXMLGregorianCalendar();
70             xgc.setYear(1975);
71             xgc.setMonth(DatatypeConstants.AUGUST);
72             xgc.setDay(11);
73             xgc.setHour(6);
74             xgc.setMinute(44);
75             xgc.setSecond(0);
76             xgc.setMillisecond(0);
77             xgc.setTimezone(5);
78             xgc.add(myPhone);
79             System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc);
80             
81             //adding a duration to XMLGregorianCalendar
82
xgc.add(myLife);
83             System.out.println("Adding the duration myLife to the above calendar:" + xgc);
84             
85             // create a new XMLGregorianCalendar using the string format of xgc.
86
XMLGregorianCalendar JavaDoc xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());
87             
88             // should be equal-if not what happened!!
89
if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) {
90                 System.out.println("oooops!");
91             }
92             else {
93                 System.out.println("Very good: " + xgc + " is equal to " + xgcCopy);
94             }
95         }
96         catch (DatatypeConfigurationException JavaDoc dce) {
97             System.err.println("error: Datatype error occurred - " + dce.getMessage());
98             dce.printStackTrace(System.err);
99         }
100     } // main(String[])
101

102 } // DatatypeAPIUsage
103
Popular Tags