fix code quality as reported by Github CodeQL
This commit is contained in:
parent
f4c8060401
commit
0e72380ee1
16 changed files with 137 additions and 149 deletions
|
@ -3,14 +3,13 @@ package org.xbib.content.io;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class BytesStreamInput extends InputStream {
|
public class BytesStreamInput extends InputStream {
|
||||||
|
|
||||||
private byte[] buf;
|
private final byte[] buf;
|
||||||
private int pos;
|
|
||||||
private int count;
|
private final int count;
|
||||||
|
|
||||||
|
private long pos;
|
||||||
|
|
||||||
public BytesStreamInput(byte[] buf) {
|
public BytesStreamInput(byte[] buf) {
|
||||||
this(buf, 0, buf.length);
|
this(buf, 0, buf.length);
|
||||||
|
@ -37,7 +36,8 @@ public class BytesStreamInput extends InputStream {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
return pos < count ? buf[pos++] & 0xff : -1;
|
int i = (int) pos++;
|
||||||
|
return pos < count ? buf[i] & 0xff : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -48,21 +48,21 @@ public class BytesStreamInput extends InputStream {
|
||||||
if (pos >= count) {
|
if (pos >= count) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int l = len;
|
long l = len;
|
||||||
if (pos + l > count) {
|
if (pos + l > count) {
|
||||||
l = count - pos;
|
l = count - pos;
|
||||||
}
|
}
|
||||||
if (l <= 0) {
|
if (l <= 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
System.arraycopy(buf, pos, b, off, l);
|
System.arraycopy(buf, (int) pos, b, off, (int) l);
|
||||||
pos += l;
|
pos += l;
|
||||||
return l;
|
return (int) l;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reset() throws IOException {
|
public void reset() throws IOException {
|
||||||
pos = 0;
|
pos = 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -13,13 +13,18 @@ import java.nio.charset.Charset;
|
||||||
public final class CharOutputSink implements CharSink {
|
public final class CharOutputSink implements CharSink {
|
||||||
|
|
||||||
private static final short BATCH_SIZE = 256;
|
private static final short BATCH_SIZE = 256;
|
||||||
|
|
||||||
private final Charset charset;
|
private final Charset charset;
|
||||||
|
|
||||||
private Writer writer;
|
private Writer writer;
|
||||||
|
|
||||||
private OutputStream outputStream;
|
private OutputStream outputStream;
|
||||||
|
|
||||||
private boolean closeOnEndStream;
|
private boolean closeOnEndStream;
|
||||||
|
|
||||||
private StringBuilder buffer;
|
private StringBuilder buffer;
|
||||||
|
|
||||||
private short bufferSize;
|
private long bufferSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates class instance with default charset encoding.
|
* Creates class instance with default charset encoding.
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.xbib.content.rdf.io.source;
|
package org.xbib.content.rdf.io.source;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import javax.xml.XMLConstants;
|
||||||
import org.xbib.content.rdf.io.sink.XmlSink;
|
import org.xbib.content.rdf.io.sink.XmlSink;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
@ -9,35 +11,40 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
import javax.xml.parsers.SAXParser;
|
import javax.xml.parsers.SAXParser;
|
||||||
import javax.xml.parsers.SAXParserFactory;
|
import javax.xml.parsers.SAXParserFactory;
|
||||||
|
|
||||||
final class XmlSource extends AbstractSource<XmlSink> {
|
final class XmlSource extends AbstractSource<XmlSink> {
|
||||||
|
|
||||||
private XMLReader xmlReader = null;
|
private XMLReader xmlReader;
|
||||||
|
|
||||||
XmlSource(XmlSink sink) {
|
XmlSource(XmlSink sink) {
|
||||||
super(sink);
|
super(sink);
|
||||||
|
try {
|
||||||
|
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
|
||||||
|
parserFactory.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||||
|
parserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||||
|
parserFactory.setFeature("http://xml.org/sax/features/validation", false);
|
||||||
|
parserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||||
|
parserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||||
|
parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
|
||||||
|
parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||||
|
parserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||||
|
parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||||
|
parserFactory.setXIncludeAware(false);
|
||||||
|
SAXParser saxParser = parserFactory.newSAXParser();
|
||||||
|
setXmlReader(saxParser.getXMLReader());
|
||||||
|
} catch (ParserConfigurationException | SAXException e) {
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static XMLReader getDefaultXmlReader() throws SAXException, ParserConfigurationException {
|
|
||||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
||||||
factory.setNamespaceAware(true);
|
|
||||||
factory.setValidating(false);
|
|
||||||
SAXParser parser = factory.newSAXParser();
|
|
||||||
return parser.getXMLReader();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Reader reader, String mimeType, String baseUri) throws IOException {
|
public void process(Reader reader, String mimeType, String baseUri) throws IOException {
|
||||||
try {
|
try {
|
||||||
initXmlReader();
|
xmlReader.setContentHandler(sink);
|
||||||
} catch (SAXException | ParserConfigurationException e) {
|
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", sink);
|
||||||
throw new IOException("can not instantinate XMLReader", e);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
sink.setBaseUri(baseUri);
|
sink.setBaseUri(baseUri);
|
||||||
xmlReader.parse(new InputSource(reader));
|
xmlReader.parse(new InputSource(reader));
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
|
@ -52,24 +59,12 @@ final class XmlSource extends AbstractSource<XmlSink> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(InputStream inputStream, String mimeType, String baseUri) throws IOException {
|
public void process(InputStream inputStream, String mimeType, String baseUri) throws IOException {
|
||||||
try (Reader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"))) {
|
try (Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
|
||||||
process(reader, mimeType, baseUri);
|
process(reader, mimeType, baseUri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initXmlReader() throws SAXException, ParserConfigurationException {
|
|
||||||
if (xmlReader == null) {
|
|
||||||
xmlReader = getDefaultXmlReader();
|
|
||||||
}
|
|
||||||
xmlReader.setContentHandler(sink);
|
|
||||||
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", sink);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setXmlReader(XMLReader xmlReader) throws SAXException, ParserConfigurationException {
|
public void setXmlReader(XMLReader xmlReader) throws SAXException, ParserConfigurationException {
|
||||||
if (xmlReader == null) {
|
|
||||||
this.xmlReader = getDefaultXmlReader();
|
|
||||||
} else {
|
|
||||||
this.xmlReader = xmlReader;
|
this.xmlReader = xmlReader;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -7,11 +7,28 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.content.resource.IRI;
|
import org.xbib.content.resource.IRI;
|
||||||
import org.xbib.content.resource.IRISyntaxException;
|
import org.xbib.content.resource.IRISyntaxException;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class IRITest {
|
public class IRITest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUserInfo() {
|
||||||
|
IRI iri = IRI.create("http://localhost");
|
||||||
|
assertNull(iri.getUserInfo());
|
||||||
|
assertEquals("localhost", iri.getHost());
|
||||||
|
assertEquals(-1, iri.getPort());
|
||||||
|
iri = IRI.create("http://user@localhost");
|
||||||
|
assertEquals("user", iri.getUserInfo());
|
||||||
|
assertEquals("localhost", iri.getHost());
|
||||||
|
assertEquals(-1, iri.getPort());
|
||||||
|
iri = IRI.create("http://user:password@localhost");
|
||||||
|
assertEquals("user:password", iri.getUserInfo());
|
||||||
|
assertEquals("localhost", iri.getHost());
|
||||||
|
assertEquals(-1, iri.getPort());
|
||||||
|
iri = IRI.create("http://user:password@localhost:1234");
|
||||||
|
assertEquals("user:password", iri.getUserInfo());
|
||||||
|
assertEquals("localhost", iri.getHost());
|
||||||
|
assertEquals(1234, iri.getPort());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJsonLd() {
|
public void testJsonLd() {
|
||||||
IRI iri = IRI.create("@context");
|
IRI iri = IRI.create("@context");
|
||||||
|
|
|
@ -12,13 +12,7 @@ import org.xbib.content.resource.IRI;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class RouteRdfXContentBuilderTest {
|
public class RouteRdfXContentBuilderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -57,7 +51,7 @@ public class RouteRdfXContentBuilderTest {
|
||||||
sb.append(builder.string());
|
sb.append(builder.string());
|
||||||
})
|
})
|
||||||
.parse();
|
.parse();
|
||||||
assertStream(new InputStreamReader(getClass().getResourceAsStream("viaf.json"), StandardCharsets.UTF_8),
|
assertStream("viaf.json", getClass().getResourceAsStream("viaf.json"),
|
||||||
new StringReader(sb.toString()));
|
sb.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,55 +1,62 @@
|
||||||
package org.xbib.content.rdf;
|
package org.xbib.content.rdf;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.Channels;
|
||||||
|
import java.nio.channels.ReadableByteChannel;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class StreamTester {
|
public class StreamTester {
|
||||||
|
|
||||||
public static void assertStream(InputStream expected, InputStream actual) {
|
public static void assertStream(String name, Path path1, Path path2) throws IOException {
|
||||||
int offset = 0;
|
assertStream(name, Files.newInputStream(path1), Files.newInputStream(path2));
|
||||||
try {
|
|
||||||
while (true) {
|
|
||||||
final int exp = expected.read();
|
|
||||||
if (exp == -1) {
|
|
||||||
assertEquals(-1, actual.read());
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
final int act = actual.read();
|
|
||||||
assertEquals(exp, act);
|
|
||||||
}
|
|
||||||
offset++;
|
|
||||||
}
|
|
||||||
expected.close();
|
|
||||||
actual.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
fail("Exception at offset " + offset + ": " + e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void assertStream(Reader expected, Reader actual) {
|
public static void assertStream(String name, Path path, InputStream expected) throws IOException {
|
||||||
int offset = 0;
|
assertStream(name, expected, Files.newInputStream(path));
|
||||||
try {
|
|
||||||
while (true) {
|
|
||||||
final int exp = expected.read();
|
|
||||||
if (exp == -1) {
|
|
||||||
assertEquals(-1, actual.read(), "Expecting end of actual stream at offset " + offset);
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
final int act = actual.read();
|
|
||||||
assertEquals(exp, act, "Expecting same data at offset " + offset);
|
|
||||||
}
|
|
||||||
offset++;
|
|
||||||
}
|
|
||||||
expected.close();
|
|
||||||
actual.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
fail("Exception at offset " + offset + ": " + e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void assertStream(String name, InputStream expected, String actual) throws IOException {
|
||||||
|
assertStream(name, expected, new ByteArrayInputStream(actual.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void assertStream(String name, InputStream expected, InputStream actual) throws IOException {
|
||||||
|
int offset = 0;
|
||||||
|
try (ReadableByteChannel ch1 = Channels.newChannel(expected);
|
||||||
|
ReadableByteChannel ch2 = Channels.newChannel(actual)) {
|
||||||
|
ByteBuffer buf1 = ByteBuffer.allocateDirect(4096);
|
||||||
|
ByteBuffer buf2 = ByteBuffer.allocateDirect(4096);
|
||||||
|
while (true) {
|
||||||
|
int n1 = ch1.read(buf1);
|
||||||
|
int n2 = ch2.read(buf2);
|
||||||
|
if (n1 == -1 || n2 == -1) {
|
||||||
|
if (n1 != n2) {
|
||||||
|
fail(name + ": stream length mismatch: " + n1 + " != " + n2 + " offset=" + offset);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf1.flip();
|
||||||
|
buf2.flip();
|
||||||
|
for (int i = 0; i < Math.min(n1, n2); i++) {
|
||||||
|
int b1 = buf1.get() & 0xFF;
|
||||||
|
int b2 = buf2.get() & 0xFF;
|
||||||
|
if (b1 != b2) {
|
||||||
|
fail(name + ": mismatch at offset " + (offset + i)
|
||||||
|
+ " (" + Integer.toHexString(b1)
|
||||||
|
+ " != " + Integer.toHexString(b2) + ")"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf1.compact();
|
||||||
|
buf2.compact();
|
||||||
|
offset += Math.min(n1, n2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,6 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class EuropeanaEDMReaderTest extends StreamTester {
|
public class EuropeanaEDMReaderTest extends StreamTester {
|
||||||
|
|
||||||
private static final IRI GEO_LAT = IRI.create("http://www.w3.org/2003/01/geo/wgs84_pos#lat");
|
private static final IRI GEO_LAT = IRI.create("http://www.w3.org/2003/01/geo/wgs84_pos#lat");
|
||||||
|
@ -47,8 +44,7 @@ public class EuropeanaEDMReaderTest extends StreamTester {
|
||||||
Resource resource = resourceIterator.next();
|
Resource resource = resourceIterator.next();
|
||||||
builder.receive(resource);
|
builder.receive(resource);
|
||||||
}
|
}
|
||||||
//System.err.println(builder.string());
|
assertStream("edm.nt", getClass().getResource("edm.nt").openStream(),
|
||||||
assertStream(getClass().getResource("edm.nt").openStream(),
|
|
||||||
builder.streamInput());
|
builder.streamInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,7 @@ import org.xbib.content.resource.IRINamespaceContext;
|
||||||
import org.xbib.content.rdf.StreamTester;
|
import org.xbib.content.rdf.StreamTester;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class GNDRdfXmlReaderTest extends StreamTester {
|
public class GNDRdfXmlReaderTest extends StreamTester {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -28,8 +23,7 @@ public class GNDRdfXmlReaderTest extends StreamTester {
|
||||||
reader.setRdfContentBuilderProvider(() -> turtleBuilder(params));
|
reader.setRdfContentBuilderProvider(() -> turtleBuilder(params));
|
||||||
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
||||||
reader.parse();
|
reader.parse();
|
||||||
assertStream(new InputStreamReader(getClass().getResourceAsStream("gnd.ttl")),
|
assertStream("gnd.ttl", getClass().getResourceAsStream("gnd.ttl"), sb.toString());
|
||||||
new StringReader(sb.toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -42,8 +36,7 @@ public class GNDRdfXmlReaderTest extends StreamTester {
|
||||||
reader.setRdfContentBuilderProvider(() -> turtleBuilder(params));
|
reader.setRdfContentBuilderProvider(() -> turtleBuilder(params));
|
||||||
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
||||||
reader.parse();
|
reader.parse();
|
||||||
assertStream(new InputStreamReader(getClass().getResourceAsStream("gnd.ttl")),
|
assertStream("gnd.ttl", getClass().getResourceAsStream("gnd.ttl"), sb.toString());
|
||||||
new StringReader(sb.toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -55,8 +48,7 @@ public class GNDRdfXmlReaderTest extends StreamTester {
|
||||||
reader.setRdfContentBuilderProvider(RdfContentFactory::ntripleBuilder);
|
reader.setRdfContentBuilderProvider(RdfContentFactory::ntripleBuilder);
|
||||||
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
||||||
reader.parse();
|
reader.parse();
|
||||||
assertStream(new InputStreamReader(getClass().getResourceAsStream("GND.nt")),
|
assertStream("GND.nt", getClass().getResourceAsStream("GND.nt"), sb.toString());
|
||||||
new StringReader(sb.toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,7 @@ import org.xbib.content.resource.IRINamespaceContext;
|
||||||
import org.xbib.content.rdf.StreamTester;
|
import org.xbib.content.rdf.StreamTester;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class RdfXmlReaderTest extends StreamTester {
|
public class RdfXmlReaderTest extends StreamTester {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -29,8 +23,6 @@ public class RdfXmlReaderTest extends StreamTester {
|
||||||
reader.setRdfContentBuilderProvider(() -> turtleBuilder(params));
|
reader.setRdfContentBuilderProvider(() -> turtleBuilder(params));
|
||||||
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
reader.setRdfContentBuilderHandler(builder -> sb.append(builder.string()));
|
||||||
reader.parse();
|
reader.parse();
|
||||||
assertStream(new InputStreamReader(getClass().getResourceAsStream("118540238.ttl"), StandardCharsets.UTF_8),
|
assertStream("118540238.ttl", getClass().getResourceAsStream("118540238.ttl"), sb.toString());
|
||||||
new StringReader(sb.toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,7 @@ import org.xbib.content.rdf.StreamTester;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class VIAFRdfXmlReaderTest extends StreamTester {
|
public class VIAFRdfXmlReaderTest extends StreamTester {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -29,7 +24,6 @@ public class VIAFRdfXmlReaderTest extends StreamTester {
|
||||||
.setRdfContentBuilderProvider(() -> turtleBuilder(params))
|
.setRdfContentBuilderProvider(() -> turtleBuilder(params))
|
||||||
.setRdfContentBuilderHandler(builder -> sb.append(builder.string()))
|
.setRdfContentBuilderHandler(builder -> sb.append(builder.string()))
|
||||||
.parse();
|
.parse();
|
||||||
assertStream(new InputStreamReader(getClass().getResource("viaf.ttl").openStream()),
|
assertStream("viaf.ttl", getClass().getResource("viaf.ttl").openStream(), sb.toString());
|
||||||
new StringReader(sb.toString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,7 @@ public class TurtleTest extends StreamTester {
|
||||||
TurtleContentParams params = new TurtleContentParams(context, false);
|
TurtleContentParams params = new TurtleContentParams(context, false);
|
||||||
RdfContentBuilder<TurtleContentParams> builder = turtleBuilder(params);
|
RdfContentBuilder<TurtleContentParams> builder = turtleBuilder(params);
|
||||||
builder.receive(resource);
|
builder.receive(resource);
|
||||||
assertStream(getClass().getResource("turtle-test.ttl").openStream(), builder.streamInput());
|
assertStream("turtle-test.ttl", getClass().getResource("turtle-test.ttl").openStream(), builder.streamInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Resource createResource2() {
|
private Resource createResource2() {
|
||||||
|
@ -142,7 +142,7 @@ public class TurtleTest extends StreamTester {
|
||||||
TurtleContentParams params = new TurtleContentParams(IRINamespaceContext.getInstance(), false);
|
TurtleContentParams params = new TurtleContentParams(IRINamespaceContext.getInstance(), false);
|
||||||
RdfContentBuilder<TurtleContentParams> builder = turtleBuilder(params);
|
RdfContentBuilder<TurtleContentParams> builder = turtleBuilder(params);
|
||||||
builder.receive(resource);
|
builder.receive(resource);
|
||||||
assertStream(getClass().getResourceAsStream("turtle-indent.ttl"),
|
assertStream("turtle-indent.ttl", getClass().getResourceAsStream("turtle-indent.ttl"),
|
||||||
builder.streamInput());
|
builder.streamInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +180,7 @@ public class TurtleTest extends StreamTester {
|
||||||
TurtleContentParams params = new TurtleContentParams(IRINamespaceContext.getInstance(), false);
|
TurtleContentParams params = new TurtleContentParams(IRINamespaceContext.getInstance(), false);
|
||||||
RdfContentBuilder<TurtleContentParams> builder = turtleBuilder(params);
|
RdfContentBuilder<TurtleContentParams> builder = turtleBuilder(params);
|
||||||
builder.receive(resource);
|
builder.receive(resource);
|
||||||
assertStream(getClass().getResourceAsStream("deep-nested.ttl"),
|
assertStream("deep-nested.ttl", getClass().getResourceAsStream("deep-nested.ttl"),
|
||||||
builder.streamInput());
|
builder.streamInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,6 @@ public class OAITest extends StreamTester {
|
||||||
XmlContentParser<TurtleContentParams> parser = new XmlContentParser<>(in);
|
XmlContentParser<TurtleContentParams> parser = new XmlContentParser<>(in);
|
||||||
parser.builder(builder);
|
parser.builder(builder);
|
||||||
parser.setHandler(xmlHandler).parse();
|
parser.setHandler(xmlHandler).parse();
|
||||||
assertStream(getClass().getResourceAsStream("oai.ttl"), builder.streamInput());
|
assertStream("oai.ttl", getClass().getResourceAsStream("oai.ttl"), builder.streamInput());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,7 @@ public class XmlReaderTest extends StreamTester {
|
||||||
new XmlContentParser<TurtleContentParams>(in)
|
new XmlContentParser<TurtleContentParams>(in)
|
||||||
.setHandler(xmlHandler)
|
.setHandler(xmlHandler)
|
||||||
.parse();
|
.parse();
|
||||||
assertStream(getClass().getResource("dc.ttl").openStream(),
|
assertStream("dc.ttl", getClass().getResource("dc.ttl").openStream(),
|
||||||
builder.streamInput());
|
builder.streamInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,16 +17,11 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class IRI implements Comparable<IRI>, Node {
|
public class IRI implements Comparable<IRI>, Node {
|
||||||
|
|
||||||
private static final SchemeRegistry registry = SchemeRegistry.getInstance();
|
private static final SchemeRegistry registry = SchemeRegistry.getInstance();
|
||||||
private static final Pattern IRIPATTERN =
|
private static final Pattern IRIPATTERN =
|
||||||
Pattern.compile("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?");
|
Pattern.compile("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?");
|
||||||
private static final Pattern AUTHORITYPATTERN =
|
|
||||||
Pattern.compile("^(?:(.*)?@)?((?:\\[.*\\])|(?:[^:]*))?(?::(\\d+))?");
|
|
||||||
private Scheme schemeClass;
|
private Scheme schemeClass;
|
||||||
private String scheme;
|
private String scheme;
|
||||||
private String schemeSpecificPart;
|
private String schemeSpecificPart;
|
||||||
|
@ -552,16 +547,13 @@ public class IRI implements Comparable<IRI>, Node {
|
||||||
|
|
||||||
private void parseAuthority() {
|
private void parseAuthority() {
|
||||||
if (authority != null) {
|
if (authority != null) {
|
||||||
Matcher auth = AUTHORITYPATTERN.matcher(authority);
|
// [ <userinfo> '@' ] <host> [ ':' <port> ]
|
||||||
if (auth.find()) {
|
int pos = authority.lastIndexOf('@');
|
||||||
userinfo = auth.group(1);
|
userinfo = pos >= 0 ? authority.substring(0, pos) : null;
|
||||||
host = auth.group(2);
|
String s = pos >= 0 ? authority.substring(pos + 1) : authority;
|
||||||
if (auth.group(3) != null) {
|
pos = s.indexOf(':');
|
||||||
port = Integer.parseInt(auth.group(3));
|
host = pos >= 0 ? s.substring(0, pos) : s;
|
||||||
} else {
|
port = pos >= 0 ? Integer.parseInt(s.substring(pos + 1)) : -1;
|
||||||
port = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
CharUtils.verify(userinfo, Profile.IUSERINFO);
|
CharUtils.verify(userinfo, Profile.IUSERINFO);
|
||||||
CharUtils.verify(host, Profile.IHOST);
|
CharUtils.verify(host, Profile.IHOST);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.xbib.content.xml.util;
|
package org.xbib.content.xml.util;
|
||||||
|
|
||||||
|
import javax.xml.XMLConstants;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.SAXNotRecognizedException;
|
import org.xml.sax.SAXNotRecognizedException;
|
||||||
|
@ -18,7 +19,7 @@ import javax.xml.parsers.SAXParserFactory;
|
||||||
*/
|
*/
|
||||||
public class XMLFilterReader extends XMLFilterImpl {
|
public class XMLFilterReader extends XMLFilterImpl {
|
||||||
|
|
||||||
private SAXParser parser;
|
private final SAXParser parser;
|
||||||
|
|
||||||
public XMLFilterReader() {
|
public XMLFilterReader() {
|
||||||
try {
|
try {
|
||||||
|
@ -30,6 +31,9 @@ public class XMLFilterReader extends XMLFilterImpl {
|
||||||
parserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
parserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||||
parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
|
parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
|
||||||
parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||||
|
parserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||||
|
parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||||
|
parserFactory.setXIncludeAware(false);
|
||||||
parser = parserFactory.newSAXParser();
|
parser = parserFactory.newSAXParser();
|
||||||
} catch (ParserConfigurationException | SAXException e) {
|
} catch (ParserConfigurationException | SAXException e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
group = org.xbib
|
group = org.xbib
|
||||||
name = content
|
name = content
|
||||||
version = 5.0.0
|
version = 5.0.1
|
||||||
|
|
||||||
org.gradle.warning.mode = ALL
|
org.gradle.warning.mode = ALL
|
||||||
|
|
Loading…
Reference in a new issue