00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper;
00011
00012 import java.net.URI;
00013 import java.net.URISyntaxException;
00014 import java.util.ArrayList;
00015 import java.util.HashMap;
00016
00017 import org.apache.http.HttpResponse;
00018 import org.apache.http.client.HttpClient;
00019 import org.apache.http.client.methods.HttpGet;
00020 import org.apache.http.impl.client.DefaultHttpClient;
00021 import org.apache.http.util.EntityUtils;
00022
00023 import dochelper.exceptions.InvalidValueException;
00028 public class URLRecord implements Record {
00029
00030
00032 protected URI url;
00033
00035 protected String value;
00036
00038 protected String name;
00039
00041 protected HashMap<String, String> _params;
00042
00044 static final HttpClient httpClient = new DefaultHttpClient();
00045
00049 public URLRecord(String url, String name) throws InvalidValueException {
00050 try {
00051 this.url = new URI(url);
00052 this.name = name;
00053 }
00054 catch(URISyntaxException use) {
00055 throw new InvalidValueException();
00056 }
00057 }
00058
00072 public ResultNode execute(ArrayList<DocPattern> patterns) {
00073
00074 String value = this.getValue();
00075
00076 ResultNode root = new ResultNode(null, _params, name);
00077
00078
00079 if (patterns != null) {
00080 for (DocPattern pattern : patterns) {
00081 root.addChild(pattern.execute(value));
00082 }
00083 }
00084
00085 return root;
00086
00087 }
00088
00089 public URI getURL() {
00090 return url;
00091 }
00092
00100 public String getValue() {
00101
00102 try {
00103 if(value==null || value.length()<2) {
00104
00105 HttpGet request = new HttpGet(this.url);
00106
00107
00108 HttpResponse response = httpClient.execute(request);
00109
00110
00111 if(response.getStatusLine().getStatusCode()== 200) {
00112 value = EntityUtils.toString(response.getEntity());
00113 }
00114 else {
00115
00116 value = "";
00117 }
00118
00119 response.getEntity().consumeContent();
00120 }
00121
00122 }
00123 catch(Exception e) {
00124
00125 e.printStackTrace();
00126 }
00127
00128 return value;
00129 }
00130
00139 public boolean equals(Object record) {
00140
00141 if(record instanceof URLRecord)
00142 return this.getURL().equals( ((URLRecord)record).getURL());
00143
00144 return false;
00145
00146 }
00147
00148
00149 }