File: DatatypeAPIUsage.java

package info (click to toggle)
libxerces2-java 2.8.1-1%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 11,056 kB
  • ctags: 15,838
  • sloc: java: 117,816; xml: 12,454; sh: 37; makefile: 10
file content (102 lines) | stat: -rw-r--r-- 4,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright 2005 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jaxp;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * <p>A sample which demonstrates usage of the JAXP 1.3 Datatype API.</p>
 * 
 * @version $Id: DatatypeAPIUsage.java 320415 2005-06-01 04:56:47Z mrglavas $
 */
public class DatatypeAPIUsage {
    
    public static void main (String [] args) {
        try {
            DatatypeFactory df = DatatypeFactory.newInstance();
            // my work number in milliseconds:
            Duration myPhone = df.newDuration(9054133519l);
            Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
            int compareVal = myPhone.compare(myLife);
            switch (compareVal) {
                case DatatypeConstants.LESSER:
                    System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
                    break;
                case DatatypeConstants.EQUAL:
                    System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
                    break;
                case DatatypeConstants.GREATER:
                    System.out.println("There are more milliseconds in my phone number than my lifespan.");
                    break;
                case DatatypeConstants.INDETERMINATE:
                    System.out.println("The comparison could not be carried out.");
            }
            
            // create a yearMonthDuration
            Duration ymDuration = df.newDurationYearMonth("P12Y10M");
            System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());
            
            // create a dayTimeDuration (really this time)
            Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S");
            System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());
            
            // try to fool the factory!
            try {
                ymDuration = df.newDurationYearMonth("P12Y10M1D");
            }
            catch(IllegalArgumentException e) {
                System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
            }
            
            XMLGregorianCalendar xgc = df.newXMLGregorianCalendar();
            xgc.setYear(1975);
            xgc.setMonth(DatatypeConstants.AUGUST);
            xgc.setDay(11);
            xgc.setHour(6);
            xgc.setMinute(44);
            xgc.setSecond(0);
            xgc.setMillisecond(0);
            xgc.setTimezone(5);
            xgc.add(myPhone);
            System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc);
            
            //adding a duration to XMLGregorianCalendar
            xgc.add(myLife);
            System.out.println("Adding the duration myLife to the above calendar:" + xgc);
            
            // create a new XMLGregorianCalendar using the string format of xgc.
            XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());
            
            // should be equal-if not what happened!!
            if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) {
                System.out.println("oooops!");
            }
            else {
                System.out.println("Very good: " + xgc +  " is equal to " + xgcCopy);
            }
        }
        catch (DatatypeConfigurationException dce) {
            System.err.println("error: Datatype error occurred - " + dce.getMessage());
            dce.printStackTrace(System.err);
        }
    } // main(String[])
    
} // DatatypeAPIUsage