KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > datetime > EqualDateTimeValueURIHandler


1 /**
2  * $Id: EqualDateTimeValueURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.valueuri.datetime;
30
31 import java.text.ParseException JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34
35 import com.idaremedia.antx.apis.Requester;
36 import com.idaremedia.antx.helpers.DateTimeFormat;
37 import com.idaremedia.antx.helpers.Tk;
38 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton;
39
40 /**
41  * Value URI handler that compares two timestamps for equality. Both timestamps must
42  * be supplied (or implied); otherwise the handler returns <i>null</i>.
43  * <p/>
44  * <b>Example Usage:</b><pre>
45  * &lt;do true="${$<b>equaldate:</b>@(crdate1),,@(crdate2)}"&gt;
46  * ...
47  *
48  * -- To Install --
49  * &lt;manageuris action="install"&gt;
50  * &lt;parameter name="samedate"
51  * value="com.idaremedia.antx.valueuri.datetime.EqualDateTimeValueURIHandler"/&gt;
52  * &lt;parameter name="equaldate"
53  * value="com.idaremedia.antx.valueuri.datetime.EqualDateTimeValueURIHandler"/&gt;
54  * &lt;/manageuris&gt;
55  * </pre>
56  *
57  * @since JWare/AntX 0.5
58  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
59  * @version 0.5
60  * @.safety multiple
61  * @.group api,helper
62  **/

63
64 public class EqualDateTimeValueURIHandler extends ValueURIHandlerSkeleton
65 {
66     /**
67      * Initializes a new same datetime value comparer handler.
68      **/

69     public EqualDateTimeValueURIHandler()
70     {
71     }
72
73
74     /**
75      * Converts the comparision operation into a normalized true|false
76      * string. Will return <i>null</i> if uri is malformed or unable to
77      * parse date information.
78      */

79     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
80     {
81         uriFragment = Tk.resolveString(clnt.getProject(),uriFragment,true);
82
83         String JavaDoc ds1=null,ds2=null;
84         final int N = uriFragment.length();
85         int j,i;
86         String JavaDoc fs = null;
87  
88         i = uriFragment.indexOf(",,");
89         if (i>0) {
90             ds1 = uriFragment.substring(0,i);
91             i += 2;
92             if (i<N) {
93                 j = uriFragment.indexOf("?",i);
94                 if (j<0) {
95                     ds2 = uriFragment.substring(i);
96                 } else {
97                     ds2 = uriFragment.substring(i,j);
98                     j++;
99                     if (j<N) {
100                         fs = uriFragment.substring(j);
101                     }
102                 }
103             }
104         }
105         if (ds1!=null && ds2!=null) {
106             Date JavaDoc d1=null,d2=null;
107             SimpleDateFormat JavaDoc df = DateTimeFormat.CHANGELOG;
108             if (fs!=null) {
109                 df = new SimpleDateFormat JavaDoc(fs);
110             }
111             synchronized(df) {
112                 try {
113                     d1 = df.parse(ds1);
114                 } catch(ParseException JavaDoc px) {/*burp*/}
115                 try {
116                     d2 = df.parse(ds2);
117                 } catch(ParseException JavaDoc px) {/*burp*/}
118             }
119             df = null;
120             if (d1!=null && d2!=null) {
121                 return String.valueOf(compare(d1,d2));
122             }
123         }
124         return null;
125     }
126     
127
128     /**
129      * Perform the comparision operation and return value as a match
130      * or not. Will do an equality test by default; subclasses should
131      * override to be perform other comparison tests.
132      * @param d1 first date in uri (non-null)
133      * @param d2 second date in uri (non-null)
134      * @return <i>true</i> if comparision condition met.
135      */

136     protected boolean compare(Date JavaDoc d1, Date JavaDoc d2)
137     {
138         return d1.equals(d2);
139     }
140 }
141
142 /* end-of-EqualDateTimeValueURIHandler.java */
Popular Tags