Merge branch 'master' of git.oschina.net:flashsword20/webmagic

Conflicts:
	webmagic-core/src/main/java/us/codecraft/webmagic/selector/RegexSelector.java
pull/358/head
yihua.huang 11 years ago
commit 4f68368db0

@ -1,93 +1,93 @@
package us.codecraft.webmagic.selector; package us.codecraft.webmagic.selector;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
/** /**
* Selector in regex.<br> * Selector in regex.<br>
* *
* @author code4crafter@gmail.com <br> * @author code4crafter@gmail.com <br>
* @since 0.1.0 * @since 0.1.0
*/ */
public class RegexSelector implements Selector { public class RegexSelector implements Selector {
private String regexStr; private String regexStr;
private Pattern regex; private Pattern regex;
private int group = 1; private int group = 1;
public RegexSelector(String regexStr, int group) { public RegexSelector(String regexStr, int group) {
if (StringUtils.isBlank(regexStr)) { if (StringUtils.isBlank(regexStr)) {
throw new IllegalArgumentException("regex must not be empty"); throw new IllegalArgumentException("regex must not be empty");
} }
// Check bracket for regex group. Add default group 1 if there is no group. // Check bracket for regex group. Add default group 1 if there is no group.
// Only check if there exists the valid left parenthesis, leave regexp validation for Pattern. // Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") == if (StringUtils.countMatches(regexStr, "(") - StringUtils.countMatches(regexStr, "\\(") ==
StringUtils.countMatches(regexStr, "(?:") - StringUtils.countMatches(regexStr, "\\(?:")) { StringUtils.countMatches(regexStr, "(?:") - StringUtils.countMatches(regexStr, "\\(?:")) {
regexStr = "(" + regexStr + ")"; regexStr = "(" + regexStr + ")";
} }
this.regexStr = regexStr; this.regexStr = regexStr;
try { try {
regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) { } catch (PatternSyntaxException e) {
throw new IllegalArgumentException("invalid regex", e); throw new IllegalArgumentException("invalid regex", e);
} }
this.group = group; this.group = group;
} }
public RegexSelector(String regexStr) { public RegexSelector(String regexStr) {
this(regexStr, 1); this(regexStr, 1);
} }
@Override @Override
public String select(String text) { public String select(String text) {
return selectGroup(text).get(group); return selectGroup(text).get(group);
} }
@Override @Override
public List<String> selectList(String text) { public List<String> selectList(String text) {
List<String> strings = new ArrayList<String>(); List<String> strings = new ArrayList<String>();
List<RegexResult> results = selectGroupList(text); List<RegexResult> results = selectGroupList(text);
for (RegexResult result : results) { for (RegexResult result : results) {
strings.add(result.get(group)); strings.add(result.get(group));
} }
return strings; return strings;
} }
public RegexResult selectGroup(String text) { public RegexResult selectGroup(String text) {
Matcher matcher = regex.matcher(text); Matcher matcher = regex.matcher(text);
if (matcher.find()) { if (matcher.find()) {
String[] groups = new String[matcher.groupCount() + 1]; String[] groups = new String[matcher.groupCount() + 1];
for (int i = 0; i < groups.length; i++) { for (int i = 0; i < groups.length; i++) {
groups[i] = matcher.group(i); groups[i] = matcher.group(i);
} }
return new RegexResult(groups); return new RegexResult(groups);
} }
return RegexResult.EMPTY_RESULT; return RegexResult.EMPTY_RESULT;
} }
public List<RegexResult> selectGroupList(String text) { public List<RegexResult> selectGroupList(String text) {
Matcher matcher = regex.matcher(text); Matcher matcher = regex.matcher(text);
List<RegexResult> resultList = new ArrayList<RegexResult>(); List<RegexResult> resultList = new ArrayList<RegexResult>();
while (matcher.find()) { while (matcher.find()) {
String[] groups = new String[matcher.groupCount() + 1]; String[] groups = new String[matcher.groupCount() + 1];
for (int i = 0; i < groups.length; i++) { for (int i = 0; i < groups.length; i++) {
groups[i] = matcher.group(i); groups[i] = matcher.group(i);
} }
resultList.add(new RegexResult(groups)); resultList.add(new RegexResult(groups));
} }
return resultList; return resultList;
} }
@Override @Override
public String toString() { public String toString() {
return regexStr; return regexStr;
} }
} }

Loading…
Cancel
Save