Skip to content

Commit

Permalink
RE minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
filip26 committed Dec 5, 2023
1 parent d20122a commit 0bdecb6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 95 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,18 @@ public String expand(final String value) throws JsonLdError {
String result = value;

// 6. If value contains a colon (:) anywhere after the first character, it is
// either an IRI,
// a compact IRI, or a blank node identifier
final int splitIndex = result.indexOf(':', 1);

if (splitIndex != -1) {

// 6.1. Split value into a prefix and suffix at the first occurrence of a colon
// (:).
// 6.1. Split value into a prefix and suffix
// at the first occurrence of a colon (:).
// 6.2. If prefix is underscore (_) or suffix begins with double-forward-slash
// (//),
// return value as it is already an IRI or a blank node identifier.
if (result.charAt(0) == '_' || (((splitIndex + 2) < result.length())
&& result.charAt(splitIndex + 1) == '/'
&& result.charAt(splitIndex + 2) == '/')) {
// (//), return value as it is already an IRI or a blank node identifier.
if ((splitIndex == 1 && result.charAt(0) == '_')
|| (((splitIndex + 2) < result.length())
&& result.charAt(splitIndex + 1) == '/'
&& result.charAt(splitIndex + 2) == '/')) {
return result;
}

Expand Down
103 changes: 54 additions & 49 deletions src/main/java/com/apicatalog/jsonld/http/link/LinkHeaderParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import com.apicatalog.jsonld.StringUtils;
import com.apicatalog.jsonld.http.HttpAlphabet;
Expand All @@ -32,7 +33,8 @@

/**
*
* @see <a href="https://tools.ietf.org/html/rfc8288#appendix-B">Appendix B. Algorithms for Parsing Link Header Fields</a>
* @see <a href="https://tools.ietf.org/html/rfc8288#appendix-B">Appendix B.
* Algorithms for Parsing Link Header Fields</a>
*
*/
final class LinkHeaderParser {
Expand All @@ -41,8 +43,12 @@ final class LinkHeaderParser {
private static final String ANCHOR = "anchor";
private static final String TYPE = "type";

private enum State { INIT, URI_REF, PARAMS, PARAM_NAME_BEGIN, PARAM_NAME, PARAM_NAME_END, PARAM_VALUE,
STRING_VALUE, LITERAL_VALUE, ESCAPE, UNEXPECTED }
private static final Pattern REL_SPLIT_RE = Pattern.compile("[\\s\\t]+");

private enum State {
INIT, URI_REF, PARAMS, PARAM_NAME_BEGIN, PARAM_NAME, PARAM_NAME_END, PARAM_VALUE,
STRING_VALUE, LITERAL_VALUE, ESCAPE, UNEXPECTED
}

private URI baseUri;

Expand Down Expand Up @@ -71,50 +77,50 @@ public List<Link> parse(String httpLink) {
for (final char ch : linkHeader) {

switch (state) {
case INIT:
initParser(ch);
break;

case URI_REF:
parseTargetUri(ch);
break;

case PARAMS:
parseParameters(ch);
break;

case PARAM_NAME_BEGIN:
parseParamNameBegin(ch);
break;

case PARAM_NAME:
parseParamName(ch);
break;

case PARAM_NAME_END:
parseParamNameEnd(ch);
break;

case PARAM_VALUE:
parseParamValue(ch);
break;

case LITERAL_VALUE:
parseLiteral(ch);
break;

case STRING_VALUE:
parseString(ch);
break;

case ESCAPE:
escape(ch);
break;

default:
addParameter();
addLink();
return links;
case INIT:
initParser(ch);
break;

case URI_REF:
parseTargetUri(ch);
break;

case PARAMS:
parseParameters(ch);
break;

case PARAM_NAME_BEGIN:
parseParamNameBegin(ch);
break;

case PARAM_NAME:
parseParamName(ch);
break;

case PARAM_NAME_END:
parseParamNameEnd(ch);
break;

case PARAM_VALUE:
parseParamValue(ch);
break;

case LITERAL_VALUE:
parseLiteral(ch);
break;

case STRING_VALUE:
parseString(ch);
break;

case ESCAPE:
escape(ch);
break;

default:
addParameter();
addLink();
return links;
}
}

Expand Down Expand Up @@ -158,7 +164,7 @@ private final void addLink() {
MediaType type = null;

if (attributes.containsKey(REL) && attributes.get(REL) != null) {
rel = new HashSet<>(Arrays.asList(StringUtils.strip(attributes.get(REL).get(0).value()).split("[\\s\\t]+")));
rel = new HashSet<>(Arrays.asList(REL_SPLIT_RE.split(StringUtils.strip(attributes.get(REL).get(0).value()))));
attributes.remove(REL);
}
if (attributes.containsKey(ANCHOR) && attributes.get(ANCHOR) != null) {
Expand Down Expand Up @@ -346,7 +352,6 @@ private final void parseParamValue(final char ch) {
state = State.UNEXPECTED;
}


private final void parseString(final char ch) {
if (ch == '"') {
attributeValue = valueBuilder.toString();
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/com/apicatalog/jsonld/lang/LanguageTagParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.IntPredicate;
import java.util.regex.Pattern;

import com.apicatalog.jsonld.lang.LanguageTag.Extension;
import com.apicatalog.rdf.lang.RdfAlphabet;

/**
* Language tags are used to help identify languages and are defined by <code>RFC 5646</code>.
* Language tags are used to help identify languages and are defined by
* <code>RFC 5646</code>.
*
* @see <a href="https://datatracker.ietf.org/doc/html/rfc5646">RFC 5643</a>
*/
final class LanguageTagParser {

static final Pattern LANG_DEL_RE = Pattern.compile("-");

final String languageTag;
final String[] tags;

Expand Down Expand Up @@ -71,23 +75,22 @@ private static final LanguageTagParser create(final String languageTag, boolean

final String stripped = languageTag.trim();

// must start with ALPHA and ends with ALPHANUM
// must start with ALPHA and ends with ALPHANUM
if (stripped.length() == 0
|| RdfAlphabet.ASCII_ALPHA.negate().test(stripped.codePointAt(0))
|| RdfAlphabet.ASCII_ALPHA_NUM.negate().test(stripped.codePointAt(stripped.length() - 1))
) {
|| RdfAlphabet.ASCII_ALPHA_NUM.negate().test(stripped.codePointAt(stripped.length() - 1))) {
return new LanguageTagParser(languageTag, null, verifierMode);
}

final String[] tags = stripped.split("-");

final String[] tags = LANG_DEL_RE.split(stripped);

if (tags == null || tags.length == 0) {
return new LanguageTagParser(languageTag, null, verifierMode);
}

return new LanguageTagParser(languageTag, tags, verifierMode);
}

/**
* Parses the language tag.
*
Expand Down Expand Up @@ -116,10 +119,10 @@ LanguageTag parse() throws IllegalArgumentException {
acceptAlpha(3, tag::addLanguageExtension);
}

// reserved 4ALPHA or registered for future use 5*8ALPHA
// reserved 4ALPHA or registered for future use 5*8ALPHA
} else if (acceptAlpha(4, 8, tag::setLanguage)) {

// private use
// private use
} else if (acceptPrivateUse(tag)) {

if (tagIndex != tags.length) {
Expand All @@ -136,14 +139,15 @@ LanguageTag parse() throws IllegalArgumentException {
acceptAlpha(4, tag::setScript); // script = 4ALPHA

// ["-" region]
if (!acceptAlpha(2, tag::setRegion)) { // region = 2ALPHA | 3DIGIT
if (!acceptAlpha(2, tag::setRegion)) { // region = 2ALPHA | 3DIGIT
acceptDigit(3, tag::setRegion);
}

// *("-" variant)
// variant = 5*8alphanum | (DIGIT 3alphanum)
while (acceptAlphaNun(5, 8, tag::addVariant)
|| (digitRange(0, 1) && alphaNumRange(1, 3) && accept(4, tag::addVariant)));
|| (digitRange(0, 1) && alphaNumRange(1, 3) && accept(4, tag::addVariant)))
;

// *("-" extension)
// extension = singleton 1*("-" (2*8alphanum))
Expand All @@ -158,7 +162,8 @@ LanguageTag parse() throws IllegalArgumentException {
break;
}

while (acceptAlphaNun(2, 8, extension::addTag));
while (acceptAlphaNun(2, 8, extension::addTag))
;

tag.addExtension(extension);
}
Expand All @@ -182,7 +187,8 @@ boolean acceptPrivateUse(final LanguageTag tag) {
tagIndex--;

} else {
while (acceptAlphaNun(1, 8, tag::addPrivateUse));
while (acceptAlphaNun(1, 8, tag::addPrivateUse))
;
return true;
}
}
Expand Down Expand Up @@ -259,11 +265,9 @@ boolean digitRange(int index, int length) {
}

boolean range(int index, int length, IntPredicate predicate) {
return
tagIndex < tags.length
&& index < tags[tagIndex].length()
&& (index + length) <= tags[tagIndex].length()
&& tags[tagIndex].substring(index, index + length).chars().allMatch(predicate)
;
return tagIndex < tags.length
&& index < tags[tagIndex].length()
&& (index + length) <= tags[tagIndex].length()
&& tags[tagIndex].substring(index, index + length).chars().allMatch(predicate);
}
}
38 changes: 19 additions & 19 deletions src/main/java/com/apicatalog/jsonld/uri/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import com.apicatalog.jsonld.StringUtils;

public final class Path {

public static final Path EMPTY = new Path(new ArrayList<>(), null, true);

private static final Pattern SEGMENTS_DEL_RE = Pattern.compile("/");

private final List<String> segments;
private final String last;
private final boolean relative;
Expand All @@ -41,17 +44,15 @@ public static final Path of(final String path) {
final boolean relative = !path.startsWith("/");

final List<String> segments = new ArrayList<>(
Arrays.asList(
(relative
? path
: path.substring(1)
)
.split("/")
)
);
Arrays.asList(
SEGMENTS_DEL_RE.split(
(relative
? path
: path.substring(1)))));

final String last = (path.length() > 1 && path.endsWith("/"))
? null
: segments.remove(segments.size() - 1);
? null
: segments.remove(segments.size() - 1);

return new Path(segments, (last == null || StringUtils.isBlank(last)) ? null : last, relative);
}
Expand Down Expand Up @@ -105,13 +106,13 @@ public Path relativize(final Path base) {
if (!segments.get(segments.size() - rightIndex - 1).equals(base.segments.get(base.segments.size() - rightIndex - 1))) {
break;
}
diff.add(".."); //TOD ?!?
diff.add(".."); // TOD ?!?
}
for (int i=0; i < (base.segments.size() - leftIndex - rightIndex); i++) {
for (int i = 0; i < (base.segments.size() - leftIndex - rightIndex); i++) {
diff.add("..");
}

for (int i=0; i < (segments.size() - leftIndex - rightIndex ); i++) {
for (int i = 0; i < (segments.size() - leftIndex - rightIndex); i++) {
diff.add(segments.get(i + leftIndex));
}

Expand All @@ -133,12 +134,11 @@ public boolean isRelative() {
@Override
public String toString() {
return (relative
? ""
: "/")
.concat(String.join("/", segments))
.concat(segments.isEmpty() ? "" : "/")
.concat(last != null ? last : "")
;
? ""
: "/")
.concat(String.join("/", segments))
.concat(segments.isEmpty() ? "" : "/")
.concat(last != null ? last : "");
}

public String getLeaf() {
Expand Down

0 comments on commit 0bdecb6

Please sign in to comment.