8000 [master] Creating basic implementation of RemoteMapper. · Dyzio18/java-web-bot-library@1922325 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1922325

Browse files
committed
[master] Creating basic implementation of RemoteMapper.
1 parent 83ffb72 commit 1922325

11 files changed

+269
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package stdBot;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
6+
public class AddressUtility {
7+
8+
static public boolean isHttp(String address){
9+
try {
10+
new URI(address);
11+
return true;
12+
} catch (URISyntaxException e) {
13+
return false;
14+
}
15+
}
16+
17+
static public boolean isInternalRef(String address){
18+
return !address.contains(".");
19+
}
20+
21+
static public String clearHttpAddress(String address){
22+
try {
23+
URI uri = new URI(address);
24+
String domain = uri.getHost();
25+
return domain.startsWith("www.") ? domain.substring(4) : domain;
26+
} catch (URISyntaxException e) {
27+
return null;
28+
}
29+
}
30+
31+
static public String getScheme(String address){
32+
try {
33+
URI uri = new URI(address);
34+
return uri.getScheme();
35+
} catch (URISyntaxException e) {
36+
return null;
37+
}
38+
}
39+
}

src/main/java/stdBot/App.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ public class App
55
{
66
public static void main( String[] args )
77
{
8-
System.out.println( "Hello!" );
8+
RemoteMapper remoteMapper = new RemoteMapper();
9+
10+
remoteMapper.start("http://www.wggios.agh.edu.pl/", CollectorTypeEnum.Map, 1);
11+
12+
SiteMap siteMap = remoteMapper.getResult();
13+
14+
siteMap.print();
915
}
1016
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package stdBot;
2+
3+
public interface CollectableRemotely {
4+
void setEntryPoint(String address);
5+
void collectOne(RemoteHandler handler);
6+
SiteMap getMap();
7+
boolean hasFinished();
8+
void setMaxRecursionLevel(int level);
9+
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package stdBot;
2+
3+
public class CollectorsFactory {
4+
5+
RemoteCollector create(CollectorTypeEnum collectorTypeEnum, RemoteHandler handler, String address, int maxRecurtionLevel){
6+
switch (collectorTypeEnum){
7+
case Map:
8+
return new MapCollector(handler, address, maxRecurtionLevel);
9+
case HyperLinks:
10+
return new HyperLinksCollector(handler, address, maxRecurtionLevel);
11+
}
12+
return null;
13+
}
14+
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package stdBot;
2+
3+
public class HyperLinksCollector extends RemoteCollector {
4+
5+
public HyperLinksCollector(RemoteHandler handler, String address, int maxRecurtionLevel) {
6+
super(handler, address, maxRecurtionLevel);
7+
collectableRemotely = factory.create(MapTypeEnum.HyperLinksMap);
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package stdBot;
2+
3+
public class MapCollector extends RemoteCollector {
4+
public MapCollector(RemoteHandler handler, String address, int maxRecurtionLevel) {
5+
super(handler, address, maxRecurtionLevel);
6+
collectableRemotely = factory.create(MapTypeEnum.SiteMap);
7+
}
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package stdBot;
2+
3+
public abstract class RemoteCollector extends Thread {
4+
5+
static protected MapsFactory factory = new MapsFactory();
6+
protected CollectableRemotely collectableRemotely = null;
7+
private String address;
8+
private RemoteHandler handler;
9+
protected int maxRecurtionLevel;
10+
11+
public RemoteCollector(RemoteHandler handler, String address, int maxRecurtionLevel){
12+
this.handler = handler;
13+
this.address = address;
14+
this.maxRecurtionLevel = maxRecurtionLevel;
15+
}
16+
17+
@Override
18+
public void run(){
19+
collectableRemotely.setEntryPoint(address);
20+
collectableRemotely.setMaxRecursionLevel(maxRecurtionLevel);
21+
22+
while (!collectableRemotely.hasFinished())
23+
collectableRemotely.collectOne(handler);
24+
}
25+
26+
public SiteMap collectAll(){
27+
28+
try {
29+
join();
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
34+
return collectableRemotely.getMap();
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package stdBot;
2+
3+
public class RemoteHandlersFactory {
4+
5+
public RemoteHandler create(String address){
6+
if (AddressUtility.isHttp(address))
7+
return new HttpRemoteHandler();
8+
return null;
9+
}
10+
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package stdBot;
2+
3+
public class RemoteHyperLinksMap implements CollectableRemotely{
4+
@Override
5+
public void setEntryPoint(String address) {
6+
7+
}
8+
9+
@Override
10+
public void collectOne(RemoteHandler handler) {
11+
12+
}
13+
14+
@Override
15+
public SiteMap getMap() {
16+
return null;
17+
}
18+
19+
@Override
20+
public boolean hasFinished() {
21+
return false;
22+
}
23+
24+
@Override
25+
public void setMaxRecursionLevel(int level) {
26+
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package stdBot;
2+
3+
public class RemoteMapper {
4+
static private CollectorsFactory collectorsFactory = new CollectorsFactory();
5+
static private RemoteHandlersFactory remoteHandlersFactory = new RemoteHandlersFactory();
6+
private RemoteCollector collector;
7+
private RemoteHandler handler;
8+
9+
public void start(String address, CollectorTypeEnum collectorTypeEnum, int maxRecurtionLevel){
10+
handler = remoteHandlersFactory.create(address);
11+
collector = collectorsFactory.create(collectorTypeEnum, handler, address, maxRecurtionLevel);
12+
collector.start();
13+
}
14+
15+
public SiteMap getResult(){
16+
return collector.collectAll();
17+
}
18+
19+
}

0 commit comments

Comments
 (0)
0