Skip to content

Commit 486d9d2

Browse files
committed
#45 Remove multi in ExtractBy
1 parent aaa53f5 commit 486d9d2

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

webmagic-extension/src/main/java/us/codecraft/webmagic/example/AppStore.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ public class AppStore {
2323
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..userRatingCount")
2424
private int userRatingCount;
2525

26-
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..screenshotUrls",multi = true)
26+
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..screenshotUrls")
2727
private List<String> screenshotUrls;
2828

29+
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..supportedDevices")
30+
private List<String> supportedDevices;
31+
2932
public static void main(String[] args) {
3033
AppStore appStore = OOSpider.create(Site.me(), AppStore.class).<AppStore>get("http://itunes.apple.com/lookup?id=653350791&country=cn&entity=software");
3134
System.out.println(appStore.trackName);
3235
System.out.println(appStore.description);
3336
System.out.println(appStore.userRatingCount);
3437
System.out.println(appStore.screenshotUrls);
38+
System.out.println(appStore.supportedDevices);
3539
}
3640
}

webmagic-extension/src/main/java/us/codecraft/webmagic/model/PageModelExtractor.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ private FieldExtractor getAnnotationExtractByUrl(Class clazz, Field field) {
131131
if (regexPattern.trim().equals("")) {
132132
regexPattern = ".*";
133133
}
134-
fieldExtractor = new FieldExtractor(field, new RegexSelector(regexPattern), FieldExtractor.Source.Url, extractByUrl.notNull(), extractByUrl.multi());
134+
fieldExtractor = new FieldExtractor(field,
135+
new RegexSelector(regexPattern), FieldExtractor.Source.Url, extractByUrl.notNull(),
136+
extractByUrl.multi() || List.class.isAssignableFrom(field.getType()));
135137
Method setterMethod = getSetterMethod(clazz, field);
136138
if (setterMethod != null) {
137139
fieldExtractor.setSetterMethod(setterMethod);
@@ -157,7 +159,7 @@ private FieldExtractor getAnnotationExtractCombo(Class clazz, Field field) {
157159
selector = new AndSelector(ExtractorUtils.getSelectors(extractBies));
158160
}
159161
fieldExtractor = new FieldExtractor(field, selector, comboExtract.source() == ComboExtract.Source.RawHtml ? FieldExtractor.Source.RawHtml : FieldExtractor.Source.Html,
160-
comboExtract.notNull(), comboExtract.multi());
162+
comboExtract.notNull(), comboExtract.multi() || List.class.isAssignableFrom(field.getType()));
161163
Method setterMethod = getSetterMethod(clazz, field);
162164
if (setterMethod != null) {
163165
fieldExtractor.setSetterMethod(setterMethod);
@@ -172,7 +174,7 @@ private FieldExtractor getAnnotationExtractBy(Class clazz, Field field) {
172174
if (extractBy != null) {
173175
Selector selector = ExtractorUtils.getSelector(extractBy);
174176
fieldExtractor = new FieldExtractor(field, selector, extractBy.source() == ExtractBy.Source.RawHtml ? FieldExtractor.Source.RawHtml : FieldExtractor.Source.Html,
175-
extractBy.notNull(), extractBy.multi());
177+
extractBy.notNull(), extractBy.multi() || List.class.isAssignableFrom(field.getType()));
176178
Method setterMethod = getSetterMethod(clazz, field);
177179
if (setterMethod != null) {
178180
fieldExtractor.setSetterMethod(setterMethod);
@@ -359,7 +361,7 @@ private List<Object> convert(List<String> values, ObjectFormatter objectFormatte
359361
}
360362

361363
private void setField(Object o, FieldExtractor fieldExtractor, Object value) throws IllegalAccessException, InvocationTargetException {
362-
if (value==null){
364+
if (value == null) {
363365
return;
364366
}
365367
if (fieldExtractor.getSetterMethod() != null) {

webmagic-extension/src/main/java/us/codecraft/webmagic/model/annotation/ComboExtract.java

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public static enum Source {
7575
* Define whether the extractor return more than one result.
7676
* When set to 'true', the extractor return a list of string (so you should define the field as List). <br>
7777
*
78+
* Deprecated since 0.4.2. This option is determined automatically by the class of field.
79+
* @deprecated since 0.4.2
7880
* @return whether the extractor return more than one result
7981
*/
8082
boolean multi() default false;

webmagic-extension/src/main/java/us/codecraft/webmagic/model/annotation/ExtractBy.java

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public static enum Source {
6767
* Define whether the extractor return more than one result.
6868
* When set to 'true', the extractor return a list of string (so you should define the field as List). <br>
6969
*
70+
* Deprecated since 0.4.2. This option is determined automatically by the class of field.
71+
* @deprecated since 0.4.2
7072
* @return whether the extractor return more than one result
7173
*/
7274
boolean multi() default false;

webmagic-extension/src/main/java/us/codecraft/webmagic/model/annotation/ExtractByUrl.java

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* Define whether the extractor return more than one result.
3434
* When set to 'true', the extractor return a list of string (so you should define the field as List). <br>
3535
*
36+
* Deprecated since 0.4.2. This option is determined automatically by the class of field.
37+
* @deprecated since 0.4.2
3638
* @return whether the extractor return more than one result
3739
*/
3840
boolean multi() default false;

0 commit comments

Comments
 (0)