Looking for a class to handle url arguments

C

Christophe C

Hi,
I am searching every where nut I can't find a class doing that :

URLHandler handler = new URLHandler("/tutu/toto.jsp?arg1=one&arg2=two");
handler.add("arg3","tree");
handler.update("arg1","1");
handle.delete("arg2");

String newURL = handler.getURL();

do this kind of stuff exist somewhere ?

Thanks for your help

Christophe
 
T

Tom Dyess

Christophe C said:
Hi,
I am searching every where nut I can't find a class doing that :

URLHandler handler = new URLHandler("/tutu/toto.jsp?arg1=one&arg2=two");
handler.add("arg3","tree");
handler.update("arg1","1");
handle.delete("arg2");

String newURL = handler.getURL();

do this kind of stuff exist somewhere ?

Thanks for your help

Christophe

If you can't find anything, I threw this together that will probably
accomplish what you are looking for.

import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

public class UrlParser {

private String rawUrl;
private String host;
private HashMap args = new HashMap();

public String getHost() {
return host;
}
public HashMap getArgs() {
return args;
}
public String getUrl() {
String result = host;
Iterator i = args.keySet().iterator();
int argNum = 0;
while (i.hasNext()) {
String param = i.next().toString();
if (argNum == 0) {
result = result + "?";
} else {
result = result + "&";
}
argNum++;
result = result + param + "=" + args.get(param);
}
return result;
}
public UrlParser(String rawUrl) {
super();
parseUrl(rawUrl);
}
private void parseUrl(String rawUrl) {
StringTokenizer st = new StringTokenizer(rawUrl, "?");
args.clear();
if (st.hasMoreTokens()) {
host = st.nextToken();
if (st.hasMoreTokens()) {
String rawArgs = st.nextToken();
parseArgs(rawArgs, args);
}
}
}
private void parseArgs(String rawArgs, HashMap args) {
StringTokenizer st = new StringTokenizer(rawArgs, "&");
while (st.hasMoreTokens()) {
String rawArg = st.nextToken();
parseArg(rawArg, args);
}
}
private void parseArg(String rawArg, HashMap args) {
StringTokenizer st = new StringTokenizer(rawArg, "=");
String param = "";
String value = "";
if (st.hasMoreTokens()) {
param = st.nextToken();
}
if (st.hasMoreTokens()) {
value = st.nextToken();
}
args.put(param, value);
}
 
T

Tom Dyess

Ahh, just messed with java.net.URL. Looks like it does most of that minus
the querystring parsing. Might want to inherit from that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top