FileDocCategorySizeDatePackage
ConsoleSearch.javaAPI DocExample3763Mon Mar 01 16:32:06 GMT 2004com.develop.ss

ConsoleSearch.java

package com.develop.ss;

import com.develop.ss.search.QueryBean;
import com.develop.ss.search.HitBean;
import com.develop.ss.config.IndexPathBean;

import com.develop.ss.config.ConfigBean;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.PrintStream;

import junit.textui.TestRunner;

public class ConsoleSearch {
    public static void main(String[] args)
    {
        // get configured index path from IndexPathBean
        ConfigBean config = new ConfigBean();
        String indexPath = config.getCurIndexPath();
        if(indexPath == "") return;

        // check args for index flag, retrieve initial page, execute index
        if(!checkArgs(args)) {
            writeUsage();
            return;
        }

        String indexInitialPage = getIndexFlag(args);
        if(indexInitialPage != "")
        {
            doIndex(indexInitialPage);
            return;
        }

        // Allow multiple queries from command line
        BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
        try {
             while (true) {
              System.out.print("Query: ");
              String line = rdr.readLine();
                if (line.length() == 0) {
                  break;
                }
             QueryBean query = new QueryBean(indexPath, line, "contents");
             query.execute();
             HitBean[] hits = query.getResults();
             for(int i = 0;i<hits.length;i++)
             {
                 System.out.println(hits[i].getScoreAsString() + " " + hits[i].getUrl());
             }
             }
        }
        catch(Exception e)
        {
             e.printStackTrace();
        }
    }

    // check args for value "/i:", return url
    private static String getIndexFlag(String[] args)
    {
      for(int i=0;i<args.length;i++)
      {
          if(args[i].startsWith("/i:"))
          {
              return args[i].substring(3, args[i].length());
          }
      }
      return "";
    }



    // perform index
    private static void doIndex(String indexflag)
    {
        try
            {

            ConfigBean config = new ConfigBean();
            String nextIndex;
            try
            {
               nextIndex = config.getNextIndexPath();
            }
            catch(Exception ex)
            {
                return;
            }

            IndexLinks lts = new IndexLinks(nextIndex, config.getMaxLinks(), config.getSkippedLinksFile());
            lts.initFollowPrefixesFromSystemProperties();
            lts.initAvoidPrefixesFromSystemProperties();
            lts.setAvoidPrefixes(config.getAvoidLinks().split(" "));
            lts.setInitialLink(indexflag);

            config.flipIndexPath();

            }
            catch(Exception e)
            {
                //System.out.print(e.getStackTrace());
            }
    }

    private static void writeUsage()
    {
        System.out.println("Usage: java ConsoleSearch");
        System.out.println(" -- /i:http://YOUR_INITIAL_INDEX_PATH");
        System.out.println(" -- /?:show this message");
    }

    private static boolean checkArgs(String[] args)
    {
        if(args.length < 2)
        {
            if(args.length > 0)
            {
                if(args[0].startsWith("/i") || args[0].startsWith("/?"))
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }
        return false;
    }
}