📄 searchquicklinks.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.13 $ $Date: 2006/02/08 17:21:47 $ * * Copyright (c) 2002-2003 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Collection;import java.util.Iterator;import java.util.Locale;import java.util.NoSuchElementException;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.server.*;/** * This demonstrates how to integrate Quick Links with general * search. * <p> * An EditorialSearchable is constructed that delivers results from * Quick Links defined on an Ultraseek server, and then * general results from the same server. * <p> * For this demo to work properly, you need to have English * Quick Links defined on your Ultraseek Server. * <p> * You must also include the location of SAX2 XML parser on your * command line for running this application. * A suitable one is available at * <a target="_blank" href="http://xml.apache.org/xerces-j/">http://xml.apache.org/xerces-j/</a> * * @see EditorialSearchable * @see QuickLinksSearchable * @see UltraseekServer#getQuickLinksSearchable * @since XPA2.0 */public class SearchQuickLinks { /** How many results to print */ public final static int MAX_RESULTS = 4; /** Utility function to print out verbose messages */ public static void VerbosePrint(String msg) { if (VERBOSE) System.out.println(msg); } public final static boolean VERBOSE = false; /** * Utility function to collect the hostname:port of * the Ultraseek server to use. */ public static UltraseekServer inputUltraseek(BufferedReader in, String prompt) throws IOException { System.out.println(prompt); System.out.println("Enter the hostname and TCP port of your"); System.out.println("running Ultraseek instance."); System.out.print("hostname: "); String host = in.readLine(); System.out.print("TCP port: "); int port = Integer.parseInt(in.readLine()); UltraseekServer searchServer = new UltraseekServer(host,port); VerbosePrint("Here is the SearchServer object:\n" + searchServer); return searchServer; } /** * Utility function to collect the name of a collection * from an Ultraseek Search Server. */ public static UltraseekCollection inputCollection(BufferedReader in, String prompt, UltraseekServer server) throws IOException { Collection colls = server.getSearchCollections(); System.out.println("Enter the internal name of the collection."); Iterator iterator = colls.iterator(); System.out.print( " (One of:" ); while (iterator.hasNext()) { SearchCollection c = (SearchCollection) iterator.next(); System.out.print( " " + c.getID() ); }; System.out.println( ")" ); System.out.print("collection: "); String id = in.readLine(); UltraseekCollection searchCollection = (UltraseekCollection) server.getSearchCollection(id); System.out.println(); VerbosePrint("Here is the SearchCollection object:\n" + searchCollection); return searchCollection; } /** * Utility function to input a query, * parse it, * start a search, * and display the search results. */ public static void QueryLoop(BufferedReader in, Searchable searchable) throws IOException { for (;;) { System.out.println(); System.out.println("Enter a query."); System.out.print("search: "); String line = in.readLine(); if (line==null) break; Query query = Query.parse(line,Locale.ENGLISH); VerbosePrint("\nHere is the Query object:\n" + query); SearchResultList searchResultList = null; /* Start the search */ try { searchResultList = searchable.search(query); VerbosePrint("\nHere is the SearchResultList object:\n" + searchResultList); } catch (QueryNotSupportedException e) { System.out.println( "Your Query: " + line ); System.out.println( "is not supported: " + e ); continue; } /* Start showing search results */ /* Show the RelatedTopics */ System.out.println(); Collection relatedTopics = searchResultList.getRelatedTopics(); System.out.println(); if (!relatedTopics.isEmpty()) { System.out.println("Here are the Related Topics:"); Iterator iterator = relatedTopics.iterator(); while (iterator.hasNext()) { SearchTopic searchTopic = (SearchTopic)iterator.next(); System.out.println(searchTopic); } } else { System.out.println("There are no Related Topics."); } /* Show the SearchResults */ System.out.println(); if (!searchResultList.isEmpty()) { /* Get an approximate count of SearchResults */ System.out.println("There are about " + searchResultList.getResultCount() + " results." ); System.out.println("Here are first few Search Results:"); Iterator iterator = searchResultList.iterator(); try { int count = 0; while (iterator.hasNext() && count++ < MAX_RESULTS) { SearchResult searchResult = (SearchResult)iterator.next(); System.out.println(); /* Display the result number */ System.out.print( "" + count + " " ); /* QuickLinks have a special display */ if (searchResult instanceof QuickLinksSearchResult) { // Use distinguished method for printing Quick Links. System.out.print( "QuickLink: " ); QuickLinksSearchResult qlsr = (QuickLinksSearchResult) searchResult; System.out.print( "(for " + qlsr.getCollections().size() + " collections) " ); // Print the names of the collections for this quicklink Iterator iterator2 = qlsr.getCollections().iterator(); while (iterator2.hasNext()) { String colName = (String)iterator2.next(); System.out.print( colName + " " ); } } if (VERBOSE) System.out.println(searchResult); else System.out.println("" + searchResult.getURL() + "\n" + " " + searchResult.getTitle() ); } } catch (NoSuchElementException e) { /* No more results from server, not an error. */ } } else { System.out.println("There are no Search Results."); } } } /** * Ensures that a SAX2 Parser can be found. * QuickLinksSearchable requires an XML SAX2 parser be available to parse * the XML files provided by the Ultraseek Server. * As this is a common problem when developing XPA applications with * QuickLinksSearchable, this function checks your application environmental * settings to ensure one can be found. * @throws System.exit if no XML parser can be found. */ public static void FindSAXParser() { try { org.xml.sax.XMLReader x = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); System.out.println( "XML Files will be parsed with: " + x.getClass().getName() ); System.out.println( "from Class location: " + x.getClass().getResource("") ); } catch (Exception e) { System.err.println( "Exception trying to find SAX2 parser:\n" + e ); System.err.println( "\n\nUnable to find XML SAX2 parser: (eg: xercesImpl.jar)\n"); System.err.println( "1) (JDK1.4) Ensure your parser implementation jar files\n" + " (or) are installed in $JAVA_HOME/jre/lib/endorsed/" ); System.err.println( " (JDK1.*) Ensure your parser implementation jar files\n" + " are included in your CLASSPATH." ); System.err.println( "2) Ensure that System property org.xml.sax.driver is\n" + " set to the name of the Class to use for parsing XML." ); System.err.println( " (java -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser ...)" ); System.err.println( "\n" ); System.exit(1); } } public static void main(String[] args) throws Exception { InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); FindSAXParser(); System.out.println(); System.out.println("This demo application displays results from a"); System.out.println(" QuickLinks search, "); System.out.println("followed by results from a"); System.out.println(" general search of an Ultraseek collection."); UltraseekServer searchServer = inputUltraseek(bufferedReader,""); UltraseekCollection searchCollection = inputCollection(bufferedReader,"",searchServer); QuickLinksSearchable quickLinks = searchServer.getQuickLinksSearchable(searchCollection); Searchable edS = new EditorialSearchable( quickLinks, // First return QuickLinks searchCollection, // Then return general results DedupingSearchable.URL_CMP); // Don't duplicate the same URL System.out.println(); VerbosePrint("Here is the EditorialSearchable object:\n" + edS); /* Now show results until tired... */ QueryLoop(bufferedReader, edS); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -