RobotsTXTLineSet.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:3k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.util.html;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. /**
  5.  * $Id: RobotsTXTLineSet.java,v 1.2 2003/02/13 20:12:56 vanrogu Exp $
  6.  */
  7. public class RobotsTXTLineSet {
  8.     public static final String USER_AGENT="user-agent:";
  9.     protected String userAgent;
  10.     protected RobotsTXTLine[] lines;
  11.     RobotsTXTLineSet(String userAgent, RobotsTXTLine[] lines) {
  12.         this.userAgent = userAgent;
  13.         this.lines = lines;
  14.     }
  15.     public String getUserAgent ( ) {
  16.         return userAgent;
  17.     }
  18.     public RobotsTXTLine[] getLines ( ) {
  19.         return lines;
  20.     }
  21.     public static RobotsTXTLineSet findLineSet ( InputStream is, String spiderUserAgent) throws IOException {
  22.         BufferedReader br = new BufferedReader(new InputStreamReader(is));
  23.         return findLineSet(br, spiderUserAgent);
  24.     }
  25.     public static RobotsTXTLineSet findLineSet(BufferedReader br, String spiderUserAgent) throws IOException {
  26.         String userAgent = findUserAgent ( br, spiderUserAgent );
  27.         if ( userAgent == null ) {
  28.           return null;
  29.         } else {
  30.           RobotsTXTLine[] lines = parseRules ( br );
  31.           return new RobotsTXTLineSet(userAgent, lines);
  32.         }
  33.     }
  34.     private static String findUserAgent ( BufferedReader br, String spiderUserAgent ) throws IOException {
  35.         if ( spiderUserAgent == null ) {
  36.             spiderUserAgent = "";
  37.         }
  38.         String spiderUserAgentLowerCase = spiderUserAgent.toLowerCase();
  39.         String line = br.readLine();
  40.         while (line != null) {
  41.             line = line.trim();
  42.             if (line.toLowerCase().startsWith(USER_AGENT)) {
  43.                 String userAgent = line.substring(USER_AGENT.length() + 1).trim();
  44.                 if (userAgent.equals("*") || spiderUserAgentLowerCase.indexOf(userAgent.toLowerCase()) > -1) {
  45.                     return userAgent;
  46.                 }
  47.             }
  48.             line = br.readLine();
  49.         }
  50.         return null;
  51.     }
  52.     private static RobotsTXTLine[] parseRules ( BufferedReader br ) throws IOException {
  53.         ArrayList al = new ArrayList();
  54.         String line = br.readLine();
  55.         while (line != null && (line.toLowerCase().indexOf(USER_AGENT) == -1)) {
  56.             RobotsTXTLine robotsTXTline = RobotsTXTLine.parse(line);
  57.             if ( robotsTXTline != null && robotsTXTline.getType() == RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW ) {
  58.                 al.add ( robotsTXTline );
  59.             }
  60.             line = br.readLine();
  61.         }
  62.         return (RobotsTXTLine[]) al.toArray(new RobotsTXTLine[al.size()]);
  63.     }
  64. }