View Javadoc
1   package us.codecraft.webmagic.selector;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   /**
7    * Selectable plain text.<br>
8    * Can not be selected by XPath or CSS Selector.
9    *
10   * @author code4crafter@gmail.com <br>
11   * @since 0.1.0
12   */
13  public class PlainText extends AbstractSelectable {
14  
15      protected List<String> sourceTexts;
16  
17      public PlainText(List<String> sourceTexts) {
18          this.sourceTexts = sourceTexts;
19      }
20  
21      public PlainText(String text) {
22          this.sourceTexts = new ArrayList<String>();
23          sourceTexts.add(text);
24      }
25  
26      public static PlainText create(String text) {
27          return new PlainText(text);
28      }
29  
30      @Override
31      public Selectable xpath(String xpath) {
32          throw new UnsupportedOperationException("XPath can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
33      }
34  
35      @Override
36      public Selectable $(String selector) {
37  		throw new UnsupportedOperationException("$ can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
38      }
39  
40      @Override
41      public Selectable $(String selector, String attrName) {
42  		throw new UnsupportedOperationException("$ can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
43      }
44  
45      @Override
46      public Selectable links() {
47  		throw new UnsupportedOperationException("Links can not apply to plain text. Please check whether you use a previous xpath with attribute select (/@href etc).");
48      }
49  
50      @Override
51      public List<Selectable> nodes() {
52          List<Selectable> nodes = new ArrayList<Selectable>(getSourceTexts().size());
53          for (String string : getSourceTexts()) {
54              nodes.add(PlainText.create(string));
55          }
56          return nodes;
57      }
58  
59      @Override
60      protected List<String> getSourceTexts() {
61          return sourceTexts;
62      }
63  }