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.cli;
019
020import dev.enola.common.context.Context;
021import dev.enola.format.tika.TikaThingConverter;
022import dev.enola.format.xml.XmlThingConverter;
023import dev.enola.model.enola.files.FileThingConverter;
024import dev.enola.model.enola.mediatype.TikaMediaTypesThingConverter;
025import dev.enola.rdf.io.RdfResourceIntoThingConverter;
026import dev.enola.thing.io.Loader;
027import dev.enola.thing.io.UriIntoThingConverter;
028import dev.enola.thing.io.UriIntoThingConverters;
029
030import picocli.CommandLine;
031
032import java.util.ArrayList;
033
034public abstract class CommandWithResourceProviderAndLoader extends CommandWithResourceProvider {
035
036    @CommandLine.Option(
037            names = {"--file-loader"},
038            negatable = true,
039            required = true,
040            defaultValue = "true",
041            fallbackValue = "true",
042            showDefaultValue = CommandLine.Help.Visibility.ALWAYS,
043            description = "Whether file:/ resources create File (or Directory) Things")
044    boolean fileLoader;
045
046    @CommandLine.Option(
047            names = {"--tika-loader"},
048            negatable = true,
049            required = true,
050            defaultValue = "true",
051            fallbackValue = "true",
052            showDefaultValue = CommandLine.Help.Visibility.ALWAYS,
053            description = "Whether resources are loaded with Tika parsers to create Things")
054    boolean tikaLoader;
055
056    protected Loader loader() {
057        // TODO Move this (and other) initialization out of CLI, to a dev.enola.Enola...
058        var uriIntoThingConverters = new ArrayList<UriIntoThingConverter>(7);
059        uriIntoThingConverters.add(new RdfResourceIntoThingConverter<>());
060        uriIntoThingConverters.add(new XmlThingConverter(rp));
061        if (fileLoader) uriIntoThingConverters.add(new FileThingConverter());
062        if (tikaLoader) uriIntoThingConverters.add(new TikaThingConverter(rp));
063        uriIntoThingConverters.add(new TikaMediaTypesThingConverter());
064
065        var ritc = new UriIntoThingConverters(uriIntoThingConverters);
066        return new Loader(ritc);
067    }
068
069    @Override
070    protected void setup(Context ctx) {
071        super.setup(ctx);
072
073        // TODO Rethink this in a better way...
074        // This was originally motivated by keeping e.g. the mediaType/graph.gv.svg clean;
075        // because it looks overwhelming and ugly with the links to mediaTypes.ttl, from everything.
076        // Perhaps later when we can properly filter what to render (probably with sparql:...), then
077        // there will be a better way for this, and no need for this slight hack?
078        //
079        // Only set enola:origin if --file-loader was enabled
080        ctx.push(UriIntoThingConverters.Flags.ORIGIN, fileLoader);
081    }
082}