Created
February 2, 2011 05:43
-
-
Save patrickwilsonwelsh/807298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parsing; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class UrlComponents { | |
private static final String PROTOCOL_KEY = "protocol"; | |
private static final String DOMAIN_KEY = "domain"; | |
private static final String FILE_PATH_KEY = "path"; | |
private Map<String, String> components; | |
public UrlComponents() { | |
components = new HashMap<String, String>(); | |
} | |
public void addProtocol(String protocol) { | |
components.put(PROTOCOL_KEY, protocol.toLowerCase()); | |
} | |
public String getProtocol() { | |
return components.get(PROTOCOL_KEY); | |
} | |
public String getDomain() { | |
return components.get(DOMAIN_KEY); | |
} | |
public void addDomain(String domain) { | |
components.put(DOMAIN_KEY, domain); | |
} | |
public void addFilePath(String path) { | |
components.put(FILE_PATH_KEY, path); | |
} | |
public String getFilePath() { | |
return components.get(FILE_PATH_KEY); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parsing; | |
public class UrlParser { | |
private static final int WIDTH_OF_COLON_AND_DOUBLE_SLASH = 3; | |
private static final int STARTING_POSITION = 0; | |
private static final String EMPTY = ""; | |
private StringBuffer url; | |
private static final String SLASH = "/"; | |
private static final String COLON = ":"; | |
private UrlComponents components; | |
public UrlComponents parse(String url) { | |
this.url = new StringBuffer(url); | |
components = new UrlComponents(); | |
parseProtocol(); | |
parseDomain(); | |
parseFilePath(); | |
return components; | |
} | |
private void parseFilePath() { | |
String filePath = extractInstance(STARTING_POSITION, url.length()); | |
components.addFilePath(filePath); | |
} | |
private void parseProtocol() { | |
components.addProtocol(extractInstance(0, COLON)); | |
url = removeProtocolAndPunctuation(); | |
} | |
private void parseDomain() { | |
String domain = extractInstance(STARTING_POSITION, SLASH); | |
components.addDomain(domain); | |
url = removeDomain(domain); | |
} | |
private StringBuffer removeDomain(String domain) { | |
return url.replace(STARTING_POSITION, domain.length(), EMPTY); | |
} | |
private StringBuffer removeProtocolAndPunctuation() { | |
return url.replace(STARTING_POSITION, positionOf(COLON) + WIDTH_OF_COLON_AND_DOUBLE_SLASH, EMPTY); | |
} | |
private String extractInstance(int startingIndex, int endingIndex) { | |
return url.substring(startingIndex, endingIndex); | |
} | |
private String extractInstance(int startingIndex, String endingPattern) { | |
return extractInstance(startingIndex, positionOf(endingPattern)); | |
} | |
private int positionOf(String pattern) { | |
return url.indexOf(pattern); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parsing; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class UrlSplittingTest { | |
/* | |
* TODO Test drive the simplest code you can (in the src folder) that parses URLs like these... | |
http://sites.google.com/site/tddproblems/all-problems-1/URL-splitting | |
http://en.wikipedia.org/wiki/Test-driven_development | |
http://pragprog.com/magazines/2010-12/cohesive-software-design | |
http://sites.google.com/site/tddproblems/all-problems-1/text-editor-end-of-line-trimming | |
ftp://www.kumquat.com/sales/pricing | |
ftp://user:password@server:port/path;type=typecode | |
...into their three main components: protocol, domain, path (and, in the case of some ftp urls, others) | |
So, for "http://sites.google.com/site/tddproblems/all-problems-1/URL-splitting" : | |
The protocol: "http" | |
The domain name: "sites.google.com" | |
The path: "/site/tddproblems/all-problems-1/URL-splitting" | |
* | |
*/ | |
@Test | |
public void canExtract_http_Protocol() throws Exception { | |
String url = "http://sites.google.com/site/tddproblems/all-problems-1/URL-splitting"; | |
UrlComponents components = new UrlParser().parse(url); | |
assertEquals("http", components.getProtocol()); | |
} | |
@Test | |
public void canExtract_ftp_Protocol() throws Exception { | |
String url = "ftp://www.kumquat.com/sales/pricing"; | |
UrlComponents components = new UrlParser().parse(url); | |
assertEquals("ftp", components.getProtocol()); | |
} | |
@Test | |
public void canExtractDomain_ForHttp() throws Exception { | |
String url = "http://sites.google.com/site/tddproblems/all-problems-1/URL-splitting"; | |
UrlComponents components = new UrlParser().parse(url); | |
assertEquals("sites.google.com", components.getDomain()); | |
} | |
@Test | |
public void canExtractFilePathForHttp() throws Exception { | |
String url = "http://sites.google.com/site/tddproblems/all-problems-1/URL-splitting"; | |
UrlComponents components = new UrlParser().parse(url); | |
assertEquals("/site/tddproblems/all-problems-1/URL-splitting", components.getFilePath()); | |
} | |
//TODO: canExtractFilePath_ForHttp | |
//TODO: canExtractDomain_ForFtp_WithNoAuthentication | |
//TODO: canExtractServer_ForFtp | |
//TODO: canExtractPort_ForFtp_WithServerAndPort | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment