KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > property > ranges > DAVDateRange


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.dav.server.property.ranges;
21
22 import java.text.*;
23 import java.text.SimpleDateFormat JavaDoc;
24
25 import org.openharmonise.commons.dsi.*;
26 import org.openharmonise.commons.xml.*;
27 import org.openharmonise.commons.xml.namespace.*;
28 import org.openharmonise.rm.resources.metadata.properties.ranges.*;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31
32 import com.ibm.webdav.*;
33 import com.ibm.webdav.WebDAVException;
34
35 /**
36  * A subclass of <code>DAVRange</code> which handles
37  * date property ranges.
38  *
39  * @author Michael Bell
40  * @version $Revision: 1.2 $
41  * @since November 20, 2003
42  */

43 public class DAVDateRange extends DAVRange {
44
45     static final public String JavaDoc DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss G";
46     static final public String JavaDoc DATE_FORMAT = "yyyy-MM-dd G";
47     static final private SimpleDateFormat JavaDoc m_datetime_formatter = new SimpleDateFormat JavaDoc(DATETIME_FORMAT);
48     static final private SimpleDateFormat JavaDoc m_date_formatter = new SimpleDateFormat JavaDoc(DATE_FORMAT);
49
50     /**
51      * @param dsi
52      */

53     public DAVDateRange(AbstractDataStoreInterface dsi) {
54         super(dsi, new DateRange());
55
56     }
57
58     /**
59      * @param dsi
60      * @param range
61      */

62     public DAVDateRange(AbstractDataStoreInterface dsi, Range range) {
63         super(dsi, range);
64
65     }
66
67     /**
68      * @param dsi
69      * @param davPropEl
70      */

71     public DAVDateRange(AbstractDataStoreInterface dsi, Element JavaDoc davPropEl)
72         throws WebDAVException {
73         super(dsi, new DateRange(), davPropEl);
74
75     }
76
77     /**
78      * Adds range details for to the given range element for date ranges
79      *
80      * @param rangeEl
81      * @param range
82      * @param doc
83      * @throws WebDAVException
84      */

85     protected void addRangeDetails(
86         Element JavaDoc rangeEl,
87         Range range,
88         Document JavaDoc doc) {
89
90         Element JavaDoc restrEl =
91             doc.createElementNS(
92                 NamespaceType.XML_SCHEMA.getURI(),
93                 TAG_RESTRICTION);
94         restrEl.setPrefix(NamespaceType.XML_SCHEMA.getPrefix());
95
96         DateRange dateRange = (DateRange) range;
97         
98         String JavaDoc sDateTag = TYPE_DATETIME;
99         
100         if(dateRange.includeTime() == false) {
101             sDateTag = TYPE_DATE;
102         }
103
104         restrEl.setAttributeNS(
105             NamespaceType.XML_SCHEMA.getURI(),
106             ATTRIB_PREFIXED_BASE,
107             sDateTag);
108
109         restrEl.setAttribute(
110             "xmlns:" + NamespaceType.XML_SCHEMA.getPrefix(),
111             NamespaceType.XML_SCHEMA.getURI());
112             
113         
114             
115         if (dateRange.hasMin() == true) {
116             String JavaDoc sTag = TAG_MININCLUSIVE;
117             
118             Element JavaDoc minEl =
119                 doc.createElementNS(
120                     NamespaceType.XML_SCHEMA.getURI(),
121                     sTag);
122             minEl.setPrefix(NamespaceType.XML_SCHEMA.getPrefix());
123             minEl.appendChild(
124                 doc.createTextNode(m_datetime_formatter.format(dateRange.getMin())));
125
126             restrEl.appendChild(minEl);
127         }
128
129         if (dateRange.hasMax() == true) {
130             String JavaDoc sTag = TAG_MAXINCLUSIVE;
131
132             Element JavaDoc maxEl =
133                 doc.createElementNS(
134                     NamespaceType.XML_SCHEMA.getURI(),
135                     sTag);
136             maxEl.setPrefix(NamespaceType.XML_SCHEMA.getPrefix());
137             maxEl.appendChild(
138                 doc.createTextNode(m_datetime_formatter.format(dateRange.getMax())));
139             restrEl.appendChild(maxEl);
140         }
141
142         rangeEl.appendChild(restrEl);
143     }
144
145     /* (non-Javadoc)
146      * @see org.openharmonise.dav.server.property.ranges.DAVRange#populate(org.w3c.dom.Element)
147      */

148     public void populate(Element JavaDoc propEl) throws WebDAVException {
149         DateRange range = (DateRange) m_range;
150         
151         Element JavaDoc restrEl = XMLUtils.getFirstNamedChild(propEl, TAG_RESTRICTION);
152
153         String JavaDoc base = restrEl.getAttributeNS(NamespaceType.XML_SCHEMA.getURI(), "base");
154         
155         SimpleDateFormat JavaDoc formatter = m_datetime_formatter;
156         
157         if(base.equals(restrEl.getPrefix() + ":date")) {
158             formatter = m_date_formatter;
159             range.includeTime(false);
160         }
161
162         try {
163             Element JavaDoc maxEl = XMLUtils.getFirstNamedChild(restrEl, TAG_MAXINCLUSIVE);
164             
165             
166             
167             if (maxEl != null) {
168                 String JavaDoc sVal = maxEl.getChildNodes().item(0).getNodeValue();
169
170                 range.setMax(formatter.parse(sVal));
171
172             }
173
174             Element JavaDoc minEl =
175                 XMLUtils.getFirstNamedChild(restrEl, TAG_MININCLUSIVE);
176             
177
178             if (minEl != null) {
179                 String JavaDoc sVal = minEl.getChildNodes().item(0).getNodeValue();
180
181                 range.setMin(formatter.parse(sVal));
182
183             }
184         } catch (ParseException e) {
185             throw new WebDAVException(
186                 WebDAVStatus.SC_BAD_REQUEST,
187                 "Wrong date format");
188         }
189     }
190
191 }
192
Popular Tags