-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (core): Introduce XmlThingConverter
- Loading branch information
Showing
29 changed files
with
538 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.common.xml; | ||
|
||
import com.google.common.net.MediaType; | ||
|
||
import dev.enola.common.convert.CatchingConverterInto; | ||
import dev.enola.common.io.mediatype.MediaTypes; | ||
import dev.enola.common.io.resource.ReadableResource; | ||
|
||
import org.xml.sax.helpers.DefaultHandler; | ||
|
||
import javax.xml.parsers.SAXParser; | ||
import javax.xml.parsers.SAXParserFactory; | ||
|
||
public class XmlResourceParser implements CatchingConverterInto<ReadableResource, DefaultHandler> { | ||
|
||
@Override | ||
public boolean convertIntoThrows(ReadableResource from, DefaultHandler into) throws Exception { | ||
if (!MediaTypes.normalizedNoParamsEquals(from.mediaType(), MediaType.XML_UTF_8)) | ||
return false; | ||
|
||
if (from.byteSource().isEmpty()) return true; | ||
|
||
SAXParserFactory factory = SAXParserFactory.newInstance(); | ||
factory.setNamespaceAware(true); | ||
factory.setXIncludeAware(false); | ||
factory.setValidating(false); | ||
SAXParser saxParser = factory.newSAXParser(); | ||
|
||
try (var is = from.byteSource().openStream()) { | ||
saxParser.parse(is, 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Copyright 2023 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. | ||
|
||
load("@rules_java//java:defs.bzl", "java_library") | ||
load("//tools/bazel:junit.bzl", "junit_tests") | ||
|
||
java_library( | ||
name = "xml", | ||
srcs = glob( | ||
["*.java"], | ||
exclude = [ | ||
"*Test.java", | ||
"Test*.java", | ||
], | ||
), | ||
plugins = ["//tools/bazel/java_plugin:autoservice"], | ||
resource_strip_prefix = "java/", | ||
resources = glob(["**/*.html"]), | ||
visibility = ["//:__subpackages__"], | ||
deps = [ | ||
"//java/dev/enola/common/convert", | ||
"//java/dev/enola/common/io", | ||
"//java/dev/enola/common/xml", | ||
"//java/dev/enola/thing:thing_java", | ||
"@maven//:com_google_auto_service_auto_service_annotations", | ||
"@maven//:com_google_errorprone_error_prone_annotations", | ||
"@maven//:com_google_errorprone_error_prone_type_annotations", | ||
"@maven//:com_google_guava_guava", | ||
"@maven//:org_jspecify_jspecify", | ||
"@maven//:org_slf4j_slf4j_api", | ||
], | ||
) | ||
|
||
junit_tests( | ||
name = "tests", | ||
srcs = glob([ | ||
"*Test.java", | ||
]), | ||
deps = [ | ||
":xml", | ||
"//java/dev/enola/common/io", | ||
"//java/dev/enola/thing:thing_java", | ||
"//java/dev/enola/thing/testlib", | ||
"//test", | ||
], | ||
) |
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,135 @@ | ||
/* | ||
* 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.format.xml; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import dev.enola.common.io.iri.namespace.NamespaceRepository; | ||
import dev.enola.common.io.iri.namespace.NamespaceRepositoryBuilder; | ||
import dev.enola.thing.Thing; | ||
import dev.enola.thing.impl.IImmutablePredicatesObjects; | ||
import dev.enola.thing.impl.ImmutablePredicatesObjects; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
import org.xml.sax.Attributes; | ||
import org.xml.sax.ContentHandler; | ||
import org.xml.sax.helpers.DefaultHandler; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
|
||
/** | ||
* XML SAX {@link ContentHandler} which transforms XML into a {@link dev.enola.thing.Thing}. | ||
* | ||
* <p>This is NOT thread-safe! | ||
*/ | ||
public class XMLToThingHandler extends DefaultHandler { | ||
|
||
// TODO Consider implementing this via & through the existing JSON support instead? | ||
|
||
private final String iri; | ||
private final NamespaceRepositoryBuilder nrb = new NamespaceRepositoryBuilder(); | ||
|
||
private @Nullable String previousElementQName = null; | ||
// TODO rm? private final | ||
// List<List<IImmutablePredicatesObjects.Builder<IImmutablePredicatesObjects>>> | ||
// builders = new ArrayList<>(); | ||
private final Deque<IImmutablePredicatesObjects.Builder<IImmutablePredicatesObjects>> | ||
thingBuilders = new ArrayDeque<>(); | ||
private ImmutableList.@Nullable Builder<IImmutablePredicatesObjects> currentListBuilder; | ||
|
||
@SuppressWarnings("unchecked") | ||
public XMLToThingHandler(String iri, Thing.Builder<?> thingBuilder) { | ||
this(iri, (IImmutablePredicatesObjects.Builder<IImmutablePredicatesObjects>) thingBuilder); | ||
} | ||
|
||
public XMLToThingHandler( | ||
String iri, | ||
IImmutablePredicatesObjects.Builder<IImmutablePredicatesObjects> thingBuilder) { | ||
thingBuilders.push(thingBuilder); | ||
this.iri = iri; | ||
} | ||
|
||
public NamespaceRepository getNamespaces() { | ||
return nrb.build(); | ||
} | ||
|
||
@Override | ||
public void startPrefixMapping(String prefix, String uri) { | ||
nrb.store(prefix, uri); | ||
} | ||
|
||
@Override | ||
public void endPrefixMapping(String prefix) { | ||
// Ignore. | ||
} | ||
|
||
private String iri(String uri, String localName, String qName) { | ||
if (Strings.isNullOrEmpty(uri)) uri = iri; | ||
if (Strings.isNullOrEmpty(localName)) throw new IllegalStateException(uri + " " + qName); | ||
return uri + "/" + localName; | ||
} | ||
|
||
@Override | ||
public void startElement(String uri, String localName, String qName, Attributes attributes) { | ||
// TODO Remove this System.out.println again later! | ||
System.out.println( | ||
"startElement: uri=" + uri + ", localName=" + localName + ", qName=" + qName); | ||
if (Strings.isNullOrEmpty(qName)) throw new IllegalStateException(localName); | ||
if (!qName.equals(previousElementQName)) { | ||
// Nested element | ||
currentListBuilder = ImmutableList.builder(); | ||
} else { | ||
// Same level element | ||
} | ||
previousElementQName = qName; | ||
|
||
var nested = ImmutablePredicatesObjects.builder(); | ||
thingBuilders.add(nested); | ||
|
||
// TODO Attributes, with #! | ||
} | ||
|
||
@Override | ||
public void endElement(String uri, String localName, String qName) { | ||
// TODO Remove this System.out.println again later! | ||
System.out.println( | ||
"endElement: uri=" + uri + ", localName=" + localName + ", qName=" + qName); | ||
if (Strings.isNullOrEmpty(qName)) throw new IllegalStateException(localName); | ||
if (!qName.equals(previousElementQName)) { | ||
// Nested element | ||
if (currentListBuilder == null) throw new IllegalStateException(); | ||
currentListBuilder.add(thingBuilders.removeLast().build()); | ||
var list = currentListBuilder.build(); | ||
// TODO rm? builders.peek().set(iri(uri, localName, qName), list); | ||
var nested = thingBuilders.removeLast().set(iri(uri, localName, qName), list); | ||
thingBuilders.peek().set(iri(uri, localName, qName), nested.build()); | ||
currentListBuilder = null; | ||
|
||
} else { | ||
// Same level element | ||
// NOOP. | ||
} | ||
} | ||
|
||
@Override | ||
public void characters(char[] ch, int start, int length) { | ||
// TODO set https://enola.dev/content-as-text ? | ||
} | ||
} |
Oops, something went wrong.