<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Here are the requirements for my test that I have done..<div><br></div><div><div><font face="Courier New, sans-serif">=================================================</font></div><div><font face="Courier New, sans-serif"> Prerequisities</font></div><div><font face="Courier New, sans-serif">-------------------------------------------------</font></div><div><font face="Courier New, sans-serif"> - JDK 1.6 installed with JAVA_HOME env variable set to this location </font></div><div><font face="Courier New, sans-serif"><br></font></div><div><font face="Courier New, sans-serif">=================================================</font></div><div><font face="Courier New, sans-serif"> Instructions</font></div><div><font face="Courier New, sans-serif">-------------------------------------------------</font></div><div><font face="Courier New, sans-serif"> - Implement link counter that returns the number of HTML links on each HTTP address provided as a </font></div><div><font face="Courier New, sans-serif"> command line argument:</font></div><div><font face="Courier New, sans-serif"> Ex.:</font></div><div><font face="Courier New, sans-serif"> $ java -jar build/libs/linkcounter.jar http://acme.com/ http://mysitewithlinks/ http://192.168.2.3/</font></div><div><font face="Courier New, sans-serif"> http://acme.com/ 66</font></div><div><font face="Courier New, sans-serif"> http://mysitewithlinks/ 23989</font></div><div><font face="Courier New, sans-serif"> http://192.168.2.3/ 9456</font></div><div><font face="Courier New, sans-serif"> </font></div><div><font face="Courier New, sans-serif"> - Implementation constrains:</font></div><div><font face="Courier New, sans-serif"> * For simplification assume that the link is defined as '<[whitespace]a[whitespace]' or '<[whitespace]A[whitespace]'. </font></div><div><font face="Courier New, sans-serif"> ('<a ', '< a h', '<A >', '<a<span class="Apple-tab-span" style="white-space:pre"> </span>attr=' are all valid links)</font></div><div><font face="Courier New, sans-serif"> * All the processed pages use 8 bit encoding</font></div><div><font face="Courier New, sans-serif"> * DO NOT use any 3rd party libraries, just what you get from JRE</font></div><div><font face="Courier New, sans-serif"> * The html pages to process can be very, VERY large!</font></div><div><font face="Courier New, sans-serif"> * Process the list in parallel.</font></div><div><font face="Courier New, sans-serif"> * You may write tests to src/test/java using provided libraries (junit, hamcrest, mockito)</font></div><div><font face="Courier New, sans-serif"><br></font></div><div><font face="Courier New, sans-serif">=================================================</font></div><div><font face="Courier New, sans-serif"> Building and Testing</font></div><div><font face="Courier New, sans-serif">-------------------------------------------------</font></div><div><font face="Courier New, sans-serif"> - To build the project execute "./gradlew build"</font></div><div><font face="Courier New, sans-serif"> - To execute tests gradlew build "./gradlew test"</font></div><div><font face="Courier New, sans-serif"><br></font></div><div><font face="Courier New, sans-serif">=================================================</font></div><div><font face="Courier New, sans-serif"> Running the Application</font></div><div><font face="Courier New, sans-serif">-------------------------------------------------</font></div><div><font face="Courier New, sans-serif"> - To run the application do the following from the current (this) directory:</font></div><div><font face="Courier New, sans-serif"> java -jar build/libs/linkcounter.jar http://www.acme.com/ http://mysitewithlinks/ http://192.168.2.3/</font></div><div> </div><div>And this is the code I wrote, please critique as much as you can, as I am not a developer..</div></div><div><br></div><div><br></div><div><div><font face="Courier New, sans-serif">package com.acme;</font></div><div><font face="Courier New, sans-serif">/**</font></div><div><font face="Courier New, sans-serif"> * Description: This class contains main method that returns the number of HTML links on each address provided</font></div><div><font face="Courier New, sans-serif"> * by command line argument:</font></div><div><font face="Courier New, sans-serif"> * Ex.:</font></div><div><font face="Courier New, sans-serif"> * <pre></font></div><div><font face="Courier New, sans-serif"> * $ java -jar linkcounter.jar http://acme.com/ http://mysitewithlinks/ http://192.168.2.3/</font></div><div><font face="Courier New, sans-serif"> * http://acme.com/ 227</font></div><div><font face="Courier New, sans-serif"> * http://mysitewithlinks/ 23989</font></div><div><font face="Courier New, sans-serif"> * http://192.168.2.3/ 9456</font></div><div><font face="Courier New, sans-serif"> * </pre></font></div><div><font face="Courier New, sans-serif"> *</font></div><div><font face="Courier New, sans-serif"> * See README for more information.</font></div><div><font face="Courier New, sans-serif"> *</font></div><div><font face="Courier New, sans-serif"> * <p>Copyright 2000-2012, NetSuite, Inc.</p></font></div><div><font face="Courier New, sans-serif"> */</font></div><div><font face="Courier New, sans-serif">import java.io.BufferedReader;</font></div><div><font face="Courier New, sans-serif">import java.io.InputStreamReader;</font></div><div><font face="Courier New, sans-serif">import java.net.URL;</font></div><div><font face="Courier New, sans-serif">import java.net.URLConnection;</font></div><div><font face="Courier New, sans-serif">import java.util.regex.Pattern;</font></div><div><font face="Courier New, sans-serif">import java.util.regex.Matcher;</font></div><div><font face="Courier New, sans-serif">import java.io.IOException;</font></div><div><font face="Courier New, sans-serif">import java.net.MalformedURLException;</font></div><div><font face="Courier New, sans-serif"><br></font></div><div><font face="Courier New, sans-serif">public class LinkCounter extends Thread</font></div><div><font face="Courier New, sans-serif">{</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>private String url;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>private int linkCount;</font></div><div><span class="Apple-tab-span" style="white-space:pre"><font face="Courier New, sans-serif"> </font></span></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public LinkCounter(String url){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>this.url = url;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>this.linkCount = 0;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public String getURL(){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>return url;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public int getLinkCount(){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>return linkCount;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public int getCount(String url){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>try{<span class="Apple-tab-span" style="white-space:pre"> </span></font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>URL website = new URL(url);</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>URLConnection connection = website.openConnection();</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>String inputLine;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>while ((inputLine = in.readLine()) != null){ </font></div><div><font face="Courier New, sans-serif"> linkCount += countLinks(inputLine);</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"> catch (MalformedURLException e){</font></div><div><font face="Courier New, sans-serif"> <span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</font></div><div><font face="Courier New, sans-serif"> } </font></div><div><font face="Courier New, sans-serif"> catch (IOException e){</font></div><div><font face="Courier New, sans-serif"> <span class="Apple-tab-span" style="white-space:pre"> </span>e.printStackTrace();</font></div><div><font face="Courier New, sans-serif"> }</font></div><div><font face="Courier New, sans-serif"> return linkCount;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public int countLinks(String inputLine){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>int links = 0;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>Pattern pattern = Pattern.compile("<a[^>]*>(.*?)</a>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>Matcher matcher = pattern.matcher(inputLine);</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>while (matcher.find())</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>links ++;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>return links;</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>@Override</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public void run(){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>getCount(this.url);</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(getURL() + "/ " + getLinkCount());</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String args[])</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>{</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>for(int i = 0; i < args.length; i++){</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>new LinkCounter(args[i]).start();</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif"><span class="Apple-tab-span" style="white-space:pre"> </span>}</font></div><div><font face="Courier New, sans-serif">}</font></div><div><br></div></div> </div></body>
</html>