home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-11-02 | 5.4 KB | 184 lines |
- import java.applet.*;
- import java.awt.*;
- import java.util.*;
- import java.io.*;
- import java.net.*;
- import java.util.zip.*;
-
- class SearchEngine {
- Properties tab;
- Hashtable result;
- SearchEngine() { tab=new Properties(); }
- void load(InputStream in) throws IOException {
- tab.load(in);
- in.close();
- }
- void loadZIP(InputStream in) throws IOException {
- load(new InflaterInputStream(in));
- }
- int size() { return tab.size(); }
- void addEntry(String s) {
- if(s!=null) {
- for(int i=0; i<s.length(); i+=2) {
- result.put(s.substring(i, i+2), "");
- }
- }
- }
- static boolean match(String wild, String key, int nw, int nk) {
- while(nw<wild.length() && nk<key.length()) {
- if(wild.charAt(nw)=='?') {
- ++nk; // ?: Zeichen ignorieren
- ++nw;
- }
- else if(wild.charAt(nw)=='*') { // *: Beliebig viele Zeichen
- while(nk<=key.length()) {
- if(match(wild, key, nw+1, nk++)) return true;
- }
- return false;
- }
- else
- if(key.charAt(nk++)!=wild.charAt(nw++))
- return false;
- }
- return nw==wild.length() && nk==key.length();
- }
- SearchEngineResult[] lookup(String keyword) {
- result=new Hashtable();
- if(!keyword.startsWith(".")) { // .-EintrΣge sind reserviert!
- if(keyword.indexOf('?')<0 && keyword.indexOf('*')<0) {
- addEntry(tab.getProperty(keyword)); // Suche ohne Wildcards
- }
- else {
- // Wildcard-Suche: Alle Schlⁿsselw÷rter durchgehen
- for(Enumeration e=tab.keys(); e.hasMoreElements() ;) {
- String key=(String)e.nextElement();
- if(!key.startsWith("."))
- if(match(keyword, key, 0, 0))
- addEntry(tab.getProperty(key));
- }
- }
- }
- SearchEngineResult r[]=new SearchEngineResult[result.size()];
- int i=0;
- for(Enumeration e=result.keys(); e.hasMoreElements() ;) {
- String doc=(String)e.nextElement();
- r[i++]=new SearchEngineResult(tab.getProperty("."+doc));
- }
- return r;
- }
- }
-
- public class ClientSearch extends Applet implements Runnable {
- Thread loader=null;
- SearchEngine engine;
- SearchEngineResult[] result;
- TextField t1;
- Button b1;
- List l1;
- final static String rawfile="searchR.dat";
- final static String zipfile="searchZ.dat";
-
- public String getAppletInfo() {
- return "Name: ClientSearch\r\n" +
- "Autor: Gerhard Schild\r\n" +
- "(c)PCONLiNE, Vogel Verlag 1997";
- }
- private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) {
- add(c);
- gb.setConstraints(c, (GridBagConstraints)gbc.clone());
- }
- public void run() {
- // Feststellen, ob InflaterInputStream verfⁿgbar ist (ab JDK 1.1)
- boolean compress=true;
- try {
- Class c=Class.forName("java.util.zip.InflaterInputStream");
- } catch(Exception e) {
- compress=false;
- }
- String filename=compress?zipfile:rawfile;
- try {
- URL url=new URL(getDocumentBase(), filename);
- showStatus("Lade "+url.getFile());
- InputStream in=url.openStream();
- if(compress) engine.loadZIP(in); else engine.load(in);
- } catch(Exception e) {
- System.err.println("Exception: "+e.getMessage());
- } finally {
- showStatus("Suchmaschine bereit ("+engine.size()+" EintrΣge).");
- }
- b1.enable();
- }
- Color getColor(String name) {
- String s=getParameter(name);
- return s==null ? null : new Color(Integer.valueOf(s, 16).intValue());
- }
- public void init() {
- Color bg=getColor("bgcolor"), fg=getColor("fgcolor");
- if(bg!=null) setBackground(bg);
- if(fg!=null) setForeground(fg);
- GridBagConstraints gbc=new GridBagConstraints();
- GridBagLayout gb=new GridBagLayout();
- setLayout(gb);
- gbc.gridx=GridBagConstraints.RELATIVE;
- gbc.gridy=0;
- gbc.gridwidth=1;
- gbc.gridheight=1;
- gbc.weightx=0.0;
- gbc.fill=GridBagConstraints.BOTH;
- add(new Label("Suchbegriff:", Label.LEFT), gb, gbc);
- gbc.weightx=1.0;
- add(t1=new TextField(""), gb, gbc);
- gbc.weightx=0.0;
- add(b1=new Button("Suchen"), gb, gbc);
- gbc.gridx=0;
- gbc.gridy=1;
- gbc.gridwidth=3;
- gbc.weighty=1.0;
- add(l1=new List(), gb, gbc);
- t1.requestFocus();
- if(bg!=null) l1.setBackground(bg);
- if(fg!=null) l1.setForeground(fg);
- engine=new SearchEngine();
- b1.disable();
- }
- public void start() {
- if(loader==null) {
- loader=new Thread(this);
- loader.start();
- }
- }
- public boolean action(Event e, Object o) {
- if(e.target==b1) { search(); return true; }
- if(e.target==t1) { if(b1.isEnabled()) search(); return true; }
- if(e.target==l1) { go(); return true; }
- return false;
- }
- void go() {
- String base=getParameter("base");
- if(base==null) base="";
- try {
- URL url=new URL(base+result[l1.getSelectedIndex()].document);
- showStatus("Verzweige zu "+url.toString());
- getAppletContext().showDocument(url);
- }
- catch(Exception e) {
- }
- }
- void search() {
- showStatus("Suche...");
- b1.disable();
- result=engine.lookup(t1.getText().toUpperCase());
- showStatus("Treffer: "+result.length);
- l1.clear();
- if(result.length>0) {
- for(int i=0; i<result.length; ++i) {
- l1.addItem(result[i].getString());
- }
- l1.select(0);
- l1.requestFocus();
- }
- b1.enable();
- }
- }
-
-