01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.xml.parser;
28:
29: import java.util.Calendar;
30: import java.util.Date;
31:
32: import org.w3c.dom.Node;
33: import org.w3c.dom.NodeList;
34:
35: /**
36: * Parses dates that are a part of schedule tags or preferences.
37: */
38: public class DateParser {
39: /**
40: * Includes fix for jdk1.2.1 "bug" (months are 0 based)
41: *
42: * Calendar.set sets the date to be month+1 i.e. 0 = Jan
43: *
44: */
45: public Date getDate(Node node) {
46: Calendar cal = Calendar.getInstance();
47: NodeList nlist = node.getChildNodes();
48: int nlength = nlist.getLength();
49: int year = -1;
50: int month = -1;
51: int day = -1;
52: int hour = -1;
53: int minute = -1;
54: int second = -1;
55:
56: for (int i = 0; i < nlength; i++) {
57: Node child = nlist.item(i);
58: String childname = child.getNodeName();
59:
60: if (child.getNodeType() == Node.ELEMENT_NODE) {
61: if (childname.equals("year")) {
62: Node data = child.getFirstChild();
63: Integer j = new Integer(data.getNodeValue());
64: year = j.intValue();
65: } else if (childname.equals("month")) {
66: Node data = child.getFirstChild();
67: Integer j = new Integer(data.getNodeValue());
68: month = j.intValue();
69: } else if (childname.equals("day")) {
70: Node data = child.getFirstChild();
71: Integer j = new Integer(data.getNodeValue());
72: day = j.intValue();
73: } else if (childname.equals("hour")) {
74: Node data = child.getFirstChild();
75: Integer j = new Integer(data.getNodeValue());
76: hour = j.intValue();
77: } else if (childname.equals("minute")) {
78: Node data = child.getFirstChild();
79: Integer j = new Integer(data.getNodeValue());
80: minute = j.intValue();
81: } else if (childname.equals("second")) {
82: Node data = child.getFirstChild();
83: Integer j = new Integer(data.getNodeValue());
84: second = j.intValue();
85: }
86: }
87: }
88:
89: // reset all the fields, including the MILLISECONDS field in
90: // the calendar
91: cal.clear();
92:
93: cal.set(year, month - 1, day, hour, minute, second);
94: // Note jdk1.2 "bug"^^
95:
96: return cal.getTime();
97: }
98: }
|