home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-11-06 | 7.0 KB | 235 lines |
- // ClientSearch Version 1.0
- // Die Suchmaschine fⁿr Jedermann
- // Autor: Gerhard Schild, 1997
- // Fⁿr: PC-ONLiNE
-
- import java.applet.*;
- import java.awt.*;
- import java.util.*;
- import java.io.*;
- import java.net.*;
- import java.util.zip.*;
-
- // Die Suchmaschine
- class SearchEngine {
- Properties tab; // Suchtabelle
- Hashtable result; // Ergebnisliste
- 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), "");
- }
- }
- }
- // Wort mit Suchmuster vergleichen
- 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;
- }
- while(nw<wild.length() && wild.charAt(nw)=='*')
- ++nw;
- return nw==wild.length() && nk==key.length();
- }
- // Suche durchfⁿhren
- 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;
- }
- }
-
- // Das Such-Applet
- 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";
- final static String version="1.0";
- boolean loaded=false;
-
- public String getAppletInfo() {
- return "Name: ClientSearch Version "+version+"\r\n" +
- "Autor: Gerhard Schild, 1997\r\n" +
- "Fuer: PC-ONLiNE";
- }
- private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) {
- add(c);
- gb.setConstraints(c, (GridBagConstraints)gbc.clone());
- }
- void status(String s) {
- showStatus(s+" - ClientSearch Version "+version+" - die Suchmaschine der PCONLiNE");
- }
- // Tabelle laden
- void load(boolean compress) {
- String filename=compress?zipfile:rawfile;
- try {
- URL url=new URL(getDocumentBase(), filename);
- status("Lade "+url.getFile());
- InputStream in=url.openStream();
- if(compress) engine.loadZIP(in); else engine.load(in);
- loaded=true;
- } catch(Exception e) {
- System.err.println("Exception: "+e.getMessage());
- }
- }
- // Asynchroner Lade-Thread
- 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;
- }
- load(compress); // Erster Ladeversuch
- if(!loaded && compress)// Falls komprimierte Daten nicht zur Verfⁿgung
- load(false); // stehen, aber Dekompressor: Unkomprimiert laden
- if(loaded) {
- status("Suchmaschine bereit ("+engine.size()+" EintrΣge)");
- b1.enable();
- }
- else
- status("Suchdaten konnten nicht geladen werden");
- }
- Color getColor(String name) {
- String s=getParameter(name);
- return s==null ? null : new Color(Integer.valueOf(s, 16).intValue());
- }
- // Applet initialisieren: Komponenten erzeugen
- 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();
- }
- // Applet starten: Lade-Thread ansto▀en
- public void start() {
- if(loader==null) {
- loader=new Thread(this);
- loader.start();
- }
- }
- // Ereignis-Behandlung
- 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;
- }
- public boolean keyDown(Event e, int key) {
- if(e.target==l1)
- if(key=='\r' || key=='\n') { go(); return true; }
- return false;
- }
- // Zielseite aufrufen
- void go() {
- if(l1.getSelectedIndex()<0) return;
- String base=getParameter("base");
- if(base==null) base="";
- String link=result[l1.getSelectedIndex()].document;
- URL url=null;
- try {
- url=new URL(new URL(base), link); // Gültige Basis-URL?
- } catch(Exception e) {
- }
- if(url==null) try {
- url=new URL(getDocumentBase(), base+link);
- } catch(Exception e) {
- }
- if(url==null) try {
- url=new URL(base+link);
- } catch(Exception e) {
- }
- if(url==null)
- status("Ungueltige Verzweigung ("+base+","+link+")");
- else {
- status("Verzweige zu "+url.toString());
- getAppletContext().showDocument(url);
- }
- }
- // Suche starten
- void search() {
- status("Suche...");
- b1.disable();
- result=engine.lookup(t1.getText().toUpperCase());
- status("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();
- }
- }
-
-