ViewVersionList.java sample program
Lists the current and all previous version numbers of retrieved documents.
Location:
In your program files directory: [...]\IxiaSoft\TextmlServer[version]\SDK\java\com\ixiasoft\samples\ViewVersionList.java
package com.ixiasoft.samples;
/**
* Title: ViewVersionList.java - a sample that displays: a list of documents;
* the current version number of each document;
* and (for each document) a list of the version
* numbers of all previous versions (if any).
*
* Description: This sample shows how to:
* * Search for documents in a docbase.
* * For each hit (i.e., for each document found):
* - Retrieve the *current* version number.
* - Retrieve a count of previous versions retained
* in the docbase.
* - For each *previous* version of the document:
* * Retrieve the version number.
*
* Syntax: ViewVersionList user=<domain\\user>
* password=<password>
* server=<ServerName>
* docbase=<docBaseName>
* queryfile=<queryFile>
*
* Copyright: Copyright (c) 2003, 2011
* Company: Ixiasoft Technologies Inc.
*
* @version 2.0
* Modified: 2011-02-01
*/
import com.ixia.textmlserver.*;
import java.io.*;
import java.util.*;
public class ViewVersionList
{
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
("ViewVersionList 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 <subList>.
// Use the second parameter to specify
// what you want the <Result> object to
// contain. We're asking for a list of
// the version numbers of any previous
// version of the document
IxiaDocumentServices.Result []
documents =
docServices.GetDocuments
(subList,
Constants.TEXTML_DOCUMENT_VERSIONS_LIST);
// Process each entry in the sublist.
// Either:
// * Success: display the version info.
// * Failure: display an error message.
for (int i = 0; i < documents.length;
++i)
{
if (documents[i].error != null)
{
System.out.println
(documents[i].error);
}
else
// Display the name and version number of this document, followed by the
// version number of each previous version that is still stored in the docbase.
{
System.out.format ("Document: %s" +
"\n Current version number: %d." +
"\n Number of previous versions retained: %d",
documents[i].document.GetName(),
documents[i].document.GetVersion(),
documents[i].document.GetPreviousVersionList().length);
// If there are any previous versions of the document, display their
// version numbers as a list.
if (documents[i].document.GetPreviousVersionList().length > 0)
{
System.out.println("\n Version numbers (of the previous versions):");
for (int j=0;
j<documents[i].document.GetPreviousVersionList().length;
++j)
{
System.out.format
(" %d ",
documents[i].document.GetPreviousVersionList()[j]);
}
}
System.out.println("");
System.out.println("");
} // End of else block
} // end of for loop
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);
}
}
}