home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 December / PCO1297.ISO / FilesBBS / FREI / CLS.ARJ / CLS.ZIP / ClientSearch.java < prev    next >
Encoding:
Java Source  |  1997-11-02  |  5.4 KB  |  184 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.zip.*;
  7.  
  8. class SearchEngine {
  9.   Properties tab;
  10.   Hashtable result;
  11.   SearchEngine() { tab=new Properties(); }
  12.   void load(InputStream in) throws IOException {
  13.     tab.load(in);
  14.     in.close();
  15.   }
  16.   void loadZIP(InputStream in) throws IOException {
  17.     load(new InflaterInputStream(in));
  18.   }
  19.   int size() { return tab.size(); }
  20.   void addEntry(String s) {
  21.     if(s!=null) {
  22.       for(int i=0; i<s.length(); i+=2) {
  23.         result.put(s.substring(i, i+2), "");
  24.       }
  25.     }
  26.   }
  27.   static boolean match(String wild, String key, int nw, int nk) {
  28.     while(nw<wild.length() && nk<key.length()) {
  29.       if(wild.charAt(nw)=='?') {
  30.         ++nk;                                  // ?: Zeichen ignorieren
  31.         ++nw;
  32.       }
  33.       else if(wild.charAt(nw)=='*') {          // *: Beliebig viele Zeichen
  34.         while(nk<=key.length()) {
  35.           if(match(wild, key, nw+1, nk++)) return true;
  36.         }
  37.         return false;
  38.       }
  39.       else
  40.         if(key.charAt(nk++)!=wild.charAt(nw++))
  41.           return false;
  42.     }
  43.     return nw==wild.length() && nk==key.length();
  44.   }
  45.   SearchEngineResult[] lookup(String keyword) {
  46.     result=new Hashtable();
  47.     if(!keyword.startsWith(".")) {            // .-EintrΣge sind reserviert!
  48.       if(keyword.indexOf('?')<0 && keyword.indexOf('*')<0) {
  49.         addEntry(tab.getProperty(keyword));   // Suche ohne Wildcards
  50.       }
  51.       else {
  52.         // Wildcard-Suche: Alle Schlⁿsselw÷rter durchgehen
  53.         for(Enumeration e=tab.keys(); e.hasMoreElements() ;) {
  54.           String key=(String)e.nextElement();
  55.           if(!key.startsWith("."))
  56.             if(match(keyword, key, 0, 0))
  57.               addEntry(tab.getProperty(key));
  58.         }
  59.       }
  60.     }
  61.     SearchEngineResult r[]=new SearchEngineResult[result.size()];
  62.     int i=0;
  63.     for(Enumeration e=result.keys(); e.hasMoreElements() ;) {
  64.       String doc=(String)e.nextElement();
  65.       r[i++]=new SearchEngineResult(tab.getProperty("."+doc));
  66.     }
  67.     return r;
  68.   }
  69. }
  70.  
  71. public class ClientSearch extends Applet implements Runnable {
  72.   Thread loader=null;
  73.   SearchEngine engine;
  74.   SearchEngineResult[] result;
  75.   TextField t1;
  76.   Button b1;
  77.   List l1;
  78.   final static String rawfile="searchR.dat";
  79.   final static String zipfile="searchZ.dat";
  80.  
  81.   public String getAppletInfo() {
  82.     return "Name: ClientSearch\r\n" +
  83.            "Autor: Gerhard Schild\r\n" +
  84.            "(c)PCONLiNE, Vogel Verlag 1997";
  85.   }
  86.   private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) {
  87.     add(c);
  88.     gb.setConstraints(c, (GridBagConstraints)gbc.clone());
  89.   }
  90.   public void run() {
  91.     // Feststellen, ob InflaterInputStream verfⁿgbar ist (ab JDK 1.1)
  92.     boolean compress=true;
  93.     try {
  94.       Class c=Class.forName("java.util.zip.InflaterInputStream");
  95.     } catch(Exception e) {
  96.       compress=false;
  97.     }
  98.     String filename=compress?zipfile:rawfile;
  99.     try {
  100.       URL url=new URL(getDocumentBase(), filename);
  101.       showStatus("Lade "+url.getFile());
  102.       InputStream in=url.openStream();
  103.       if(compress) engine.loadZIP(in); else engine.load(in);
  104.     } catch(Exception e) {
  105.       System.err.println("Exception: "+e.getMessage());
  106.     } finally {
  107.       showStatus("Suchmaschine bereit ("+engine.size()+" EintrΣge).");
  108.     }
  109.     b1.enable();
  110.   }
  111.   Color getColor(String name) {
  112.     String s=getParameter(name);
  113.     return s==null ? null : new Color(Integer.valueOf(s, 16).intValue());
  114.   }
  115.   public void init() {
  116.     Color bg=getColor("bgcolor"), fg=getColor("fgcolor");
  117.     if(bg!=null) setBackground(bg);
  118.     if(fg!=null) setForeground(fg);
  119.     GridBagConstraints gbc=new GridBagConstraints();
  120.     GridBagLayout gb=new GridBagLayout();
  121.     setLayout(gb);
  122.     gbc.gridx=GridBagConstraints.RELATIVE;
  123.     gbc.gridy=0;
  124.     gbc.gridwidth=1;
  125.     gbc.gridheight=1;
  126.     gbc.weightx=0.0;
  127.     gbc.fill=GridBagConstraints.BOTH;
  128.     add(new Label("Suchbegriff:", Label.LEFT), gb, gbc);
  129.     gbc.weightx=1.0;
  130.     add(t1=new TextField(""), gb, gbc);
  131.     gbc.weightx=0.0;
  132.     add(b1=new Button("Suchen"), gb, gbc);
  133.     gbc.gridx=0;
  134.     gbc.gridy=1;
  135.     gbc.gridwidth=3;
  136.     gbc.weighty=1.0;
  137.     add(l1=new List(), gb, gbc);
  138.     t1.requestFocus();
  139.     if(bg!=null) l1.setBackground(bg);
  140.     if(fg!=null) l1.setForeground(fg);
  141.     engine=new SearchEngine();
  142.     b1.disable();
  143.   }
  144.   public void start() {
  145.     if(loader==null) {
  146.       loader=new Thread(this);
  147.       loader.start();
  148.     }
  149.   }
  150.   public boolean action(Event e, Object o) {
  151.     if(e.target==b1) { search(); return true; }
  152.     if(e.target==t1) { if(b1.isEnabled()) search(); return true; }
  153.     if(e.target==l1) { go(); return true; }
  154.     return false;
  155.   }
  156.   void go() {
  157.     String base=getParameter("base");
  158.     if(base==null) base="";
  159.     try {
  160.       URL url=new URL(base+result[l1.getSelectedIndex()].document);
  161.       showStatus("Verzweige zu "+url.toString());
  162.       getAppletContext().showDocument(url);
  163.     }
  164.     catch(Exception e) {
  165.     }
  166.   }
  167.   void search() {
  168.     showStatus("Suche...");
  169.     b1.disable();
  170.     result=engine.lookup(t1.getText().toUpperCase());
  171.     showStatus("Treffer: "+result.length);
  172.     l1.clear();
  173.     if(result.length>0) {
  174.       for(int i=0; i<result.length; ++i) {
  175.         l1.addItem(result[i].getString());
  176.       }
  177.       l1.select(0);
  178.       l1.requestFocus();
  179.     }
  180.     b1.enable();
  181.   }
  182. }
  183.  
  184.