Skip to content

Commit

Permalink
Make it compilable
Browse files Browse the repository at this point in the history
  • Loading branch information
filip26 committed Dec 7, 2023
1 parent 43b75f0 commit 4256f83
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/main/java/com/apicatalog/rdf/Rdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.apicatalog.rdf.io.error.UnsupportedContentException;
import com.apicatalog.rdf.lang.XsdConstants;
import com.apicatalog.rdf.spi.RdfProvider;
import com.apicatalog.rdf.uri.UriUtils;

public final class Rdf {

Expand Down Expand Up @@ -125,11 +126,11 @@ public static RdfValue createValue(String value) {
throw new IllegalArgumentException();
}

if (BlankNode.isWellFormed(value)) {
if (UriUtils.isBlanNode(value)) {
return RdfProvider.provider().createBlankNode(value);
}

if (UriUtils.isAbsoluteUri(value, true)) {
if (UriUtils.isAbsoluteUri(value)) {
return RdfProvider.provider().createIRI(value);
}

Expand Down Expand Up @@ -176,11 +177,11 @@ public static RdfResource createResource(String resource) {
throw new IllegalArgumentException("The resource value cannot be null.");
}

if (BlankNode.isWellFormed(resource)) {
if (UriUtils.isBlanNode(resource)) {
return RdfProvider.provider().createBlankNode(resource);
}

if (UriUtils.isAbsoluteUri(resource, true)) {
if (UriUtils.isAbsoluteUri(resource)) {
return RdfProvider.provider().createIRI(resource);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/apicatalog/rdf/io/nquad/NQuadsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.apicatalog.rdf.io.error.RdfReaderException;
import com.apicatalog.rdf.io.nquad.Tokenizer.Token;
import com.apicatalog.rdf.io.nquad.Tokenizer.TokenType;
import com.apicatalog.rdf.uri.UriUtils;

/**
*
Expand Down Expand Up @@ -241,7 +242,7 @@ private void skipWhitespace(int min) throws RdfReaderException {
}

private static final void assertAbsoluteIri(final String iri, final String what) throws RdfReaderException {
if (UriUtils.isNotAbsoluteUri(iri)) {
if (!UriUtils.isAbsoluteUri(iri)) {
throw new RdfReaderException(what + " must be an absolute IRI [" + iri + "]. ");
}
}
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/com/apicatalog/rdf/uri/UriUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.apicatalog.rdf.uri;

import java.util.Arrays;

import com.apicatalog.rdf.lang.RdfAlphabet;

public final class UriUtils {

private UriUtils() {
}

public static final boolean isAbsoluteUri(final String uri) {
// then validate just a scheme
return startsWithScheme(uri);
}

private static final boolean startsWithScheme(final String uri) {

if (uri == null
|| uri.length() < 2 // a scheme must have at least one letter followed by ':'
|| !Character.isLetter(uri.codePointAt(0)) // a scheme name must start with a letter
) {
return false;
}

for (int i = 1; i < uri.length(); i++) {

if (
// a scheme name must start with a letter followed by a letter/digit/+/-/.
Character.isLetterOrDigit(uri.codePointAt(i))
|| uri.charAt(i) == '-' || uri.charAt(i) == '+' || uri.charAt(i) == '.'
) {
continue;
}

// a scheme name must be terminated by ':'
return uri.charAt(i) == ':';
}
return false;
}

/**
* BLANK_NODE_LABEL ::= '_:' (PN_CHARS_U | [0-9]) ((PN_CHARS | '.')* PN_CHARS)?
*
* @see <a href="https://www.w3.org/TR/n-quads/#sec-grammar">N-Quads Grammar</a>
*
* @param blankNodeId to check
* @return <code>true</code> if the provided string is well formed blank node identifier
*/
public static boolean isBlanNode(final String blankNodeId) {

if (blankNodeId == null) {
throw new IllegalArgumentException();
}

if (blankNodeId.length() < 3) {
return false;
}

int[] chars = blankNodeId.codePoints().toArray();

if (chars[0] != '_'
|| chars[1] != ':'
|| (RdfAlphabet.PN_CHARS_U.negate().test(chars[2])
&& RdfAlphabet.ASCII_DIGIT.negate().test(chars[2]))
|| chars[chars.length - 1] == '.'
) {
return false;
}

if (chars.length == 3) {
return true;
}

return Arrays.stream(chars, 3, chars.length - 1).allMatch(RdfAlphabet.PN_CHARS.or(ch -> ch == '.'));
}

}

0 comments on commit 4256f83

Please sign in to comment.