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

📄 searchtohtml.java

📁 站内文本搜索Java小程序 SearchToHTML applet小程序可以让你对指定的若干文件进行文本搜索
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   Panel ptop=new Panel();
   ptop.setLayout(new BorderLayout());
   Panel p=new Panel();
   p.add(new Label(searchbox_label_txt,Label.RIGHT));
   p.add(searchbox=new TextField(20));
   if((color=getColor(getParameter("searchboxbgcolor")))!=null) {
     searchbox.setBackground(color);
   }
   else searchbox.setBackground(Color.white);
   if((color=getColor(getParameter("searchboxfgcolor")))!=null) {
     searchbox.setForeground(color);
   }
   else searchbox.setForeground(Color.black);
   String initsearchwrds=getParameter("startwords");
   if(initsearchwrds!=null) searchbox.setText(initsearchwrds);
   //Color buttonbgcolor,buttonfgcolor;
   p.add(b=new Button(search_btn_txt));
   if((color=getColor(getParameter("buttonbgcolor")))!=null) {
     b.setBackground(color);
   }
   if((color=getColor(getParameter("buttonfgcolor")))!=null) {
     b.setForeground(color);
   }
   ptop.add("Center",p);
   add(ptop);
   Panel p2=new Panel();
   p2.setLayout(new FlowLayout(FlowLayout.CENTER));
   p2.add(HTMLbox=new Checkbox(xhtml_chkbx_txt));
   p2.add(Exactbox=new Checkbox(exact_chkbx_txt));
   if((color=getColor(getParameter("checkboxbgcolor")))!=null) {
     HTMLbox.setBackground(color);
     Exactbox.setBackground(color);
   }
   //else let default thing happen
   if((color=getColor(getParameter("checkboxfgcolor")))!=null) {
     HTMLbox.setForeground(color);
     Exactbox.setForeground(color);
   }
   
   //Check the checkboxes...
   if ("true".equals(getParameter("xhtml_chkbx_checked")))
       HTMLbox.setState(true);
   if ("true".equals(getParameter("exact_chkbx_checked")))
       Exactbox.setState(true);
   
   //else let default happen
   add(p2);
   searchbox.requestFocus();
   validate();
 }
 
 public static Color getColor(String s) {
   if(s==null) return null;
   s=s.toLowerCase();
   if(s.startsWith("#")) {
     if(s.length()!=7) return null;
     else {
       try {
         int num=Integer.parseInt(s.substring(1,7),16);//parse a hex. string to dec.
         return new Color(num);
       }
       catch(NumberFormatException e) {
         return null;
       }
     }
   }
   else if("black".equals(s)) return Color.black;
   else if("blue".equals(s)) return Color.blue;
   else if("darkblue".equals(s)) return Color.blue.darker().darker().darker();
   else if("lightblue".equals(s)) return Color.blue.brighter().brighter().brighter();
   else if("cyan".equals(s)) return Color.cyan;
   else if("darkgray".equals(s)) return Color.darkGray;
   else if("lightgray".equals(s)) return Color.lightGray;
   else if("green".equals(s)) return Color.green;
   else if("gray".equals(s)) return Color.gray;
   else if("magenta".equals(s)) return Color.magenta;
   else if("orange".equals(s)) return Color.orange;
   else if("pink".equals(s)) return Color.pink;
   else if("red".equals(s)) return Color.red;
   else if("white".equals(s)) return Color.white;
   else if("yellow".equals(s)) return Color.yellow;
   else return Color.getColor(s);
 }
 
 //Maybe this should be changed to getParameter(String,Object)
 //so it can handle all of our needs?
 //Compiler is choking on name of "default"...it's not a 
 //java reserved word, is it?
 public String getParameter(String name, String alt) {
   String val=getParameter(name);
   if(val!=null) return val;
   return alt;
 }
 
 //public so that HDocSearcher can use it...
 public final static String makeHTMLSafe(String track) {
   StringBuffer train=new StringBuffer();
   char c;
   for(int i=0;i<track.length();i++) {
      c=track.charAt(i);
      if(c=='<') train.append("&lt;");
      else if(c=='>') train.append("&gt;");
      else if(c=='&') train.append("&#38;");
      else train.append(c);
   }
   return train.toString();
 }
 
 //public so that HDocSearcher can use it...
 public final static String replaceChar(String track,char out,String in) {
   StringBuffer train=new StringBuffer();
   char c;
   for(int i=0;i<track.length();i++) {
      c=track.charAt(i);
      if(c==out) train.append(in);
      else train.append(c);
   }
   return train.toString();
 }
 
 public Insets insets() {
   return insets;
 }
 
 public void paint(Graphics g) {
   //Don't worry too much about making this pretty...
   //It's just to let the viewer (probably a disappointed webmaster)
   //know that there's something (probably bad) going on.
   if (displayMessage) 
       g.drawString(message,3,size().height/2);
 }
 
 public void reset() {
   stopAllSearches();
   numreported=0;
 }
 
 public void stopAllSearches() {
   searching=false;
   for (int i=0;i<workers.length;i++) 
       workers[i].stopSearch();
 }
 
 //Append with following format:  URL:title:anchor:pageinfo:context,[next entry]
 public synchronized void receiveMatch(int i,String anchor,String context) {
   //XXX!
   if (!searching)
       return;
   numreported++;
   numOfMatches++;
   results.append(URLEncoder.encode(urls[i])+":"+URLEncoder.encode(titles[i])+
":"+URLEncoder.encode(anchor)+":"+URLEncoder.encode(makeHTMLSafe(pageinfo[i]))+":"+
URLEncoder.encode(context)+",");
   if (numreported>=numWorkers || numOfMatches>=maxNumOfMatches || !waitForAll) {
     try {
       String tempstr=results.toString();
       tempstr=replaceChar(tempstr,'+',"%20");
       URL tempURL=new URL(searchbase+tempstr);
       getAppletContext().showDocument(tempURL,target);
     }
     catch (MalformedURLException muei3) {
         muei3.printStackTrace();
     }
   }
   if (numreported>=numWorkers || numOfMatches>=maxNumOfMatches) {
     if (!search_btn_txt.equals(b.getLabel())) 
         b.setLabel(search_btn_txt);
     searching=false;
     //XXX!
     stopAllSearches();
   }
 }
 
 /**
  * Receive the title of the document at index.
  *
  * @param title The title.
  * @param index The index of the document.
  */
 public final void receiveTitle(String title, int index) {
	 //XXX! Allows the title to be overwritten by successive calls.
    titles[index]=title;
 }
 
 /**
  * Have we got the title of the document at index?.
  *
  * @param index A document index.
  * @return Whether the title has been receive.
  */
 public final boolean hasTitle(int index) {
    //It's okay to directly compare the Objects because
    //the compiler makes a table of static Strings so
    //"" will match with the default value assigned in init().
    if (titles[index]=="" || titles[index]==null)
 	    return false;
    else
        return true;
 }
 
 //An HDocSearcher that is errored can only ever call this once
 //because its errored flag is set
 public synchronized void receiveNoMatch(int i) {
   if(workers[i].isErrored()) {
     numWorkers--;
   }
   else 
      numreported++;
   if (!searching)
       return;
   if(numreported>=numWorkers) {
     searching=false;
     try{ 
       URL tempURL=new URL(searchbase+replaceChar(results.toString(),'+',"%20"));
       if(!search_btn_txt.equals(b.getLabel())) b.setLabel(search_btn_txt);
       getAppletContext().showDocument(tempURL,target);
     }
     catch(MalformedURLException muei3) {System.out.println(muei3);}
   }
   repaint();
 }
 
 //called after an HDocSearcher opens a connection for the
 //first time and can get some extra info like last modified date
 //and file size
 public void addInfo(int i,String info) {
   pageinfo[i]=info;
 }
 
 public boolean action(Event evt, Object arg) {
   if(evt.target == searchbox) {
     search(searchbox.getText(),Exactbox.getState(),HTMLbox.getState());
     b.setLabel(stop_btn_txt);
   }
   else if(evt.target == b) {
     if(search_btn_txt.equals(b.getLabel())) {
       b.setLabel(stop_btn_txt);
       search(searchbox.getText(),Exactbox.getState(),HTMLbox.getState());
     }
     else {
       b.setLabel(search_btn_txt);
       stopAllSearches();
       reset();
     }
   }
   return true;
 }
 
 protected void search(String s,boolean bexact,boolean cutHTML) {
   numOfMatches=0;
   repaint();
   reset();
   searching=true;
   results.setLength(0);
   results.append(resultspage);
   results.append('?');
   results.append(URLEncoder.encode(makeHTMLSafe(s)));//Decode with JavaScript unescape(s)...
   results.append(',');
   if(bexact) results.append('y');
   else results.append('n');
   if(cutHTML) results.append('y');
   else results.append('n');
   //results.append(',');
   StringTokenizer st=new StringTokenizer(s,searchTokenSeparators,true);
   Vector tempv=new Vector();
   String currWord="";//this probably should be a StringBuffer
   String currToken="";
   boolean insideQuote=false;
   while(st.hasMoreTokens()) {
     currToken=st.nextToken();
     if(searchTokenSeparators.indexOf(currToken)!=-1) {
       if("\"".equals(currToken)) {
         insideQuote=!insideQuote;
         if (insideQuote) 
             currWord="";
         else {
           if (currWord.length()>0) 
               tempv.addElement((new String(currWord)).toLowerCase());
           currWord="";
         }
       }
       else if (insideQuote) 
           currWord+=currToken;
     }
     else if (!insideQuote) {
       tempv.addElement((new String(currToken)).toLowerCase());
       currWord="";
     }
     else if (insideQuote) {
       currWord+=currToken;
     }
   }
   if (currWord.length()>0) tempv.addElement((new String(currWord)).toLowerCase());
   String[] ss=new String[tempv.size()];
   tempv.copyInto(ss);
   int workerLen = workers.length;
   for (int i2=0;i2<workerLen;i2++) 
   	   workers[i2].searchFor(ss,bexact,cutHTML);
 }
 
 public void stop() {
   stopAllSearches();
   for (int i=0;i<workers.length;i++) 
       workers[i].stopRunning();
   //don't tie up CPU time when we're hidden or about to die
 }
 
}

⌨️ 快捷键说明

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