]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
efa79651026ab2e03bcd16d9d76c4fd50bd76222
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.structureddate;
2
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;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.joda.time.IllegalFieldValueException;
11
12 public class ParseDates {
13         
14         /**
15          * Parse a newline-separated list of strings from a file (or standard input),
16          * and print the results to standard output.
17          * 
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.
21          */
22         public static void main(String[] args) {
23                 BufferedReader in = null;
24                 
25                 if (args.length > 0) {
26                         String filename = args[0];
27                         
28                         try {
29                                 in = new BufferedReader(new FileReader(filename));
30                         } catch (FileNotFoundException e) {
31                                 System.err.println("File not found: " + filename);
32                         }
33                 }
34                 else {
35                         in = new BufferedReader(new InputStreamReader(System.in));
36                 }
37                 
38                 if (in == null) {
39                         return;
40                 }
41                 
42                 try {
43                         for(String line; (line = in.readLine()) != null; ) {
44                                 line = StringUtils.trim(line);
45                                 
46                                 if (StringUtils.isNotEmpty(line)) {
47                                         parse(line);
48                                 }
49                         }
50                 }
51                 catch(IOException e) {
52                         System.err.println("Error reading file: " + e.getLocalizedMessage());
53                 }
54                 
55                 try {
56                         in.close();
57                 }
58                 catch(IOException e) {
59                         System.err.println("Error closing file: " + e.getLocalizedMessage());
60                 }
61         }
62         
63         private static void parse(String displayDate) {
64                 System.out.print(displayDate + "\t");
65                 
66                 String result = "";
67                 String scalar = "";
68                 
69                 try {
70                         StructuredDateInternal structuredDate = StructuredDateInternal.parse(displayDate);
71                         Date earliestSingleDate = structuredDate.getEarliestSingleDate();
72                         Date latestDate = structuredDate.getLatestDate();
73                         
74                         result = 
75                                 earliestSingleDate.getYear() + "-" +
76                                 earliestSingleDate.getMonth() + "-" +
77                                 earliestSingleDate.getDay() + " " +
78                                 earliestSingleDate.getEra().toDisplayString(); // use toString() to get the data value (refname)
79
80                                 // These don't get filled in by the parser, so no need to print.
81                         
82                                 // earliestSingleDate.getCertainty();
83                                 // earliestSingleDate.getQualifierType();
84                                 // earliestSingleDate.getQualifierValue();
85                                 // earliestSingleDate.getQualifierUnit();
86                                 // earliestSingleDate.getScalarValue();
87                         
88                         if (latestDate != null) {
89                                 result += " - " +
90                                         latestDate.getYear() + "-" +
91                                         latestDate.getMonth() + "-" +
92                                         latestDate.getDay() + " " +
93                                         latestDate.getEra().toDisplayString(); // use toString() to get the data value (refname)
94                         }
95                         
96                         try {
97                                 structuredDate.computeScalarValues();
98                         
99                                 scalar = structuredDate.getEarliestScalarDate() + " - " + structuredDate.getLatestScalarDate();
100                         }
101                         catch(InvalidDateException e) {
102                                 scalar = "[invalid date: " + e.getMessage() + "]";
103                         }
104                 }
105                 catch(StructuredDateFormatException e) {
106                         result = "[unable to parse]";
107                         scalar = "";
108                 }
109                 
110                 System.out.println(result + "\t" + scalar);
111         }
112 }