Example usage for org.apache.commons.validator DateValidator isValid

List of usage examples for org.apache.commons.validator DateValidator isValid

Introduction

In this page you can find the example usage for org.apache.commons.validator DateValidator isValid.

Prototype

public boolean isValid(String value, String datePattern, boolean strict) 

Source Link

Document

Checks if the field is a valid date.

Usage

From source file:org.ala.repository.Validator.java

/**
 * Validate a DC file (parsed into list of String[])
 *
 * @param lines //  w  ww.jav a  2 s  . c  o  m
 * @throws MalformedURLException
 * @throws IllegalArgumentException
 * @throws NoSuchFieldError
 * @throws Exception
 */
protected void validateDcFile(List<String[]> lines)
        throws MalformedURLException, IllegalArgumentException, NoSuchFieldError, Exception {
    // initialise requiredDcFields
    ArrayList<String> requiredDcFields = new ArrayList<String>();
    requiredDcFields.add(Field.IDENTIFIER.name); // alt value: Predicates.DC_IDENTIFIER.getLocalPart()
    requiredDcFields.add(Field.FORMAT.name);
    requiredDcFields.add(Field.MODIFIED.name);
    //requiredDcFields.add(Field.URI.name);

    for (String[] data : lines) {
        logger.debug("DC entries (" + data.length + ") = " + StringUtils.join(data, "|"));
        // Check for expected number of tab fields
        Assert.isTrue(data.length == FileType.DC.getFieldCount(), "Entry not expected size of "
                + FileType.DC.getFieldCount() + ", got " + data.length + " - " + StringUtils.join(data, "|"));

        if (data[0].endsWith(Field.FORMAT.name)) {
            // Check "format" field
            requiredDcFields.remove(Field.FORMAT.name);
            Assert.isTrue(MimeType.getAllMimeTypes().contains(data[1]),
                    Field.FORMAT.name + " does not contain an accepted value: " + data[1] + " - "
                            + StringUtils.join(MimeType.getAllMimeTypes(), "|"));
        } else if (data[0].endsWith(Field.IDENTIFIER.name)) {
            // Check "identifier" field
            requiredDcFields.remove(Field.IDENTIFIER.name);
            Assert.isTrue(data[1].length() > 0, Field.IDENTIFIER.name + " is empty");
        } else if (data[0].endsWith(Field.MODIFIED.name)) {
            // Check "modified" date field
            requiredDcFields.remove(Field.MODIFIED.name);
            Assert.isTrue(data[1].length() > 0, Field.MODIFIED.name + " date is empty");
            DateValidator validator = DateValidator.getInstance();
            if (!validator.isValid(data[1], "yyyy-MM-dd", true)) {
                throw new IllegalArgumentException(
                        Field.MODIFIED.name + " date is not a valid date: " + data[1]);
            }
        } else if (data[0].endsWith(Field.URI.name)) {
            // Check "URI" field
            requiredDcFields.remove(Field.URI.name);
            new URL(data[1]); // throws MalformedURLException if not valid URL
        }
    }

    if (!requiredDcFields.isEmpty()) {
        throw new NoSuchFieldError("Required fields not found: " + StringUtils.join(requiredDcFields, ", "));
    }
}