View Javadoc
1   package us.codecraft.webmagic.handler;
2   
3   import us.codecraft.webmagic.Request;
4   
5   import java.util.regex.Pattern;
6   
7   /**
8    * Created with IntelliJ IDEA.
9    * User: Sebastian MA
10   * Date: April 03, 2014
11   * Time: 10:00
12   * <p>
13   * A PatternHandler is in charge of both page extraction and data processing by implementing
14   * its two abstract methods.
15   */
16  public abstract class PatternRequestMatcher implements RequestMatcher {
17  
18      /**
19       * match pattern. only matched page should be handled.
20       */
21      protected String pattern;
22  
23      private Pattern patternCompiled;
24  
25      /**
26       * @param pattern url pattern to handle
27       */
28      public PatternRequestMatcher(String pattern) {
29          this.pattern = pattern;
30          this.patternCompiled = Pattern.compile(pattern);
31      }
32  
33      @Override
34      public boolean match(Request request) {
35          return patternCompiled.matcher(request.getUrl()).matches();
36      }
37  }