1 26 28 package de.nava.informa.utils; 29 30 import java.net.URL ; 31 import java.text.SimpleDateFormat ; 32 import java.util.Calendar ; 33 import java.util.Date ; 34 import java.util.Locale ; 35 import java.util.TimeZone ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.jdom.Element; 40 import org.jdom.Namespace; 41 42 47 public final class ParserUtils { 48 49 private static Log logger = LogFactory.getLog(ParserUtils.class); 50 51 private ParserUtils() { 52 } 53 54 public static URL getURL(String toURL) { 55 URL result = null; 56 try { 57 if ((toURL != null) && (toURL.trim().length() > 0)) 58 result = new URL (toURL); 59 } catch (java.net.MalformedURLException e) { 60 logger.warn("Invalid URL " + toURL + " given."); 61 } 62 return result; 63 } 64 65 public static Namespace getDefaultNS(Element element) { 66 return getNamespace(element, ""); 67 } 68 69 public static Namespace getNamespace(Element element, String prefix) { 70 Namespace ns = (prefix == null) ? element.getNamespace("") : element 80 .getNamespace(prefix); 81 return ns; 82 } 83 84 private static SimpleDateFormat [] dateFormats = null; 85 86 static { 87 final String [] possibleDateFormats = { "EEE, dd MMM yyyy HH:mm:ss z", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:sszzzz", 89 "yyyy-MM-dd'T'HH:mm:ss z", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HHmmss.SSSz", 91 92 "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd"}; 93 94 dateFormats = new SimpleDateFormat [possibleDateFormats.length]; 95 TimeZone gmtTZ = TimeZone.getTimeZone("GMT"); 96 for (int i = 0; i < possibleDateFormats.length; i++) { 97 dateFormats[i] = new SimpleDateFormat (possibleDateFormats[i], 98 Locale.ENGLISH); 99 dateFormats[i].setTimeZone(gmtTZ); 100 } 101 102 } 103 104 private static SimpleDateFormat dfA = new SimpleDateFormat ( 106 "EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); 107 108 private static SimpleDateFormat dfB = new SimpleDateFormat ( 110 "yyyy-MM-dd'T'HH:mm:ssZ"); 111 112 private static SimpleDateFormat dfC = new SimpleDateFormat ( 114 "yyyy-MM-dd'T'HH:mm:ss"); 115 116 private static SimpleDateFormat dfD = new SimpleDateFormat ("yyyy-MM-dd"); 118 119 public static Date getDate(String strdate) { 120 Date result = null; 121 strdate = strdate.trim(); 122 if (strdate.length() > 10) { 123 124 if ((strdate.substring(strdate.length() - 5).indexOf("+") == 0 || strdate 126 .substring(strdate.length() - 5).indexOf("-") == 0) 127 && strdate.substring(strdate.length() - 5).indexOf(":") == 2) { 128 129 String sign = strdate.substring(strdate.length() - 5, 130 strdate.length() - 4); 131 132 strdate = strdate.substring(0, strdate.length() - 5) + sign + "0" 133 + strdate.substring(strdate.length() - 4); 134 137 } 138 139 String dateEnd = strdate.substring(strdate.length() - 6); 140 141 if ((dateEnd.indexOf("-") == 0 || dateEnd.indexOf("+") == 0) 144 && dateEnd.indexOf(":") == 3) { 145 if ("GMT".equals(strdate.substring(strdate.length() - 9, strdate 147 .length() - 6))) { 148 logger.debug("General time zone with offset, no change "); 149 } else { 150 String oldDate = strdate; 152 String newEnd = dateEnd.substring(0, 3) + dateEnd.substring(4); 153 strdate = oldDate.substring(0, oldDate.length() - 6) + newEnd; 154 } 156 } 157 } 158 int i = 0; 159 while (i < dateFormats.length) { 160 try { 161 result = dateFormats[i].parse(strdate); 162 break; 165 } catch (java.text.ParseException eA) { 166 logger.debug("parsing " + strdate + " [" 167 + dateFormats[i].toPattern() + "] without success, trying again."); 168 i++; 169 } 170 } 171 172 return result; 173 } 174 175 179 public static Date getDateOLD(String strdate) { 180 Date result = null; 181 182 try { 183 result = dfA.parse(strdate); 184 } catch (java.text.ParseException eA) { 185 logger.warn("Error parsing date (A): " + eA.getMessage()); 186 try { 187 result = dfB.parse(strdate); 188 } catch (java.text.ParseException eB) { 189 logger.warn("Error parsing date (B): " + eB.getMessage()); 190 try { 191 result = dfC.parse(strdate); 192 result = extractTimeZone(strdate, result); 194 } catch (java.text.ParseException eC) { 195 logger.warn("Error parsing date (C): " + eC.getMessage()); 196 try { 197 result = dfD.parse(strdate); 198 } catch (java.text.ParseException eD) { 199 logger.warn("Error parsing date (D): " + eD.getMessage()); 200 eD.printStackTrace(); 201 } 202 } 203 } 204 } 205 if (logger.isDebugEnabled()) { 206 logger.debug("Parsing date '" + strdate + "' resulted in: " + result); 207 } 208 if (result == null) { 209 logger.warn("No appropiate date could be extracted from " + strdate); 210 211 } 212 return result; 213 } 214 215 private static Date extractTimeZone(String strdate, Date thedate) { 216 String tzSign = strdate.substring(strdate.length() - 6, 218 strdate.length() - 5); 219 String tzHour = strdate.substring(strdate.length() - 5, 220 strdate.length() - 3); 221 String tzMin = strdate.substring(strdate.length() - 2); 222 if (tzSign.equals("-") || tzSign.equals("+")) { 223 int h = Integer.parseInt(tzHour); 224 int m = Integer.parseInt(tzMin); 225 if (tzSign.equals("+")) { 227 h = -1 * h; 228 m = -1 * m; 229 } 230 Calendar cal = Calendar.getInstance(); 231 cal.setTime(thedate); 232 cal.add(Calendar.HOUR_OF_DAY, h); 233 cal.add(Calendar.MINUTE, m); 234 cal.add(Calendar.MILLISECOND, localTimeDiff(cal.getTimeZone(), thedate)); 236 thedate = cal.getTime(); 237 } 238 return thedate; 239 } 240 241 private static int localTimeDiff(TimeZone tz, Date date) { 242 if (tz.inDaylightTime(date)) { 243 int dstSavings = 0; 244 if (tz.useDaylightTime()) { 245 dstSavings = 3600000; } 247 return tz.getRawOffset() + dstSavings; 248 } 249 return tz.getRawOffset(); 250 } 251 252 public static String formatDate(Date aDate) { 253 return dfA.format(aDate); 254 } 255 256 public static String decodeBase64(String s) { 257 return Base64Decoder.decode(s); 259 } 260 261 public static String unEscape(String s) { 262 String value = s; 263 value = value.replaceAll("<", "<"); 264 value = value.replaceAll(">", ">"); 265 value = value.replaceAll("&", "&"); 266 value = value.replaceAll(""", "\""); 267 value = value.replaceAll("'", "'"); 268 return value; 269 } 270 271 } | Popular Tags |