View Javadoc
1   package us.codecraft.webmagic.configurable;
2   
3   import us.codecraft.webmagic.selector.JsonPathSelector;
4   import us.codecraft.webmagic.selector.Selector;
5   
6   import static us.codecraft.webmagic.selector.Selectors.*;
7   
8   /**
9    * @author code4crafter@gmail.com
10   */
11  public class ExtractRule {
12  
13      private String fieldName;
14  
15      private ExpressionType expressionType;
16  
17      private String expressionValue;
18  
19      private String[] expressionParams;
20  
21      private boolean multi = false;
22  
23      private volatile Selector selector;
24  
25      private boolean notNull = false;
26  
27      public String getFieldName() {
28          return fieldName;
29      }
30  
31      public void setFieldName(String fieldName) {
32          this.fieldName = fieldName;
33      }
34  
35      public ExpressionType getExpressionType() {
36          return expressionType;
37      }
38  
39      public void setExpressionType(ExpressionType expressionType) {
40          this.expressionType = expressionType;
41      }
42  
43      public String getExpressionValue() {
44          return expressionValue;
45      }
46  
47      public void setExpressionValue(String expressionValue) {
48          this.expressionValue = expressionValue;
49      }
50  
51      public String[] getExpressionParams() {
52          return expressionParams;
53      }
54  
55      public void setExpressionParams(String[] expressionParams) {
56          this.expressionParams = expressionParams;
57      }
58  
59      public boolean isMulti() {
60          return multi;
61      }
62  
63      public void setMulti(boolean multi) {
64          this.multi = multi;
65      }
66  
67      public Selector getSelector() {
68          if (selector == null) {
69              synchronized (this) {
70                  if (selector == null) {
71                      selector = compileSelector();
72                  }
73              }
74          }
75          return selector;
76      }
77  
78      private Selector compileSelector() {
79          switch (expressionType) {
80              case Css:
81                  if (expressionParams.length >= 1) {
82                      return $(expressionValue, expressionParams[0]);
83                  } else {
84                      return $(expressionValue);
85                  }
86              case XPath:
87                  return xpath(expressionValue);
88              case Regex:
89                  if (expressionParams.length >= 1) {
90                      return regex(expressionValue, Integer.parseInt(expressionParams[0]));
91                  } else {
92                      return regex(expressionValue);
93                  }
94              case JsonPath:
95                  return new JsonPathSelector(expressionValue);
96              default:
97                  return xpath(expressionValue);
98          }
99      }
100 
101     public void setSelector(Selector selector) {
102         this.selector = selector;
103     }
104 
105     public boolean isNotNull() {
106         return notNull;
107     }
108 
109     public void setNotNull(boolean notNull) {
110         this.notNull = notNull;
111     }
112 }