一、前言
关于informa源码包informa-0.6.0.jar中的de.nava.informa.utils.ParserUtils解析类工具类,对字符串资源地址解析转换getURL、获取jdom源码Namespace命名空间、获取日期getDate及格式化、本地时区对比localTimeDiff、Base64字符串加密decodeBase64、html特殊字符串转换处理unEscape等解析处理操作,详情参见源码说明部分。
二、源码说明
1. ParserUtils工具类
package de.nava.informa.utils;@b@@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.Calendar;@b@import java.util.Date;@b@import java.util.Locale;@b@import java.util.TimeZone;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.jdom.Element;@b@import org.jdom.Namespace;@b@@b@public final class ParserUtils@b@{@b@ private static Log logger = LogFactory.getLog(ParserUtils.class);@b@ private static SimpleDateFormat[] dateFormats = null;@b@ private static SimpleDateFormat dfA;@b@ private static SimpleDateFormat dfB;@b@ private static SimpleDateFormat dfC;@b@ private static SimpleDateFormat dfD;@b@@b@ public static URL getURL(String paramString)@b@ {@b@ URL localURL = null;@b@ try@b@ {@b@ if ((paramString != null) && (paramString.trim().length() > 0))@b@ localURL = new URL(paramString);@b@ }@b@ catch (MalformedURLException localMalformedURLException)@b@ {@b@ logger.warn("Invalid URL " + paramString + " given.");@b@ }@b@ return localURL;@b@ }@b@@b@ public static Namespace getDefaultNS(Element paramElement)@b@ {@b@ return getNamespace(paramElement, "");@b@ }@b@@b@ public static Namespace getNamespace(Element paramElement, String paramString)@b@ {@b@ Namespace localNamespace = (paramString == null) ? paramElement.getNamespace("") : paramElement.getNamespace(paramString);@b@ return localNamespace;@b@ }@b@@b@ public static Date getDate(String paramString)@b@ {@b@ Date localDate = null;@b@ paramString = paramString.trim();@b@ if (paramString.length() > 10)@b@ {@b@ if ((((paramString.substring(paramString.length() - 5).indexOf("+") == 0) || (paramString.substring(paramString.length() - 5).indexOf("-") == 0))) && (paramString.substring(paramString.length() - 5).indexOf(":") == 2))@b@ {@b@ str1 = paramString.substring(paramString.length() - 5, paramString.length() - 4);@b@ paramString = paramString.substring(0, paramString.length() - 5) + str1 + "0" + paramString.substring(paramString.length() - 4);@b@ }@b@ String str1 = paramString.substring(paramString.length() - 6);@b@ if ((((str1.indexOf("-") == 0) || (str1.indexOf("+") == 0))) && (str1.indexOf(":") == 3))@b@ if ("GMT".equals(paramString.substring(paramString.length() - 9, paramString.length() - 6)))@b@ {@b@ logger.debug("General time zone with offset, no change ");@b@ }@b@ else@b@ {@b@ String str2 = paramString;@b@ String str3 = str1.substring(0, 3) + str1.substring(4);@b@ paramString = str2.substring(0, str2.length() - 6) + str3;@b@ }@b@ }@b@ int i = 0;@b@ if (i < dateFormats.length)@b@ try@b@ {@b@ localDate = dateFormats[i].parse(paramString);@b@ }@b@ catch (ParseException localParseException)@b@ {@b@ while (true)@b@ {@b@ logger.debug("parsing " + paramString + " [" + dateFormats[i].toPattern() + "] without success, trying again.");@b@ ++i;@b@ }@b@ }@b@ return localDate;@b@ }@b@@b@ public static Date getDateOLD(String paramString)@b@ {@b@ Date localDate = null;@b@ try@b@ {@b@ localDate = dfA.parse(paramString);@b@ }@b@ catch (ParseException localParseException1)@b@ {@b@ logger.warn("Error parsing date (A): " + localParseException1.getMessage());@b@ try@b@ {@b@ localDate = dfB.parse(paramString);@b@ }@b@ catch (ParseException localParseException2)@b@ {@b@ logger.warn("Error parsing date (B): " + localParseException2.getMessage());@b@ try@b@ {@b@ localDate = dfC.parse(paramString);@b@ localDate = extractTimeZone(paramString, localDate);@b@ }@b@ catch (ParseException localParseException3)@b@ {@b@ logger.warn("Error parsing date (C): " + localParseException3.getMessage());@b@ try@b@ {@b@ localDate = dfD.parse(paramString);@b@ }@b@ catch (ParseException localParseException4)@b@ {@b@ logger.warn("Error parsing date (D): " + localParseException4.getMessage());@b@ localParseException4.printStackTrace();@b@ }@b@ }@b@ }@b@ }@b@ if (logger.isDebugEnabled())@b@ logger.debug("Parsing date '" + paramString + "' resulted in: " + localDate);@b@ if (localDate == null)@b@ logger.warn("No appropiate date could be extracted from " + paramString);@b@ return localDate;@b@ }@b@@b@ private static Date extractTimeZone(String paramString, Date paramDate)@b@ {@b@ String str1 = paramString.substring(paramString.length() - 6, paramString.length() - 5);@b@ String str2 = paramString.substring(paramString.length() - 5, paramString.length() - 3);@b@ String str3 = paramString.substring(paramString.length() - 2);@b@ if ((str1.equals("-")) || (str1.equals("+")))@b@ {@b@ int i = Integer.parseInt(str2);@b@ int j = Integer.parseInt(str3);@b@ if (str1.equals("+"))@b@ {@b@ i = -1 * i;@b@ j = -1 * j;@b@ }@b@ Calendar localCalendar = Calendar.getInstance();@b@ localCalendar.setTime(paramDate);@b@ localCalendar.add(11, i);@b@ localCalendar.add(12, j);@b@ localCalendar.add(14, localTimeDiff(localCalendar.getTimeZone(), paramDate));@b@ paramDate = localCalendar.getTime();@b@ }@b@ return paramDate;@b@ }@b@@b@ private static int localTimeDiff(TimeZone paramTimeZone, Date paramDate)@b@ {@b@ if (paramTimeZone.inDaylightTime(paramDate))@b@ {@b@ int i = 0;@b@ if (paramTimeZone.useDaylightTime())@b@ i = 3600000;@b@ return (paramTimeZone.getRawOffset() + i);@b@ }@b@ return paramTimeZone.getRawOffset();@b@ }@b@@b@ public static String formatDate(Date paramDate)@b@ {@b@ return dfA.format(paramDate);@b@ }@b@@b@ public static String decodeBase64(String paramString)@b@ {@b@ return Base64Decoder.decode(paramString);@b@ }@b@@b@ public static String unEscape(String paramString)@b@ {@b@ String str = paramString;@b@ str = str.replaceAll("<", "<");@b@ str = str.replaceAll(">", ">");@b@ str = str.replaceAll("&", "&");@b@ str = str.replaceAll(""", "\"");@b@ str = str.replaceAll("'", "'");@b@ return str;@b@ }@b@@b@ static@b@ {@b@ String[] arrayOfString = { "EEE, dd MMM yyyy HH:mm:ss z", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:sszzzz", "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", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd" };@b@ dateFormats = new SimpleDateFormat[arrayOfString.length];@b@ TimeZone localTimeZone = TimeZone.getTimeZone("GMT");@b@ for (int i = 0; i < arrayOfString.length; ++i)@b@ {@b@ dateFormats[i] = new SimpleDateFormat(arrayOfString[i], Locale.ENGLISH);@b@ dateFormats[i].setTimeZone(localTimeZone);@b@ }@b@ dfA = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);@b@ dfB = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");@b@ dfC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");@b@ dfD = new SimpleDateFormat("yyyy-MM-dd");@b@ }@b@}
2.Base64Decoder类
package de.nava.informa.utils;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.FilterInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.UnsupportedEncodingException;@b@@b@class Base64Decoder extends FilterInputStream@b@{@b@ private static final char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };@b@ private static final int[] ints = new int[128];@b@ private int charCount;@b@ private int carryOver;@b@@b@ public Base64Decoder(InputStream paramInputStream)@b@ {@b@ super(paramInputStream);@b@ }@b@@b@ public int read()@b@ throws IOException@b@ {@b@ int k;@b@ do@b@ {@b@ i = this.in.read();@b@ if (i == -1)@b@ return -1;@b@ }@b@ while (Character.isWhitespace((char)i));@b@ this.charCount += 1;@b@ if (i == 61)@b@ return -1;@b@ int i = ints[i];@b@ int j = (this.charCount - 1) % 4;@b@ if (j == 0)@b@ {@b@ this.carryOver = (i & 0x3F);@b@ return read();@b@ }@b@ if (j == 1)@b@ {@b@ k = (this.carryOver << 2) + (i >> 4) & 0xFF;@b@ this.carryOver = (i & 0xF);@b@ return k;@b@ }@b@ if (j == 2)@b@ {@b@ k = (this.carryOver << 4) + (i >> 2) & 0xFF;@b@ this.carryOver = (i & 0x3);@b@ return k;@b@ }@b@ if (j == 3)@b@ {@b@ k = (this.carryOver << 6) + i & 0xFF;@b@ return k;@b@ }@b@ return -1;@b@ }@b@@b@ public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)@b@ throws IOException@b@ {@b@ for (int i = 0; i < paramInt2; ++i)@b@ {@b@ int j = read();@b@ if ((j == -1) && (i == 0))@b@ return -1;@b@ if (j == -1)@b@ break;@b@ paramArrayOfByte[(paramInt1 + i)] = (byte)j;@b@ }@b@ return i;@b@ }@b@@b@ public static String decode(String paramString)@b@ {@b@ byte[] arrayOfByte1 = null;@b@ try@b@ {@b@ arrayOfByte1 = paramString.getBytes("8859_1");@b@ }@b@ catch (UnsupportedEncodingException localUnsupportedEncodingException)@b@ {@b@ }@b@ Base64Decoder localBase64Decoder = new Base64Decoder(new ByteArrayInputStream(arrayOfByte1));@b@ ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream((int)(arrayOfByte1.length * 0.67D));@b@ try@b@ {@b@ byte[] arrayOfByte2 = new byte[4096];@b@ while ((i = localBase64Decoder.read(arrayOfByte2)) != -1)@b@ {@b@ int i;@b@ localByteArrayOutputStream.write(arrayOfByte2, 0, i);@b@ }@b@ localByteArrayOutputStream.close();@b@ return localByteArrayOutputStream.toString("8859_1");@b@ }@b@ catch (IOException localIOException)@b@ {@b@ }@b@ return null;@b@ }@b@@b@ static@b@ {@b@ for (int i = 0; i < 64; ++i)@b@ ints[chars[i]] = i;@b@ }@b@}