SetDocuments.java sample program
How to add documents to an existing document base.
package com.ixiasoft.samples;
/**
* Title: SetDocuments.java - a sample to add documents to a docbase
*
* Description: This sample shows how to add documents into
* a document base.
*
* Syntax: SetDocuments [-r] user=<domain\\user>
* password=<password>
* server=<ServerName>
* docbase=<DocBaseName>
* path=<Path>
*
* Copyright: Copyright (c) 2003, 2010
* Company: Ixiasoft Technologies Inc.
*
* @version 2.0
* Modified: 2010-06-10
*/
import com.ixia.textmlserver.*;
import java.io.*;
import java.util.*;
public class SetDocuments
{
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_PATH = "PATH";
final static String TOKEN_RECURSE = "RECURSE";
// Valid parameters accepted from command-line
final static String [] validTokens =
{ TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE, TOKEN_PATH};
// Which parameters must be specified
final static boolean[] mandatory =
{ true , true , true , true , true };
// If run-time parameters are missing or invalid, display Help
private static void Usage()
{
System.out.println
("SetDocuments [-r] user=<domain\\user> password=<password> " +
"server=<ServerName> docbase=<DocBaseName> path=<path>");
System.out.println
("\t-r Recurse into any directories in the path");
System.out.println
("\t<domain\\user> Domain and username used to login to server");
System.out.println
("\t<password> Password of the user");
System.out.println
("\t<ServerName> Name of the server hosting an instance " +
"of TEXTML Server");
System.out.println
("\t<DocBaseName> Name of the target document base");
System.out.println
("\t<path> Path of the documents to be added to docbase");
}
// Extract run-time parameters from command-line
private static HashMap <String, Object> Extract(String [] args)
{
HashMap<String, Object> retval = new HashMap<String, Object>(10);
for (int i = 0; i < args.length; ++i)
{
if (args[i].equalsIgnoreCase("-r") ||
args[i].equalsIgnoreCase("/r"))
retval.put(TOKEN_RECURSE, new Boolean(true));
else
{
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;
}
// Returns the contents of a file as an array of bytes
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;
}
// Add all documents found in the source path to the target docbase
private static void Process(File sourceDirectory, // Documents to add
IxiaDocumentServices ds, // to this docbase
boolean recurse)
{
try
{
// Create an array of File objects, one for each file/directory
// stored in sourceDirectory
File[] files = sourceDirectory.listFiles();
// Create an ArrayList structure with one element for
// each file/directory
ArrayList documents = new ArrayList(files.length);
// For each file/directory...
for (int i = 0; i < files.length; ++i)
{
// If it is a directory, and user wants recursion, then recurse
if (files[i].isDirectory())
{
if (recurse)
Process(files[i], ds, true);
}
else
{
// It is a file. Create a Document object for it.
IxiaDocument document = IxiaDocument.getInstance();
document.SetName(files[i].getName());
// Let's assume that all files in the directory are
// XML files.
document.SetMimeType("text/xml");
document.AttachContent
(IxiaDocument.MakeContentFromFile(files[i]));
// Add the document to the ArrayList of documents
documents.add(document);
}
}
// Create docList,
// an array of documents of the same size as the ArrayList
IxiaDocument[] docList = new IxiaDocument[documents.size()];
// Fill the array with the contents of the ArrayList.
// Note: toArray() is an overloaded generic method!
docList = (IxiaDocument[]) documents.toArray(docList);
System.out.println
("Adding " + String.valueOf(docList.length) +
" documents from " + sourceDirectory.getCanonicalPath());
// Add the documents to the docbase.
// Index the documents. (Only XML files are indexable.)
// If a document with the same name already exists in the docbase,
// then replace the old document with the new one.
// The documents are all of type "user document" (i.e., they are
// not system documents).
TextmlserverError [] err =
ds.SetDocuments(docList, Constants.TEXTML_ADD_DOCUMENT |
Constants.TEXTML_REPLACE_DOCUMENT |
Constants.TEXTML_INDEX_DOCUMENT,
Constants.TEXTML_DOCUMENT);
// Process the errors, if any
int countError = 0;
if (err != null)
{
for (int i = 0; i < err.length; ++i)
{
if (err[i] != null)
{
++countError;
if (countError == 1)
{
System.err.println
("Error = " + err[i].getMessage());
}
}
}
}
System.err.println("End of adding the documents") ;
System.err.println("Error count = " + countError);
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
// Main routine
public static void main(String[] args)
{
String user = null;
// Parse the command line
HashMap map = Extract(args);
if (map.isEmpty())
{
Usage();
return;
}
// Validate the parameters
user = (String) map.get(TOKEN_USER);
if (user.indexOf("\\") == -1)
{
Usage();
return;
}
// Prepare parameters for ClientServicesFactory
HashMap parms = new HashMap(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));
try
{
// Get the ServerServices for the specified server
IxiaServerServices ss =
cs.ConnectServer((String) map.get(TOKEN_SERVER));
try
{
// Then, get the DocbaseServices for the specified docbase
// on that server
IxiaDocBaseServices docbase =
ss.ConnectDocBase((String) map.get(TOKEN_DOCBASE));
try
{
// Then, get the DocumentServices for the docbase
IxiaDocumentServices ds =
docbase.DocumentServices();
try
{
// We're now ready to add the documents
// into the docbase
File rootDir =
new File((String) map.get(TOKEN_PATH));
if(!rootDir.exists() || !rootDir.isDirectory())
{
System.err.println
((String) map.get(TOKEN_PATH) +
" is not a valid directory.");
return ;
}
// Add the files in rootDir to docbase ds.
// Recurse any directories in rootDir
// only if the user asked for it
Process(rootDir, ds,
map.containsKey(TOKEN_RECURSE));
}
finally
{
ds.Release(); // Tidy up before you exit
}
}
finally
{
docbase.Release();
}
}
finally
{
ss.Release();
}
}
finally
{
// And don't forget to logout!
cs.Logout();
}
}
// Handle all exceptions here
catch (Exception e)
{
System.err.println
("**Exception thrown in SetDocuments.main: " + e);
e.printStackTrace(System.err);
}
}
}