Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created March 17, 2012 12:49
Show Gist options
  • Save laclefyoshi/2058551 to your computer and use it in GitHub Desktop.
Save laclefyoshi/2058551 to your computer and use it in GitHub Desktop.
class for manipulating URL
package org.saekiyoshiyasu.common;
/**
Copyright: (c) SAEKI Yoshiyasu
License : MIT-style license
<http://www.opensource.org/licenses/mit-license.php>
last updated: 2012/03/16
**/
import java.net.URL;
import java.net.MalformedURLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import org.saekiyoshiyasu.common.Pair;
/**
* class for manuplating URL
* Usage:
* MyURL url = new MyURL("http://user:pass@example.org:8888/cgi/foo.php?bar=1#head");
*/
public class MyURL {
private String scheme = null;
private String userinfo = null;
private String host = null;
private int port = -1;
private String path = null;
private List<Pair<String, String> > query = null;
private String queryAsString = null;
private String fragment = null;
public MyURL(String url) throws MalformedURLException {
URL basedURL = new URL(url);
this.scheme = basedURL.getProtocol();
this.userinfo = basedURL.getUserInfo();
this.host = basedURL.getHost();
this.port = basedURL.getPort();
this.path = basedURL.getPath();
this.queryAsString = basedURL.getQuery();
this.query = splitPairs(queryAsString);
this.fragment = basedURL.getRef();
}
public MyURL(URL url) throws MalformedURLException {
this(url.toString());
}
public String toString() {
String resultURL = scheme() + "://";
if(userinfo() != null && ! "".equals(userinfo())) {
resultURL += userinfo() + "@";
}
resultURL += host();
if(port() > 0) {
resultURL += ":" + port();
}
resultURL += path();
if(queryAsString() != null && ! "".equals(queryAsString())) {
resultURL += "?" + queryAsString();
}
if(fragment() != null && ! "".equals(fragment())) {
resultURL += "#" + fragment();
}
try {
URL url = new URL(resultURL);
} catch(MalformedURLException murle) {
murle.printStackTrace();
System.err.println("URL generate error!");
showElements();
resultURL = null;
}
return resultURL;
}
public URL toURL() throws MalformedURLException {
return new URL(toString());
}
public String scheme() { return scheme; }
public String userinfo() { return userinfo; }
public String host() { return host; }
public int port() { return port; }
public String path() { return path; }
public List<Pair<String, String> > query() { return query; }
public String queryAsString() { return queryAsString; }
public String fragment() { return fragment; }
public void setScheme(String scheme) { this.scheme = scheme; }
public void setUserinfo(String userinfo) { this.userinfo = userinfo; }
public void setHost(String host) { this.host = host; }
public void setPort(int port) { this.port = port; }
public void setPath(String path) { this.path = path; }
public void setQuery(List<Pair<String, String> > query) {
this.query = query;
this.queryAsString = joinPairs(query);
}
public void setQueryAsString(String queryAsString) {
this.queryAsString = queryAsString;
this.query = splitPairs(queryAsString);
}
public void setQuery(String queryAsString) {
this.queryAsString = queryAsString;
this.query = splitPairs(queryAsString);
}
public void setFragment(String fragment) { this.fragment = fragment; }
private void showElements() {
System.err.println(
String.format(
"Scheme: %s\nUserInfo: %s\nHost: %s\nPort: %d\nPath: %s\nQuery: %s\nFragment: %s",
scheme(), userinfo(), host(), port(), path(), queryAsString(), fragment()
));
}
/**
* if all components are same, these instances are same.
*/
public boolean equals(Object obj) {
if(obj == null)
return false;
if(! (obj instanceof MyURL))
return false;
MyURL uobj = (MyURL)obj;
return (
this.scheme() == null ? uobj.scheme() == null : this.scheme().equals(uobj.scheme()) &&
this.userinfo() == null ? uobj.userinfo() == null : this.userinfo().equals(uobj.userinfo()) &&
this.host() == null ? uobj.host() == null : this.host().equals(uobj.host()) &&
this.port() == uobj.port() &&
this.path() == null ? uobj.path() == null : this.path().equals(uobj.path()) &&
this.queryAsString() == null ? uobj.queryAsString() == null : this.queryAsString().equals(uobj.queryAsString()) &&
this.fragment() == null ? uobj.fragment() == null : this.fragment().equals(uobj.fragment())
);
}
private List<Pair<String, String> > splitPairs(String allPairs) {
if(allPairs != null && ! "".equals(allPairs)) {
List<Pair<String, String> > pairList = new ArrayList<Pair<String, String> >();
String[] pairs = allPairs.split("&");
for(String pairString: pairs) {
String[] tuple = pairString.split("=");
String key = pairString;
try {
key = tuple[0];
} catch (ArrayIndexOutOfBoundsException aiobe) {
System.err.println(pairString + " is strange query.");
}
String value = null;
if((! pairString.equals("=")) && pairString.contains("=")) {
value = "";
}
try {
value = tuple[1];
} catch (ArrayIndexOutOfBoundsException aiobe) {
// System.err.println(key + " has no value.");
}
Pair<String, String> pair = new Pair<String, String>(key, value);
pairList.add(pair);
}
return pairList;
} else {
return null;
}
}
private String joinPairs(List<Pair<String, String> > pairList) {
if(pairList != null) {
StringBuffer pairsSB = new StringBuffer();
Iterator<Pair<String, String> > iter = pairList.iterator();
if(iter.hasNext()) {
Pair<String, String> firstPair = iter.next();
if(firstPair.second() == null) {
pairsSB.append(firstPair.first());
} else {
pairsSB.append(firstPair.first() + "=" + firstPair.second());
}
}
while(iter.hasNext()) {
Pair<String, String> pair = iter.next();
String key = pair.first();
String value = pair.second();
pairsSB.append("&");
if(value == null) {
pairsSB.append(key);
} else {
pairsSB.append(key + "=" + value);
}
}
return pairsSB.toString();
} else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment