Reputation: 538
I need to be able to convert:
(url) http://www.joe90.com/showroom
to
(namespace) com.joe90.showroom
I can do this using tokens etc, and a enforced rule set.
However, is there a way (a java package) that will do this for me? or do i need to write one myself?
Thanks
Upvotes: 1
Views: 637
Reputation: 625087
java.net.URL url = new java.net.URL("http://www.joe90.com/showroom");
String tokens[] = url.getHostname().split(".");
StringBuilder sb = new StringBuilder();
for (int i=0; i<tokens.length; i++) {
if (i > 1) {
sb.append('.');
}
sb.append(tokens[i]);
}
String namespace = sb.toString();
Alternatively you can parse the hostname out.
Pattern p = Pattern.compile("^(\\w+://)?(.*?)/");
Matcher m = p.matcher(url); // string
if (m.matches()) {
String tokens[] = m.group(2).split(".");
// etc
}
Of course that regex doesn't match all URLs, for example:
http://[email protected]/...
That's why I suggested using java.net.URL: it does all the URL validation and parsing for you.
Upvotes: 2
Reputation: 11824
Your best bet would be to split the string based on the .
and /
characters (e.g. using Sting.split()
, and then concatenate the pieces in reverse order, skipping over any you don't want to include (e.g. www
)
Upvotes: 1