001/* 002 * SPDX-License-Identifier: Apache-2.0 003 * 004 * Copyright 2024-2026 The Enola <https://enola.dev> Authors 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 * https://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 dev.enola.thing.validation; 019 020import dev.enola.thing.Link; 021import dev.enola.thing.PredicatesObjects; 022import dev.enola.thing.repo.ThingRepository; 023 024import java.net.URI; 025 026public class LinksValidator implements Validator<PredicatesObjects> { 027 028 private final ThingRepository repo; 029 030 public LinksValidator(ThingRepository repo) { 031 this.repo = repo; 032 } 033 034 @Override 035 public void validate(PredicatesObjects thing, Collector collector) { 036 thing.datatypes() 037 .forEach((predicateIRI, datatypeIRI) -> c(predicateIRI, datatypeIRI, collector)); 038 thing.properties().forEach((predicateIRI, object) -> c(predicateIRI, object, collector)); 039 } 040 041 private void c(String predicateIRI, String linkedIRI, Collector collector) { 042 if (linkedIRI.startsWith("file:")) 043 // TODO Remove this workaround again eventually 044 // file: shouldn't be Links in the first place (but :URL) 045 return; 046 047 if (repo.get(linkedIRI) == null) { 048 collector.add(predicateIRI, "Unknown thing: " + linkedIRI); 049 } 050 } 051 052 private void c(String predicateIRI, Object object, Collector collector) { 053 if (object instanceof Link link) { 054 c(predicateIRI, link.iri(), collector); 055 } else if (object instanceof URI uri) { 056 c(predicateIRI, uri.toString(), collector); 057 } else if (object instanceof PredicatesObjects predicatesObjects) { 058 validate(predicatesObjects, collector); 059 } else if (object instanceof Iterable<?> iterable) { 060 for (var element : iterable) { 061 if (element instanceof PredicatesObjects thing) { 062 validate(thing, collector); 063 } 064 } 065 } // else skip it 066 } 067}