⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hdocsearcher.java

📁 站内文本搜索Java小程序 SearchToHTML applet小程序可以让你对指定的若干文件进行文本搜索
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      parent.receiveNoMatch(index);
      return false;
    }
    return true;
  }
  
  private final void closeConnection() {
    try {
        if (bis!=null) 
            bis.close(); 
        bis=null;
    }
    catch (IOException e) {
        System.out.println(e);
    }
  }
  
  //dosearch should only be called from active run
  //so as to take advantage of multithreading.
  private final void dosearch() {
    //search loop
    char c,lowerc;
    int b;
    boolean inTag=false;
    boolean inANCHOR=false;
    String newestanchor="";
    boolean inTITLE=false;
    StringBuffer curranchor=new StringBuffer();
    StringBuffer title=new StringBuffer();
    anchorfinder.reset();
    titlefinder.reset();
    contextsize=0;
    contextposition=0;
    if (!openConnection()) 
        return;//problems taken care of by openConnection()
    for (;;) {
        if (!searching) 
            break;
       try {
         b = bis.read();
         if (b == -1)
            c = '\0';
         else
            c = (char) b;
       }
       catch(IOException e67) {
         System.out.println(e67);
         parent.receiveNoMatch(index); 
         closeConnection(); 
         return;
       }
       lowerc=Character.toLowerCase(c);
       if(!inANCHOR) inANCHOR=anchorfinder.addChar(lowerc);
       else {
         if (lowerc=='\"' || lowerc=='\'' || lowerc==' ' || lowerc=='>' ||
             lowerc=='\n' || lowerc=='\r' || lowerc=='\t' || c == '\0') {
           if (curranchor.length()>0) {
             newestanchor=curranchor.toString();
             inANCHOR=false;
           }
           curranchor.setLength(0);
         }
         else curranchor.append(c);
       }
       if(!gotTitle) {
         if (!inTITLE) 
            inTITLE=titlefinder.addChar(lowerc);
         else {
           if (lowerc=='<' || c == '\0') {
             if (title.length()>0) 
                parent.receiveTitle(title.toString(),index);
             gotTitle=true;
             inTITLE=false;
             title.setLength(0);
           }
           else 
            title.append(c);
         }
       }
       //XXX! JavaScripts and comments might louse up this
       //current cheesy method for cutting HTML tags.
       if (lowerc=='<')
           inTag=true;
       else if (lowerc=='>') {
           if (inTag && cutHTML) {
               inTag=false;
               continue;
           }
           inTag=false;
       }
       
       if (cutHTML && inTag) {/*nada*/}
       else {
         if (leadingContextLength>0) {
            if (contextsize<leadingContextLength) 
                contextsize++;
            context[contextposition]=c;
            contextposition++;
            contextposition%=context.length;
         }
         for (int i=0;i<searchers.length;i++) {
            if (searchers[i].addChar(lowerc)) {
              StringBuffer contextBuffer;
              if (leadingContextLength>0) {
                  String contextstr=getContext();
                  int len=searchers[i].getKeyLength();
                  if (bexact) 
                      contextstr=contextstr.substring(0,contextstr.length()-1);
                  int contextLen = contextstr.length();
                  if (len>contextLen) 
                      len=contextLen;
                  contextBuffer = new StringBuffer(contextLen+trailingContextLength+7);
                  contextBuffer.append(SearchToHTML.makeHTMLSafe(contextstr.substring(0,contextLen-len)));
                  contextBuffer.append("<b>");
                  contextBuffer.append(SearchToHTML.makeHTMLSafe(contextstr.substring(contextLen-len, 
                                                                                        contextLen)));
                  contextBuffer.append("</b>");
                  if (bexact) 
                     contextBuffer.append(c);
                  //XXX! For "exact" searches, the "leading" context will
                  //be one character too short and the "trailing" one too long.
              }
              else
                  contextBuffer = new StringBuffer(trailingContextLength);
              
              String trailingContext = getTrailingContext();
              contextBuffer.append(SearchToHTML.makeHTMLSafe(trailingContext));
              
              if (inTITLE) {
                  finishTitle(title);
                  parent.receiveTitle(title.toString(), index);
              }
              if (searching) 
                parent.receiveMatch(index,newestanchor,contextBuffer.toString());
              closeConnection();
              return;
            }
         }
       }
       if (b == -1) //EOF
           break;
    }
    //tell the parent the bad news
    parent.receiveNoMatch(index);
    closeConnection();
  }

    /**
     * The maximum extra length for a title.
     */
    private static final int TITLE_KILL_NUM = 300; //A completely arbitrary number.

    //XXX! Hack attack... uggh.
    /**
     * Read the rest of the title into titleBuf.
     * <br>
     * This method assumes that the connection is still open.
     * The calling method is responsible for broadcasting the 
     * title and for cleaning up the connection.
     *
     * @param titleBuffer StringBuffer to append the rest of the title to.
     */
    private final void finishTitle(StringBuffer titleBuffer) {
        int b;
        int numRead=0;
        while (numRead<TITLE_KILL_NUM) {
            try {
                b=bis.read();
                if (b=='<' || b==-1) //end of title or EOF
                    return;
                titleBuffer.append((char)b);
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
                return;
            }
            numRead++;
        }
    }

    //XXX! Note that this method contains no sanity checks!
    //Hoo hoo hoo, hee hee hee hee, ha ha, hmmm...
    /**
     * Get <code>trailingContextLength</code> char from the document.
     * <br>
     * This method assumes that the connection is still open.
     * Closing the connection is left to the caller as well.
     * 
     * @return The trailing context.
     */
    private final String getTrailingContext() {
        int b;
        int lenAppended=0;
        boolean inTag=false;
        StringBuffer contextBuffer = new StringBuffer(trailingContextLength);
        while (lenAppended<trailingContextLength) {
            try {
                b = bis.read();
                if (b==-1) //EOF
                    break;
                if (b=='<')
                    inTag=true;
                else if (b=='>') {
                    inTag=false;
                    if (cutHTML)
                        continue;
                }
                if (cutHTML && inTag)
                    continue;
                contextBuffer.append((char)b);
                lenAppended++;
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
                break;
            }
        }
        return contextBuffer.toString();
    }

  /**
   * Returns the last leadingContextLength chars to pass through 
   * the SearchSieves to give more of an idea to the user of what 
   * was around a match.
   * <br>
   * This is not set up to deal with the case that context is still changing as
   * it is asked for or concurrent calls to getContext()...reuses some variables.
   *
   * @return The context of the match.
   */
  private final String getContext() {
    if (leadingContextLength==0)
        return "";
    if (contextsize==context.length) {
      if (contextposition==0) 
        return new String(context);//already in the right order
      else {
        return new String(context,contextposition,context.length-contextposition) + 
               new String(context,0,contextposition);
      }
    } 
    else 
        return new String(context,0,contextposition);
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -