001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2025 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.rdf.io;
019
020import dev.enola.common.convert.ConversionException;
021import dev.enola.common.io.resource.ReadableResource;
022import dev.enola.thing.proto.Thing;
023
024import java.util.List;
025import java.util.Optional;
026
027// TODO Move this interface to another package, as it's not actually RDF specific
028public class FilteringResourceIntoProtoThingConverter implements ResourceIntoProtoThingConverter {
029
030    // TODO This is an ugly temporary hack, to unblock https://github.com/enola-dev/enola/pull/1781.
031    // The better solution might be to use a proper cache? Or (or also) make RdfReaderConverterInto
032    // ignore HTML instead of RDF? It should already do that, but it clearly didn't work (well),
033    // yet. Without this, the "CI=1 ./test.bash" fails.
034
035    private final ResourceIntoProtoThingConverter delegate;
036    private final List<String> uriPrefixesToSkip;
037
038    public FilteringResourceIntoProtoThingConverter(ResourceIntoProtoThingConverter delegate) {
039        this.delegate = delegate;
040        this.uriPrefixesToSkip =
041                List.of(
042                        "https://example.org",
043                        "http://example.org",
044                        "http://www.w3.org",
045                        "https://schema.org",
046                        "https://enola.dev",
047                        "https://docs.enola.dev");
048    }
049
050    @Override
051    public Optional<List<Thing.Builder>> convert(ReadableResource input)
052            throws ConversionException {
053        var url = input.uri().toString();
054        if (uriPrefixesToSkip.stream().noneMatch(url::startsWith)) return delegate.convert(input);
055        else return Optional.empty();
056    }
057}