-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (core): Graph Commons JSON Converter
- Loading branch information
Showing
11 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
java/dev/enola/thing/gen/graphcommons/GraphCommonsJsonGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2024 The Enola <https://enola.dev> Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.enola.thing.gen.graphcommons; | ||
|
||
import static com.google.gson.FormattingStyle.PRETTY; | ||
import static com.google.gson.Strictness.STRICT; | ||
|
||
import com.google.common.io.CharStreams; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import dev.enola.common.context.TLC; | ||
import dev.enola.common.convert.ConversionException; | ||
import dev.enola.thing.Thing; | ||
import dev.enola.thing.gen.ThingsIntoAppendableConverter; | ||
import dev.enola.thing.repo.StackedThingProvider; | ||
import dev.enola.thing.repo.ThingProvider; | ||
|
||
import java.io.IOException; | ||
|
||
/** Generator of JSON Format used by <a href="https://graphcommons.com/>Graph Commons</a>. */ | ||
public class GraphCommonsJsonGenerator implements ThingsIntoAppendableConverter { | ||
|
||
@Override | ||
public boolean convertInto(Iterable<Thing> from, Appendable out) | ||
throws ConversionException, IOException { | ||
var writer = CharStreams.asWriter(out); | ||
var jsonWriter = new JsonWriter(writer); | ||
jsonWriter.setStrictness(STRICT); | ||
jsonWriter.setFormattingStyle(PRETTY); // TODO FormattingStyle.COMPACT, if !pretty | ||
jsonWriter.setSerializeNulls(false); | ||
jsonWriter.setHtmlSafe(true); // TODO ? | ||
jsonWriter.beginObject(); | ||
try (var ctx = TLC.open()) { | ||
ctx.push(ThingProvider.class, new StackedThingProvider(from)); | ||
jsonWriter.name("nodes").beginArray(); | ||
for (Thing thing : from) printThingNode(thing, jsonWriter); | ||
jsonWriter.endArray().name("edges").beginArray(); | ||
for (Thing thing : from) printThingEdges(thing, jsonWriter); | ||
jsonWriter.endArray().name("nodeTypes").beginArray(); | ||
// TODO printThingNodeTypes() | ||
jsonWriter.endArray().name("edgeTypes").beginArray(); | ||
// TODO printThingEdgeTypes() | ||
jsonWriter.endArray().name("name").value("Enola.dev"); | ||
} | ||
jsonWriter.endObject(); | ||
jsonWriter.flush(); | ||
writer.close(); | ||
return true; | ||
} | ||
|
||
private void printThingNode(Thing thing, JsonWriter jsonWriter) throws IOException { | ||
jsonWriter.beginObject(); | ||
jsonWriter.name("id").value(thing.iri()); | ||
jsonWriter.endObject(); | ||
} | ||
|
||
private void printThingEdges(Thing thing, JsonWriter jsonWriter) {} | ||
} |
38 changes: 38 additions & 0 deletions
38
java/dev/enola/thing/gen/graphcommons/GraphCommonsMediaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2024 The Enola <https://enola.dev> Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.enola.thing.gen.graphcommons; | ||
|
||
import com.google.common.collect.ImmutableMultimap; | ||
import com.google.common.collect.Multimap; | ||
import com.google.common.net.MediaType; | ||
|
||
import dev.enola.common.io.mediatype.MediaTypeProvider; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
public class GraphCommonsMediaType implements MediaTypeProvider { | ||
|
||
public static final MediaType GCJ = | ||
MediaType.create("text", "vnd.enola.graphcommons+json") | ||
.withCharset(StandardCharsets.UTF_8); | ||
|
||
@Override | ||
public Multimap<String, MediaType> extensionsToTypes() { | ||
return ImmutableMultimap.of(".graphcommons.json", GCJ); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
java/dev/enola/thing/gen/graphcommons/GraphCommonsResourceConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2024 The Enola <https://enola.dev> Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.enola.thing.gen.graphcommons; | ||
|
||
import dev.enola.common.io.mediatype.MediaTypes; | ||
import dev.enola.common.io.resource.ReadableResource; | ||
import dev.enola.common.io.resource.WritableResource; | ||
import dev.enola.common.io.resource.convert.CatchingResourceConverter; | ||
import dev.enola.thing.io.Loader; | ||
|
||
public class GraphCommonsResourceConverter implements CatchingResourceConverter { | ||
|
||
private final GraphCommonsJsonGenerator graphcommons = new GraphCommonsJsonGenerator(); | ||
private final Loader loader; | ||
|
||
public GraphCommonsResourceConverter(Loader loader) { | ||
this.loader = loader; | ||
} | ||
|
||
@Override | ||
public boolean convertIntoThrows(ReadableResource from, WritableResource into) | ||
throws Exception { | ||
if (!MediaTypes.normalizedNoParamsEquals(into.mediaType(), GraphCommonsMediaType.GCJ)) | ||
return false; | ||
|
||
var things = loader.loadAtLeastOneThing(from.uri()); | ||
graphcommons.convertIntoOrThrow(things, into); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters