| 1 28 29 package com.idaremedia.antx.valueuri.datetime; 30 31 import java.text.Format ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Map ; 34 35 import org.apache.tools.ant.Project; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.AntXFixture; 39 import com.idaremedia.antx.ValueURIHandler; 40 import com.idaremedia.antx.apis.Requester; 41 import com.idaremedia.antx.helpers.DateTimeFormat; 42 import com.idaremedia.antx.helpers.DurationFormat; 43 import com.idaremedia.antx.helpers.Tk; 44 45 90 91 public final class DateTimeValueURIHandler implements ValueURIHandler 92 { 93 private static final Map LINKS = AntXFixture.newMap(); 94 static { 95 LINKS.put("$longdatetime:",DateTimeFormat.STANDARD); 98 LINKS.put("$longtime:",DateTimeFormat.STANDARD_TIME); 99 LINKS.put("$longdate:",DateTimeFormat.STANDARD_DATE); 100 LINKS.put("$gmtdatetime:",DateTimeFormat.GMT); 101 LINKS.put("$gmtdate:",DateTimeFormat.GMT_DATE); 102 LINKS.put("$gmttime:",DateTimeFormat.GMT_TIME); 103 LINKS.put("$shortdatetime:",DateTimeFormat.ABBREV); 104 LINKS.put("$shorttime:",DateTimeFormat.ABBREV_TIME); 105 LINKS.put("$shortdate:",DateTimeFormat.ABBREV_DATE); 106 LINKS.put("$datetime:",DateTimeFormat.STANDARD); 107 LINKS.put("$time:",DateTimeFormat.STANDARD_TIME); 108 LINKS.put("$date:",DateTimeFormat.STANDARD_DATE); 109 LINKS.put("$duration:",DurationFormat.INSTANCE); 110 LINKS.put("$changelogdate:",DateTimeFormat.CHANGELOG); 111 LINKS.put("$cvsdate:",DateTimeFormat.CHANGELOG); 112 LINKS.put("$svndate:",DateTimeFormat.CHANGELOG); 113 } 114 115 116 117 120 public DateTimeValueURIHandler() 121 { 122 } 123 124 125 126 133 public static Format defaultFormatter(String fullUri, Requester clnt) 134 { 135 Format dfmt= null; 136 int i= fullUri.indexOf(':'); 137 if (i>0) { 138 String which= fullUri.substring(0,++i); 139 dfmt = (Format )LINKS.get(which); 140 if (dfmt==null && (i=fullUri.indexOf("?",i))>0) { 141 i++; 142 if (i<fullUri.length()-1) { 143 String fmtstr = fullUri.substring(i); 144 fmtstr = Tk.resolveString(clnt.getProject(),fmtstr,true); 145 try { 146 dfmt = new SimpleDateFormat (fmtstr); 147 } catch(IllegalArgumentException mfX) { 148 clnt.problem(mfX.getMessage(),Project.MSG_WARN); 149 } 150 } 151 } 152 } 153 return dfmt; 154 } 155 156 157 158 165 public static Map copyOfMappings() 166 { 167 synchronized(LINKS) { 168 return AntXFixture.newMapCopy(LINKS); 169 } 170 } 171 172 173 174 183 public static Boolean isTimestampScheme(String scheme) 184 { 185 Boolean yes = null; 186 if (scheme!=null) { 187 scheme = Tk.lowercaseFrom(scheme); 188 if (scheme.indexOf("date")>=0 || scheme.indexOf("time")>=0) { 189 yes = Boolean.TRUE; 190 } else if (scheme.indexOf("duration")>=0) { 191 yes = Boolean.FALSE; 192 } 193 } 194 return yes; 195 } 196 197 198 199 208 public String format(long time, String fullUri, Requester clnt) 209 { 210 Format dfmt= defaultFormatter(fullUri,clnt); 211 if (dfmt!=null) { 212 return DateTimeFormat.format(time,dfmt); 213 } 214 return null; 215 } 216 217 218 219 223 public String getDefaultValue(String fullUri, Requester clnt) 224 { 225 final long NOW = now(); 226 if (fullUri.endsWith(":now")) { 227 return format(NOW,fullUri,clnt); 228 } 229 if (fullUri.indexOf(":now?")>1) { 230 return format(NOW,fullUri,clnt); 231 } 232 return null; 233 } 234 235 236 237 243 public String valueFrom(String uriFragment, String fullUri, Requester clnt) 244 { 245 String timestring = uriFragment; 246 int i = uriFragment.indexOf('?'); 247 if (i>=0) { 248 timestring = uriFragment.substring(0,i++); 249 } 250 timestring = Tk.resolveString(clnt.getProject(),timestring,true); 251 252 if (timestring.length()==0 || "now".equals(timestring)) { 253 return format(now(),fullUri,clnt); 254 } 255 256 long timestamp = Tk.longFrom(timestring,-1L); 257 if (timestamp>0L) { 258 return format(timestamp,fullUri,clnt); 259 } 260 261 return null; 262 } 263 264 265 271 public void setNow(long timestamp) 272 { 273 AntX.require_(timestamp>=0L,AntX.utilities+"DateTimeValueURIHandler:", 274 "setnow- valid timstamp"); 275 m_nowTS = timestamp; 276 } 277 278 279 private long now() 280 { 281 return m_nowTS<0L ? System.currentTimeMillis() : m_nowTS; 282 } 283 284 285 private long m_nowTS = -1L; 286 } 287 288 | Popular Tags |