]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-753: Return scalar values when parsing structured dates.
authorRay Lee <ray.lee@lyrasis.org>
Tue, 22 Oct 2019 18:25:49 +0000 (11:25 -0700)
committerRay Lee <ray.lee@lyrasis.org>
Tue, 22 Oct 2019 20:26:37 +0000 (13:26 -0700)
services/structureddate/jaxb/src/main/resources/structureddate-common.xsd
services/structureddate/service/src/main/java/org/collectionspace/services/structureddate/StructuredDateResource.java
services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/DateUtils.java
services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/ParseDates.java
services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/StructuredDateInternal.java
services/structureddate/structureddate/src/main/java/org/collectionspace/services/structureddate/antlr/ANTLRStructuredDateEvaluator.java

index 68af2e685b452ba404bec506703b8fc00045e6d6..ee27baac9a0f55cf71d1f6ed3327a2a8a77ff62a 100644 (file)
@@ -2,14 +2,14 @@
 
 <!--
     StructuredDate schema (XSD)
-    
+
     Entity  : StructuredDate
     Part    : Common
     Used for: JAXB binding between XML and Java objects
 
 -->
 
-<xs:schema 
+<xs:schema
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
   jaxb:version="1.0" elementFormDefault="unqualified"
                            <xs:element name="period" type="xs:string"/>
                            <xs:element name="earliestSingleDate" type="date_common"/>
                            <xs:element name="latestDate" type="date_common"/>
-                           <xs:element name="earliestScalarDate" type="xs:string"/>
-                           <xs:element name="latestScalarDate" type="xs:string"/>
+                           <xs:element name="earliestScalarValue" type="xs:string"/>
+                           <xs:element name="latestScalarValue" type="xs:string"/>
                            <xs:element name="scalarValuesComputed" type="xs:boolean"/>
                        </xs:sequence>
                </xs:complexType>
     </xs:element>
-    
+
     <xs:complexType name="date_common">
         <xs:sequence>
             <xs:element name="year" type="xs:integer"/>
@@ -52,5 +52,5 @@
             <xs:element name="qualifierUnit" type="xs:string"/>
         </xs:sequence>
     </xs:complexType>
-        
+
 </xs:schema>
index a19e658fceb118b27396bbbc1e9161f44357ff46..327a3a2f891b78c47ceaa2990fb693067ee95f85 100644 (file)
@@ -40,26 +40,26 @@ public class StructuredDateResource extends AbstractCollectionSpaceResourceImpl<
                // TODO Auto-generated method stub
                return null;
        }
-       
+
        //
        // API Endpoints
        //
