View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.mods;
20  
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.HashMap;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.TimeZone;
28  
29  import org.mycore.datamodel.common.MCRISO8601Date;
30  
31  /**
32   * Different supported MODS date formats.
33   * @author Thomas Scheffler
34   * @since 2015.03
35   *
36   */
37  public enum MCRMODSDateFormat {
38      iso8601("iso8601", null),
39      w3cdtf_10("w3cdtf-10", "yyyy-MM-dd"),
40      w3cdtf_19("w3cdtf-19",
41          "yyyy-MM-dd'T'HH:mm:ss"),
42      marc_4("marc-4", "yyyy"),
43      iso8601_4("iso8601-4", "yyyy"),
44      iso8601_7("iso8601-7",
45          "yyyy-MM"),
46      iso8601_8("iso8601-8", "yyyyMMdd"),
47      iso8601_15("iso8601-15", "yyyyMMdd'T'HHmmss"),
48  
49      // Try to guess encoding from date length 
50      unknown_4("unknown-4", "yyyy"),
51      unknown_8("unknown-8", "yyyyMMdd"),
52      unknown_10("unknown-10",
53          "yyyy-MM-dd"),
54      unknown_19("unknown-19", "yyyy-MM-dd'T'HH:mm:ss"),
55      unknown_15("unknown-15", "yyyyMMdd'T'HHmmss");
56  
57      private static volatile Map<String, MCRMODSDateFormat> encodingToFormatMap;
58  
59      private String encoding, attributeValue;
60  
61      private String dateFormat;
62  
63      boolean dateOnly;
64  
65      static final TimeZone MODS_TIMEZONE = TimeZone.getTimeZone("UTC");
66  
67      static final Locale DATE_LOCALE = Locale.ROOT;
68  
69      MCRMODSDateFormat(String encoding, String dateFormat) {
70          this.encoding = encoding;
71          this.attributeValue = encoding == "iso8601" ? encoding : encoding.split("-")[0];
72          this.dateFormat = dateFormat;
73          this.dateOnly = dateFormat != null && !dateFormat.endsWith("ss"); //see above
74      }
75  
76      public static MCRMODSDateFormat getFormat(String encoding) {
77          if (encodingToFormatMap == null) {
78              initMap();
79          }
80          return encodingToFormatMap.get(encoding);
81      }
82  
83      private static void initMap() {
84          Map<String, MCRMODSDateFormat> encodingToFormatMap = new HashMap<>();
85          for (MCRMODSDateFormat f : values()) {
86              encodingToFormatMap.put(f.encoding, f);
87          }
88          MCRMODSDateFormat.encodingToFormatMap = encodingToFormatMap;
89      }
90  
91      public String getEncoding() {
92          return encoding;
93      }
94  
95      public boolean isDateOnly() {
96          return dateOnly;
97      }
98  
99      public String asEncodingAttributeValue() {
100         return attributeValue;
101     }
102 
103     public Date parseDate(String text) throws ParseException {
104         if (this == iso8601) {
105             MCRISO8601Date isoDate = new MCRISO8601Date(text);
106             return isoDate.getDate();
107         }
108         return getDateFormat().parse(text);
109     }
110 
111     public String formatDate(Date date) {
112         if (this == iso8601) {
113             MCRISO8601Date isoDate = new MCRISO8601Date();
114             isoDate.setDate(date);
115             return isoDate.getISOString();
116         }
117         return getDateFormat().format(date);
118     }
119 
120     public SimpleDateFormat getDateFormat() {
121         SimpleDateFormat dateFormat = new SimpleDateFormat(this.dateFormat, MCRMODSDateFormat.DATE_LOCALE);
122         dateFormat.setTimeZone(MCRMODSDateFormat.MODS_TIMEZONE);
123         return dateFormat;
124     }
125 }