Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing smells #1015

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion webmagic-core/src/main/java/us/codecraft/webmagic/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Page {

private byte[] bytes;

private List<Request> targetRequests = new ArrayList<Request>();
private List<Request> targetRequests = new ArrayList<>();

private String charset;

Expand Down Expand Up @@ -108,6 +108,7 @@ public Json getJson() {
* @deprecated since 0.4.0
* The html is parse just when first time of calling {@link #getHtml()}, so use {@link #setRawText(String)} instead.
*/
@Deprecated
public void setHtml(Html html) {
this.html = html;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class Request implements Serializable {
/**
* cookies for current url, if not set use Site's cookies
*/
private Map<String, String> cookies = new HashMap<String, String>();
private Map<String, String> cookies = new HashMap<>();

private Map<String, String> headers = new HashMap<String, String>();
private Map<String, String> headers = new HashMap<>();

/**
* Priority of the request.<br>
Expand Down Expand Up @@ -94,7 +94,7 @@ public <T> T getExtra(String key) {

public <T> Request putExtra(String key, T value) {
if (extras == null) {
extras = new HashMap<String, Object>();
extras = new HashMap<>();
}
extras.put(key, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public class ResultItems {

private Map<String, Object> fields = new LinkedHashMap<String, Object>();
private Map<String, Object> fields = new LinkedHashMap<>();

private Request request;

Expand Down
10 changes: 5 additions & 5 deletions webmagic-core/src/main/java/us/codecraft/webmagic/Site.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class Site {

private String userAgent;

private Map<String, String> defaultCookies = new LinkedHashMap<String, String>();
private Map<String, String> defaultCookies = new LinkedHashMap<>();

private Map<String, Map<String, String>> cookies = new HashMap<String, Map<String, String>>();
private Map<String, Map<String, String>> cookies = new HashMap<>();

private String charset;

Expand All @@ -38,11 +38,11 @@ public class Site {

private int timeOut = 5000;

private static final Set<Integer> DEFAULT_STATUS_CODE_SET = new HashSet<Integer>();
private static final Set<Integer> DEFAULT_STATUS_CODE_SET = new HashSet<>();

private Set<Integer> acceptStatCode = DEFAULT_STATUS_CODE_SET;

private Map<String, String> headers = new HashMap<String, String>();
private Map<String, String> headers = new HashMap<>();

private boolean useGzip = true;

Expand Down Expand Up @@ -83,7 +83,7 @@ public Site addCookie(String name, String value) {
*/
public Site addCookie(String domain, String name, String value) {
if (!cookies.containsKey(domain)){
cookies.put(domain,new HashMap<String, String>());
cookies.put(domain,new HashMap<>());
}
cookies.get(domain).put(name, value);
return this;
Expand Down
18 changes: 9 additions & 9 deletions webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class Spider implements Runnable, Task {

protected Downloader downloader;

protected List<Pipeline> pipelines = new ArrayList<Pipeline>();
protected List<Pipeline> pipelines = new ArrayList<>();

protected PageProcessor pageProcessor;

Expand All @@ -86,11 +86,11 @@ public class Spider implements Runnable, Task {

protected boolean exitWhenComplete = true;

protected final static int STAT_INIT = 0;
protected static final int STAT_INIT = 0;

protected final static int STAT_RUNNING = 1;
protected static final int STAT_RUNNING = 1;

protected final static int STAT_STOPPED = 2;
protected static final int STAT_STOPPED = 2;

protected boolean spawnUrl = true;

Expand Down Expand Up @@ -173,6 +173,7 @@ public Spider setUUID(String uuid) {
* @param scheduler scheduler
* @return this
* @see #setScheduler(us.codecraft.webmagic.scheduler.Scheduler)
* @deprecated
*/
@Deprecated
public Spider scheduler(Scheduler scheduler) {
Expand Down Expand Up @@ -247,7 +248,7 @@ public Spider setPipelines(List<Pipeline> pipelines) {
* @return this
*/
public Spider clearPipeline() {
pipelines = new ArrayList<Pipeline>();
pipelines = new ArrayList<>();
return this;
}

Expand Down Expand Up @@ -438,7 +439,6 @@ private void onDownloadSuccess(Request request, Page page) {
logger.info("page status code error, page {} , code: {}", request.getUrl(), page.getStatusCode());
}
sleep(site.getSleepTime());
return;
}

private void onDownloaderFail(Request request) {
Expand Down Expand Up @@ -544,7 +544,7 @@ protected CollectorPipeline getCollectorPipeline() {
public <T> T get(String url) {
List<String> urls = WMCollections.newArrayList(url);
List<T> resultItemses = getAll(urls);
if (resultItemses != null && resultItemses.size() > 0) {
if (resultItemses != null && !(resultItemses.isEmpty())) {
return resultItemses.get(0);
} else {
return null;
Expand Down Expand Up @@ -677,7 +677,7 @@ public Status getStatus() {


public enum Status {
Init(0), Running(1), Stopped(2);
INIT(0), RUNNING(1), STOPPED(2);

private Status(int value) {
this.value = value;
Expand All @@ -696,7 +696,7 @@ public static Status fromValue(int value) {
}
}
//default value
return Init;
return INIT;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Html download(String url) {
*/
public Html download(String url, String charset) {
Page page = download(new Request(url), Site.me().setCharset(charset).toTask());
return (Html) page.getHtml();
return page.getHtml();
}

protected void onSuccess(Request request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class HttpClientDownloader extends AbstractDownloader {

private Logger logger = LoggerFactory.getLogger(getClass());

private final Map<String, CloseableHttpClient> httpClients = new HashMap<String, CloseableHttpClient>();
private final Map<String, CloseableHttpClient> httpClients = new HashMap<>();

private HttpClientGenerator httpClientGenerator = new HttpClientGenerator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
public class HttpRequestBody implements Serializable {

private static final String ILL_ENC = "illegal encoding ";

private static final long serialVersionUID = 5659170945717023595L;

public static abstract class ContentType {
Expand Down Expand Up @@ -68,15 +70,15 @@ public static HttpRequestBody json(String json, String encoding) {
try {
return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
throw new IllegalArgumentException(ILL_ENC + encoding, e);
}
}

public static HttpRequestBody xml(String xml, String encoding) {
try {
return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
throw new IllegalArgumentException(ILL_ENC + encoding, e);
}
}

Expand All @@ -92,7 +94,7 @@ public static HttpRequestBody form(Map<String,Object> params, String encoding){
try {
return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("illegal encoding " + encoding, e);
throw new IllegalArgumentException(ILL_ENC + encoding, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public abstract class CharsetUtils {

private static final String CHR = "charset";
private static Logger logger = LoggerFactory.getLogger(CharsetUtils.class);

public static String detectCharset(String contentType, byte[] contentBytes) throws IOException {
Expand All @@ -40,9 +41,9 @@ public static String detectCharset(String contentType, byte[] contentBytes) thro
for (Element link : links) {
// 2.1、html4.01 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
String metaContent = link.attr("content");
String metaCharset = link.attr("charset");
if (metaContent.indexOf("charset") != -1) {
metaContent = metaContent.substring(metaContent.indexOf("charset"), metaContent.length());
String metaCharset = link.attr(CHR);
if (metaContent.indexOf(CHR) != -1) {
metaContent = metaContent.substring(metaContent.indexOf(CHR), metaContent.length());
charset = metaContent.split("=")[1];
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
public class ZipCodePageProcessor implements PageProcessor {


private Site site = Site.me().setCharset("gb2312")
.setSleepTime(100);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class WebDriverPool {
* @throws IOException
*/
public void configure() throws IOException {

// Read config file
sConfig = new Properties();
String configFile = DEFAULT_CONFIG_FILE;
Expand Down