1 package org.collectionspace.services.structureddate;
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
9 import org.apache.commons.lang.StringUtils;
10 import org.joda.time.IllegalFieldValueException;
12 public class ParseDates {
15 * Parse a newline-separated list of strings from a file (or standard input),
16 * and print the results to standard output.
18 * @param args The first argument to the program is the name of the file
19 * containing strings to parse. If not supplied, strings are
20 * read from standard input.
22 public static void main(String[] args) {
23 BufferedReader in = null;
25 if (args.length > 0) {
26 String filename = args[0];
29 in = new BufferedReader(new FileReader(filename));
30 } catch (FileNotFoundException e) {
31 System.err.println("File not found: " + filename);
35 in = new BufferedReader(new InputStreamReader(System.in));
43 for(String line; (line = in.readLine()) != null; ) {
44 line = StringUtils.trim(line);
46 if (StringUtils.isNotEmpty(line)) {
51 catch(IOException e) {
52 System.err.println("Error reading file: " + e.getLocalizedMessage());
58 catch(IOException e) {
59 System.err.println("Error closing file: " + e.getLocalizedMessage());
63 private static void parse(String displayDate) {
64 System.out.print(displayDate + "\t");
70 StructuredDateInternal structuredDate = StructuredDateInternal.parse(displayDate);
71 Date earliestSingleDate = structuredDate.getEarliestSingleDate();
72 Date latestDate = structuredDate.getLatestDate();
75 earliestSingleDate.getYear() + "-" +
76 earliestSingleDate.getMonth() + "-" +
77 earliestSingleDate.getDay() + " " +
78 earliestSingleDate.getEra().toDisplayString(); // use toString() to get the data value (refname)
80 // These don't get filled in by the parser, so no need to print.
82 // earliestSingleDate.getCertainty();
83 // earliestSingleDate.getQualifierType();
84 // earliestSingleDate.getQualifierValue();
85 // earliestSingleDate.getQualifierUnit();
86 // earliestSingleDate.getScalarValue();
88 if (latestDate != null) {
90 latestDate.getYear() + "-" +
91 latestDate.getMonth() + "-" +
92 latestDate.getDay() + " " +
93 latestDate.getEra().toDisplayString(); // use toString() to get the data value (refname)
97 structuredDate.computeScalarValues();
99 scalar = structuredDate.getEarliestScalarDate() + " - " + structuredDate.getLatestScalarDate();
101 catch(InvalidDateException e) {
102 scalar = "[invalid date: " + e.getMessage() + "]";
105 catch(StructuredDateFormatException e) {
106 result = "[unable to parse]";
110 System.out.println(result + "\t" + scalar);