org.mycore.mods.MCRMODSDateHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.mycore.mods.MCRMODSDateHelper.java

Source

/*
 * $Revision$ $Date$
 *
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * This program is free software; you can use it, redistribute it
 * and / or modify it under the terms of the GNU General Public License
 * (GPL) as published by the Free Software Foundation; either version 2
 * of the License or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, in a file called gpl.txt or license.txt.
 * If not, write to the Free Software Foundation Inc.,
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
 */

package org.mycore.mods;

import static org.mycore.mods.MCRMODSDateFormat.DATE_LOCALE;
import static org.mycore.mods.MCRMODSDateFormat.MODS_TIMEZONE;

import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

import org.jdom2.Element;
import org.mycore.common.MCRException;

/**
 * Helper class to parse and build MODS date elements, see
 * http://www.loc.gov/standards/mods/userguide/generalapp.html#encoding
 * 
 * @author Frank L\u00FCtzenkirchen
 */
public class MCRMODSDateHelper {

    public static Date getDate(Element element) {
        if (element == null) {
            return null;
        }

        String text = element.getTextTrim();
        if ((text == null) || text.isEmpty()) {
            return null;
        }

        String encoding = element.getAttributeValue("encoding", "unknown").toLowerCase(DATE_LOCALE);
        String key = encoding + "-" + text.length();
        MCRMODSDateFormat format = firstNonNull(MCRMODSDateFormat.getFormat(key),
                MCRMODSDateFormat.getFormat(encoding));
        if (format == null) {
            throw reportParseException(encoding, text, null);
        }
        try {
            return format.parseDate(text);
        } catch (ParseException ex) {
            throw reportParseException(encoding, text, ex);
        }
    }

    @SafeVarargs
    private static <T> T firstNonNull(T... o) {
        for (T test : Objects.requireNonNull(o)) {
            if (test != null) {
                return test;
            }
        }
        throw new NullPointerException();
    }

    public static GregorianCalendar getCalendar(Element element) {
        Date date = getDate(element);
        if (date == null) {
            return null;
        }
        GregorianCalendar cal = new GregorianCalendar(MODS_TIMEZONE, DATE_LOCALE);
        cal.setTime(date);
        return cal;
    }

    public static void setDate(Element element, Date date, MCRMODSDateFormat encoding) {
        Objects.requireNonNull(encoding, "encoding is required: " + encoding);
        element.setText(encoding.formatDate(date));
        element.setAttribute("encoding", encoding.asEncodingAttributeValue());
    }

    public static void setDate(Element element, GregorianCalendar cal, MCRMODSDateFormat encoding) {
        setDate(element, encoding.isDateOnly() ? adjustTimeOffset(cal) : cal.getTime(), encoding);
    }

    private static Date adjustTimeOffset(GregorianCalendar cal) {
        GregorianCalendar clone = new GregorianCalendar(MODS_TIMEZONE, DATE_LOCALE);
        clone.set(cal.get(GregorianCalendar.YEAR), cal.get(GregorianCalendar.MONTH),
                cal.get(GregorianCalendar.DAY_OF_MONTH));
        return clone.getTime();
    }

    private static MCRException reportParseException(String encoding, String text, ParseException ex) {
        String msg = "Unable to parse MODS date encoded " + encoding + " " + text;
        return new MCRException(msg, ex);
    }
}