~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/xerces-2_9_1/samples/jaxp/DatatypeAPIUsage.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 * 
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 */
 
17
 
 
18
package jaxp;
 
19
 
 
20
import javax.xml.datatype.DatatypeConfigurationException;
 
21
import javax.xml.datatype.DatatypeConstants;
 
22
import javax.xml.datatype.DatatypeFactory;
 
23
import javax.xml.datatype.Duration;
 
24
import javax.xml.datatype.XMLGregorianCalendar;
 
25
 
 
26
/**
 
27
 * <p>A sample which demonstrates usage of the JAXP 1.3 Datatype API.</p>
 
28
 * 
 
29
 * @version $Id: DatatypeAPIUsage.java,v 1.2 2009/12/10 03:18:46 matthewoliver Exp $
 
30
 */
 
31
public class DatatypeAPIUsage {
 
32
    
 
33
    public static void main (String [] args) {
 
34
        try {
 
35
            DatatypeFactory df = DatatypeFactory.newInstance();
 
36
            // my work number in milliseconds:
 
37
            Duration myPhone = df.newDuration(9054133519l);
 
38
            Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
 
39
            int compareVal = myPhone.compare(myLife);
 
40
            switch (compareVal) {
 
41
                case DatatypeConstants.LESSER:
 
42
                    System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
 
43
                    break;
 
44
                case DatatypeConstants.EQUAL:
 
45
                    System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
 
46
                    break;
 
47
                case DatatypeConstants.GREATER:
 
48
                    System.out.println("There are more milliseconds in my phone number than my lifespan.");
 
49
                    break;
 
50
                case DatatypeConstants.INDETERMINATE:
 
51
                    System.out.println("The comparison could not be carried out.");
 
52
            }
 
53
            
 
54
            // create a yearMonthDuration
 
55
            Duration ymDuration = df.newDurationYearMonth("P12Y10M");
 
56
            System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());
 
57
            
 
58
            // create a dayTimeDuration (really this time)
 
59
            Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S");
 
60
            System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());
 
61
            
 
62
            // try to fool the factory!
 
63
            try {
 
64
                ymDuration = df.newDurationYearMonth("P12Y10M1D");
 
65
            }
 
66
            catch(IllegalArgumentException e) {
 
67
                System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
 
68
            }
 
69
            
 
70
            XMLGregorianCalendar xgc = df.newXMLGregorianCalendar();
 
71
            xgc.setYear(1975);
 
72
            xgc.setMonth(DatatypeConstants.AUGUST);
 
73
            xgc.setDay(11);
 
74
            xgc.setHour(6);
 
75
            xgc.setMinute(44);
 
76
            xgc.setSecond(0);
 
77
            xgc.setMillisecond(0);
 
78
            xgc.setTimezone(5);
 
79
            xgc.add(myPhone);
 
80
            System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc);
 
81
            
 
82
            //adding a duration to XMLGregorianCalendar
 
83
            xgc.add(myLife);
 
84
            System.out.println("Adding the duration myLife to the above calendar:" + xgc);
 
85
            
 
86
            // create a new XMLGregorianCalendar using the string format of xgc.
 
87
            XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());
 
88
            
 
89
            // should be equal-if not what happened!!
 
90
            if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) {
 
91
                System.out.println("oooops!");
 
92
            }
 
93
            else {
 
94
                System.out.println("Very good: " + xgc +  " is equal to " + xgcCopy);
 
95
            }
 
96
        }
 
97
        catch (DatatypeConfigurationException dce) {
 
98
            System.err.println("error: Datatype error occurred - " + dce.getMessage());
 
99
            dce.printStackTrace(System.err);
 
100
        }
 
101
    } // main(String[])
 
102
    
 
103
} // DatatypeAPIUsage