Compare commits

..

No commits in common. "4a2758b194fff23b9fdd7b1d55f729313ba61e1b" and "5b15e5edacfed24d15f7ff8effdb91c29e4e7f10" have entirely different histories.

405 changed files with 4246 additions and 1815 deletions

8
.deepsource.toml Normal file
View file

@ -0,0 +1,8 @@
version = 1
[[analyzers]]
name = "java"
enabled = true
[analyzers.meta]
runtime_version = "11"

14
README.adoc Normal file
View file

@ -0,0 +1,14 @@
# xbib Content
image:https://api.travis-ci.org/xbib/content.svg[title="Build status", link="https://travis-ci.org/xbib/content/"]
image:https://img.shields.io/sonar/http/nemo.sonarqube.com/org.xbib%3Acontent/coverage.svg?style=flat-square[title="Coverage", link="https://sonarqube.com/dashboard/index?id=org.xbib%3Acontent"]
image:https://maven-badges.herokuapp.com/maven-central/org.xbib/content/badge.svg[title="Maven Central", link="http://search.maven.org/#search%7Cga%7C1%7Cxbib%20content"]
image:https://img.shields.io/badge/License-Apache%202.0-blue.svg[title="Apache License 2.0", link="https://opensource.org/licenses/Apache-2.0"]
image:https://img.shields.io/twitter/url/https/twitter.com/xbib.svg?style=social&label=Follow%20%40xbib[title="Twitter", link="https://twitter.com/xbib"]
image:https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif[title="PayPal", link="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GVHFQYZ9WZ8HG"]
This is a Java library for processing structured data ("content") in the most popular formats, such as
JSON, SMILE-JSON, YAML, XML, CSV, and also semantic descriptions in RDF (N-Triples, Turtle, RDF/XML).
xbib Content offers stream-based Java builders, parsers, and a settings API oder the formats, so it is very convenienant
to use. The only dependency is https://github.com/fasterxml/jackson[the Java Jackson library].

View file

@ -1,9 +0,0 @@
# xbib Content
Deperecated, use datastructures instead.
This is a Java library for processing structured data ("content") in the most popular formats, such as
JSON, SMILE-JSON, YAML, XML, CSV, and also semantic descriptions in RDF (N-Triples, Turtle, RDF/XML).
xbib Content offers stream-based Java builders, parsers, and a settings API oder the formats, so it is very convenienant
to use. The only dependency is https://github.com/fasterxml/jackson[the Java Jackson library].

View file

@ -1,24 +1,27 @@
plugins {
id 'maven-publish'
id 'signing'
id "io.github.gradle-nexus.publish-plugin" version "2.0.0-rc-1"
id "org.xbib.gradle.plugin.asciidoctor" version "3.0.0"
id "de.marcphilipp.nexus-publish" version "0.4.0"
id "io.codearte.nexus-staging" version "0.21.1"
id "org.xbib.gradle.plugin.asciidoctor" version "2.5.2.2"
id "org.cyclonedx.bom" version "1.7.2"
id "com.github.spotbugs" version "5.0.3"
id "pmd"
id "checkstyle"
}
wrapper {
gradleVersion = libs.versions.gradle.get()
distributionType = Wrapper.DistributionType.BIN
distributionType = Wrapper.DistributionType.ALL
}
ext {
user = 'joerg'
user = 'xbib'
name = 'content'
description = 'Content processing library for Java'
inceptionYear = '2016'
url = 'https://xbib.org/' + user + '/' + name
scmUrl = 'https://xbib.org/' + user + '/' + name
scmConnection = 'scm:git:git://xbib.org/' + user + '/' + name + '.git'
scmDeveloperConnection = 'scm:git:ssh://forgejo@xbib.org:' + user + '/' + name + '.git'
url = 'https://github.com/' + user + '/' + name
scmUrl = 'https://github.com/' + user + '/' + name
scmConnection = 'scm:git:git://github.com/' + user + '/' + name + '.git'
scmDeveloperConnection = 'scm:git:ssh://git@github.com:' + user + '/' + name + '.git'
issueManagementSystem = 'Github'
issueManagementUrl = ext.scmUrl + '/issues'
licenseName = 'The Apache License, Version 2.0'
@ -26,10 +29,15 @@ ext {
}
subprojects {
apply from: rootProject.file('gradle/repositories/maven.gradle')
apply from: rootProject.file('gradle/ide/idea.gradle')
apply from: rootProject.file('gradle/compile/java.gradle')
apply from: rootProject.file('gradle/test/junit5.gradle')
apply from: rootProject.file('gradle/publish/maven.gradle')
apply from: rootProject.file('gradle/repositories/maven.gradle')
apply from: rootProject.file('gradle/quality/spotbugs.gradle')
apply from: rootProject.file('gradle/quality/checkstyle.gradle')
apply from: rootProject.file('gradle/quality/pmd.gradle')
apply from: rootProject.file('gradle/publishing/publication.gradle')
}
apply from: rootProject.file('gradle/publish/sonatype.gradle')
apply from: rootProject.file('gradle/publish/forgejo.gradle')
apply from: rootProject.file('gradle/publishing/sonatype.gradle')
apply from: rootProject.file('gradle/quality/cyclonedx.gradle')

5
config/build.gradle Normal file
View file

@ -0,0 +1,5 @@
dependencies {
api project(':settings-datastructures')
testImplementation project(':settings-datastructures-json')
testImplementation project(':settings-datastructures-yaml')
}

View file

@ -0,0 +1,13 @@
import org.xbib.config.ConfigLogger;
import org.xbib.config.NullConfigLogger;
import org.xbib.config.SystemConfigLogger;
import org.xbib.settings.SettingsLoader;
module org.xbib.config {
exports org.xbib.config;
uses ConfigLogger;
uses SettingsLoader;
provides ConfigLogger with NullConfigLogger, SystemConfigLogger;
requires org.xbib.settings.api;
requires transitive org.xbib.settings.datastructures;
}

View file

@ -0,0 +1,17 @@
package org.xbib.config;
@SuppressWarnings("serial")
public class ConfigException extends RuntimeException {
public ConfigException(Exception e) {
super(e);
}
public ConfigException(String message) {
super(message);
}
public ConfigException(String message, Throwable cause) {
super(message, cause);
}
}

View file

@ -0,0 +1,168 @@
package org.xbib.config;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConfigFinder {
private final FileSystem fileSystem;
private final EnumSet<FileVisitOption> opts;
private final List<Path> result;
private Comparator<Path> comparator;
public ConfigFinder() {
this(FileSystems.getDefault(), EnumSet.of(FileVisitOption.FOLLOW_LINKS));
}
public ConfigFinder(FileSystem fileSystem, EnumSet<FileVisitOption> opts) {
this.fileSystem = fileSystem;
this.opts = opts;
this.result = new ArrayList<>();
}
public ConfigFinder find(String path, String pathPattern) throws IOException {
return find(null, null, fileSystem.getPath(path), pathPattern);
}
public ConfigFinder find(String base, String basePattern, String path, String pathPattern) throws IOException {
return find(base == null || base.isEmpty() ? null : fileSystem.getPath(base), basePattern,
path == null || path.isEmpty() ? null : fileSystem.getPath(path), pathPattern);
}
public ConfigFinder find(Path base, String basePattern, Path path, String pathPattern) throws IOException {
return find(base, basePattern, path, pathPattern, null);
}
/**
* Find the most recent version of a file.
*
* @param base the path of the base directory
* @param basePattern a pattern to match directory entries in the base directory or null to match '*'
* @param path the path of the file if no recent path can be found in the base directory or null
* @param pathPattern the file name pattern to match
* @param modifiedSince time stamp for file or null
* @return this Finder
* @throws IOException if find fails
*/
public ConfigFinder find(Path base,
String basePattern,
Path path,
String pathPattern,
FileTime modifiedSince) throws IOException {
if (base != null && path == null) {
// find input in base
final PathMatcher baseMatcher = base.getFileSystem()
.getPathMatcher("glob:" + (basePattern != null ? basePattern : "*"));
final PathMatcher pathMatcher = base.getFileSystem()
.getPathMatcher("glob:" + (pathPattern != null ? pathPattern : "*"));
List<Path> directories = new ArrayList<>();
List<Path> list = Files.find(base, 1,
(p, a) -> {
if (Files.isDirectory(p) && baseMatcher.matches(p.getFileName())) {
directories.add(p);
return false;
}
return Files.isRegularFile(p) && pathMatcher.matches(p.getFileName());
}, FileVisitOption.FOLLOW_LINKS)
.collect(Collectors.toList());
if (directories.isEmpty()) {
return this;
}
list.sort(LAST_MODIFIED_COMPARATOR.reversed());
result.addAll(list);
path = list.iterator().next();
}
if (path == null) {
return this;
}
final PathMatcher pathMatcher = path.getFileSystem()
.getPathMatcher("glob:" + (pathPattern != null ? pathPattern : "*"));
Files.walkFileTree(path, opts, Integer.MAX_VALUE, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path p, BasicFileAttributes a) {
if ((Files.isRegularFile(p) && pathMatcher.matches(p.getFileName())) &&
(modifiedSince == null || a.lastModifiedTime().toMillis() > modifiedSince.toMillis())) {
result.add(p);
}
return FileVisitResult.CONTINUE;
}
});
return this;
}
public ConfigFinder sortBy(String mode) {
if ("lastmodified".equals(mode)) {
this.comparator = LAST_MODIFIED_COMPARATOR;
} else if ("name".equals(mode)) {
this.comparator = PATH_NAME_COMPARATOR;
}
return this;
}
public ConfigFinder order(String mode) {
if ("desc".equals(mode)) {
this.comparator = Collections.reverseOrder(comparator);
}
return this;
}
public Stream<Path> getPathFiles() {
return getPathFiles(-1);
}
public Stream<Path> getPathFiles(long max) {
if (comparator != null) {
result.sort(comparator);
}
return result.stream().limit(max < 0 ? result.size() : max);
}
public Stream<Path> skipPathFiles(long skip) {
if (comparator != null) {
result.sort(comparator);
}
return result.stream().skip(skip < 0 ? 0 : skip);
}
public Stream<String> getPaths() {
return getPaths(-1);
}
public Stream<String> getPaths(long max) {
if (comparator != null) {
result.sort(comparator);
}
return result.stream()
.map(p -> p.toAbsolutePath().toString())
.limit(max < 0 ? result.size() : max);
}
private static final Comparator<Path> LAST_MODIFIED_COMPARATOR = Comparator.comparing(p -> {
try {
return Files.getLastModifiedTime(p);
} catch (IOException e) {
return null;
}
});
private static final Comparator<Path> PATH_NAME_COMPARATOR = Comparator.comparing(Path::toString);
}

