SearchDocuments.java sample program
How to search for documents in a document base, using a query coded in XML.
package com.ixiasoft.samples;
/**
* Title: SearchDocuments - a sample that searches for documents in a docbase
*
* Description: This sample shows how to use a query to search for documents
* in a docbase, and to retrieve the documents.
*
* Syntax: DoSearch user=<domain\\user>
* password=<password>
* server=<ServerName>
* docbase=<docBaseName>
* queryfile=<queryFile>
*
* Copyright: Copyright (c) 2003, 2010
* Company: Ixiasoft Technologies Inc.
*
* @version 2.0
* Modified: 2010-07-05
*/
import com.ixia.textmlserver.*;
import java.io.*;
import java.util.*;
public class SearchDocuments
{
final static String TOKEN_USER = "USER";
final static String TOKEN_PASSWORD = "PASSWORD";
final static String TOKEN_SERVER = "SERVER";
final static String TOKEN_DOCBASE = "DOCBASE";
final static String TOKEN_QUERYFILE = "QUERYFILE";
// Valid parameters accepted from command-line
final static String [] validTokens =
{ TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE,
TOKEN_QUERYFILE };
// Which parameters must be specified
final static boolean[] mandatory =
{ true , true , true , true ,
true };
final static String newLine = System.getProperty("line.separator") ;
// If run-time parameters are missing or invalid, display Help
private static void Usage()
{
System.out.println
("SearchDocuments user=<domain\\user> password=<password> " +
"server=<serverName> docbase=<docBaseName> " +
"queryfile=<[path\\]queryFileName>");
System.out.println
("\t<domain\\user> Name of the user used for security purpose");
System.out.println
("\t<password> Password of the user");
System.out.println
("\t<ServerName> Name of TEXTML Server instance");
System.out.println
("\t<DocBaseName> Document base name");
System.out.println
("\t<[path\\]queryFileName> XML file containing query");
}
// Extract run-time parameters from command-line
private static HashMap Extract(String [] args)
{
HashMap retval = new HashMap(10);
for (int i = 0; i < args.length; ++i)
{
StringTokenizer tokens = new StringTokenizer(args[i], "=", false);
String token = null, value = null;
if (tokens.hasMoreElements())
token = tokens.nextToken();
if (tokens.hasMoreElements())
value = tokens.nextToken();
if (token == null || value == null)
{
retval.clear();
return retval;
}
boolean found = false;
for (int j = 0; j < validTokens.length && !found; ++j)
{
if (validTokens[j].equalsIgnoreCase(token) &&
!retval.containsKey(validTokens[j]))
{
retval.put(validTokens[j], value);
found = true;
}
}
if (!found)
{
retval.clear();
return retval;
}
}
for (int i = 0; i < validTokens.length; ++i)
{
if (mandatory[i] && !retval.containsKey(validTokens[i]))
{
retval.clear();
return retval;
}
}
return retval;
}
// Read the query file, and return its contents as a String object
private static String ReadQuery(String fileName) throws java.io.IOException
{
FileInputStream stream = new FileInputStream(fileName);
InputStreamReader reader = new InputStreamReader(stream, "UTF-16");
StringBuffer buff = new StringBuffer(stream.available());
try
{
while (reader.ready())
buff.append((char)reader.read());
}
finally
{
reader.close();
}
return buff.toString();
}
//*** This function is never used ***
private static byte[] ReadFile(File file) throws java.io.IOException
{
FileInputStream f = new FileInputStream(file);
byte[] retval = new byte[f.available()];
try
{
f.read(retval);
}
finally
{
f.close();
}
return retval;
}
public static void main(String[] args)
{
String user = null;
// Parse the command line
HashMap map = Extract(args);
if (map.isEmpty())
{
Usage();
return;
}
// Validate the command-line parameters
user = (String) map.get(TOKEN_USER);
if (user.indexOf("\\") == -1)
{
Usage();
return;
}
// Prepare parameters for ClientServicesFactory
HashMap<String, String> parms = new HashMap<String, String>(1);
try
{
// Get the ClientServices object
ClientServices cs =
com.ixia.textmlserver.ClientServicesFactory.getInstance
("CORBA", parms);
// extract domain (or machine-name) from <user>
String domain = user.substring(0, user.indexOf("\\"));
String userName = user.substring(user.indexOf("\\") + 1);
// Prepare to login to the TEXTML Server instance
cs.Login(domain, userName, (String) map.get(TOKEN_PASSWORD));
// Read the query from the query file
String query = ReadQuery((String)map.get(TOKEN_QUERYFILE));
try
{
// Get the ServerServices for the server specified by the user
IxiaServerServices ss =
cs.ConnectServer((String) map.get(TOKEN_SERVER));
try
{
// Get the DocbaseServices for the specified docbase
// on that server
IxiaDocBaseServices docbase =
ss.ConnectDocBase((String) map.get(TOKEN_DOCBASE));
try
{
// then, the SearchServices for that docbase
IxiaSearchServices search = docbase.SearchServices();
try
{
System.out.println("Executing query:");
System.out.println(query);
System.out.print(newLine) ;
// Search the specified docbase with the
// specified query. Store the results in
// a ResultSpace, which is a container for
// the data returned by a SearchServices search.
IxiaResultSpace result =
search.SearchDocuments(query);
// If you got this far, then your query did
// not throw an exception.
try
{
// How many hits did the query produce?
int count = result.Count();
System.out.println
("Query found " + count + " documents");
System.out.print(newLine) ;
if (count > 100)
count = 100;
if (count != 0)
{
System.out.println("First " + count +
" Document names:");
// Store the ResultSpace of the search
// as a SubList, an implementation
// of the Java List interface.
IxiaSubList subList =
new IxiaSubList(result);
// Mark a range of items that
// includes all items.
// Each item is data about one document.
subList.markRange(0, count - 1);
// Get a DocumentServices object for
// the docbase that we just searched
IxiaDocumentServices docServices =
docbase.DocumentServices();
try
{
// Retrieve <documents> (an array of
// Result objects), one element for
// each document in the <subList>.
// Use the second parameter to specify
// what you want the documents to
// contain. You can specify: the
// document, its content, indexed
// content, properties, custom
// properties, search result
// information, related errors
// and version number.
// We're asking for them all!
IxiaDocumentServices.Result []
documents =
docServices.GetDocuments
(subList,
Constants.TEXTML_DOCUMENT_ALL_FLAGS);
// Process each entry in the sublist.
// Either:
// * Success: print the document name.
// * Failure: print the error message.
for (int i = 0; i < documents.length;
++i)
{
if (documents[i].error != null)
System.out.println
(documents[i].error);
else
System.out.println
(documents[i].document.GetName());
}
System.out.print(newLine) ;
}
finally
{
docServices.Release(); // Free memory
}
}
else // result.Count() returned zero
System.out.println
("No documents in query");
}
finally
{
result.Release();
}
}
finally
{
search.Release();
}
}
finally
{
docbase.Release();
}
}
finally
{
ss.Release();
}
}
finally
{
// Don't forget to logout
cs.Logout();
}
}
catch (Exception e)
{
System.err.println("Exception occurred: ");
e.printStackTrace(System.err);
}
}
}