diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/knowcenter/wag/egov/egiz/pdf/EGIZDate.java | 340 |
1 files changed, 185 insertions, 155 deletions
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/EGIZDate.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/EGIZDate.java index 7b71d27..8b8475c 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/EGIZDate.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/EGIZDate.java @@ -17,9 +17,15 @@ */
package at.knowcenter.wag.egov.egiz.pdf;
+import java.text.ParseException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.commons.lang.time.DateUtils;
+
/**
* Represents a signature date and the signing time as can be found in the
* SIG_DATE field.
@@ -30,160 +36,184 @@ import java.util.regex.Pattern; *
* @author wprinz
*/
-public class EGIZDate
-{
-
- /**
- * The year.
- */
- protected int year;
-
- /**
- * The month.
- */
- protected int month;
-
- /**
- * The day.
- */
- protected int day;
-
- /**
- * The hour.
- */
- protected int hour;
-
- /**
- * The minute.
- */
- protected int minute;
-
- /**
- * The second.
- */
- protected int second;
-
- /**
- * Constructor that fills the date with values.
- *
- * @param year
- * The year.
- * @param month
- * The month.
- * @param day
- * The day.
- * @param hour
- * The hour.
- * @param minute
- * The minute.
- * @param second
- * The second.
- */
- public EGIZDate(int year, int month, int day, int hour, int minute, int second)
- {
- this.year = year;
- this.month = month;
- this.day = day;
- this.hour = hour;
- this.minute = minute;
- this.second = second;
- }
-
- /**
- * Parses the date information from a given date value.
- *
- * <p>
- * Usually the date value is one extracted from the value of the SIG_DATE
- * field.
- * </p>
- *
- * @param date_value
- * The date value String.
- * @return Returns the parsed EGIZDate. An IllegalArgumentException is thrown
- * if the date String has an illegal format.
- */
- public static EGIZDate parseFromString(String date_value)
- {
- // find the according RFC standard and cite it
-
- // BKU writes a Z after the date for some reason.
- Pattern date_pattern = Pattern.compile("^\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d[Z]?$");
- Matcher date_matcher = date_pattern.matcher(date_value);
- if (!date_matcher.matches())
- {
- throw new IllegalArgumentException("The date_value (" + date_value + " has an illegal format.");
- }
- // for some strange reasons capture groups don't work
-
- int year = Integer.parseInt(date_value.substring(0, 4));
- int month = Integer.parseInt(date_value.substring(5, 7));
- int day = Integer.parseInt(date_value.substring(8, 10));
- int hour = Integer.parseInt(date_value.substring(11, 13));
- int minute = Integer.parseInt(date_value.substring(14, 16));
- int second = Integer.parseInt(date_value.substring(17, 19));
-
- return new EGIZDate(year, month, day, hour, minute, second);
- }
-
- /**
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(Object obj)
- {
- if (!(obj instanceof EGIZDate))
- {
- return false;
- }
-
- return toString().equals(obj.toString());
- }
-
- /**
- * @see java.lang.Object#hashCode()
- */
- public int hashCode()
- {
- return toString().hashCode();
- }
-
- /**
- * @see java.lang.Object#toString()
- */
- public String toString()
- {
- return "parsed date = " + year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second;
- }
-
- /**
- * Converts the date to a long integer.
- *
- * <p>
- * An earlier date is lower than a later date.
- * </p>
- * <p>
- * E.g. a date in 1999 will get a smaller number than a date in 2006.
- * </p>
- *
- * @return Returns the compareable long.
- */
- protected long toCompareableLong()
- {
- return +this.year * 12 * 31 * 24 * 60 * 60 + this.month * 31 * 24 * 60 * 60 + this.day * 24 * 60 * 60 + this.hour * 60 * 60 + this.minute * 60 + this.second;
- }
-
- /**
- * Compares this EGIZDate to another EXIZDate.
- *
- * @param other
- * The other EGIZDate.
- * @return Returns negative if this date is earlier (lower) than the other
- * date. Returns 0 if both dates are equal. Returns positive if this
- * date is later (higher) than the other date.
- */
- public int compareTo(EGIZDate other)
- {
- long diff = toCompareableLong() - other.toCompareableLong();
- return (int) diff;
- }
+public class EGIZDate {
+
+ /**
+ * The year.
+ */
+ protected int year;
+
+ /**
+ * The month.
+ */
+ protected int month;
+
+ /**
+ * The day.
+ */
+ protected int day;
+
+ /**
+ * The hour.
+ */
+ protected int hour;
+
+ /**
+ * The minute.
+ */
+ protected int minute;
+
+ /**
+ * The second.
+ */
+ protected int second;
+
+ /**
+ * Constructor that fills the date with values.
+ *
+ * @param year
+ * The year.
+ * @param month
+ * The month.
+ * @param day
+ * The day.
+ * @param hour
+ * The hour.
+ * @param minute
+ * The minute.
+ * @param second
+ * The second.
+ */
+ public EGIZDate(int year, int month, int day, int hour, int minute,
+ int second) {
+ this.year = year;
+ this.month = month;
+ this.day = day;
+ this.hour = hour;
+ this.minute = minute;
+ this.second = second;
+ }
+
+ /**
+ * Parses the date information from a given date value.
+ *
+ * <p>
+ * Usually the date value is one extracted from the value of the SIG_DATE
+ * field.
+ * </p>
+ *
+ * @param date_value
+ * The date value String.
+ * @return Returns the parsed EGIZDate. An IllegalArgumentException is
+ * thrown if the date String has an illegal format.
+ */
+ public static EGIZDate parseFromString(String date_value) {
+ // find the according RFC standard and cite it
+
+ /*
+ * Pattern date_pattern =
+ * Pattern.compile("^\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d(Z|((\\+|\\-)\\d\\d:\\d\\d))?$");
+ * Matcher date_matcher = date_pattern.matcher(date_value); if
+ * (!date_matcher.matches()) { throw new IllegalArgumentException("The
+ * date_value (" + date_value + ") has an illegal format."); } // for
+ * some strange reasons capture groups don't work
+ *
+ * int year = Integer.parseInt(date_value.substring(0, 4)); int month =
+ * Integer.parseInt(date_value.substring(5, 7)); int day =
+ * Integer.parseInt(date_value.substring(8, 10)); int hour =
+ * Integer.parseInt(date_value.substring(11, 13)); int minute =
+ * Integer.parseInt(date_value.substring(14, 16)); int second =
+ * Integer.parseInt(date_value.substring(17, 19)); return new
+ * EGIZDate(year, month, day, hour, minute, second);
+ */
+
+ String[] parsePatterns = { "yyyy-MM-dd'T'HH:mm:ss",
+ "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ssZZ" };
+ if (date_value.length() > 19) {
+ int li = date_value.lastIndexOf(":");
+ if (li >= 19) {
+ date_value = new StringBuffer(date_value).deleteCharAt(li).toString();
+ }
+ if (date_value.endsWith("Z")) {
+ date_value = date_value.substring(0, date_value.length()-2) + "UTC";
+ }
+ } else {
+ date_value += "UTC";
+ }
+
+ Date date;
+ try {
+ date = DateUtils.parseDate(date_value, parsePatterns);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException("The date_value (" + date_value
+ + ") has an illegal format.");
+ }
+ Calendar calendar = new GregorianCalendar();
+ calendar.setTime(date);
+
+ return new EGIZDate(calendar.get(Calendar.YEAR), calendar
+ .get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
+ calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE),
+ calendar.get(Calendar.SECOND));
+
+ }
+
+ /**
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object obj) {
+ if (!(obj instanceof EGIZDate)) {
+ return false;
+ }
+
+ return toString().equals(obj.toString());
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode() {
+ return toString().hashCode();
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return "parsed date = " + year + "-" + month + "-" + day + "T" + hour
+ + ":" + minute + ":" + second;
+ }
+
+ /**
+ * Converts the date to a long integer.
+ *
+ * <p>
+ * An earlier date is lower than a later date.
+ * </p>
+ * <p>
+ * E.g. a date in 1999 will get a smaller number than a date in 2006.
+ * </p>
+ *
+ * @return Returns the compareable long.
+ */
+ protected long toCompareableLong() {
+ return +this.year * 12 * 31 * 24 * 60 * 60 + this.month * 31 * 24 * 60
+ * 60 + this.day * 24 * 60 * 60 + this.hour * 60 * 60
+ + this.minute * 60 + this.second;
+ }
+
+ /**
+ * Compares this EGIZDate to another EXIZDate.
+ *
+ * @param other
+ * The other EGIZDate.
+ * @return Returns negative if this date is earlier (lower) than the other
+ * date. Returns 0 if both dates are equal. Returns positive if this
+ * date is later (higher) than the other date.
+ */
+ public int compareTo(EGIZDate other) {
+ long diff = toCompareableLong() - other.toCompareableLong();
+ return (int) diff;
+ }
}
|