View file

@ -0,0 +1,311 @@
package org.xbib.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.stream.Collectors;
import org.xbib.settings.Settings;
import org.xbib.settings.SettingsBuilder;
import org.xbib.settings.SettingsLoader;
import org.xbib.settings.SettingsLoaderService;
/**
* A configuration loader for configuration files.
*/
public class ConfigLoader {
private static final Map<ConfigParams, Settings> map = new HashMap<>();
private ConfigLogger logger;
private ConfigLoader() {
}
private static class Holder {
private static ConfigLogger createConfigLogger() {
ServiceLoader<ConfigLogger> serviceLoader = ServiceLoader.load(ConfigLogger.class);
Optional<ConfigLogger> optionalConfigLogger = serviceLoader.findFirst();
return optionalConfigLogger.orElse(new NullConfigLogger());
}
private static final ConfigLoader configLoader = new ConfigLoader().withLogger(createConfigLogger());
}
public static ConfigLoader getInstance() {
return Holder.configLoader;
}
public ConfigLoader withLogger(ConfigLogger logger) {
this.logger = logger;
return this;
}
public synchronized Settings load(ConfigParams configParams) throws ConfigException {
map.computeIfAbsent(configParams, p -> internalLoad(p)
.replacePropertyPlaceholders()
.build());
return map.get(configParams);
}
private SettingsBuilder internalLoad(ConfigParams params) throws ConfigException {
SettingsBuilder settings = Settings.settingsBuilder();
if (params.withSystemEnvironment) {
settings.loadFromSystemEnvironment();
}
if (params.withSystemProperties) {
settings.loadFromSystemProperties();
}
if (!params.settings.isEmpty()) {
for (Settings s : params.settings) {
settings.put(s);
}
}
if (!params.reader.isEmpty()) {
for (ConfigParams.SuffixedReader reader : params.reader) {
SettingsBuilder readerSettings = createSettingsFromReader(reader.reader, reader.suffix);
if (readerSettings != null) {
settings.put(readerSettings.build());
if (!params.includeAll) {
return overrideFromProperties(params, settings);
}
}
}
}
if (params.args != null) {
SettingsBuilder argsSettings = createSettingsFromArgs(params);
if (argsSettings != null) {
settings.put(argsSettings.build());
if (!params.includeAll) {
return overrideFromProperties(params, settings);
}
}
}
if (params.withStdin) {
SettingsBuilder stdinSettings = createSettingsFromStdin();
if (stdinSettings != null) {
settings.put(stdinSettings.build());
if (!params.includeAll) {
return overrideFromProperties(params, settings);
}
}
}
if (!params.fileLocations.isEmpty()) {
SettingsBuilder fileSettings = createSettingsFromFile(params.fileLocations);
if (fileSettings != null) {
settings.put(fileSettings.build());
if (!params.includeAll) {
return overrideFromProperties(params, settings);
}
}
}
if (!params.fileNamesWithoutSuffix.isEmpty()) {
for (String fileNameWithoutSuffix : params.fileNamesWithoutSuffix) {
SettingsBuilder fileSettings = createSettingsFromFile(createListOfLocations(params, fileNameWithoutSuffix));
if (fileSettings != null) {
settings.put(fileSettings.build());
if (!params.includeAll) {
return overrideFromProperties(params, settings);
}
}
}
for (String fileNameWithoutSuffix : params.fileNamesWithoutSuffix) {
if (params.classLoaders != null) {
for (ClassLoader cl : params.classLoaders) {
if (cl != null) {
SettingsBuilder classpathSettings = createClasspathSettings(params, cl, fileNameWithoutSuffix);
if (classpathSettings != null) {
settings.put(classpathSettings.build());
if (!params.includeAll) {
return overrideFromProperties(params, settings);
}
}
}
}
}
}
}
if (!params.jdbcLookups.isEmpty()) {
for (ConfigParams.JdbcLookup jdbcLookup : params.jdbcLookups) {
try {
settings.fromJdbc(jdbcLookup.connection, jdbcLookup.statement, jdbcLookup.params);
} catch (SQLException sqlException) {
throw new ConfigException(sqlException);
}
}
}
if (params.includeAll) {
return overrideFromProperties(params, settings);
}
if (params.failIfEmpty) {
throw new ConfigException("no config found");
}
return settings;
}
private SettingsBuilder createSettingsFromArgs(ConfigParams params) throws ConfigException {
if (!params.fileNamesWithoutSuffix.isEmpty() && params.args != null) {
for (String fileNameWithoutSuffix : params.fileNamesWithoutSuffix) {
for (String suffix : SettingsLoaderService.getInstance().getSuffixes()) {
for (int i = 0; i < params.args.size() - 1; i++) {
String arg = params.args.get(i);
String s = params.directoryName != null ?
"--" + params.directoryName + "-" + fileNameWithoutSuffix + "." + suffix :
"--" + fileNameWithoutSuffix + "." + suffix;
if (arg.equals(s)) {
return createSettingsFromReader(new StringReader(params.args.get(i + 1)), suffix);
}
}
}
}
}
return null;
}
private SettingsBuilder createSettingsFromStdin() throws ConfigException {
if (System.in != null) {
try {
int numBytesWaiting = System.in.available();
if (numBytesWaiting > 0) {
String suffix = System.getProperty("config.format", "yaml");
return createSettingsFromStream(System.in, "." + suffix);
}
} catch (IOException e) {
throw new ConfigException(e);
}
}
return null;
}
private SettingsBuilder createSettingsFromFile(List<String> settingsFileNames) throws ConfigException {
SettingsBuilder settings = Settings.settingsBuilder();
for (String settingsFileName: settingsFileNames) {
int pos = settingsFileName.lastIndexOf('.');
String suffix = (pos > 0 ? settingsFileName.substring(pos + 1) : "").toLowerCase(Locale.ROOT);
Path path = Paths.get(settingsFileName);
if (logger != null) {
logger.info("trying " + path);
}
if (Files.exists(path)) {
if (logger != null) {
logger.info("found path: " + path);
}
System.setProperty("config.path", path.getParent().toString());
try {
InputStream inputStream = Files.newInputStream(path);
SettingsBuilder fileSettings = createSettingsFromStream(inputStream, suffix);
if (fileSettings != null) {
settings.put(fileSettings.build());
}
} catch (Exception e) {
throw new ConfigException(e);
}
}
}
return settings.isEmpty() ? null : settings;
}
private SettingsBuilder createClasspathSettings(ConfigParams params,
ClassLoader classLoader,
String fileNameWithoutSuffix) throws ConfigException {
SettingsBuilder settings = Settings.settingsBuilder();
for (String suffix : SettingsLoaderService.getInstance().getSuffixes()) {
String path = params.directoryName != null ?
params.directoryName + '-' + fileNameWithoutSuffix + suffix : fileNameWithoutSuffix + suffix;
InputStream inputStream = classLoader.getResourceAsStream(path);
if (inputStream != null) {
if (logger != null) {
logger.info("found resource: " + path);
}
SettingsBuilder streamSettings = createSettingsFromStream(inputStream, suffix);
if (streamSettings != null) {
settings.put(streamSettings.build());
}
}
}
return settings.isEmpty() ? null : settings;
}
private SettingsBuilder createSettingsFromStream(InputStream inputStream,
String suffix) throws ConfigException {
if (inputStream == null) {
if (logger != null) {
logger.error("unable to open input stream");
}
return null;
}
return createSettingsFromReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), suffix);
}
private SettingsBuilder createSettingsFromReader(Reader reader,
String suffix) throws ConfigException {
if (reader == null) {
if (logger != null) {
logger.error("unable to open reader");
}
return null;
}
SettingsLoader settingsLoader = SettingsLoaderService.getInstance().loaderFromResource(suffix);
if (settingsLoader != null) {
SettingsBuilder settings;
try (BufferedReader bufferedReader = new BufferedReader(reader)) {
String content = bufferedReader.lines().collect(Collectors.joining("\n"));
settings = Settings.settingsBuilder().put(settingsLoader.load(content));
} catch (IOException e) {
throw new ConfigException(e);
}
return settings;
} else {
if (logger != null) {
logger.error("suffix is invalid: " + suffix);
}
}
return null;
}
private SettingsBuilder overrideFromProperties(ConfigParams params, SettingsBuilder settingsBuilder) {
if (params.withSystemPropertiesOverride) {
settingsBuilder.map(e -> {
String key = e.getKey();
String value = System.getProperty(params.directoryName != null ? params.directoryName + '.' + key : key);
return value != null ? Map.entry(key, value) : Map.entry(key, e.getValue());
});
}
return settingsBuilder;
}
private List<String> createListOfLocations(ConfigParams params,
String fileNameWithoutSuffix) {
List<String> list = new ArrayList<>();
for (String suffix : SettingsLoaderService.getInstance().getSuffixes()) {
String xdgConfigHome = System.getenv("XDG_CONFIG_HOME");
if (xdgConfigHome == null) {
xdgConfigHome = System.getProperty("user.home") + "/.config";
}
if (params.directoryName != null) {
list.add(params.directoryName + '-' + fileNameWithoutSuffix + "." + suffix);
list.add(xdgConfigHome + '/' + params.directoryName + '/' + fileNameWithoutSuffix + "." + suffix);
list.add("/etc/" + params.directoryName + '/' + fileNameWithoutSuffix + "." + suffix);
} else {
list.add(fileNameWithoutSuffix + "." + suffix);
list.add(xdgConfigHome + '/' + fileNameWithoutSuffix + "." + suffix);
list.add("/etc/" + fileNameWithoutSuffix + "." + suffix);
}
}
return list;
}
}

View file

@ -0,0 +1,10 @@
package org.xbib.config;
public interface ConfigLogger {
void info(String string);
void warn(String string);
void error(String message);
}

View file

