KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > ValueConverter


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common;
25
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28
29 import org.infoglue.cms.exception.ConstraintException;
30
31 /**
32  *
33  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
34  */

35 public class ValueConverter
36 {
37   // --- [Constants] -----------------------------------------------------------
38
// --- [Attributes] ----------------------------------------------------------
39
// --- [Static] --------------------------------------------------------------
40
// --- [Constructors] --------------------------------------------------------
41

42   /**
43    *
44    */

45   private ValueConverter() {}
46
47
48
49   // --- [Public] --------------------------------------------------------------
50

51   /**
52    *
53    */

54   public static final boolean isDate(String JavaDoc value) {
55     return toDate(value) != null;
56   }
57
58   /**
59    * <todo/>
60    */

61   public static final Date JavaDoc toDate(String JavaDoc value)
62   {
63     return new Date JavaDoc();
64   }
65
66
67     /**
68      *
69      */

70      public static final Date JavaDoc getDate(String JavaDoc dateString, String JavaDoc fieldName) throws ConstraintException
71      {
72         Date JavaDoc publishDate = null;
73         try
74         {
75             publishDate = toDate(dateString, "yyyy-MM-dd");
76         }
77         catch(Exception JavaDoc e)
78         {
79             throw new ConstraintException(fieldName, "305");
80         }
81         return publishDate;
82      }
83
84     /**
85      *
86      */

87     
88     public static final Date JavaDoc toDate(String JavaDoc dateString, String JavaDoc pattern) throws Exception JavaDoc
89     {
90         if(dateString == null || dateString.length() == 0)
91             return null;
92             
93         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern);
94         return formatter.parse(dateString);
95     }
96
97   /**
98    *
99    */

100   public static final boolean isBoolean(String JavaDoc value) {
101     return toBoolean(value) != null;
102   }
103
104   /**
105    * <todo/>
106    */

107   public static final Boolean JavaDoc toBoolean(String JavaDoc value) {
108     return Boolean.valueOf(value);
109   }
110
111   /**
112    *
113    */

114   public static final boolean isNonNegativeInteger(String JavaDoc value) {
115     return toNonNegativeInteger(value) != null;
116   }
117
118   /**
119    *
120    */

121   public static final Integer JavaDoc toNonNegativeInteger(String JavaDoc value) {
122     try {
123       final int intValue = Integer.parseInt(value);
124       return (intValue < 0) ? null : new Integer JavaDoc(intValue);
125     } catch(NumberFormatException JavaDoc e) {
126       return null;
127     }
128   }
129
130   /**
131    *
132    */

133   public static final boolean isNonNegativeFloat(String JavaDoc value) {
134     return toNonNegativeFloat(value) != null;
135   }
136
137   /**
138    *
139    */

140   public static final Float JavaDoc toNonNegativeFloat(String JavaDoc value) {
141     try {
142       final float floatValue = Float.parseFloat(value);
143       return (floatValue < 0.0) ? null : new Float JavaDoc(floatValue);
144     } catch(NumberFormatException JavaDoc e) {
145       return null;
146     }
147   }
148
149
150
151   // --- [X implementation] ----------------------------------------------------
152
// --- [X Overrides] ---------------------------------------------------------
153
// --- [Package protected] ---------------------------------------------------
154
// --- [Private] -------------------------------------------------------------
155
// --- [Protected] -----------------------------------------------------------
156
// --- [Inner classes] -------------------------------------------------------
157
}
Popular Tags