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.core.rosetta; 019 020import dev.enola.common.io.resource.ReadableResource; 021import dev.enola.common.io.resource.ResourceProvider; 022import dev.enola.common.io.resource.WritableResource; 023import dev.enola.common.io.resource.convert.CatchingResourceConverter; 024import dev.enola.common.protobuf.ProtoIO; 025import dev.enola.rdf.io.RdfResourceIntoProtoThingConverter; 026import dev.enola.thing.io.ThingMediaTypes; 027 028public class RdfResourceIntoProtoThingResourceConverter implements CatchingResourceConverter { 029 030 private final ThingMediaTypes thingMediaTypes = new ThingMediaTypes(); 031 private final RdfResourceIntoProtoThingConverter ritc; 032 private final ProtoIO protoIO = new ProtoIO(); 033 034 public RdfResourceIntoProtoThingResourceConverter(ResourceProvider rp) { 035 this.ritc = new RdfResourceIntoProtoThingConverter(rp); 036 } 037 038 @Override 039 public boolean convertIntoThrows(ReadableResource from, WritableResource into) 040 throws Exception { 041 if (!(thingMediaTypes.knownTypesWithAlternatives().containsKey(into.mediaType()))) 042 return false; 043 var intoMediaType = thingMediaTypes.detect(into); 044 if (intoMediaType.isPresent()) { 045 var optThingsList = ritc.convert(from); 046 if (optThingsList.isEmpty()) return false; 047 048 // if (ThingMediaTypes.THING_HTML_UTF_8.equals(intoMediaType.get())) { 049 // TODO Convert Thing to HTML fragment! (Test if mkdocs allows embedding HTML?) 050 051 // } else { 052 var thingsList = optThingsList.get(); 053 var message = ritc.asMessage(thingsList).build(); 054 protoIO.write(message, into); 055 // } 056 return true; 057 } else { 058 return false; 059 } 060 } 061}