001/* 002 * GeoAPI - Java interfaces for OGC/ISO standards 003 * http://www.geoapi.org 004 * 005 * Copyright (C) 2008-2019 Open Geospatial Consortium, Inc. 006 * All Rights Reserved. http://www.opengeospatial.org/ogc/legal 007 * 008 * Permission to use, copy, and modify this software and its documentation, with 009 * or without modification, for any purpose and without fee or royalty is hereby 010 * granted, provided that you include the following on ALL copies of the software 011 * and documentation or portions thereof, including modifications, that you make: 012 * 013 * 1. The full text of this NOTICE in a location viewable to users of the 014 * redistributed or derivative work. 015 * 2. Notice of any changes or modifications to the OGC files, including the 016 * date changes were made. 017 * 018 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE 019 * NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 020 * TO, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT 021 * THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY 022 * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. 023 * 024 * COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR 025 * CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION. 026 * 027 * The name and trademarks of copyright holders may NOT be used in advertising or 028 * publicity pertaining to the software without specific, written prior permission. 029 * Title to copyright in this software and any associated documentation will at all 030 * times remain with copyright holders. 031 */ 032package org.opengis.test.metadata; 033 034import java.util.Collection; 035import java.lang.reflect.Array; 036import org.opengis.util.InternationalString; 037 038import org.opengis.test.Validator; 039import org.opengis.test.ValidatorContainer; 040import static org.opengis.test.Assert.*; 041 042 043/** 044 * Base class for validators of {@code org.opengis.metadata} package and sub-packages. 045 * 046 * <p>This class is provided for users wanting to override the validation methods. When the default 047 * behavior is sufficient, the {@link org.opengis.test.Validators} static methods provide a more 048 * convenient way to validate various kinds of objects.</p> 049 * 050 * @author Martin Desruisseaux (Geomatys) 051 * @version 3.1 052 * @since 2.2 053 */ 054public abstract class MetadataValidator extends Validator { 055 /** 056 * Creates a new validator instance. 057 * 058 * @param container the set of validators to use for validating other kinds of objects 059 * (see {@linkplain #container field javadoc}). 060 * @param packageName the name of the package containing the classes to be validated. 061 */ 062 protected MetadataValidator(final ValidatorContainer container, final String packageName) { 063 super(container, packageName); 064 } 065 066 /** 067 * Returns all collection elements in an array of the given type. This method ensures that 068 * all elements are non-null and of the expected type. Callers should iterate over the 069 * returned array and validate each elements, if needed. 070 * 071 * @param <T> the type of elements in the collection, or {@code null} if none. 072 * @param elementType the type of elements in the collection. 073 * @param objects the collection to validate (never {@code null}). 074 */ 075 final <T> T[] toArray(final Class<T> elementType, final Collection<? extends T> objects) { 076 assertNotNull("Null collection. Should be an empty one if there is no elements.", objects); 077 validate(objects); 078 @SuppressWarnings("unchecked") 079 final T[] array = (T[]) Array.newInstance(elementType, objects.size()); 080 int count = 0; 081 for (final T element : objects) { 082 assertNotNull("Collection should not contain null element.", element); 083 assertInstanceOf("Wrong element type in the collection.", elementType, element); 084 array[count++] = element; 085 } 086 assertEquals("Unexpected end of iteration", array.length, count); 087 return array; 088 } 089 090 /** 091 * Validates the given mandatory string. 092 * 093 * @param object the object to validate. 094 */ 095 final void validateMandatory(final InternationalString object) { 096 mandatory("Missing mandatory metadata attribute.", object); 097 container.validate(object); 098 } 099 100 /** 101 * Validates the given optional string. 102 * 103 * @param object the object to validate, or {@code null}. 104 */ 105 final void validateOptional(final InternationalString object) { 106 container.validate(object); 107 } 108 109 /** 110 * Returns {@code true} if the given text is {@code null}, white or 111 * {@linkplain String#isEmpty() empty}. 112 * 113 * @param text the text to test, or {@code null}. 114 * @return {@code true} if the given text is null, white or empty. 115 * 116 * @since 3.1 117 */ 118 static boolean isNullOrEmpty(final CharSequence text) { 119 return (text == null) || text.toString().trim().isEmpty(); 120 } 121 122 /** 123 * Returns {@code true} if the given collection is {@code null} or 124 * {@linkplain Collection#isEmpty() empty}. 125 * 126 * @param collection the text to test, or {@code null}. 127 * @return {@code true} if the given collection is null or empty. 128 * 129 * @since 3.1 130 */ 131 static boolean isNullOrEmpty(final Collection<?> collection) { 132 return (collection == null) || collection.isEmpty(); 133 } 134}