@ -0,0 +1,192 @@
package org.xbib.config;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import org.xbib.settings.Settings;
import org.xbib.settings.datastructures.DatastructureSettings;
public class ConfigParams implements Comparable<ConfigParams> {
private static final Comparator<ConfigParams> COMPARATOR =
Comparator.comparing(ConfigParams::toString);
boolean withSystemEnvironment = false;
boolean withSystemProperties = false;
boolean failIfEmpty = false;
boolean includeAll = false;
boolean withStdin = false;
boolean withSystemPropertiesOverride = false;
List<ClassLoader> classLoaders = null;
final List<SuffixedReader> reader = new ArrayList<>();
final List<JdbcLookup> jdbcLookups = new ArrayList<>();
final List<DatastructureSettings> settings = new ArrayList<>();
List<String> args = null;
String directoryName = null;
final List<String> fileNamesWithoutSuffix = new ArrayList<>();
final List<String> fileLocations = new ArrayList<>();
public ConfigParams() {
}
public ConfigParams withSystemEnvironment() {
this.withSystemEnvironment = true;
return this;
}
public ConfigParams withSystemProperties() {
this.withSystemProperties = true;
return this;
}
public ConfigParams withSystemPropertiesOverride() {
this.withSystemPropertiesOverride = true;
return this;
}
public ConfigParams includeAll() {
this.includeAll = true;
return this;
}
public ConfigParams failIfEmpty() {
this.failIfEmpty = true;
return this;
}
public ConfigParams withStdin(boolean withStdin) {
this.withStdin = withStdin;
return this;
}
public ConfigParams withArgs(String[] args) {
this.args = Arrays.asList(args);
return this;
}
public ConfigParams withClassLoaders(ClassLoader... classLoaders) {
this.classLoaders = Arrays.asList(classLoaders);
return this;
}
public ConfigParams withReader(Reader reader, String suffix) {
SuffixedReader suffixedReader = new SuffixedReader();
suffixedReader.reader = reader;
suffixedReader.suffix = suffix;
this.reader.add(suffixedReader);
return this;
}
public ConfigParams withSettings(Settings settings) {
this.settings.add(DatastructureSettings.builder().put(settings.getAsMap()).build());
return this;
}
public ConfigParams withDirectoryName(String directoryName) {
this.directoryName = directoryName;
return this;
}
public ConfigParams withFileNamesWithoutSuffix(String... fileNamesWithoutSuffix) {
this.fileNamesWithoutSuffix.addAll(Arrays.asList(fileNamesWithoutSuffix));
return this;
}
public ConfigParams withLocation(String location) {
this.fileLocations.add(location);
return this;
}
public ConfigParams withPath(String basePath, String basePattern, String path, String pathPattern) throws IOException {
ConfigFinder configFinder = new ConfigFinder();
configFinder.find(basePath, basePattern, path, pathPattern).getPaths().forEach(this::withLocation);
return this;
}
public ConfigParams withJdbc(Connection connection, String statement, String[] params) {
JdbcLookup jdbcLookup = new JdbcLookup();
jdbcLookup.connection = connection;
jdbcLookup.statement = statement;
jdbcLookup.params = params;
jdbcLookups.add(jdbcLookup);
return this;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ConfigParams that = (ConfigParams) o;
return withSystemEnvironment == that.withSystemEnvironment &&
withSystemProperties == that.withSystemProperties &&
failIfEmpty == that.failIfEmpty &&
includeAll == that.includeAll &&
withStdin == that.withStdin &&
withSystemPropertiesOverride == that.withSystemPropertiesOverride &&
Objects.equals(classLoaders, that.classLoaders) &&
Objects.equals(reader, that.reader) &&
Objects.equals(jdbcLookups, that.jdbcLookups) &&
Objects.equals(settings, that.settings) &&
Objects.equals(args, that.args) &&
Objects.equals(directoryName, that.directoryName) &&
Objects.equals(fileNamesWithoutSuffix, that.fileNamesWithoutSuffix) &&
Objects.equals(fileLocations, that.fileLocations);
}
@Override
public int compareTo(ConfigParams o) {
return COMPARATOR.compare(this, o);
}
@Override
public String toString() {
return "" +
withSystemEnvironment +
withSystemProperties +
withStdin +
classLoaders +
reader +
args +
directoryName +
fileNamesWithoutSuffix +
fileLocations;
}
static class SuffixedReader {
Reader reader;
String suffix;
}
static class JdbcLookup {
Connection connection;
String statement;
String[] params;
}
}

View file

@ -0,0 +1,19 @@
package org.xbib.config;
public class NullConfigLogger implements ConfigLogger {
public NullConfigLogger() {
}
@Override
public void info(String string) {
}
@Override
public void warn(String message) {
}
@Override
public void error(String message) {
}
}

View file

@ -0,0 +1,22 @@
package org.xbib.config;
public class SystemConfigLogger implements ConfigLogger {
public SystemConfigLogger() {
}
@Override
public void info(String string) {
System.err.println("info: " + string);
}
@Override
public void warn(String message) {
System.err.println("warning: " + message);
}
@Override
public void error(String message) {
System.err.println("error: " + message);
}
}

View file

@ -0,0 +1,4 @@
/**
* Classes for configuration setup.
*/
package org.xbib.config;

View file

@ -0,0 +1,76 @@
package org.xbib.config.test;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.junit.jupiter.api.Test;
import org.xbib.config.ConfigLoader;
import org.xbib.config.ConfigParams;
import org.xbib.settings.Settings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ConfigLoaderTest {
@Test
public void configEmptyTest() {
Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams());
assertTrue(settings.isEmpty());
}
@Test
public void configSettingsTest() {
Settings configSettings = Settings.settingsBuilder()
.put("hello", "world")
.build();
Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams().withSettings(configSettings));
assertEquals("world", settings.get("hello"));
}
@Test
public void configArgsTest() {
String[] args = new String[] {
"--config.yaml",
"hello: world"
};
Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams()
.withArgs(args)
.withFileNamesWithoutSuffix("config"));
assertEquals("world", settings.get("hello"));
}
@Test
public void configPropertiesTest() {
Reader reader = new StringReader("a=b");
Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams()
.withReader(reader, "properties"));
assertEquals("b", settings.get("a"));
}
@Test
public void configFileTest() throws IOException {
Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams()
.withPath(null, null, "src/test/resources", "config.*"));
assertEquals("world", settings.get("hello"));
assertEquals("world2", settings.get("hello2"));
}
@Test
public void testSystemPropertiesOverride() throws IOException {
System.setProperty("hello", "override");
Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams()
.withPath(null, null, "src/test/resources", "config.*"));
assertEquals("world", settings.get("hello"));
settings = ConfigLoader.getInstance()
.load(new ConfigParams()
.withSystemPropertiesOverride()
.withPath(null, null, "src/test/resources", "config.*"));
assertEquals("override", settings.get("hello"));
}
}

View file

@ -0,0 +1,4 @@
/**
* Test classes for config.
*/
package org.xbib.config.test;

View file

@ -0,0 +1,2 @@
org.xbib.config.NullConfigLogger
org.xbib.config.SystemConfigLogger

View file

@ -0,0 +1,3 @@
org.xbib.settings.datastructures.PropertiesSettingsLoader
org.xbib.settings.datastructures.json.JsonSettingsLoader
org.xbib.settings.datastructures.yaml.YamlSettingsLoader

View file

@ -0,0 +1,3 @@
{
"hello2": "world2"
}

View file

@ -0,0 +1 @@
hello: world

View file

@ -1,11 +0,0 @@
def moduleName = 'org.xbib.content.csv.test'
def patchArgs = ['--patch-module', "$moduleName=" + files(sourceSets.test.resources.srcDirs).asPath ]
tasks.named('compileTestJava') {
options.compilerArgs += patchArgs
}
tasks.named('test') {
jvmArgs += patchArgs
}

View file

@ -1,6 +0,0 @@
module org.xbib.content.csv.test {
requires org.junit.jupiter.api;
requires org.xbib.content.csv;
exports org.xbib.content.csv.test;
opens org.xbib.content.csv.test to org.junit.platform.commons;
}

View file

@ -1,8 +1,6 @@
package org.xbib.content.csv.test;
package org.xbib.content.csv;
import org.junit.jupiter.api.Test;
import org.xbib.content.csv.CSVGenerator;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;

View file

