diff --git a/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java b/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java index bb7eb73b..3a6397bf 100644 --- a/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java +++ b/src/main/java/com/apicatalog/jsonld/expansion/UriExpansion.java @@ -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; } diff --git a/src/main/java/com/apicatalog/jsonld/http/link/LinkHeaderParser.java b/src/main/java/com/apicatalog/jsonld/http/link/LinkHeaderParser.java index 1dd6dae1..05a59780 100644 --- a/src/main/java/com/apicatalog/jsonld/http/link/LinkHeaderParser.java +++ b/src/main/java/com/apicatalog/jsonld/http/link/LinkHeaderParser.java @@ -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; @@ -32,7 +33,8 @@ /** * - * @see Appendix B. Algorithms for Parsing Link Header Fields + * @see Appendix B. + * Algorithms for Parsing Link Header Fields * */ final class LinkHeaderParser { @@ -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; @@ -71,50 +77,50 @@ public List 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; } } @@ -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) { @@ -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(); diff --git a/src/main/java/com/apicatalog/jsonld/lang/LanguageTagParser.java b/src/main/java/com/apicatalog/jsonld/lang/LanguageTagParser.java index d9b7d5ab..da3aed16 100644 --- a/src/main/java/com/apicatalog/jsonld/lang/LanguageTagParser.java +++ b/src/main/java/com/apicatalog/jsonld/lang/LanguageTagParser.java @@ -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 RFC 5646. + * Language tags are used to help identify languages and are defined by + * RFC 5646. * * @see RFC 5643 */ final class LanguageTagParser { + static final Pattern LANG_DEL_RE = Pattern.compile("-"); + final String languageTag; final String[] tags; @@ -71,16 +75,14 @@ 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); @@ -88,6 +90,7 @@ private static final LanguageTagParser create(final String languageTag, boolean return new LanguageTagParser(languageTag, tags, verifierMode); } + /** * Parses the language tag. * @@ -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) { @@ -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)) @@ -158,7 +162,8 @@ LanguageTag parse() throws IllegalArgumentException { break; } - while (acceptAlphaNun(2, 8, extension::addTag)); + while (acceptAlphaNun(2, 8, extension::addTag)) + ; tag.addExtension(extension); } @@ -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; } } @@ -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); } } \ No newline at end of file diff --git a/src/main/java/com/apicatalog/jsonld/uri/Path.java b/src/main/java/com/apicatalog/jsonld/uri/Path.java index 1bf66385..81c5223d 100644 --- a/src/main/java/com/apicatalog/jsonld/uri/Path.java +++ b/src/main/java/com/apicatalog/jsonld/uri/Path.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.regex.Pattern; import com.apicatalog.jsonld.StringUtils; @@ -26,6 +27,8 @@ 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 segments; private final String last; private final boolean relative; @@ -41,17 +44,15 @@ public static final Path of(final String path) { final boolean relative = !path.startsWith("/"); final List 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); } @@ -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)); } @@ -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() {