-       
+
     @GET
     public StructureddateCommon get(@Context UriInfo ui) {
        StructureddateCommon result = null;
-       
+
        try {
                ServiceContext<StructureddateCommon, StructureddateCommon> ctx = createServiceContext(getServiceName());
                MultivaluedMap<String,String> queryParams = ui.getQueryParameters();
                String dateToParse = queryParams.getFirst(StructuredDateClient.DATE_TO_PARSE_QP);
-               if (Tools.isEmpty(dateToParse) != true) {       
+               if (Tools.isEmpty(dateToParse) != true) {
                        StructuredDateInternal structuredDate = StructuredDateInternal.parse(dateToParse);
                        result = toStructureddateCommon(ctx.getTenantName(), structuredDate);
                } else {
                        String msg = String.format("Use the '%s' query parameter to specify a date string you want parsed.",
                                        StructuredDateClient.DATE_TO_PARSE_QP);
-                       Response response = 
+                       Response response =
                                Response.status(Response.Status.BAD_REQUEST).entity(msg).type("text/plain").build();
                        throw new CSWebApplicationException(response);
                }
@@ -67,83 +67,95 @@ public class StructuredDateResource extends AbstractCollectionSpaceResourceImpl<
                Response response = Response.status(Response.Status.BAD_REQUEST).entity(fe.getMessage()).type("text/plain").build();
             throw new CSWebApplicationException(response);
        } catch (Exception e) {
-               throw bigReThrow(e, ServiceMessages.GET_FAILED); 
-       }
-       
-       return result;
-    }
-    
-    private StructureddateCommon toStructureddateCommon(String tenantDomain, StructuredDateInternal structuredDate) {
-       StructureddateCommon result = new StructureddateCommon();
-       
-       String association = structuredDate.getAssociation();
-       if (!Tools.isEmpty(association)) {
-               result.setAssociation(association);
+               throw bigReThrow(e, ServiceMessages.GET_FAILED);
        }
-       
-       String displayDate = structuredDate.getDisplayDate();
-       if (!Tools.isEmpty(displayDate)) {
-               result.setDisplayDate(displayDate);
-       }       
-       
-       String earliestScalarDate = structuredDate.getEarliestScalarDate();
-       if (!Tools.isEmpty(earliestScalarDate)) {
-               result.setEarliestScalarDate(earliestScalarDate);
-       }       
-       
-       Date earliestSingleDate = structuredDate.getEarliestSingleDate();
-       if (earliestSingleDate != null) {
-               result.setEarliestSingleDate(toDateCommon(tenantDomain, earliestSingleDate));
-       }       
-       
-       result.setLatestDate(toDateCommon(tenantDomain, structuredDate.getLatestDate()));
-       Date latestDate = structuredDate.getLatestDate();
-       if (latestDate != null) {
-               result.setLatestDate(toDateCommon(tenantDomain, latestDate));
-       }       
-       
+
        return result;
     }
 
+               private StructureddateCommon toStructureddateCommon(String tenantDomain, StructuredDateInternal structuredDate) {
+                       StructureddateCommon result = new StructureddateCommon();
+
+                       String association = structuredDate.getAssociation();
+
+                       if (!Tools.isEmpty(association)) {
+                               result.setAssociation(association);
+                       }
+
+                       String displayDate = structuredDate.getDisplayDate();
+
+                       if (!Tools.isEmpty(displayDate)) {
+                               result.setDisplayDate(displayDate);
+                       }
+
+                       String earliestScalarValue = structuredDate.getEarliestScalarValue();
+
+                       if (!Tools.isEmpty(earliestScalarValue)) {
+                               result.setEarliestScalarValue(earliestScalarValue);
+                       }
+
+                       String latestScalarValue = structuredDate.getLatestScalarValue();
+
+                       if (!Tools.isEmpty(latestScalarValue)) {
+                               result.setLatestScalarValue(latestScalarValue);
+                       }
+
+                       result.setScalarValuesComputed(structuredDate.areScalarValuesComputed());
+
+                       Date earliestSingleDate = structuredDate.getEarliestSingleDate();
+
+                       if (earliestSingleDate != null) {
+                               result.setEarliestSingleDate(toDateCommon(tenantDomain, earliestSingleDate));
+                       }
+
+                       Date latestDate = structuredDate.getLatestDate();
+
+                       if (latestDate != null) {
+                               result.setLatestDate(toDateCommon(tenantDomain, latestDate));
+                       }
+
+                       return result;
+               }
+
     private DateCommon toDateCommon(String tenantDomain, org.collectionspace.services.structureddate.Date date) {
        DateCommon result = null;
-       
+
        if (date != null) {
                result = new DateCommon();
-               
+
                if (date.getCertainty() != null) {
                        result.setCertainty(date.getCertainty().toString());
                }
-               
+
                if (date.getDay() != null) {
                        result.setDay(BigInteger.valueOf(date.getDay()));
                }
-               
+
                if (date.getEra() != null) {
                        result.setEra(date.getEra().toString(tenantDomain));
                }
-               
+
                if (date.getMonth() != null) {
                        result.setMonth(BigInteger.valueOf(date.getMonth()));
                }
-               
+
                if (date.getQualifierType() != null) {
                        result.setQualifierType(date.getQualifierType().toString());
                }
-               
+
                if (date.getQualifierUnit() != null) {
                        result.setQualifierUnit(date.getQualifierUnit().toString());
                }
-               
+
                if (date.getQualifierValue() != null) {
                        result.setQualifierValue(date.getQualifierValue().toString());
                }
-               
+
                if (date.getYear() != null) {
                        result.setYear(BigInteger.valueOf(date.getYear()));
                }
        }
-       
+
        return result;
     }
 
index a6883d72de6840421dd00ab7b8e9da5bce611e9d..77b6a7f76ebe3ac7a0699991de3c982c8b857339 100644 (file)
@@ -5,27 +5,28 @@ import org.joda.time.DateTime;
 import org.joda.time.DateTimeConstants;
 import org.joda.time.Days;
 import org.joda.time.IllegalFieldValueException;
+import org.joda.time.LocalDate;
 import org.joda.time.MutableDateTime;
 import org.joda.time.Years;
 import org.joda.time.chrono.GJChronology;
 import org.joda.time.format.DateTimeFormat;
 import org.joda.time.format.DateTimeFormatter;
-import org.joda.time.LocalDate;
 
 public class DateUtils {
        private static final DateTimeFormatter monthFormatter = DateTimeFormat.forPattern("MMMM");
-       private static final DateTimeFormatter scalarDateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
+       private static final DateTimeFormatter scalarValueFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
+       private static final DateTimeFormatter timestampFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
 
        // The chronology to use for date calculations, which are done using the joda-time library.
        // See http://www.joda.org/joda-time/apidocs/org/joda/time/Chronology.html for descriptions of
        // the chronologies supported by joda-time.
-       
+
        // GJChronology (http://www.joda.org/joda-time/apidocs/org/joda/time/chrono/GJChronology.html)
        // seems best for representing a mix of modern and historical dates, as might be seen by an
        // anthropology museum.
-       
+
        private static final Chronology chronology = GJChronology.getInstance();
-       
+
        // Define the DateTime that serves as the basis for circa calculations, using the algorithm
        // ported from the XDB date parser. Its comment states:
        //
@@ -33,29 +34,29 @@ public class DateUtils {
        //    as +/- 5% of the difference between that year/century
        //    and the present (2100), so that the farther we go back
        //    in time, the wider the range of meaning of "circa."
-       
+
        private static final DateTime circaBaseDateTime = new DateTime(2100, 12, 31, 0, 0, 0, 0, chronology);
-       
+
        /**
         * Gets the number (1-12) of a month for a given name.
-        * 
+        *
         * @param monthName The name of the month
         * @return          The number of the month, between 1 and 12
         */
        public static int getMonthByName(String monthName) {
                // Normalize "sept" to "sep", since DateTimeFormat doesn't
                // understand the former.
-               
+
                if (monthName.equals("sept")) {
                        monthName = "sep";
                }
-               
+
                return monthFormatter.parseDateTime(monthName).getMonthOfYear();
        }
-       
+
        /**
         * Gets the number of days in a given month.
-        * 
+        *
         * @param month The month number, between 1 and 12
         * @param year  The year (in order to account for leap years)
         * @return      The number of days in the month
@@ -64,7 +65,7 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                DateTime dateTime = new DateTime(chronology)
                                .withEra((era == Era.BCE) ? DateTimeConstants.BC : DateTimeConstants.AD)
                                .withYearOfEra(year)
@@ -72,46 +73,46 @@ public class DateUtils {
 
                return dateTime.dayOfMonth().getMaximumValue();
        }
-       
+
        /**
         * Gets the Date representing the first day of a given quarter year.
-        * 
+        *
         * @param year    The year
         * @param quarter The quarter, between 1 and 4
         * @return        The first day of the quarter year
         */
        public static Date getQuarterYearStartDate(int quarter, int year) {
                int startMonth = getQuarterYearStartMonth(quarter);
-               
+
                return new Date(year, startMonth, 1);
        }
-       
+
        /**
         * Gets the Date representing the last day of a given quarter year.
-        * 
+        *
         * @param year    The year
         * @param quarter The quarter, between 1 and 4
         * @return        The last day of the quarter year
         */
        public static Date getQuarterYearEndDate(int quarter, int year, Era era) {
                int endMonth = getQuarterYearEndMonth(quarter);
-               
+
                return new Date(year, endMonth, DateUtils.getDaysInMonth(endMonth, year, era));
        }
-       
+
        /**
         * Gets the first month of a given quarter in a year.
-        * 
+        *
         * @param quarter The quarter, between 1 and 4
         * @return        The number of the first month in the quarter
         */
        public static int getQuarterYearStartMonth(int quarter) {
                return ((3 * (quarter-1)) + 1);
        }
-       
+
        /**
         * Gets the last month of a given quarter in a year.
-        * 
+        *
         * @param quarter The quarter, between 1 and 4
         * @return        The number of the last month in the quarter
         */
@@ -121,34 +122,34 @@ public class DateUtils {
 
        /**
         * Gets the Date representing the first day of a given half year.
-        * 
+        *
         * @param year The year
         * @param half The half, between 1 and 2
         * @return     The first day of the half year
         */
        public static Date getHalfYearStartDate(int half, int year) {
                int startMonth = getHalfYearStartMonth(half);
-               
+
                return new Date(year, startMonth, 1);
        }
 
 
        /**
         * Gets the Date representing the last day of a given half year.
-        * 
+        *
         * @param year The year
         * @param half The half, between 1 and 2
         * @return     The last day of the half year
         */
        public static Date getHalfYearEndDate(int half, int year, Era era) {
                int endMonth = getHalfYearEndMonth(half);
-               
+
                return new Date(year, endMonth, DateUtils.getDaysInMonth(endMonth, year, era));
        }
 
        /**
         * Gets the first month of a given half in a year.
-        * 
+        *
         * @param half The half, between 1 and 2
         * @return     The number of the first month in the half
         */
@@ -158,49 +159,49 @@ public class DateUtils {
 
        /**
         * Gets the last month of a given half in a year.
-        * 
+        *
         * @param half The half, between 1 and 2
         * @return     The number of the last month in the half
         */
        public static int getHalfYearEndMonth(int half) {
                return (getHalfYearStartMonth(half) + 5);
        }
-       
+
        /**
         * Gets the Date representing the first day of a given partial year.
-        * 
+        *
         * @param year The year
         * @param part The part
         * @return     The first day of the partial year
         */
        public static Date getPartialYearStartDate(Part part, int year) {
                int startMonth = getPartialYearStartMonth(part);
-               
+
                return new Date(year, startMonth, 1);
        }
 
        /**
         * Gets the Date representing the last day of a given partial year.
-        * 
+        *
         * @param year The year
         * @param part The part
         * @return     The last day of the partial year
         */
        public static Date getPartialYearEndDate(Part part, int year, Era era) {
                int endMonth = getPartialYearEndMonth(part);
-               
+
                return new Date(year, endMonth, DateUtils.getDaysInMonth(endMonth, year, era));
        }
-       
+
        /**
         * Gets the first month of a given part of a year.
-        * 
+        *
         * @param part The part
         * @return     The number of the first month in the part
         */
        public static int getPartialYearStartMonth(Part part) {
                int month;
-               
+
                if (part == Part.EARLY) {
                        month = 1;
                }
@@ -213,19 +214,19 @@ public class DateUtils {
                else {
                        throw new IllegalArgumentException("unexpected part");
                }
-               
+
                return month;
        }
-       
+
        /**
         * Gets the last month of a given part of a year.
-        * 
+        *
         * @param part The part
         * @return     The number of the last month in the part
         */
        public static int getPartialYearEndMonth(Part part) {
                int month;
-               
+
                if (part == Part.EARLY) {
                        month = 4;
                }
@@ -238,15 +239,15 @@ public class DateUtils {
                else {
                        throw new IllegalArgumentException("unexpected part");
                }
-               
+
                return month;
        }
-       
+
        /**
         * Gets the Date representing the first day of a given partial decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param part   The part
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
@@ -258,15 +259,15 @@ public class DateUtils {
                }
 
                int startYear = getPartialDecadeStartYear(decade, part, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
 
        /**
         * Gets the Date representing the last day of a given partial decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param part   The part
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
@@ -278,15 +279,15 @@ public class DateUtils {
                }
 
                int endYear = getPartialDecadeEndYear(decade, part, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given part of a decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param part   The part
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
@@ -296,9 +297,9 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear;
-               
+
                if (era == Era.BCE) {
                        if (part == Part.EARLY) {
                                startYear = decade + 9;
@@ -327,15 +328,15 @@ public class DateUtils {
                                throw new IllegalArgumentException("unexpected part");
                        }
                }
-               
+
                return startYear;
        }
-       
+
        /**
         * Gets the last year of a given part of a decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param part   The part
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
@@ -345,9 +346,9 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear;
-               
+
                if (era == Era.BCE) {
                        if (part == Part.EARLY) {
                                endYear = decade + 7;
@@ -376,15 +377,15 @@ public class DateUtils {
                                throw new IllegalArgumentException("unexpected part");
                        }
                }
-               
+
                return endYear;
        }
 
        /**
         * Gets the Date representing the first day of a given decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
         * @return       The first day of the decade
@@ -395,15 +396,15 @@ public class DateUtils {
                }
 
                int startYear = getDecadeStartYear(decade, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
-       
+
        /**
         * Gets the Date representing the last day of a given decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
         * @return       The last day of the decade
@@ -414,15 +415,15 @@ public class DateUtils {
                }
 
                int endYear = getDecadeEndYear(decade, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
         * @return       The first year of the decade
@@ -431,24 +432,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear;
-               
+
                if (era == Era.BCE) {
                        startYear = decade + 9;
                }
                else {
                        startYear = decade;
                }
-               
+
                return startYear;
        }
 
        /**
         * Gets the last year of a given decade.
-        * 
+        *
         * @param decade The decade, specified as a number ending in 0.
-        *               For decades A.D., this is the first year of the decade. For 
+        *               For decades A.D., this is the first year of the decade. For
         *               decades B.C., this is the last year of the decade.
         * @param era    The era of the decade. If null, Date.DEFAULT_ERA is assumed.
         * @return       The last year of the decade
@@ -457,24 +458,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear;
-               
+
                if (era == Era.BCE) {
                        endYear = decade;
                }
                else {
                        endYear = decade + 9;
                }
-               
+
                return endYear;
        }
-               
+
        /**
         * Gets the Date representing the first day of a given century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -488,15 +489,15 @@ public class DateUtils {
                }
 
                int startYear = getCenturyStartYear(century, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
 
        /**
         * Gets the Date representing the last day of a given century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -510,15 +511,15 @@ public class DateUtils {
                }
 
                int endYear = getCenturyEndYear(century, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -530,24 +531,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear;
-               
+
                if (era == Era.BCE) {
                        startYear = century + 99;
                }
                else {
                        startYear = century;
                }
-               
+
                return startYear;
        }
-       
+
        /**
         * Gets the last year of a given century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -559,24 +560,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear;
-               
+
                if (era == Era.BCE) {
                        endYear = century;
                }
                else {
                        endYear = century + 99;
                }
-               
+
                return endYear;
        }
-       
+
        /**
         * Gets the Date representing the first day of a given partial century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -591,15 +592,15 @@ public class DateUtils {
                }
 
                int startYear = getPartialCenturyStartYear(century, part, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
-       
+
        /**
         * Gets the Date representing the last day of a given partial century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -614,15 +615,15 @@ public class DateUtils {
                }
 
                int endYear = getPartialCenturyEndYear(century, part, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given partial century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -635,9 +636,9 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear;
-               
+
                if (era == Era.BCE) {
                        if (part == Part.EARLY) {
                                startYear = century + 99;
@@ -666,15 +667,15 @@ public class DateUtils {
                                throw new IllegalArgumentException("unexpected part");
                        }
                }
-               
+
                return startYear;
        }
-       
+
        /**
         * Gets the last year of a given partial century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -687,9 +688,9 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear;
-               
+
                if (era == Era.BCE) {
                        if (part == Part.EARLY) {
                                endYear = century + 66;
@@ -718,15 +719,15 @@ public class DateUtils {
                                throw new IllegalArgumentException("unexpected part");
                        }
                }
-               
+
                return endYear;
        }
-       
+
        /**
         * Gets the Date representing the first day of a given half century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -741,15 +742,15 @@ public class DateUtils {
                }
 
                int startYear = getHalfCenturyStartYear(century, half, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
 
        /**
         * Gets the Date representing the last day of a given half century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -764,15 +765,15 @@ public class DateUtils {
                }
 
                int endYear = getHalfCenturyEndYear(century, half, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given half century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -785,24 +786,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear;
-               
+
                if (era == Era.BCE) {
                        startYear = (century + 99) - (50 * (half - 1));
                }
                else {
                        startYear = century + (50 * (half - 1));
                }
-               
+
                return startYear;
        }
-       
+
        /**
         * Gets the last year of a given half century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -815,24 +816,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear;
-               
+
                if (era == Era.BCE) {
                        endYear = (century + 99) - (50 * half) + 1;
                }
                else {
                        endYear = century + (50 * half) - 1;
                }
-               
+
                return endYear;
        }
-       
+
        /**
         * Gets the Date representing the first day of a given quarter century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -845,17 +846,17 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear = getQuarterCenturyStartYear(century, quarter, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
 
        /**
         * Gets the Date representing the last day of a given quarter century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -868,17 +869,17 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear = getQuarterCenturyEndYear(century, quarter, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given quarter century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -891,24 +892,24 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int startYear;
-               
+
                if (era == Era.BCE) {
                        startYear = (century + 99) - (25 * (quarter - 1));
                }
                else {
                        startYear = century + (25 * (quarter - 1));
                }
-               
+
                return startYear;
        }
-       
+
        /**
         * Gets the last year of a given quarter century.
-        * 
+        *
         * @param century The century, specified as a number ending in 00 or 01.
-        *                For centuries A.D., this is the first year of the century. For 
+        *                For centuries A.D., this is the first year of the century. For
         *                centuries B.C., this is the last year of the century. For example,
         *                the "21st century" would be specified as 2001, whereas the "2000's"
         *                would be specified as 2000. The "2nd century B.C." would be specified
@@ -921,38 +922,38 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int endYear;
-               
+
                if (era == Era.BCE) {
                        endYear = (century + 99) - (25 * quarter) + 1;
                }
                else {
                        endYear = century + (25 * quarter) - 1;
                }
-               
+
                return endYear;
        }
-       
+
        /**
         * Converts an nth century number to a year. For example, to convert "21st century"
         * to a year, call nthCenturyToYear(21), which returns 2001. For centuries A.D., the
         * year returned is the first year of the nth century. For centuries B.C., the
-        * year returned is the last year of the nth century. 
-        * 
+        * year returned is the last year of the nth century.
+        *
         * @param n The nth century number
         * @return  The first year in the nth century, for centuries A.D.
         *          The last year of the nth century, for centuries B.C.
         */
        public static int nthCenturyToYear(int n) {
                int year = (n-1) * 100 + 1;
-               
+
                return year;
        }
-       
+
        /**
         * Gets the Date representing the first day of a given millennium.
-        * 
+        *
         * @param n   The nth millennium number
         * @param era The era of the millennium. If null, Date.DEFAULT_ERA is assumed.
         * @return    The first day of the millennium
@@ -963,13 +964,13 @@ public class DateUtils {
                }
 
                int startYear = getMillenniumStartYear(n, era);
-               
+
                return new Date(startYear, 1, 1, era);
        }
 
        /**
         * Gets the Date representing the last day of a given millennium.
-        * 
+        *
         * @param n   The nth millennium number
         * @param era The era of the millennium. If null, Date.DEFAULT_ERA is assumed.
         * @return    The last day of the millennium
@@ -980,13 +981,13 @@ public class DateUtils {
                }
 
                int endYear = getMillenniumEndYear(n, era);
-               
+
                return new Date(endYear, 12, 31, era);
        }
-       
+
        /**
         * Gets the first year of a given millennium.
-        * 
+        *
         * @param n   The nth millennium number
         * @param era The era of the millennium. If null, Date.DEFAULT_ERA is assumed.
         * @return    The first year of the millennium
@@ -995,7 +996,7 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int year;
 
                if (era == Era.BCE) {
@@ -1004,13 +1005,13 @@ public class DateUtils {
                else {
                        year = (n - 1) * 1000 + 1;
                }
-               
+
                return year;
        }
 
        /**
         * Gets the last year of a given millennium.
-        * 
+        *
         * @param n   The nth millennium number
         * @param era The era of the millennium. If null, Date.DEFAULT_ERA is assumed.
         * @return    The last year of the millennium
@@ -1019,7 +1020,7 @@ public class DateUtils {
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                int year;
 
                if (era == Era.BCE) {
@@ -1028,36 +1029,36 @@ public class DateUtils {
                else {
                        year = n * 1000;
                }
-               
+
                return year;
        }
-       
+
        /**
         * Calculates the earliest date that may be considered to be "before"
         * a given date.
-        * 
+        *
         * @param date The date
         * @return     The earliest date "before" the date
         */
        public static Date getEarliestBeforeDate(Date date) {
                return getEarliestBeforeDate(date, null);
        }
-       
+
        /**
         * Calculates the latest date that may be considered to be "after"
         * a given date.
-        * 
+        *
         * @param date The date
         * @return     The latest date "after" the date
         */
        public static Date getLatestAfterDate(Date date) {
                return getLatestAfterDate(date, null);
        }
-       
+
        /**
         * Calculates the earliest date that may be considered to be "before"
         * a given date range.
-        * 
+        *
         * @param startDate The first date in the range
         * @param endDate   The last date in the range
         * @return          The earliest date "before" the range
@@ -1066,7 +1067,7 @@ public class DateUtils {
                // TODO
                // Return an empty date to be used in before date cases
                return new Date();
-               
+
                /*
                // This algorithm is inherited from the XDB fuzzydate parser,
                // which considers "before" to mean "within a lifetime before".
@@ -1074,14 +1075,14 @@ public class DateUtils {
                if (endDate == null) {
                        endDate = startDate;
                }
-               
+
                int difference = getYearsBetween(startDate, endDate);
-               
+
                Date earliestDate = startDate.copy();
                subtractYears(earliestDate, 1);
                earliestDate.setMonth(1);
                earliestDate.setDay(1);
-               
+
                if (difference < 100) {
                        // The comment from the XDB fuzzydate parser states:
                        //
@@ -1101,15 +1102,15 @@ public class DateUtils {
 
                        subtractYears(earliestDate, 175);
                }
-               
+
                return earliestDate;
                */
        }
-       
+
        /**
         * Calculates the latest date that may be considered to be "after"
-        * a given date range. 
-        * 
+        * a given date range.
+        *
         * @param startDate The first date in the range
         * @param endDate   The last date in the range
         * @return          The latest date "after" the range
@@ -1140,7 +1141,7 @@ public class DateUtils {
 
                MutableDateTime startDateTime = convertToDateTime(startDate);
                MutableDateTime endDateTime = convertToDateTime(endDate);
-               
+
                return startDateTime.compareTo(endDateTime);
        }
 
@@ -1160,34 +1161,34 @@ public class DateUtils {
                if (startDate == null || endDate == null) {
                        throw new InvalidDateException("date must not be null");
                }
-               
+
                Integer startYear = startDate.getYear();
                Integer endYear = endDate.getYear();
-               
+
                if (startYear == null || endYear == null) {
                        throw new IllegalArgumentException("year must not be null");
                }
-               
+
                Era startEra = startDate.getEra();
                Era endEra = endDate.getEra();
-               
+
                if (startEra == null || endEra == null) {
                        throw new IllegalArgumentException("era must not be null");
                }
-               
+
                MutableDateTime startDateTime = convertToDateTime(startDate);
                MutableDateTime endDateTime = convertToDateTime(endDate);
-               
+
                int years = Years.yearsBetween(startDateTime, endDateTime).getYears();
-               
+
                return years;
        }
-       
+
        /**
         * Calculates the interval, in years, that should be padded around a date so
         * that any date within that interval may be considered to be "circa" the
-        * given date. 
-        * 
+        * given date.
+        *
         * @param  year The year of the date
         * @param  era  The era of the date. If null, Date.DEFAULT_ERA is assumed.
         * @return      The number of "circa" years before and after the date
@@ -1196,14 +1197,14 @@ public class DateUtils {
                /*
                 * This algorithm is inherited from the fuzzydate parser
                 * in XDB. Its comment states:
-                * 
+                *
                 *   We define circa year/century specifications offsets
                 *   as +/- 5% of the difference between that year/century
                 *   and the present (2100), so that the farther we go back
                 *   in time, the wider the range of meaning of "circa."
-                *    
+                *
                 */
-               
+
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
@@ -1223,117 +1224,133 @@ public class DateUtils {
                        }
                        return interval;
                }
-               
+
                MutableDateTime dateTime = new MutableDateTime(chronology);
                dateTime.era().set((era == Era.BCE) ? DateTimeConstants.BC : DateTimeConstants.AD);
                dateTime.yearOfEra().set(year);
                dateTime.monthOfYear().set(1);
                dateTime.dayOfMonth().set(1);
                dateTime.setTime(0, 0, 0, 0);
-               
+
                int years = Years.yearsBetween(dateTime, circaBaseDateTime).getYears();
 
 
                // return interval;
                return ((int) Math.round(years * 0.05));
        }
-       
+
        /**
         * Adds a number of days to a date.
-        * 
-        * @param date The date 
+        *
+        * @param date The date
         * @param days The number of days to add to the date
         */
        public static void addDays(Date date, int days) {
                MutableDateTime dateTime = convertToDateTime(date);
-               
+
                dateTime.add(Days.days(days));
-               
+
                setFromDateTime(date, dateTime);
        }
-       
+
        /**
         * Adds a number of years to a date's year.
-        * 
-        * @param date  The date        
+        *
+        * @param date  The date
         * @param years The number of years to add to the date
         */
        public static void addYears(Date date, int years) {
                MutableDateTime dateTime = convertToDateTime(date);
 
                dateTime.add(Years.years(years));
-               
+
                setFromDateTime(date, dateTime);
        }
-       
+
        /**
         * Subtracts a number of years from a date's year.
-        * 
-        * @param date  The date        
+        *
+        * @param date  The date
         * @param years The number of years to subtract from the date
         */
        public static void subtractYears(Date date, int years) {
                addYears(date, -years);
        }
-       
+
        public static String getEarliestTimestamp(Date date) {
+               return formatEarliest(date, timestampFormatter);
+       }
+
+       public static String getEarliestScalarValue(Date date) {
+               return formatEarliest(date, scalarValueFormatter);
+       }
+
+       public static String formatEarliest(Date date, DateTimeFormatter formatter) {
                Era era = date.getEra();
-               
+
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                MutableDateTime dateTime = null;
-               
+
                try {
                        dateTime = convertToDateTime(date);
                }
                catch(IllegalFieldValueException e) {
                        throw new InvalidDateException(e.getMessage());
                }
-               
-               String scalarDate = scalarDateFormatter.print(dateTime);
-               
+
+               String scalarDate = formatter.print(dateTime);
+
                return scalarDate;
        }
-       
+
        public static String getLatestTimestamp(Date date) {
+               return formatLatest(date, timestampFormatter);
+       }
+
+       public static String getLatestScalarValue(Date date) {
+               return formatLatest(date, scalarValueFormatter);
+       }
+
+       public static String formatLatest(Date date, DateTimeFormatter formatter) {
                Era era = date.getEra();
-               
+
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
-               
+
                MutableDateTime dateTime = null;
-               
+
                try {
                        dateTime = convertToDateTime(date);
                }
                catch(IllegalFieldValueException e) {
                        throw new InvalidDateException(e.getMessage());
                }
-               
+
                dateTime.setTime(23, 59, 59, 999);
-               
-               String scalarDate = scalarDateFormatter.print(dateTime);
-               
+
+               String scalarDate = formatter.print(dateTime);
+
                return scalarDate;
        }
-       
+
        public static boolean isValidDate(int year, int month, int day, Era era) {
                boolean isValid = true;
-               
+
                try {
                        convertToDateTime(new Date(year, month,day, era));
                }
                catch(IllegalFieldValueException e) {
                        isValid = false;
                }
-               
+
                return isValid;
        }
 
-       /** 
+       /**
         * Converts Roman numeral to integer. Currently only supports 1-12.
         * @param romanNum The Roman number string that needs to be transformed into decimal
         * @return decimal representation of Roman number
@@ -1346,7 +1363,7 @@ public class DateUtils {
 
                for (int i = length - 1; i >= 0; i--) {
                        int cur = getRomanValue(romanNum.charAt(i));
-                       
+
                        if (i == length - 1) {
                                sum = sum + cur;
                        } else {
@@ -1358,7 +1375,7 @@ public class DateUtils {
                        }
                        pre = cur;
                }
-               
+
                return sum;
        }
 
@@ -1368,16 +1385,16 @@ public class DateUtils {
                else if (c == 'x') return 10;
                return -1;
        }
-       
+
        /**
         * Converts a Date to a joda-time DateTime.
-        * 
+        *
         * @param  date The Date
         * @return      A MutableDateTime representing the same date
         */
        private static MutableDateTime convertToDateTime(Date date) {
                Era era = date.getEra();
-               
+
                if (era == null) {
                        era = Date.DEFAULT_ERA;
                }
@@ -1391,11 +1408,11 @@ public class DateUtils {
 
                return dateTime;
        }
-       
+
        /**
         * Sets the fields in a Date so that it represents the same date
         * as a given DateTime.
-        * 
+        *
         * @param date     The Date to set
         * @param dateTime A MutableDateTime representing the desired date
         */
index efa79651026ab2e03bcd16d9d76c4fd50bd76222..e768c54b5da87e7e5e4b466c0eeb616a10697cb8 100644 (file)
@@ -7,24 +7,23 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 
 import org.apache.commons.lang.StringUtils;
-import org.joda.time.IllegalFieldValueException;
 
 public class ParseDates {
-       
+
        /**
         * Parse a newline-separated list of strings from a file (or standard input),
         * and print the results to standard output.
-        * 
+        *
         * @param args The first argument to the program is the name of the file
         *             containing strings to parse. If not supplied, strings are
         *             read from standard input.
         */
        public static void main(String[] args) {
                BufferedReader in = null;
-               
+
                if (args.length > 0) {
                        String filename = args[0];
-                       
+
                        try {
                                in = new BufferedReader(new FileReader(filename));
                        } catch (FileNotFoundException e) {
@@ -34,15 +33,15 @@ public class ParseDates {
                else {
                        in = new BufferedReader(new InputStreamReader(System.in));
                }
-               
+
                if (in == null) {
                        return;
                }
-               
+
                try {
                        for(String line; (line = in.readLine()) != null; ) {
                                line = StringUtils.trim(line);
-                               
+
                                if (StringUtils.isNotEmpty(line)) {
                                        parse(line);
                                }
@@ -51,7 +50,7 @@ public class ParseDates {
                catch(IOException e) {
                        System.err.println("Error reading file: " + e.getLocalizedMessage());
                }
-               
+
                try {
                        in.close();
                }
@@ -59,32 +58,32 @@ public class ParseDates {
                        System.err.println("Error closing file: " + e.getLocalizedMessage());
                }
        }
-       
+
        private static void parse(String displayDate) {
                System.out.print(displayDate + "\t");
-               
+
                String result = "";
                String scalar = "";
-               
+
                try {
                        StructuredDateInternal structuredDate = StructuredDateInternal.parse(displayDate);
                        Date earliestSingleDate = structuredDate.getEarliestSingleDate();
                        Date latestDate = structuredDate.getLatestDate();
-                       
-                       result = 
+
+                       result =
                                earliestSingleDate.getYear() + "-" +
                                earliestSingleDate.getMonth() + "-" +
                                earliestSingleDate.getDay() + " " +
                                earliestSingleDate.getEra().toDisplayString(); // use toString() to get the data value (refname)
 
                                // These don't get filled in by the parser, so no need to print.
-                       
+
                                // earliestSingleDate.getCertainty();
                                // earliestSingleDate.getQualifierType();
                                // earliestSingleDate.getQualifierValue();
                                // earliestSingleDate.getQualifierUnit();
                                // earliestSingleDate.getScalarValue();
-                       
+
                        if (latestDate != null) {
                                result += " - " +
                                        latestDate.getYear() + "-" +
@@ -92,11 +91,11 @@ public class ParseDates {
                                        latestDate.getDay() + " " +
                                        latestDate.getEra().toDisplayString(); // use toString() to get the data value (refname)
                        }
-                       
+
                        try {
                                structuredDate.computeScalarValues();
-                       
-                               scalar = structuredDate.getEarliestScalarDate() + " - " + structuredDate.getLatestScalarDate();
+
+                               scalar = structuredDate.getEarliestScalarValue() + " - " + structuredDate.getLatestScalarValue();
                        }
                        catch(InvalidDateException e) {
                                scalar = "[invalid date: " + e.getMessage() + "]";
@@ -106,7 +105,7 @@ public class ParseDates {
                        result = "[unable to parse]";
                        scalar = "";
                }
-               
+
                System.out.println(result + "\t" + scalar);
        }
 }
index a100bf677a1850d3e7e139626655b01d379516b3..0cf47a427ab06bddd3aafe283e4591dd1249e53c 100644 (file)
@@ -5,28 +5,23 @@ import org.collectionspace.services.structureddate.antlr.ANTLRStructuredDateEval
 
 
 /**
- * A CollectionSpace structured date. 
+ * A CollectionSpace structured date.
  */
 public class StructuredDateInternal {
-       // The UI layer is interpreting scalarValuesComputed as follows:
-       //  - If true, the UI should compute scalar values
-       //  - If false (or null), the UI should not compute scalar values
-       // Given that interpretation, scalarValuesComputed should default
-       // to true.
-       public static final boolean DEFAULT_SCALAR_VALUES_COMPUTED = true;
-       
+       public static final boolean DEFAULT_SCALAR_VALUES_COMPUTED = false;
+
        private String displayDate;
        private String note;
        private String association;
        private String period;
-       
+
        private Date earliestSingleDate;
        private Date latestDate;
-       
-       private String earliestScalarDate;
-       private String latestScalarDate;
+
+       private String earliestScalarValue;
+       private String latestScalarValue;
        private Boolean scalarValuesComputed;
-               
+
        public StructuredDateInternal() {
                scalarValuesComputed = DEFAULT_SCALAR_VALUES_COMPUTED;
        }
@@ -38,41 +33,41 @@ public class StructuredDateInternal {
                        "\tnote:        " + getNote() + "\n" +
                        "\tassociation: " + getAssociation() + "\n" +
                        "\tperiod:      " + getPeriod() + "\n";
-               
+
                if (getEarliestSingleDate() != null) {
-                       string += 
+                       string +=
                                "\n" +
                                "\tearliestSingleDate: \n" +
                                getEarliestSingleDate().toString() + "\n";
                }
-               
+
                if (getLatestDate() != null) {
-                       string += 
+                       string +=
                                "\n" +
                                "\tlatestDate: \n" +
                                getLatestDate().toString() + "\n";
                }
-               
+
                return string;
        }
-       
+
        @Override
        public boolean equals(Object obj) {
-               if (obj == null) { 
+               if (obj == null) {
                        return false;
                }
-               
+
                if (obj == this) {
                        return true;
                }
-               
+
                if (obj.getClass() != getClass()) {
                        return false;
                }
-               
+
                StructuredDateInternal that = (StructuredDateInternal) obj;
 
-               return 
+               return
                        new EqualsBuilder()
                                .append(this.getDisplayDate(), that.getDisplayDate())
                                .append(this.getAssociation(), that.getAssociation())
@@ -83,37 +78,37 @@ public class StructuredDateInternal {
                                .append(this.areScalarValuesComputed(), that.areScalarValuesComputed())
                                .isEquals();
        }
-       
+
        public void computeScalarValues() {
                Date earliestDate = getEarliestSingleDate();
                Date latestDate = getLatestDate();
-               
+
                if (earliestDate == null && latestDate == null) {
-                       setEarliestScalarDate(null);
-                       setLatestScalarDate(null);
-                       
+                       setEarliestScalarValue(null);
+                       setLatestScalarValue(null);
+
                        return;
                }
-               
+
                if (earliestDate == null) {
                        earliestDate = latestDate.copy();
                }
                else {
                        earliestDate = earliestDate.copy();
                }
-               
+
                if (latestDate == null) {
                        latestDate = earliestDate.copy();
                }
                else {
                        latestDate = latestDate.copy();
                }
-               
+
                if (earliestDate.getYear() == null || latestDate.getYear() == null) {
                        // The dates must at least specify a year.
                        throw new InvalidDateException("year must not be null");
                }
-               
+
                if (earliestDate.getDay() != null && earliestDate.getMonth() == null) {
                        // If a day is specified, the month must be specified.
                        throw new InvalidDateException("month may not be null when day is not null");
@@ -123,43 +118,44 @@ public class StructuredDateInternal {
                        // If a day is specified, the month must be specified.
                        throw new InvalidDateException("month may not be null when day is not null");
                }
-               
+
                if (earliestDate.getEra() == null) {
                        earliestDate.setEra(Date.DEFAULT_ERA);
                }
-               
+
                if (latestDate.getEra() == null) {
                        latestDate.setEra(Date.DEFAULT_ERA);
                }
-               
+
                if (earliestDate.getMonth() == null) {
                        earliestDate.setMonth(1);
                        earliestDate.setDay(1);
                }
-               
+
                if (latestDate.getMonth() == null) {
                        latestDate.setMonth(12);
                        latestDate.setDay(31);
                }
-               
+
                if (earliestDate.getDay() == null) {
                        earliestDate.setDay(1);
                }
-               
+
                if (latestDate.getDay() == null) {
                        latestDate.setDay(DateUtils.getDaysInMonth(latestDate.getMonth(), latestDate.getYear(), latestDate.getEra()));
                }
-               
+
                // Add one day to the latest day, since that's what the UI does.
-               // DateUtils.addDays(latestDate, 1);
-               
-               setEarliestScalarDate(DateUtils.getEarliestTimestamp(earliestDate));
-               setLatestScalarDate(DateUtils.getLatestTimestamp(latestDate));
+               DateUtils.addDays(latestDate, 1);
+
+               setEarliestScalarValue(DateUtils.getEarliestScalarValue(earliestDate));
+               setLatestScalarValue(DateUtils.getLatestScalarValue(latestDate));
+               setScalarValuesComputed(true);
        }
-       
+
        public static StructuredDateInternal parse(String displayDate) throws StructuredDateFormatException {
                StructuredDateEvaluator evaluator = new ANTLRStructuredDateEvaluator();
-               
+
                return evaluator.evaluate(displayDate);
        }
 
@@ -210,29 +206,29 @@ public class StructuredDateInternal {
        public void setLatestDate(Date latestDate) {
                this.latestDate = latestDate;
        }
-               
+
        public boolean isRange() {
                return (getLatestDate() != null);
        }
 
-       public String getEarliestScalarDate() {
-               return earliestScalarDate;
+       public String getEarliestScalarValue() {
+               return earliestScalarValue;
        }
 
-       public void setEarliestScalarDate(String earliestScalarDate) {
-               this.earliestScalarDate = earliestScalarDate;
+       public void setEarliestScalarValue(String earliestScalarValue) {
+               this.earliestScalarValue = earliestScalarValue;
        }
 
        public Boolean areScalarValuesComputed() {
                return scalarValuesComputed;
        }
 
-       public String getLatestScalarDate() {
-               return latestScalarDate;
+       public String getLatestScalarValue() {
+               return latestScalarValue;
        }
 
-       public void setLatestScalarDate(String latestScalarDate) {
-               this.latestScalarDate = latestScalarDate;
+       public void setLatestScalarValue(String latestScalarValue) {
+               this.latestScalarValue = latestScalarValue;
        }
 
        public void setScalarValuesComputed(Boolean scalarValuesComputed) {
index 937cb662efc79b9498ac50a82e8f2590a6059759..036343212777da8e043488aa9b4a0eabe5cfdc22 100644 (file)
@@ -125,7 +125,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                result = new StructuredDateInternal();
                result.setDisplayDate(displayDate);
 
-               // Instantiate a parser from the lowercased display date, so that parsing will be case insensitive 
+               // Instantiate a parser from the lowercased display date, so that parsing will be case insensitive
                ANTLRInputStream inputStream = new ANTLRInputStream(displayDate.toLowerCase());
                StructuredDateLexer lexer = new StructuredDateLexer(inputStream);
                CommonTokenStream tokenStream = new CommonTokenStream(lexer);
@@ -152,6 +152,8 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                        throw new StructuredDateFormatException(getErrorMessage(re), re);
                }
 
+               result.computeScalarValues();
+
                // The parsing was successful. Return the result.
                return result;
        }
@@ -170,7 +172,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                                temp = earliestDate;
                                earliestDate = latestDate;
                                latestDate = temp;
-       
+
                                // Check to see if the dates were reversed AND calculated. If they were
                                // Then this probably means the absolute earliestDate should have month and day as "1"
                                // and the latestDate momth 12, day 31.
@@ -248,13 +250,13 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                int earliestInterval = DateUtils.getCircaIntervalYears(earliestDate.getYear(), earliestDate.getEra());
                int latestInterval = DateUtils.getCircaIntervalYears(latestDate.getYear(), latestDate.getEra());
 
-               // Express the circa interval as a qualifier.   
+               // Express the circa interval as a qualifier.
+
+               // stack.push(earliestDate.withQualifier(QualifierType.MINUS, earliestInterval, QualifierUnit.YEARS));
+               // stack.push(latestDate.withQualifier(QualifierType.PLUS, latestInterval, QualifierUnit.YEARS));
 
-               // stack.push(earliestDate.withQualifier(QualifierType.MINUS, earliestInterval, QualifierUnit.YEARS));  
-               // stack.push(latestDate.withQualifier(QualifierType.PLUS, latestInterval, QualifierUnit.YEARS));       
+               // OR:
 
-               // OR:  
-                
                // Express the circa interval as an offset calculated into the year.
 
                DateUtils.subtractYears(earliestDate, earliestInterval);
@@ -431,7 +433,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                        stack.push(new Date(earliestYear, earlyMonth, DateUtils.getDaysInMonth(earlyMonth, earliestYear, era), era)); // Latest Early Date
                        stack.push(new Date(lateYear, latestMonth, 1, era)); // Earliest Latest Date
                        stack.push(new Date(lateYear, latestMonth, DateUtils.getDaysInMonth(latestMonth, lateYear, era), era)); // Latest Late Date
-                       
+
                }
        }
 
@@ -532,7 +534,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
        @Override
        public void exitDayFirstDate(DayFirstDateContext ctx) {
                if (ctx.exception != null) return ;
-               
+
                Era era = (ctx.era() == null) ? null : (Era) stack.pop();
                Integer year = (Integer) stack.pop();
                Integer month = (Integer) stack.pop();
@@ -690,7 +692,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                stack.push(era);
        }
 
-       @Override 
+       @Override
        public void exitSeasonYear(SeasonYearContext ctx) {
                if (ctx.exception != null) return;
 
@@ -1005,7 +1007,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                        throw new StructuredDateFormatException("unexpected half '" + n + "'");
                }
        }
-       
+
 
        @Override
        public void exitNthQuarterInYearRange(NthQuarterInYearRangeContext ctx) {
@@ -1028,7 +1030,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
 
        @Override
        public void exitNthQuarterYear(NthQuarterYearContext ctx) {
-               
+
                Era era = (ctx.era() == null) ? null : (Era) stack.pop();
 
                stack.push(era);
@@ -1237,7 +1239,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
        @Override
        public void exitRomanDate(RomanDateContext ctx) {
                if (ctx.exception != null) return;
-               
+
                Era era = (ctx.era() == null) ? null : (Era) stack.pop();
                Integer year = (Integer) stack.pop();
                Integer month = (Integer) stack.pop();
@@ -1262,7 +1264,7 @@ public class ANTLRStructuredDateEvaluator extends StructuredDateBaseListener imp
                if (ctx.exception != null) return;
 
                Integer adjustmentDate = (Integer) stack.pop();
-               Integer mainYear = (Integer) stack.pop(); 
+               Integer mainYear = (Integer) stack.pop();
 
                Integer earliestYear = (BP_ZERO_YEAR - mainYear) - adjustmentDate;
                Integer latestYear = (BP_ZERO_YEAR - mainYear) + adjustmentDate;