From d545b1193217252a43e0d53da1e21cd8d27f9be7 Mon Sep 17 00:00:00 2001 From: sszuev Date: Wed, 30 Mar 2022 17:36:18 +0300 Subject: [PATCH] ont-converter: move initializing jena-csv to main, some minor changes --- src/main/java/com/github/sszuev/Args.java | 12 +++------ src/main/java/com/github/sszuev/Main.java | 4 ++- .../java/com/github/sszuev/utils/Formats.java | 27 ------------------- .../com/github/sszuev/utils/Managers.java | 18 +------------ 4 files changed, 8 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/github/sszuev/Args.java b/src/main/java/com/github/sszuev/Args.java index 3ca9274..460375d 100644 --- a/src/main/java/com/github/sszuev/Args.java +++ b/src/main/java/com/github/sszuev/Args.java @@ -33,7 +33,8 @@ public class Args { private final OntFormat outFormat, inFormat; private final OntModelConfig.StdMode personality; private final boolean spin, force, refine, verbose, webAccess; - private boolean outDir, inDir; + private final boolean outDir; + private final boolean inDir; private Args(Path input, Path output, @@ -85,7 +86,7 @@ public static Args parse(String... args) throws IOException, IllegalArgumentExce out = out.getParent().toRealPath().resolve(out.getFileName()); } - if (Files.isDirectory(in) && Files.walk(in).filter(f -> Files.isRegularFile(f)).count() > 1) { + if (Files.isDirectory(in) && Files.walk(in).filter(Files::isRegularFile).count() > 1) { // out should be directory if (Files.exists(out)) { if (!Files.isDirectory(out)) { @@ -143,15 +144,10 @@ private static String help(Options opts, boolean usage) { if (usage) { sb.append("Full list of supported formats:").append("\n"); sb.append(" ").append(formatHeader()).append("\n"); - try { - Formats.registerJenaCSV(); OntFormat.formats() .filter(f -> f.isReadSupported() || f.isWriteSupported()) .map(Args::formatLine) .forEach(x -> sb.append(" ").append(x).append("\n")); - } finally { - Formats.unregisterJenaCSV(); - } } return sb.toString(); } @@ -160,7 +156,7 @@ private static String formatLine(OntFormat f) { return StringUtils.rightPad(f.name(), NAME_COL_LENGTH) + StringUtils.rightPad(f.isJena() ? "Apache Jena" : "OWL-API", PROVIDER_COL_LENGTH) + StringUtils.rightPad(f.isReadSupported() + "/" + f.isWriteSupported(), READ_WRITE_COL_LENGTH) + - Formats.aliases(f).stream().collect(Collectors.joining(", ")); + String.join(", ", Formats.aliases(f)); } diff --git a/src/main/java/com/github/sszuev/Main.java b/src/main/java/com/github/sszuev/Main.java index 7fbfabc..089a559 100644 --- a/src/main/java/com/github/sszuev/Main.java +++ b/src/main/java/com/github/sszuev/Main.java @@ -1,6 +1,7 @@ package com.github.sszuev; import com.github.sszuev.ontapi.IRIMap; +import com.github.sszuev.utils.Formats; import com.github.sszuev.utils.IRIs; import com.github.sszuev.utils.Managers; import org.apache.log4j.Level; @@ -33,6 +34,7 @@ public class Main { public static void main(String... inputs) throws Exception { forceDisableExternalLogging(); + Formats.registerJenaCSV(); Args args = null; try { args = Args.parse(inputs); @@ -183,7 +185,7 @@ private static void forceDisableExternalLogging() { java.util.logging.LogManager.getLogManager().reset(); try { // java9: - Class clazz = Class.forName("jdk.internal.module.IllegalAccessLogger"); + Class clazz = Class.forName("jdk.internal.module.IllegalAccessLogger"); Field logger = clazz.getDeclaredField("logger"); Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); diff --git a/src/main/java/com/github/sszuev/utils/Formats.java b/src/main/java/com/github/sszuev/utils/Formats.java index 30ca49f..62335ec 100644 --- a/src/main/java/com/github/sszuev/utils/Formats.java +++ b/src/main/java/com/github/sszuev/utils/Formats.java @@ -3,8 +3,6 @@ import org.apache.jena.lang.csv.ReaderRIOTFactoryCSV; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFParserRegistry; -import org.semanticweb.owlapi.io.OWLOntologyDocumentSource; -import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLDocumentFormat; import org.semanticweb.owlapi.model.OWLOntology; import ru.avicomp.ontapi.OntFormat; @@ -52,33 +50,12 @@ public static List aliases(OntFormat f) { /** * Registers {@link Lang#CSV} in jena system. * This operation enables {@link OntFormat#CSV} for reading operations. - * - * @see #unregisterJenaCSV() */ public static void registerJenaCSV() { RDFParserRegistry.removeRegistration(Lang.CSV); RDFParserRegistry.registerLangTriples(Lang.CSV, new ReaderRIOTFactoryCSV()); } - /** - * unregisters csv format - * - * @see #registerJenaCSV() - */ - public static void unregisterJenaCSV() { - RDFParserRegistry.removeRegistration(Lang.CSV); - } - - /** - * Determines is the specified resource can be treated as csv-file. - * - * @param iri {@link IRI} - * @return true if the resource has extension '.csv' - */ - public static boolean isCSV(IRI iri) { - return IRIs.hasExtension(OntFormat.CSV.getExt(), iri); - } - /** * Retrieves format from ontology * @@ -90,8 +67,4 @@ public static Optional format(OWLOntology o) { if (f == null) return Optional.empty(); return Optional.ofNullable(OntFormat.get(f)); } - - public static Optional format(OWLOntologyDocumentSource source) { - return source.getFormat().map(OntFormat::get); - } } diff --git a/src/main/java/com/github/sszuev/utils/Managers.java b/src/main/java/com/github/sszuev/utils/Managers.java index ac74a37..05f21b8 100644 --- a/src/main/java/com/github/sszuev/utils/Managers.java +++ b/src/main/java/com/github/sszuev/utils/Managers.java @@ -28,11 +28,6 @@ public class Managers { private static final Logger LOGGER = LoggerFactory.getLogger(Managers.class); - static { - // exclude csv by default: - Formats.unregisterJenaCSV(); - } - /** * Creates a new manager with default settings. * @@ -183,17 +178,7 @@ public static OntologyManager copyManager(OntologyManager from) { */ public static OntologyModel loadOntology(OntologyManager manager, OWLOntologyDocumentSource source) throws OWLOntologyCreationException { - Optional format = Formats.format(source); - boolean isCvs = (!format.isPresent() && Formats.isCSV(source.getDocumentIRI())) - || format.filter(s -> Objects.equals(s, OntFormat.CSV)).isPresent(); - try { - if (isCvs) { - Formats.registerJenaCSV(); - } - return manager.loadOntologyFromOntologyDocument(source); - } finally { - Formats.unregisterJenaCSV(); - } + return manager.loadOntologyFromOntologyDocument(source); } /** @@ -210,7 +195,6 @@ public static List createMappings(Path dir, OntFormat format) throws IOE return loadDirectory(dir, format, Managers::createSoftManager, true); } - /** * Loads a directory to {@link IRIMap} object. * Directory could content duplicated ontologies, in that case several mappings would be created