001/*
002 *    GeoAPI - Java interfaces for OGC/ISO standards
003 *    Copyright © 2012-2023 Open Geospatial Consortium, Inc.
004 *    http://www.geoapi.org
005 *
006 *    Licensed under the Apache License, Version 2.0 (the "License");
007 *    you may not use this file except in compliance with the License.
008 *    You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *    Unless required by applicable law or agreed to in writing, software
013 *    distributed under the License is distributed on an "AS IS" BASIS,
014 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 *    See the License for the specific language governing permissions and
016 *    limitations under the License.
017 */
018package org.opengis.test.metadata;
019
020import java.util.Date;
021import java.util.Collection;
022import org.opengis.metadata.*;
023import org.opengis.metadata.citation.*;
024import org.opengis.metadata.identification.*;
025import org.opengis.test.ValidatorContainer;
026
027
028/**
029 * Validates {@link Metadata} and related objects from the {@code org.opengis.metadata} package.
030 * This validator is named {@code MetadataBaseValidator} for consistency with the {@code "mdb"}
031 * namespace in XML schema. {@link Metadata} is usually the root of the metadata tree.
032 *
033 * <p>This class is provided for users wanting to override the validation methods. When the default
034 * behavior is sufficient, the {@link org.opengis.test.Validators} static methods provide a more
035 * convenient way to validate various kinds of objects.</p>
036 *
037 * @author  Martin Desruisseaux (Geomatys)
038 * @version 3.1
039 * @since   3.1
040 */
041public class MetadataBaseValidator extends MetadataValidator {
042    /**
043     * Creates a new validator instance.
044     *
045     * @param container  the set of validators to use for validating other kinds of objects
046     *                   (see {@linkplain #container field javadoc}).
047     */
048    public MetadataBaseValidator(final ValidatorContainer container) {
049        super(container, "org.opengis.metadata");
050    }
051
052    /**
053     * Validates the given metadata.
054     *
055     * @param  object  the object to validate, or {@code null}.
056     */
057    public void validate(final Metadata object) {
058        if (object == null) {
059            return;
060        }
061        validate(object.getMetadataIdentifier());
062        container.validate(object.getParentMetadata());
063        for (final MetadataScope scope : toArray(MetadataScope.class, object.getMetadataScopes())) {
064            mandatory(scope.getResourceScope(), "Metadata: shall have a scope code.");
065        }
066        for (final Responsibility e : toArray(Responsibility.class, object.getContacts())) {
067            container.validate(e);
068        }
069
070        final CitationDate[] dates = toArray(CitationDate.class, object.getDateInfo());
071        container.validate(dates);
072        Date creationDate = null;
073        for (final CitationDate date : dates) {
074            if (DateType.CREATION.equals(date.getDateType())) {
075                creationDate = date.getDate();
076                if (creationDate != null) break;
077            }
078        }
079        mandatory(creationDate, "Metadata: shall have a creation date.");
080
081        for (final Citation e : toArray(Citation.class, object.getMetadataStandards())) {
082            container.validate(e);
083        }
084        for (final Citation e : toArray(Citation.class, object.getMetadataProfiles())) {
085            container.validate(e);
086        }
087        for (final Citation e : toArray(Citation.class, object.getAlternativeMetadataReferences())) {
088            container.validate(e);
089        }
090        for (final OnlineResource e : toArray(OnlineResource.class, object.getMetadataLinkages())) {
091            container.validate(e);
092        }
093        validate(object.getSpatialRepresentationInfo());
094        validate(object.getReferenceSystemInfo());
095        validate(object.getMetadataExtensionInfo());
096
097        final Collection<? extends Identification> identifications = object.getIdentificationInfo();
098        mandatory(identifications != null && identifications.isEmpty() ? null : identifications,
099                  "Metadata: shall have an identification information.");
100        validate(identifications);
101
102        validate(object.getContentInfo());
103        validate(object.getDataQualityInfo());
104        validate(object.getPortrayalCatalogueInfo());
105        validate(object.getMetadataConstraints());
106        validate(object.getApplicationSchemaInfo());
107        validate(object.getAcquisitionInformation());
108        validate(object.getResourceLineages());
109    }
110
111    /**
112     * Validates the given identifier.
113     *
114     * @param  object  the identifier to validate, or {@code null}.
115     */
116    public void validate(final Identifier object) {
117        if (object == null) {
118            return;
119        }
120        mandatory(object.getCode(), "Identifier: shall have a code.");
121        final Citation citation = object.getAuthority();
122        if (citation != this) {                 // Avoid never ending loop.
123            container.validate(citation);
124        }
125    }
126}