@ -1,9 +1,7 @@
package org.xbib.content.csv.test;
package org.xbib.content.csv;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.xbib.content.csv.CSVParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@ -12,13 +10,16 @@ import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
/**
*
*/
public class CSVParserTest {
@Test
public void testCommaSeparated() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("test.csv");
InputStream in = getClass().getResourceAsStream("test.csv");
int count = 0;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
CSVParser csvParser = new CSVParser(reader);
Iterator<List<String>> it = csvParser.iterator();
while (it.hasNext()) {
@ -31,9 +32,9 @@ public class CSVParserTest {
@Test
public void testLargeFile() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("titleFile.csv");
InputStream in = getClass().getResourceAsStream("titleFile.csv");
int count = 0;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
CSVParser csvParser = new CSVParser(reader);
Iterator<List<String>> it = csvParser.iterator();
while (it.hasNext()) {

View file

@ -1,4 +1,4 @@
package org.xbib.content.csv.test;
package org.xbib.content.csv;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;

View file

@ -0,0 +1,4 @@
/**
* Classes for testing CSV content.
*/
package org.xbib.content.csv;

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{ABSOLUTE}][%-5p][%-25c][%t] %m%n"/>
</Console>
</appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</configuration>

View file

@ -1,15 +1,5 @@
dependencies {
api project(':content-core')
api libs.jackson.databind
testImplementation testLibs.mockito.core
}
def patchArgs = ['--patch-module', "org.xbib.content.json.test=" + sourceSets.test.resources.sourceDirectories.singleFile]
tasks.named('compileTestJava') {
options.compilerArgs += patchArgs
}
tasks.named('test') {
jvmArgs += patchArgs
testImplementation libs.mockito.inline
}

View file

@ -19,7 +19,6 @@ public class JsonXContentGenerator extends AbstractXContentGenerator {
private final JsonGeneratorDelegate delegate;
@SuppressWarnings("this-escape")
public JsonXContentGenerator(JsonGenerator generator) {
this.delegate = new JsonGeneratorDelegate(generator);
super.setGenerator(delegate);

View file

@ -53,7 +53,7 @@ public class JsonXContentParser extends AbstractXContentParser {
@Override
public String currentName() throws IOException {
return parser.currentName();
return parser.getCurrentName();
}
@Override

View file

@ -6,9 +6,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
/**
* Utility class to load JSON values from various sources as {@link com.fasterxml.jackson.databind.JsonNode}s.
*
* This class uses a {@link JsonNodeReader} to parse JSON inputs.
*
* @see JsonNodeReader
@ -23,28 +25,34 @@ public final class JsonLoader {
}
/**
* Read a {@link com.fasterxml.jackson.databind.JsonNode} from an input stream.
* Read a {@link com.fasterxml.jackson.databind.JsonNode} from a resource path.
*
* This method first tries and loads the resource using {@link
* Class#getResource(String)}; if not found, is tries and uses the context
* classloader and if this is not found, this class's classloader.
*
* This method throws an {@link java.io.IOException} if the resource does not
* exist.
*
* @param inputStream the input stream
* @param classLoader the class loader
* @param resource the path to the resource (must begin
* with a {@code /})
* @return the JSON document at the resource
* @throws IllegalArgumentException resource path does not begin with a
* {@code /}
* @throws java.io.IOException there was a problem loading the resource, or the JSON
* document is invalid
*/
public static JsonNode fromResource(InputStream inputStream)
public static JsonNode fromResource(ClassLoader classLoader, final String resource)
throws IOException {
URL url = JsonLoader.class.getResource(resource);
InputStream in = url != null ? url.openStream() : classLoader.getResourceAsStream(resource);
final JsonNode ret;
try {
if (inputStream == null) {
throw new IOException("not found");
}
ret = READER.fromInputStream(inputStream);
ret = READER.fromInputStream(in);
} finally {
if (inputStream != null) {
inputStream.close();
if (in != null) {
in.close();
}
}
return ret;

View file

@ -1,6 +1,5 @@
package org.xbib.content.json.jackson;
import com.fasterxml.jackson.core.ErrorReportConfiguration;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
@ -36,7 +35,7 @@ public final class JsonNodeReader {
private final ObjectReader reader;
public JsonNodeReader(final ObjectMapper mapper) {
this.reader = mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, true)
reader = mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, true)
.readerFor(JsonNode.class);
}
@ -65,14 +64,13 @@ public final class JsonNodeReader {
/**
* Read a JSON value from an {@link java.io.InputStream}.
*
* @param inputStream the input stream
* @param in the input stream
* @return the value
* @throws java.io.IOException malformed input, or problem encountered when reading
* from the stream
*/
public JsonNode fromInputStream(final InputStream inputStream) throws IOException {
try (JsonParser parser = reader.getFactory().createParser(inputStream);
MappingIterator<JsonNode> iterator = reader.readValues(parser)) {
public JsonNode fromInputStream(final InputStream in) throws IOException {
try (JsonParser parser = reader.getFactory().createParser(in); MappingIterator<JsonNode> iterator = reader.readValues(parser)) {
return readNode(iterator);
}
}
@ -80,14 +78,13 @@ public final class JsonNodeReader {
/**
* Read a JSON value from a {@link java.io.Reader}.
*
* @param reader the reader
* @param r the reader
* @return the value
* @throws java.io.IOException malformed input, or problem encountered when reading
* from the reader
*/
public JsonNode fromReader(final Reader reader) throws IOException {
try (JsonParser parser = this.reader.getFactory().createParser(reader);
MappingIterator<JsonNode> iterator = this.reader.readValues(parser)) {
public JsonNode fromReader(final Reader r) throws IOException {
try (JsonParser parser = reader.getFactory().createParser(r); MappingIterator<JsonNode> iterator = reader.readValues(parser)) {
return readNode(iterator);
}
}
@ -100,7 +97,7 @@ public final class JsonNodeReader {
private JsonParseExceptionBuilder(final JsonParser jsonParser, final Object source) {
this.jsonParser = jsonParser;
location = new JsonLocation(ContentReference.construct(false, source, ErrorReportConfiguration.defaults()), 0L, 1, 1);
location = new JsonLocation(ContentReference.construct(false, source), 0L, 1, 1);
}
JsonParseExceptionBuilder setLocation(final JsonLocation location) {

View file

@ -45,7 +45,8 @@ public abstract class TreePointer<T extends TreeNode> implements Iterable<TokenR
* @param missing the representation of a missing node (may be null)
* @param tokenResolvers the list of reference token resolvers
*/
protected TreePointer(final T missing, final List<TokenResolver<T>> tokenResolvers) {
TreePointer(final T missing,
final List<TokenResolver<T>> tokenResolvers) {
this.missing = missing;
this.tokenResolvers = new ArrayList<>(tokenResolvers);
}
@ -57,7 +58,7 @@ public abstract class TreePointer<T extends TreeNode> implements Iterable<TokenR
*
* @param tokenResolvers the list of token resolvers
*/
TreePointer(final List<TokenResolver<T>> tokenResolvers) {
protected TreePointer(final List<TokenResolver<T>> tokenResolvers) {
this(null, tokenResolvers);
}
@ -69,7 +70,7 @@ public abstract class TreePointer<T extends TreeNode> implements Iterable<TokenR
* @throws JsonPointerException input is not a valid JSON Pointer
* @throws NullPointerException input is null
*/
public static List<ReferenceToken> tokensFromInput(final String input)
protected static List<ReferenceToken> tokensFromInput(final String input)
throws JsonPointerException {
String s = input;
final List<ReferenceToken> ret = new ArrayList<>();

View file

@ -1,17 +0,0 @@
module org.xbib.content.json.test {
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires java.logging;
requires jdk.unsupported; // sun.reflect for objenesis in mockito
requires org.mockito;
requires org.junit.jupiter.api;
requires org.xbib.content.api;
requires org.xbib.content.core;
requires org.xbib.content.json;
exports org.xbib.content.json.test;
exports org.xbib.content.json.test.jackson;
exports org.xbib.content.json.test.pointer;
opens org.xbib.content.json.test to org.junit.platform.commons;
opens org.xbib.content.json.test.jackson to org.junit.platform.commons;
opens org.xbib.content.json.test.pointer to org.junit.platform.commons;
}

View file

@ -1,4 +1,4 @@
package org.xbib.content.json.test;
package org.xbib.content.json;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

View file

@ -1,10 +1,8 @@
package org.xbib.content.json.test;
package org.xbib.content.json;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.xbib.content.XContentParser;
import org.xbib.content.json.JsonXContent;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;

View file

@ -1,4 +1,4 @@
package org.xbib.content.json.test.jackson;
package org.xbib.content.json.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.spy;
@ -8,8 +8,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.xbib.content.json.jackson.JsonNodeReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

View file

@ -1,4 +1,4 @@
package org.xbib.content.json.test.jackson;
package org.xbib.content.json.jackson;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.fasterxml.jackson.databind.JsonNode;
@ -6,14 +6,14 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.xbib.content.json.jackson.JsonLoader;
import org.xbib.content.json.jackson.JsonNumEquals;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
*/
public final class JsonNumEqualsTest {
private static final JsonNodeFactory FACTORY = JsonNodeFactory.instance;
@ -62,7 +62,7 @@ public final class JsonNumEqualsTest {
private Iterator<Object[]> getInputs() throws IOException {
final List<Object[]> list = new ArrayList<>();
JsonNode reference;
JsonNode testData = JsonLoader.fromResource(getClass().getResourceAsStream("testfile.json"));
JsonNode testData = JsonLoader.fromResource(this.getClass().getClassLoader(), "testfile.json");
for (final JsonNode element : testData) {
reference = element.get("reference");
for (final JsonNode node : element.get("equivalences")) {

View file

@ -1,9 +1,7 @@
package org.xbib.content.json.test.jackson;
package org.xbib.content.json.jackson;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import org.xbib.content.json.jackson.JacksonUtils;
import org.xbib.content.json.jackson.NodeType;
import java.math.BigDecimal;
import java.util.ArrayList;

View file

@ -0,0 +1,5 @@
/**
* Classes for testing JSON jackson.
*/
package org.xbib.content.json.jackson;

View file

@ -1,4 +1,4 @@
package org.xbib.content.json.test.pointer;
package org.xbib.content.json.pointer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -9,9 +9,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.xbib.content.json.jackson.JacksonUtils;
import org.xbib.content.json.jackson.NodeType;
import org.xbib.content.json.pointer.JsonNodeResolver;
import org.xbib.content.json.pointer.ReferenceToken;
import org.xbib.content.json.test.jackson.SampleNodeProvider;
import org.xbib.content.json.jackson.SampleNodeProvider;
import java.util.ArrayList;
import java.util.List;

View file

@ -1,4 +1,4 @@
package org.xbib.content.json.test.pointer;
package org.xbib.content.json.pointer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.fasterxml.jackson.databind.JsonNode;
@ -7,9 +7,7 @@ import org.junit.jupiter.api.Test;
import org.xbib.content.json.jackson.JacksonUtils;
import org.xbib.content.json.jackson.JsonLoader;
import org.xbib.content.json.jackson.NodeType;
import org.xbib.content.json.pointer.JsonPointer;
import org.xbib.content.json.pointer.JsonPointerException;
import org.xbib.content.json.test.jackson.SampleNodeProvider;
import org.xbib.content.json.jackson.SampleNodeProvider;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@ -19,14 +17,19 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
*
*/
public final class JsonPointerTest {
private static final String PACKAGE = JsonPointerTest.class.getPackage().getName().replace('.', '/');
private final JsonNode testData;
private final JsonNode document;
public JsonPointerTest() throws IOException {
testData = JsonLoader.fromResource(getClass().getResourceAsStream("jsonpointer.json"));
public JsonPointerTest()
throws IOException {
testData = JsonLoader.fromResource(this.getClass().getClassLoader(),
PACKAGE + "/jsonpointer.json");
document = testData.get("document");
}

View file

@ -1,12 +1,9 @@
package org.xbib.content.json.test.pointer;
package org.xbib.content.json.pointer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.xbib.content.json.pointer.JsonPointerException;
import org.xbib.content.json.pointer.ReferenceToken;
import java.util.Arrays;
import java.util.List;

View file

@ -1,4 +1,4 @@
package org.xbib.content.json.test.pointer;
package org.xbib.content.json.pointer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@ -14,16 +14,14 @@ import static org.mockito.Mockito.when;
import com.fasterxml.jackson.core.TreeNode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.xbib.content.json.pointer.JsonPointerException;
import org.xbib.content.json.pointer.ReferenceToken;
import org.xbib.content.json.pointer.TokenResolver;
import org.xbib.content.json.pointer.TreePointer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
*
*/
public final class TreePointerTest {
@Test
@ -148,7 +146,8 @@ public final class TreePointerTest {
assertTrue(dummy.isEmpty());
}
private static final class DummyPointer extends TreePointer<TreeNode> {
private static final class DummyPointer
extends TreePointer<TreeNode> {
private DummyPointer(final TreeNode missing, final List<TokenResolver<TreeNode>> tokenResolvers) {
super(missing, tokenResolvers);
}

View file

@ -0,0 +1,4 @@
/**
* Classes for testing JSON pointer.
*/
package org.xbib.content.json.pointer;

View file

@ -176,11 +176,11 @@ public final class Lang extends SubtagSet {
Subtag region = getRegion();
Subtag variant = getVariant();
if (variant != null && region != null) {
return Locale.of(primary.toString(), region.toString(), variant.toString());
return new Locale(primary.toString(), region.toString(), variant.toString());
} else if (region != null) {
return Locale.of(primary.toString(), region.toString());
return new Locale(primary.toString(), region.toString());
} else {
return Locale.of(primary.toString());
return new Locale(primary.toString());
}
}

View file

@ -3,13 +3,3 @@ dependencies {
implementation project(':content-xml')
implementation project(':content-json')
}
def patchArgs = ['--patch-module', "org.xbib.content.rdf.test=" + sourceSets.test.resources.sourceDirectories.singleFile]
tasks.named('compileTestJava') {
options.compilerArgs += patchArgs
}
tasks.named('test') {
jvmArgs += patchArgs
}

View file

@ -11,7 +11,7 @@ public interface RdfContent<P extends RdfContentParams> {
StandardRdfContentType type();
RdfContentGenerator<P> createGenerator(P params, OutputStream out) throws IOException;
RdfContentGenerator<P> createGenerator(OutputStream out) throws IOException;
RdfContentParser<P> createParser(P params, InputStream in) throws IOException;
RdfContentParser<P> createParser(InputStream in) throws IOException;
}

View file

@ -17,7 +17,7 @@ public class RdfContentBuilder<P extends RdfContentParams> implements RdfContent
private final RdfContentGenerator<P> generator;
private final OutputStream outputStream;
private final OutputStream out;
private IRI subject;
@ -25,9 +25,9 @@ public class RdfContentBuilder<P extends RdfContentParams> implements RdfContent
this(rdfContent, rdfParams, new BytesStreamOutput());
}
public RdfContentBuilder(RdfContent<P> rdfContent, P rdfContentParams, OutputStream outputStream) throws IOException {
this.outputStream = outputStream;
this.generator = rdfContent.createGenerator(rdfContentParams, outputStream);
public RdfContentBuilder(RdfContent<P> rdfContent, P rdfContentParams, OutputStream out) throws IOException {
this.out = out;
this.generator = rdfContent.createGenerator(out);
this.generator.setParams(rdfContentParams);
}
@ -54,7 +54,7 @@ public class RdfContentBuilder<P extends RdfContentParams> implements RdfContent
public BytesReference bytes() throws IOException {
close();
return ((BytesStreamOutput) outputStream).bytes();
return ((BytesStreamOutput) out).bytes();
}
public InputStream streamInput() throws IOException {

View file

@ -14,42 +14,74 @@ import org.xbib.content.rdf.io.xml.XmlContentParams;
import java.io.IOException;
import java.io.OutputStream;
/**
*
*/
public class RdfContentFactory {
private RdfContentFactory() {
}
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder() throws IOException {
return NTripleContent.contentBuilder(NTripleContentParams.N_TRIPLE_CONTENT_PARAMS);
}
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder(NTripleContentParams params) throws IOException {
return NTripleContent.contentBuilder(params);
}
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder(NTripleContentParams params, OutputStream outputStream)
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder(OutputStream out) throws IOException {
return NTripleContent.contentBuilder(out, NTripleContentParams.N_TRIPLE_CONTENT_PARAMS);
}
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder(OutputStream out, NTripleContentParams params)
throws IOException {
return NTripleContent.contentBuilder(params, outputStream);
return NTripleContent.contentBuilder(out, params);
}
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder() throws IOException {
return RdfXmlContent.contentBuilder(RdfXmlContentParams.RDF_XML_CONTENT_PARAMS);
}
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder(RdfXmlContentParams params) throws IOException {
return RdfXmlContent.contentBuilder(params);
}
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder(RdfXmlContentParams params, OutputStream outputStream)
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder(OutputStream out) throws IOException {
return RdfXmlContent.contentBuilder(out, RdfXmlContentParams.RDF_XML_CONTENT_PARAMS);
}
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder(OutputStream out, RdfXmlContentParams params)
throws IOException {
return RdfXmlContent.contentBuilder(params, outputStream);
return RdfXmlContent.contentBuilder(out, params);
}
public static RdfContentBuilder<TurtleContentParams> turtleBuilder() throws IOException {
return TurtleContent.contentBuilder(TurtleContentParams.TURTLE_CONTENT_PARAMS);
}
public static RdfContentBuilder<TurtleContentParams> turtleBuilder(TurtleContentParams params) throws IOException {
return TurtleContent.contentBuilder(params);
}
public static RdfContentBuilder<TurtleContentParams> turtleBuilder(TurtleContentParams params, OutputStream outputStream) throws IOException {
return TurtleContent.contentBuilder(params, outputStream);
public static RdfContentBuilder<TurtleContentParams> turtleBuilder(OutputStream out) throws IOException {
return TurtleContent.contentBuilder(out, TurtleContentParams.TURTLE_CONTENT_PARAMS);
}
public static RdfContentBuilder<TurtleContentParams> turtleBuilder(OutputStream out, TurtleContentParams params)
throws IOException {
return TurtleContent.contentBuilder(out, params);
}
public static RdfContentBuilder<XmlContentParams> xmlBuilder() throws IOException {
return XmlContent.contentBuilder(XmlContentParams.XML_CONTENT_PARAMS);
}
public static RdfContentBuilder<XmlContentParams> xmlBuilder(XmlContentParams params) throws IOException {
return XmlContent.contentBuilder(params);
}
public static RdfContentBuilder<JsonContentParams> jsonBuilder(JsonContentParams params) throws IOException {
return JsonContent.contentBuilder(params);
public static RdfContentBuilder<JsonContentParams> jsonBuilder() throws IOException {
return JsonContent.contentBuilder(JsonContentParams.JSON_CONTENT_PARAMS);
}
}

View file

@ -28,12 +28,12 @@ public class RdfXContent implements RdfContent<RdfXContentParams> {
}
@Override
public RdfContentGenerator<RdfXContentParams> createGenerator(RdfXContentParams params, OutputStream outputStream) {
return new RdfXContentGenerator<>(params, outputStream);
public RdfContentGenerator<RdfXContentParams> createGenerator(OutputStream out) {
return new RdfXContentGenerator<>(out);
}
@Override
public RdfContentParser<RdfXContentParams> createParser(RdfXContentParams params, InputStream inputStream) {
public RdfContentParser<RdfXContentParams> createParser(InputStream in) {
throw new UnsupportedOperationException();
}
}

View file

@ -11,34 +11,61 @@ import org.xbib.content.rdf.io.xml.XmlContentParams;
import java.io.IOException;
/**
*
*/
public class RdfXContentFactory {
private RdfXContentFactory() {
}
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder() throws IOException {
return NTripleContent.contentBuilder(NTripleContentParams.N_TRIPLE_CONTENT_PARAMS);
}
public static RdfContentBuilder<NTripleContentParams> ntripleBuilder(NTripleContentParams params) throws IOException {
return NTripleContent.contentBuilder(params);
}
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder() throws IOException {
return RdfXmlContent.contentBuilder(RdfXmlContentParams.RDF_XML_CONTENT_PARAMS);
}
public static RdfContentBuilder<RdfXmlContentParams> rdfXmlBuilder(RdfXmlContentParams params) throws IOException {
return RdfXmlContent.contentBuilder(params);
}
public static RdfContentBuilder<TurtleContentParams> turtleBuilder() throws IOException {
return TurtleContent.contentBuilder(TurtleContentParams.TURTLE_CONTENT_PARAMS);
}
public static RdfContentBuilder<TurtleContentParams> turtleBuilder(TurtleContentParams params) throws IOException {
return TurtleContent.contentBuilder(params);
}
public static RdfContentBuilder<XmlContentParams> xmlBuilder() throws IOException {
return XmlContent.contentBuilder(XmlContentParams.XML_CONTENT_PARAMS);
}
public static RdfContentBuilder<XmlContentParams> xmlBuilder(XmlContentParams params) throws IOException {
return XmlContent.contentBuilder(params);
}
public static RdfContentBuilder<RdfXContentParams> rdfXContentBuilder() throws IOException {
return RdfXContent.contentBuilder(RdfXContentParams.RDF_X_CONTENT_PARAMS);
}
public static RdfContentBuilder<RdfXContentParams> rdfXContentBuilder(RdfXContentParams params) throws IOException {
return RdfXContent.contentBuilder(params);
}
public static RdfContentBuilder<RouteRdfXContentParams> routeRdfXContentBuilder() throws IOException {
return RouteRdfXContent.contentBuilder(RouteRdfXContentParams.ROUTE_RDF_X_CONTENT_PARAMS);
}
public static RdfContentBuilder<RouteRdfXContentParams> routeRdfXContentBuilder(RouteRdfXContentParams params)
throws IOException {
return RouteRdfXContent.contentBuilder(params);
}
}

View file

@ -19,7 +19,7 @@ import java.util.List;
*/
public class RdfXContentGenerator<P extends RdfXContentParams> implements RdfContentGenerator<P> {
protected final OutputStream outputStream;
protected final OutputStream out;
protected Resource resource;
@ -29,9 +29,8 @@ public class RdfXContentGenerator<P extends RdfXContentParams> implements RdfCon
private boolean flushed;
RdfXContentGenerator(P params, OutputStream outputStream) {
this.params = params;
this.outputStream = outputStream;
RdfXContentGenerator(OutputStream out) {
this.out = out;
}
@Override
@ -106,7 +105,7 @@ public class RdfXContentGenerator<P extends RdfXContentParams> implements RdfCon
return;
}
flushed = true;
builder = DefaultXContentBuilder.builder(JsonXContent.jsonContent(), outputStream);
builder = DefaultXContentBuilder.builder(JsonXContent.jsonContent(), out);
builder.startObject();
build(this.resource);
builder.endObject();

View file

@ -2,12 +2,21 @@ package org.xbib.content.rdf;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class RdfXContentParams implements RdfContentParams {
public static final RdfXContentParams RDF_X_CONTENT_PARAMS = new RdfXContentParams();
private final IRINamespaceContext namespaceContext;
private RdfXContentGenerator<RdfXContentParams> generator;
public RdfXContentParams() {
this.namespaceContext = IRINamespaceContext.newInstance();
}
public RdfXContentParams(IRINamespaceContext namespaceContext) {
this.namespaceContext = namespaceContext;
}

View file

@ -4,6 +4,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
*/
public class RouteRdfXContent implements RdfContent<RouteRdfXContentParams> {
private static final RouteRdfXContent ROUTE_RDF_X_CONTENT = new RouteRdfXContent();
@ -25,15 +28,18 @@ public class RouteRdfXContent implements RdfContent<RouteRdfXContentParams> {
}
@Override
public RdfContentGenerator<RouteRdfXContentParams> createGenerator(RouteRdfXContentParams params, OutputStream outputStream) {
return new RouteRdfXContentGenerator<>(params, outputStream);
public RdfContentGenerator<RouteRdfXContentParams> createGenerator(OutputStream out) {
return new RouteRdfXContentGenerator<>(out);
}
@Override
public RdfContentParser<RouteRdfXContentParams> createParser(RouteRdfXContentParams params, InputStream inputStream) {
public RdfContentParser<RouteRdfXContentParams> createParser(InputStream in) {
throw new UnsupportedOperationException();
}
/**
*
*/
@FunctionalInterface
public interface RouteHandler {
void complete(String content, RouteRdfXContentParams params) throws IOException;

View file

@ -13,8 +13,8 @@ public class RouteRdfXContentGenerator<R extends RouteRdfXContentParams> extends
private boolean flushed;
RouteRdfXContentGenerator(R params, OutputStream outputStream) {
super(params, outputStream);
RouteRdfXContentGenerator(OutputStream out) {
super(out);
}
@Override

View file

@ -2,8 +2,13 @@ package org.xbib.content.rdf;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class RouteRdfXContentParams extends RdfXContentParams {
public static final RouteRdfXContentParams ROUTE_RDF_X_CONTENT_PARAMS = new RouteRdfXContentParams();
private String index;
private String type;
@ -18,6 +23,20 @@ public class RouteRdfXContentParams extends RdfXContentParams {
private RouteRdfXContent.RouteHandler handler;
public RouteRdfXContentParams() {
super();
}
public RouteRdfXContentParams(String index, String type) {
super();
this.index = index;
this.type = type;
}
public RouteRdfXContentParams(IRINamespaceContext namespaceContext) {
super(namespaceContext);
}
public RouteRdfXContentParams(IRINamespaceContext namespaceContext, String index, String type) {
super(namespaceContext);
this.index = index;

View file

@ -14,14 +14,16 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
*
*/
public class DefaultRdfGraph implements RdfGraph<RdfGraphParams> {
private RdfGraphParams params;
private RdfGraphParams params = DefaultRdfGraphParams.DEFAULT_PARAMS;
private final Map<IRI, Resource> resources = new LinkedHashMap<>();
public DefaultRdfGraph(RdfGraphParams params) {
this.params = params;
public DefaultRdfGraph() {
}
@Override

View file

@ -3,14 +3,17 @@ package org.xbib.content.rdf.internal;
import org.xbib.content.rdf.RdfGraphParams;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class DefaultRdfGraphParams implements RdfGraphParams {
public static final DefaultRdfGraphParams DEFAULT_PARAMS = new DefaultRdfGraphParams();
private final IRINamespaceContext namespaceContext;
private final boolean writeNamespaceContext;
public DefaultRdfGraphParams(IRINamespaceContext namespaceContext) {
this.namespaceContext = namespaceContext;
public DefaultRdfGraphParams() {
this.namespaceContext = IRINamespaceContext.newInstance();
this.writeNamespaceContext = true;
}

View file

@ -45,13 +45,11 @@ public class DefaultResource implements Resource, Comparable<Resource>, XSDResou
this(iri, new LinkedHashMultiMap<>(), new LinkedHashMap<>());
}
@SuppressWarnings("this-escape")
public DefaultResource(DefaultResource resource) {
this(resource.id(), resource.getAttributes(), resource.getChildren());
this.deleted = resource.isDeleted();
}
@SuppressWarnings("this-escape")
public DefaultResource(IRI iri, MultiMap<IRI, Node> attributes, Map<IRI, Resource> children) {
setId(iri);
this.attributes = attributes;

View file

@ -10,6 +10,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
*/
public class JsonContent implements RdfContent<JsonContentParams> {
private static final JsonContent JSON_CONTENT = new JsonContent();
@ -36,12 +39,12 @@ public class JsonContent implements RdfContent<JsonContentParams> {
}
@Override
public RdfContentGenerator<JsonContentParams> createGenerator(JsonContentParams params, OutputStream outputStream) throws IOException {
return new JsonContentGenerator(params, outputStream);
public RdfContentGenerator<JsonContentParams> createGenerator(OutputStream out) throws IOException {
return new JsonContentGenerator(out);
}
@Override
public RdfContentParser<JsonContentParams> createParser(JsonContentParams params, InputStream inputStream) throws IOException {
return new JsonContentParser<>(params, inputStream);
public RdfContentParser<JsonContentParams> createParser(InputStream in) throws IOException {
return new JsonContentParser<>(in);
}
}

View file

@ -24,14 +24,13 @@ public class JsonContentGenerator implements RdfContentGenerator<JsonContentPara
private Resource resource;
private JsonContentParams params;
private JsonContentParams params = JsonContentParams.JSON_CONTENT_PARAMS;
JsonContentGenerator(JsonContentParams params, OutputStream out) {
this(params, new OutputStreamWriter(out, StandardCharsets.UTF_8));
JsonContentGenerator(OutputStream out) {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
JsonContentGenerator(JsonContentParams params, Writer writer) {
this.params = params;
JsonContentGenerator(Writer writer) {
this.writer = writer;
this.nsWritten = false;
this.resource = new DefaultAnonymousResource();

View file

@ -4,8 +4,13 @@ import org.xbib.content.rdf.RdfContentParams;
import org.xbib.content.rdf.io.xml.XmlContentParams;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class JsonContentParams extends XmlContentParams implements RdfContentParams {
public static final JsonContentParams JSON_CONTENT_PARAMS = new JsonContentParams(NAMESPACE_CONTEXT);
public JsonContentParams(IRINamespaceContext namespaceContext) {
super(namespaceContext);
}

View file

@ -23,8 +23,6 @@ import javax.xml.namespace.QName;
*/
public class JsonContentParser<R extends RdfContentParams> implements RdfContentParser<R> {
private final JsonContentParams params;
private final Reader reader;
private XmlHandler<R> handler;
@ -33,12 +31,11 @@ public class JsonContentParser<R extends RdfContentParams> implements RdfContent
private QName root;
public JsonContentParser(JsonContentParams params, InputStream inputStream) throws IOException {
this(params, new InputStreamReader(inputStream, StandardCharsets.UTF_8));
public JsonContentParser(InputStream in) throws IOException {
this(new InputStreamReader(in, StandardCharsets.UTF_8));
}
public JsonContentParser(JsonContentParams params, Reader reader) {
this.params = params;
public JsonContentParser(Reader reader) {
this.reader = reader;
}

View file

@ -2,11 +2,14 @@ package org.xbib.content.rdf.io.json;
import org.xbib.content.rdf.io.xml.AbstractXmlResourceHandler;
/**
*
*/
public abstract class JsonResourceHandler extends AbstractXmlResourceHandler<JsonContentParams> {
@SuppressWarnings("this-escape")
public JsonResourceHandler(JsonContentParams params) {
super(params);
super.setDefaultNamespace("", "http://json.org");
}
}

View file

@ -0,0 +1,4 @@
/**
* Classes for RDF JSON.
*/
package org.xbib.content.rdf.io.json;

View file

@ -0,0 +1,4 @@
/**
* Classes for RDF N-Quads.
*/
package org.xbib.content.rdf.io.nquads;

View file

@ -10,6 +10,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
*/
public class NTripleContent implements RdfContent<NTripleContentParams> {
private static final NTripleContent N_TRIPLE_CONTENT = new NTripleContent();
@ -25,9 +28,9 @@ public class NTripleContent implements RdfContent<NTripleContentParams> {
return new RdfContentBuilder<>(N_TRIPLE_CONTENT, params);
}
public static RdfContentBuilder<NTripleContentParams> contentBuilder(NTripleContentParams params, OutputStream outputStream)
public static RdfContentBuilder<NTripleContentParams> contentBuilder(OutputStream out, NTripleContentParams params)
throws IOException {
return new RdfContentBuilder<>(N_TRIPLE_CONTENT, params, outputStream);
return new RdfContentBuilder<>(N_TRIPLE_CONTENT, params, out);
}
@Override
@ -36,12 +39,12 @@ public class NTripleContent implements RdfContent<NTripleContentParams> {
}
@Override
public RdfContentGenerator<NTripleContentParams> createGenerator(NTripleContentParams params, OutputStream outputStream) throws IOException {
return new NTripleContentGenerator(params, outputStream);
public RdfContentGenerator<NTripleContentParams> createGenerator(OutputStream out) throws IOException {
return new NTripleContentGenerator(out);
}
@Override
public RdfContentParser<NTripleContentParams> createParser(NTripleContentParams params, InputStream inputStream) throws IOException {
return new NTripleContentParser<>(params, inputStream);
public RdfContentParser<NTripleContentParams> createParser(InputStream in) throws IOException {
return new NTripleContentParser<>(in);
}
}

View file

@ -24,14 +24,13 @@ public class NTripleContentGenerator
private final Writer writer;
private NTripleContentParams params;
private NTripleContentParams params = NTripleContentParams.N_TRIPLE_CONTENT_PARAMS;
NTripleContentGenerator(NTripleContentParams params, OutputStream out) throws IOException {
this(params, new OutputStreamWriter(out, StandardCharsets.UTF_8));
NTripleContentGenerator(OutputStream out) throws IOException {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
NTripleContentGenerator(NTripleContentParams params, Writer writer) throws IOException {
this.params = params;
NTripleContentGenerator(Writer writer) throws IOException {
this.writer = writer;
}

View file

@ -3,10 +3,18 @@ package org.xbib.content.rdf.io.ntriple;
import org.xbib.content.rdf.RdfContentParams;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class NTripleContentParams implements RdfContentParams {
public static final NTripleContentParams N_TRIPLE_CONTENT_PARAMS = new NTripleContentParams();
private final IRINamespaceContext namespaceContext;
public NTripleContentParams() {
this.namespaceContext = IRINamespaceContext.newInstance();
}
public NTripleContentParams(IRINamespaceContext namespaceContext) {
this.namespaceContext = namespaceContext;
}

View file

@ -52,14 +52,11 @@ public class NTripleContentParser<R extends RdfContentParams> implements RdfCont
private RdfContentBuilder<R> builder;
private NTripleContentParams params;
public NTripleContentParser(NTripleContentParams params, InputStream inputStream) throws IOException {
this(params, new InputStreamReader(inputStream, StandardCharsets.UTF_8));
public NTripleContentParser(InputStream in) throws IOException {
this(new InputStreamReader(in, StandardCharsets.UTF_8));
}
public NTripleContentParser(NTripleContentParams params, Reader reader) {
this.params = params;
public NTripleContentParser(Reader reader) {
this.reader = reader;
}

View file

@ -0,0 +1,4 @@
/**
* Classes for RDF N-Triples.
*/
package org.xbib.content.rdf.io.ntriple;

View file

@ -10,6 +10,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
*/
public class RdfXmlContent implements RdfContent<RdfXmlContentParams> {
private static final RdfXmlContent RDF_XML_CONTENT = new RdfXmlContent();
@ -25,9 +28,9 @@ public class RdfXmlContent implements RdfContent<RdfXmlContentParams> {
return new RdfContentBuilder<>(RDF_XML_CONTENT, params);
}
public static RdfContentBuilder<RdfXmlContentParams> contentBuilder(RdfXmlContentParams params, OutputStream outputStream)
public static RdfContentBuilder<RdfXmlContentParams> contentBuilder(OutputStream out, RdfXmlContentParams params)
throws IOException {
return new RdfContentBuilder<>(RDF_XML_CONTENT, params, outputStream);
return new RdfContentBuilder<>(RDF_XML_CONTENT, params, out);
}
@Override
@ -36,12 +39,12 @@ public class RdfXmlContent implements RdfContent<RdfXmlContentParams> {
}
@Override
public RdfContentGenerator<RdfXmlContentParams> createGenerator(RdfXmlContentParams params, OutputStream outputStream) throws IOException {
return new RdfXmlContentGenerator(params, outputStream);
public RdfContentGenerator<RdfXmlContentParams> createGenerator(OutputStream os) throws IOException {
return new RdfXmlContentGenerator(os);
}
@Override
public RdfContentParser<RdfXmlContentParams> createParser(RdfXmlContentParams params, InputStream inputStream) throws IOException {
return new RdfXmlContentParser<>(params, inputStream);
public RdfContentParser<RdfXmlContentParams> createParser(InputStream in) throws IOException {
return new RdfXmlContentParser<>(in);
}
}

View file

@ -19,6 +19,9 @@ import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* RDF/XML writer.
*/
public class RdfXmlContentGenerator implements RdfContentGenerator<RdfXmlContentParams>, Flushable, RdfConstants {
private final Writer writer;
@ -31,18 +34,18 @@ public class RdfXmlContentGenerator implements RdfContentGenerator<RdfXmlContent
private Resource resource;
private RdfXmlContentParams params;
private RdfXmlContentParams params = RdfXmlContentParams.RDF_XML_CONTENT_PARAMS;
public RdfXmlContentGenerator(RdfXmlContentParams params, OutputStream outputStream) throws IOException {
this(params, new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
public RdfXmlContentGenerator(OutputStream out) throws IOException {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
public RdfXmlContentGenerator(RdfXmlContentParams params, Writer writer) {
this.params = params;
public RdfXmlContentGenerator(Writer writer) {
this.writer = writer;
this.resource = new DefaultAnonymousResource();
}
@Override
public void flush() throws IOException {
writer.flush();

View file

@ -3,10 +3,18 @@ package org.xbib.content.rdf.io.rdfxml;
import org.xbib.content.rdf.RdfContentParams;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class RdfXmlContentParams implements RdfContentParams {
public static final RdfXmlContentParams RDF_XML_CONTENT_PARAMS = new RdfXmlContentParams();
private final IRINamespaceContext namespaceContext;
public RdfXmlContentParams() {
this.namespaceContext = IRINamespaceContext.newInstance();
}
public RdfXmlContentParams(IRINamespaceContext namespaceContext) {
this.namespaceContext = namespaceContext;
}

View file

@ -54,8 +54,6 @@ import javax.xml.parsers.SAXParserFactory;
*/
public class RdfXmlContentParser<R extends RdfContentParams> implements RdfConstants, RdfContentParser<R> {
private R params;
private final Reader reader;
private final Resource resource = new DefaultAnonymousResource();
@ -71,12 +69,11 @@ public class RdfXmlContentParser<R extends RdfContentParams> implements RdfConst
// counter for blank node generation
private int bn = 0;
public RdfXmlContentParser(R params, InputStream inputStream) {
this(params, createReader(inputStream));
public RdfXmlContentParser(InputStream inputStream) {
this(createReader(inputStream));
}
public RdfXmlContentParser(R params, Reader reader) {
this.params = params;
public RdfXmlContentParser(Reader reader) {
this.reader = reader;
}

View file

@ -0,0 +1,4 @@
/**
* Classes for RDF XML.
*/
package org.xbib.content.rdf.io.rdfxml;

View file

@ -3,10 +3,11 @@ package org.xbib.content.rdf.io.source;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
/**
*/
public abstract class BaseStreamProcessor {
public BaseStreamProcessor() {
@ -38,7 +39,7 @@ public abstract class BaseStreamProcessor {
* @throws IOException if process fails
*/
public final void process(String uri, String baseUri) throws IOException {
URL url = URI.create(uri).toURL();
URL url = new URL(uri);
URLConnection urlConnection = url.openConnection();
String mimeType = urlConnection.getContentType();
try (InputStream inputStream = urlConnection.getInputStream()) {

View file

@ -28,9 +28,9 @@ public class TurtleContent implements RdfContent<TurtleContentParams> {
return new RdfContentBuilder<>(TURTLE_CONTENT, params);
}
public static RdfContentBuilder<TurtleContentParams> contentBuilder(TurtleContentParams params, OutputStream outputStream)
public static RdfContentBuilder<TurtleContentParams> contentBuilder(OutputStream out, TurtleContentParams params)
throws IOException {
return new RdfContentBuilder<>(TURTLE_CONTENT, params, outputStream);
return new RdfContentBuilder<>(TURTLE_CONTENT, params, out);
}
@Override
@ -39,13 +39,13 @@ public class TurtleContent implements RdfContent<TurtleContentParams> {
}
@Override
public RdfContentGenerator<TurtleContentParams> createGenerator(TurtleContentParams params, OutputStream outputStream) throws IOException {
return new TurtleContentGenerator(params, outputStream);
public RdfContentGenerator<TurtleContentParams> createGenerator(OutputStream outputStream) throws IOException {
return new TurtleContentGenerator(outputStream);
}
@Override
public RdfContentParser<TurtleContentParams> createParser(TurtleContentParams params, InputStream inputStream) throws IOException {
return new TurtleContentParser<>(params, inputStream);
public RdfContentParser<TurtleContentParams> createParser(InputStream inputStream) throws IOException {
return new TurtleContentParser<>(inputStream);
}
}

View file

@ -53,14 +53,13 @@ public class TurtleContentGenerator implements RdfContentGenerator<TurtleContent
private boolean closed;
private TurtleContentParams params;
private TurtleContentParams params = TurtleContentParams.TURTLE_CONTENT_PARAMS;
public TurtleContentGenerator(TurtleContentParams params, OutputStream outputStream) throws IOException {
this(params, new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
TurtleContentGenerator(OutputStream out) throws IOException {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
public TurtleContentGenerator(TurtleContentParams params, Writer writer) throws IOException {
this.params = params;
TurtleContentGenerator(Writer writer) throws IOException {
this.writer = writer;
this.resource = new DefaultAnonymousResource();
this.nsWritten = false;

View file

@ -3,12 +3,20 @@ package org.xbib.content.rdf.io.turtle;
import org.xbib.content.rdf.RdfContentParams;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class TurtleContentParams implements RdfContentParams {
public static final TurtleContentParams TURTLE_CONTENT_PARAMS = new TurtleContentParams();
private final IRINamespaceContext namespaceContext;
private final boolean writeNamespaceContext;
public TurtleContentParams() {
this.namespaceContext = IRINamespaceContext.newInstance();
this.writeNamespaceContext = true;
}
public TurtleContentParams(IRINamespaceContext namespaceContext, boolean writeNamespaceContext) {
this.namespaceContext = namespaceContext;
this.writeNamespaceContext = writeNamespaceContext;

View file

@ -40,8 +40,6 @@ import java.util.LinkedList;
*/
public class TurtleContentParser<R extends RdfContentParams> implements RdfContentParser<R> {
private TurtleContentParams params;
private final Resource resource = new DefaultAnonymousResource();
private final HashMap<String, Node> bnodes = new HashMap<>();
@ -94,12 +92,11 @@ public class TurtleContentParser<R extends RdfContentParams> implements RdfConte
*/
private NamespaceContext context;
public TurtleContentParser(TurtleContentParams params, InputStream inputStream) throws IOException {
this(params, new InputStreamReader(inputStream, StandardCharsets.UTF_8));
public TurtleContentParser(InputStream in) throws IOException {
this(new InputStreamReader(in, StandardCharsets.UTF_8));
}
public TurtleContentParser(TurtleContentParams params, Reader reader) {
this.params = params;
public TurtleContentParser(Reader reader) {
this.reader = new PushbackReader(reader, 2);
this.context = XmlNamespaceContext.newInstance();
}

View file

@ -23,7 +23,7 @@ import javax.xml.namespace.QName;
public abstract class AbstractXmlHandler<P extends RdfContentParams>
extends DefaultHandler implements XmlHandler<P> {
protected final P params;
protected final RdfContentParams params;
protected final StringBuilder content;
@ -39,14 +39,14 @@ public abstract class AbstractXmlHandler<P extends RdfContentParams>
private int lastlevel;
public AbstractXmlHandler(P params) {
public AbstractXmlHandler(RdfContentParams params) {
this.params = params;
this.content = new StringBuilder();
this.parents = new LinkedList<>();
this.resource = new DefaultAnonymousResource();
}
public P getParams() {
public RdfContentParams getParams() {
return params;
}

View file

@ -20,7 +20,7 @@ public abstract class AbstractXmlResourceHandler<P extends RdfContentParams>
protected final LinkedList<Resource> stack = new LinkedList<>();
public AbstractXmlResourceHandler(P params) {
public AbstractXmlResourceHandler(RdfContentParams params) {
super(params);
}

View file

@ -10,6 +10,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
*/
public class XmlContent implements RdfContent<XmlContentParams> {
private static final XmlContent XML_CONTENT = new XmlContent();
@ -36,12 +39,12 @@ public class XmlContent implements RdfContent<XmlContentParams> {
}
@Override
public RdfContentGenerator<XmlContentParams> createGenerator(XmlContentParams params, OutputStream outputStream) throws IOException {
return new XmlContentGenerator(params, outputStream);
public RdfContentGenerator<XmlContentParams> createGenerator(OutputStream outputStream) throws IOException {
return new XmlContentGenerator(outputStream);
}
@Override
public RdfContentParser<XmlContentParams> createParser(XmlContentParams params, InputStream inputStream) throws IOException {
return new XmlContentParser<>(params, inputStream);
public RdfContentParser<XmlContentParams> createParser(InputStream inputStream) throws IOException {
return new XmlContentParser<>(inputStream);
}
}

View file

@ -36,14 +36,13 @@ public class XmlContentGenerator implements RdfContentGenerator<XmlContentParams
private Resource resource;
private XmlContentParams params;
private XmlContentParams params = XmlContentParams.XML_CONTENT_PARAMS;
public XmlContentGenerator(XmlContentParams params, OutputStream outputStream) {
this(params, new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
public XmlContentGenerator(OutputStream out) {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
public XmlContentGenerator(XmlContentParams params, Writer writer) {
this.params = params;
public XmlContentGenerator(Writer writer) {
this.writer = writer;
}

View file

@ -3,8 +3,13 @@ package org.xbib.content.rdf.io.xml;
import org.xbib.content.rdf.RdfContentParams;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class XmlContentParams implements RdfContentParams {
protected static final IRINamespaceContext NAMESPACE_CONTEXT = IRINamespaceContext.newInstance();
public static final XmlContentParams XML_CONTENT_PARAMS = new XmlContentParams(NAMESPACE_CONTEXT);
private final IRINamespaceContext namespaceContext;
public XmlContentParams(IRINamespaceContext namespaceContext) {

View file

@ -25,8 +25,6 @@ import javax.xml.parsers.SAXParserFactory;
*/
public class XmlContentParser<P extends RdfContentParams> implements RdfContentParser<P> {
private final P params;
private final Reader reader;
private RdfContentBuilder<P> builder;
@ -37,12 +35,11 @@ public class XmlContentParser<P extends RdfContentParams> implements RdfContentP
private boolean validate = false;
public XmlContentParser(P params, InputStream inputStream) {
this(params, new InputStreamReader(inputStream, StandardCharsets.UTF_8));
public XmlContentParser(InputStream in) {
this(new InputStreamReader(in, StandardCharsets.UTF_8));
}
public XmlContentParser(P params, Reader reader) {
this.params = params;
public XmlContentParser(Reader reader) {
this.reader = new NormalizeEolFilter(reader, System.getProperty("line.separator"), true);
}

View file

@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
@ -19,6 +20,14 @@ public class LinkedHashMultiMap<K, V> implements MultiMap<K, V> {
this.map = new LinkedHashMap<>();
}
public LinkedHashMultiMap(MultiMap<K, V> multiMap) {
Objects.requireNonNull(multiMap);
this.map = new LinkedHashMap<>();
for (K k : multiMap.keySet()) {
putAll(k, multiMap.get(k));
}
}
@Override
public int size() {
return map.size();
@ -89,7 +98,7 @@ public class LinkedHashMultiMap<K, V> implements MultiMap<K, V> {
@Override
public boolean equals(Object obj) {
return obj instanceof LinkedHashMultiMap && map.equals(((LinkedHashMultiMap<?, ?>) obj).map);
return obj != null && obj instanceof LinkedHashMultiMap && map.equals(((LinkedHashMultiMap) obj).map);
}
@Override

View file

@ -1,17 +0,0 @@
module org.xbib.content.rdf.test {
requires java.logging;
requires java.xml;
requires org.junit.jupiter.api;
requires org.xbib.content.rdf;
requires org.xbib.content.resource;
requires org.xbib.net;
opens org.xbib.content.rdf.test to org.junit.platform.commons;
opens org.xbib.content.rdf.test.internal to org.junit.platform.commons;
opens org.xbib.content.rdf.test.io to org.junit.platform.commons;
opens org.xbib.content.rdf.test.io.json to org.junit.platform.commons;
opens org.xbib.content.rdf.test.io.ntriple to org.junit.platform.commons;
opens org.xbib.content.rdf.test.io.rdfxml to org.junit.platform.commons;
opens org.xbib.content.rdf.test.io.turtle to org.junit.platform.commons;
opens org.xbib.content.rdf.test.io.xml to org.junit.platform.commons;
opens org.xbib.content.rdf.test.util to org.junit.platform.commons;
}

View file

@ -1,18 +1,22 @@
package org.xbib.content.resource.test;
package org.xbib.content.rdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.xbib.content.resource.IRI;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
*/
public class IRINamespaceCompactionTest {
@Test
public void testCompaction() {
IRINamespaceContext context = IRINamespaceContext.newInstance(getClass());
assertEquals("dc", context.getPrefix("http://purl.org/dc/elements/1.1/"));
assertEquals("http://purl.org/dc/elements/1.1/", context.getNamespaceURI("dc"));
public void testCompaction() throws Exception {
IRINamespaceContext context = IRINamespaceContext.getInstance();
assertEquals(context.getNamespaceURI("dc"), "http://purl.org/dc/elements/1.1/");
assertEquals(context.getPrefix("http://purl.org/dc/elements/1.1/"), "dc");
IRI dc = IRI.create("http://purl.org/dc/elements/1.1/creator");
assertEquals(context.compact(dc), "dc:creator");
}
}

View file

@ -1,4 +1,4 @@
package org.xbib.content.resource.test;
package org.xbib.content.rdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

View file

@ -1,16 +1,12 @@
package org.xbib.content.rdf.test;
package org.xbib.content.rdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.xbib.content.rdf.RdfXContentFactory.rdfXContentBuilder;
import org.junit.jupiter.api.Test;
import org.xbib.content.rdf.RdfContentBuilder;
import org.xbib.content.rdf.RdfXContentParams;
import org.xbib.content.rdf.Resource;
import org.xbib.content.rdf.internal.DefaultLiteral;
import org.xbib.content.rdf.internal.DefaultResource;
import org.xbib.content.resource.IRI;
import org.xbib.content.resource.IRINamespaceContext;
/**
*
@ -25,8 +21,7 @@ public class RdfXContentGeneratorTest {
resource.add("urn:property", "Hello World")
.add("urn:date", l)
.add("urn:link", IRI.create("urn:pointer"));
IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(getClass());
RdfXContentParams params = new RdfXContentParams(namespaceContext);
RdfXContentParams params = new RdfXContentParams();
try (RdfContentBuilder<RdfXContentParams> builder = rdfXContentBuilder(params)) {
builder.receive(resource);
}
@ -45,8 +40,7 @@ public class RdfXContentGeneratorTest {
.add("rdf:type", IRI.create("urn:type1"))
.newResource("urn:embedded")
.add("rdf:type", IRI.create("urn:type2"));
IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(getClass());
RdfXContentParams params = new RdfXContentParams(namespaceContext);
RdfXContentParams params = new RdfXContentParams();
try (RdfContentBuilder<RdfXContentParams> builder = rdfXContentBuilder(params)) {
builder.receive(resource);
}
@ -67,8 +61,7 @@ public class RdfXContentGeneratorTest {
.add("rdf:type", IRI.create("urn:type2"));
resource.newResource("urn:embedded2")
.add("rdf:type", IRI.create("urn:type3"));
IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(getClass());
RdfXContentParams params = new RdfXContentParams(namespaceContext);
RdfXContentParams params = new RdfXContentParams();
try (RdfContentBuilder<RdfXContentParams> builder = rdfXContentBuilder(params)) {
builder.receive(resource);
}
@ -86,8 +79,7 @@ public class RdfXContentGeneratorTest {
.add("urn:date", l)
.add("rdf:type", IRI.create("urn:type1"))
.newResource("urn:embedded"); // empty resource, do not copy
IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(getClass());
RdfXContentParams params = new RdfXContentParams(namespaceContext);
RdfXContentParams params = new RdfXContentParams();
try (RdfContentBuilder<RdfXContentParams> builder = rdfXContentBuilder(params)) {
builder.receive(resource);
}

View file

@ -1,9 +1,9 @@
package org.xbib.content.rdf.test;
package org.xbib.content.rdf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.xbib.content.rdf.RdfXContentFactory.routeRdfXContentBuilder;
import static org.xbib.content.rdf.test.StreamTester.assertStream;
import static org.xbib.content.rdf.StreamTester.assertStream;
import java.text.MessageFormat;
import java.util.concurrent.atomic.AtomicBoolean;
@ -11,14 +11,10 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
import org.xbib.content.rdf.RdfContentBuilder;
import org.xbib.content.rdf.Resource;
import org.xbib.content.rdf.RouteRdfXContentParams;
import org.xbib.content.rdf.internal.DefaultLiteral;
import org.xbib.content.rdf.internal.DefaultResource;
import org.xbib.content.rdf.io.rdfxml.RdfXmlContentParser;
import org.xbib.content.resource.IRI;
import org.xbib.content.resource.IRINamespaceContext;
import java.io.IOException;
import java.io.InputStream;
@ -35,8 +31,7 @@ public class RouteRdfXContentBuilderTest {
resource.add("urn:property", "Hello World")
.add("urn:date", l)
.add("urn:link", IRI.create("urn:pointer"));
IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(getClass());
RouteRdfXContentParams params = new RouteRdfXContentParams(namespaceContext, "index", "type");
RouteRdfXContentParams params = new RouteRdfXContentParams("index", "type");
AtomicBoolean found = new AtomicBoolean();
params.setHandler((content, p) -> {
assertEquals(p.getIndex() + " " + p.getType() + " 1 " + content,
@ -51,23 +46,22 @@ public class RouteRdfXContentBuilderTest {
@Test
public void testVIAF() throws Exception {
try (InputStream inputStream = getClass().getResourceAsStream("VIAF.rdf")) {
if (inputStream == null) {
InputStream in = getClass().getResourceAsStream("VIAF.rdf");
if (in == null) {
throw new IOException("VIAF.rdf not found");
}
StringBuilder sb = new StringBuilder();
IRINamespaceContext namespaceContext = IRINamespaceContext.newInstance(getClass());
RouteRdfXContentParams params = new RouteRdfXContentParams(namespaceContext, "index", "type");
RouteRdfXContentParams params = new RouteRdfXContentParams("index", "type");
AtomicInteger counter = new AtomicInteger();
params.setHandler((content, p) -> {
logger.log(Level.INFO, MessageFormat.format("handle: {0} {1} {2} {3}",
p.getIndex(), p.getType(), p.getId(), content));
counter.incrementAndGet();
});
RdfXmlContentParser<RouteRdfXContentParams> parser = new RdfXmlContentParser<>(params, inputStream);
parser.setRdfContentBuilderProvider(() -> routeRdfXContentBuilder(params))
new RdfXmlContentParser<RouteRdfXContentParams>(in)
.setRdfContentBuilderProvider(() -> routeRdfXContentBuilder(params))
.setRdfContentBuilderHandler(builder -> {
if (!sb.isEmpty()) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(builder.string());
@ -77,5 +71,4 @@ public class RouteRdfXContentBuilderTest {
sb.toString());
assertEquals(5, counter.get());
}
}
}

View file

@ -1,4 +1,4 @@
package org.xbib.content.rdf.test;
package org.xbib.content.rdf;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;

Some files were not shown because too many files have changed in this diff Show more