Skip to content

Commit

Permalink
Merge pull request #53 from fmela/master
Browse files Browse the repository at this point in the history
Fix warnings and simplify canonicalizer code
  • Loading branch information
smola committed Mar 29, 2015
2 parents 4c1f3ab + 09fc72a commit 5e19428
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 343 deletions.
19 changes: 11 additions & 8 deletions src/main/java/io/mola/galimatias/FormURLEncodedParser.java
Expand Up @@ -39,8 +39,11 @@ private FormURLEncodedParser() {

}

private static final FormURLEncodedParser INSTANCE = new FormURLEncodedParser();

@Deprecated
public static FormURLEncodedParser getInstance() {
return new FormURLEncodedParser();
return INSTANCE;
}

/**
Expand All @@ -51,7 +54,7 @@ public static FormURLEncodedParser getInstance() {
*
* @param input
*/
public List<NameValue> parse(final String input) {
public static List<NameValue> parse(final String input) {
return parse(input, Charset.forName("UTF-8"));
}

Expand All @@ -65,7 +68,7 @@ public List<NameValue> parse(final String input) {
* imposes restrictions on what encodings can and cannot be used. This method does not enforce
* such restrictions.
*/
public List<NameValue> parse(final String input, final Charset charset) {
public static List<NameValue> parse(final String input, final Charset charset) {
final boolean isIndex = false;

if (input == null) {
Expand Down Expand Up @@ -148,7 +151,7 @@ public List<NameValue> parse(final String input, final Charset charset) {
return pairs;
}

private String serialize(ByteBuffer bytes) {
private static String serialize(ByteBuffer bytes) {
if (bytes == null) {
throw new NullPointerException("bytes");
}
Expand Down Expand Up @@ -187,7 +190,7 @@ private static String serialize(final byte[] bytes, final int offset, final int
* @param input
* @return
*/
public String encode(final String input) {
public static String encode(final String input) {
// 1. If encoding override is not given, set it to utf-8.
return encode(input, Charset.forName("UTF-8"));
}
Expand All @@ -201,7 +204,7 @@ public String encode(final String input) {
* @param charset
* @return
*/
public String encode(final String input, final Charset charset) {
public static String encode(final String input, final Charset charset) {
return encode(parse(input), charset);
}

Expand All @@ -215,7 +218,7 @@ public String encode(final String input, final Charset charset) {
* @param input
* @return
*/
public String encode(final List<NameValue> input) {
public static String encode(final List<NameValue> input) {
// 1. If encoding override is not given, set it to utf-8.
return encode(input, Charset.forName("UTF-8"));
}
Expand All @@ -227,7 +230,7 @@ public String encode(final List<NameValue> input) {
* @param charset
* @return
*/
public String encode(final List<NameValue> input, final Charset charset) {
public static String encode(final List<NameValue> input, final Charset charset) {
if (input == null) {
throw new NullPointerException("input");
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/io/mola/galimatias/GalimatiasParseException.java
Expand Up @@ -78,23 +78,23 @@ static class Builder {

private Builder() {}

public Builder withMessage(final String message) {
this.message = message;
public Builder withMessage(final String msg) {
this.message = msg;
return this;
}

public Builder withPosition(final int position) {
this.position = position;
public Builder withPosition(final int pos) {
this.position = pos;
return this;
}

public Builder withParseIssue(final ParseIssue parseIssue) {
this.parseIssue = parseIssue;
public Builder withParseIssue(final ParseIssue issue) {
this.parseIssue = issue;
return this;
}

public Builder withCause(final Throwable cause) {
this.cause = cause;
public Builder withCause(final Throwable throwableCause) {
this.cause = throwableCause;
return this;
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/mola/galimatias/IPv4Address.java
Expand Up @@ -32,13 +32,13 @@ public class IPv4Address extends Host {

private final int address;

private IPv4Address(final byte[] addr) {
int address = 0;
address = addr[3] & 0xFF;
address |= ((addr[2] << 8) & 0xFF00);
address |= ((addr[1] << 16) & 0xFF0000);
address |= ((addr[0] << 24) & 0xFF000000);
this.address = address;
private IPv4Address(final byte[] addrBytes) {
int addr = 0;
addr = addrBytes[3] & 0xFF;
addr |= ((addrBytes[2] << 8) & 0xFF00);
addr |= ((addrBytes[1] << 16) & 0xFF0000);
addr |= ((addrBytes[0] << 24) & 0xFF000000);
this.address = addr;
}

public static IPv4Address parseIPv4Address(final String input) throws GalimatiasParseException{
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/mola/galimatias/IPv6Address.java
Expand Up @@ -26,7 +26,6 @@
import java.net.UnknownHostException;
import java.util.Arrays;

import static io.mola.galimatias.URLUtils.byteToHex;
import static io.mola.galimatias.URLUtils.isASCIIDigit;

public class IPv6Address extends Host {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/mola/galimatias/NameValue.java
Expand Up @@ -21,7 +21,6 @@
*/
package io.mola.galimatias;

import java.util.Arrays;

/**
* A name-value pair. Used for query parameters APIs.
Expand Down
80 changes: 40 additions & 40 deletions src/main/java/io/mola/galimatias/URL.java
Expand Up @@ -439,118 +439,118 @@ public static URL buildOpaque(final String scheme) throws GalimatiasParseExcepti
return new URLParser(scheme + ":").parse();
}

public URL withScheme(final String scheme) throws GalimatiasParseException {
if (this.scheme.equalsIgnoreCase(scheme)) {
public URL withScheme(final String newScheme) throws GalimatiasParseException {
if (this.scheme.equalsIgnoreCase(newScheme)) {
return this;
}
if (scheme == null) {
if (newScheme == null) {
throw new NullPointerException("null scheme");
}
if (scheme.isEmpty()) {
if (newScheme.isEmpty()) {
throw new GalimatiasParseException("empty scheme");
}
if (URLUtils.isRelativeScheme(scheme) == URLUtils.isRelativeScheme(this.scheme)) {
return new URLParser(scheme + ":", this, URLParser.ParseURLState.SCHEME_START).parse();
if (URLUtils.isRelativeScheme(newScheme) == URLUtils.isRelativeScheme(this.scheme)) {
return new URLParser(newScheme + ":", this, URLParser.ParseURLState.SCHEME_START).parse();
}
return new URLParser(toString().replaceFirst(this.scheme, scheme)).parse();
return new URLParser(toString().replaceFirst(this.scheme, newScheme)).parse();
}

public URL withUsername(final String username) throws GalimatiasParseException {
public URL withUsername(String newUserName) throws GalimatiasParseException {
if (!isHierarchical) {
throw new GalimatiasParseException("Cannot set username on opaque URL");
}
final String newUsername = (username == null)? "" : new URLParser(username).parseUsername();
if (this.username.equals(newUsername)) {
newUserName = (newUserName == null)? "" : new URLParser(newUserName).parseUsername();
if (this.username.equals(newUserName)) {
return this;
}
return new URL(this.scheme, this.schemeData, newUsername, this.password, this.host, this.port, this.path, this.query, this.fragment, true);
return new URL(this.scheme, this.schemeData, newUserName, this.password, this.host, this.port, this.path, this.query, this.fragment, true);
}

public URL withPassword(final String password) throws GalimatiasParseException {
public URL withPassword(String newPassword) throws GalimatiasParseException {
if (!isHierarchical) {
throw new GalimatiasParseException("Cannot set password on opaque URL");
}
if (this.password != null && this.password.equals(password)) {
if (this.password != null && this.password.equals(newPassword)) {
return this;
}
final String newPassword = (password == null || password.isEmpty())? null : new URLParser(password).parsePassword();
newPassword = (newPassword == null || newPassword.isEmpty())? null : new URLParser(newPassword).parsePassword();
return new URL(this.scheme, this.schemeData, this.username, newPassword, this.host, this.port, this.path, this.query, this.fragment, true);
}

public URL withHost(final String host) throws GalimatiasParseException {
public URL withHost(final String newHost) throws GalimatiasParseException {
if (!isHierarchical) {
throw new GalimatiasParseException("Cannot set host on opaque URL");
}
return withHost(Host.parseHost(host));
return withHost(Host.parseHost(newHost));
}

public URL withHost(final Host host) throws GalimatiasParseException {
public URL withHost(final Host newHost) throws GalimatiasParseException {
if (!isHierarchical) {
throw new GalimatiasParseException("Cannot set host on opaque URL");
}
if (host == null) {
if (newHost == null) {
throw new NullPointerException("null host");
}
if (this.host != null && this.host.equals(host)) {
if (this.host != null && this.host.equals(newHost)) {
return this;
}
return new URL(this.scheme, this.schemeData, this.username, this.password, host, this.port, this.path, this.query, this.fragment, true);
return new URL(this.scheme, this.schemeData, this.username, this.password, newHost, this.port, this.path, this.query, this.fragment, true);
}

public URL withPort(final int port) throws GalimatiasParseException {
public URL withPort(final int newPort) throws GalimatiasParseException {
if (!isHierarchical) {
throw new GalimatiasParseException("Cannot set port on opaque URL");
}
if (port == this.port) {
if (newPort == this.port) {
return this;
}
if (this.port == -1 && port == defaultPort()) {
if (this.port == -1 && newPort == defaultPort()) {
return this;
}
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, port, this.path, this.query, this.fragment, true);
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, newPort, this.path, this.query, this.fragment, true);
}

public URL withPath(final String path) throws GalimatiasParseException {
public URL withPath(final String newPath) throws GalimatiasParseException {
if (!isHierarchical) {
throw new GalimatiasParseException("Cannot set path on opaque URL");
}
return new URLParser(path, this, URLParser.ParseURLState.RELATIVE_PATH_START).parse();
return new URLParser(newPath, this, URLParser.ParseURLState.RELATIVE_PATH_START).parse();
}

public URL withQuery(final String query) throws GalimatiasParseException {
if (this.query == query) {
public URL withQuery(final String newQuery) throws GalimatiasParseException {
if (this.query == newQuery) {
return this;
}
if (this.query != null && this.query.equals(query)) {
if (this.query != null && this.query.equals(newQuery)) {
return this;
}
if (query == null) {
if (newQuery == null) {
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, this.port, this.path, null, this.fragment, true);
}
if (query.isEmpty()) {
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, this.port, this.path, query, this.fragment, true);
if (newQuery.isEmpty()) {
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, this.port, this.path, newQuery, this.fragment, true);
}
final String parseQuery = (query.charAt(0) == '?')? query.substring(1, query.length()) : query;
final String parseQuery = (newQuery.charAt(0) == '?')? newQuery.substring(1, newQuery.length()) : newQuery;
return new URLParser(parseQuery, this, URLParser.ParseURLState.QUERY).parse();
}

public URL withFragment(final String fragment) throws GalimatiasParseException {
public URL withFragment(final String newFragment) throws GalimatiasParseException {
//if ("javascript".equals(scheme)) {
// throw new GalimatiasParseException("Cannot set fragment on 'javascript:' URL");
//}
if (this.fragment == fragment) {
if (this.fragment == newFragment) {
return this;
}
if (this.fragment != null && this.fragment.equals(fragment)) {
if (this.fragment != null && this.fragment.equals(newFragment)) {
return this;
}
if (fragment == null) {
if (newFragment == null) {
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, this.port, this.path, this.query, null, true);
}
if (fragment.isEmpty()) {
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, this.port, this.path, this.query, fragment, true);
if (newFragment.isEmpty()) {
return new URL(this.scheme, this.schemeData, this.username, this.password, this.host, this.port, this.path, this.query, newFragment, true);
}
final String parseFragment = (fragment.charAt(0) == '#')? fragment.substring(1, fragment.length()) : fragment;
final String parseFragment = (newFragment.charAt(0) == '#')? newFragment.substring(1, newFragment.length()) : newFragment;
return new URLParser(parseFragment, this, URLParser.ParseURLState.FRAGMENT).parse();
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/mola/galimatias/URLParser.java
Expand Up @@ -874,7 +874,7 @@ private static enum EncodeSet {
USERNAME
}

private void utf8PercentEncode(final int c, final EncodeSet encodeSet, final StringBuilder buffer) {
private static void utf8PercentEncode(final int c, final EncodeSet encodeSet, final StringBuilder buffer) {
if (encodeSet != null) {
switch (encodeSet) {
case SIMPLE:
Expand Down Expand Up @@ -909,19 +909,19 @@ private void utf8PercentEncode(final int c, final EncodeSet encodeSet, final Str
}
}

private boolean isInSimpleEncodeSet(final int c) {
private static boolean isInSimpleEncodeSet(final int c) {
return c < 0x0020 || c > 0x007E;
}

private boolean isInDefaultEncodeSet(final int c) {
private static boolean isInDefaultEncodeSet(final int c) {
return isInSimpleEncodeSet(c) || c == ' ' || c == '"' || c == '#' || c == '<' || c == '>' || c == '?' || c == '`';
}

private boolean isInPasswordEncodeSet(final int c) {
private static boolean isInPasswordEncodeSet(final int c) {
return isInDefaultEncodeSet(c) || c == '/' || c == '@' || c == '\\';
}

private boolean isInUsernameEncodeSet(final int c) {
private static boolean isInUsernameEncodeSet(final int c) {
return isInPasswordEncodeSet(c) || c == ':';
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/mola/galimatias/URLParsingSettings.java
Expand Up @@ -50,8 +50,8 @@ public static URLParsingSettings create() {
return DEFAULT;
}

public URLParsingSettings withErrorHandler(final ErrorHandler errorHandler) {
return new URLParsingSettings(errorHandler);
public URLParsingSettings withErrorHandler(final ErrorHandler handler) {
return new URLParsingSettings(handler);
}

}
2 changes: 1 addition & 1 deletion src/main/java/io/mola/galimatias/URLSearchParameters.java
Expand Up @@ -34,7 +34,7 @@ public final class URLSearchParameters implements Iterable<NameValue> {

URLSearchParameters(final String query) {
if (query != null && !query.isEmpty()) {
nameValues = Collections.unmodifiableList(FormURLEncodedParser.getInstance().parse(query));
nameValues = Collections.unmodifiableList(FormURLEncodedParser.parse(query));
} else {
nameValues = EMPTY_NAME_VALUES;
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/mola/galimatias/URLUtils.java
Expand Up @@ -25,9 +25,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.IDN;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down

0 comments on commit 5e19428

Please sign in to comment.