GetDocument.cs sample program
How to retrieve a document by specifying the document name.
/**
* Title: GetDocument.cs - a sample to retrieve a document from a docbase
*
* Description: This sample shows how to retrieve a document into
* a document base *when you know the name of the document*.
*
* It also shows how to handle errors that might occur when
* you attempt to retrieve the document.
*
* Syntax: GetDocument user=<domain\\user>
* password=<password>
* server=<ServerName>
* docbase=<DocBaseName>
* docname=<DocumentName>
* path=<Path>
*
* Copyright: Copyright (c) 2003, 2010
* Company: Ixiasoft Technologies Inc.
*
* @version 2.0
* Modified: 2010-06-29
*/
using Ixiasoft.TextmlServer45;
using System;
using System.Threading;
using System.Text;
using System.Collections;
using System.IO;
public class GetDocument
{
static String TOKEN_USER = "USER";
static String TOKEN_PASSWORD = "PASSWORD";
static String TOKEN_SERVER = "SERVER";
static String TOKEN_DOCBASE = "DOCBASE";
static String TOKEN_PATH = "PATH";
static String TOKEN_DOCNAME = "DOCNAME";
// Valid parameters accepted from command-line
static String [] validTokens =
{ TOKEN_USER, TOKEN_PASSWORD, TOKEN_SERVER, TOKEN_DOCBASE,
TOKEN_PATH, TOKEN_DOCNAME };
// Which parameters must be specified in command-line
static bool[] mandatory =
{ true , true , true , true ,
true , true };
// Extract run-time parameters from command-line
private static Hashtable Extract(String[] args)
{
Hashtable retval = new Hashtable(10);
for (int i = 0; i < args.Length; ++i)
{
// StringTokenizer tokens = new StringTokenizer(args[i], "=", false);
String[] tokens = args[i].Split('=');
String token = null, value = null;
if (tokens.Length > 1)
token = tokens[0];
if (tokens.Length > 1)
value = tokens[1];
if (token == null || value == null)
{
retval.Clear();
return retval;
}
bool found = false;
for (int j = 0; j < validTokens.Length && !found; ++j)
{
if (validTokens[j].CompareTo(token.ToUpperInvariant()) == 0
&&
!retval.ContainsKey(validTokens[j]))
{
retval.Add(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;
}
// If run-time parameters are missing or invalid, display Help
private static void Usage()
{
Console.WriteLine
("GetDocument user=<domain\\user> password=<password> " +
"docbase=<DocBaseName> docname=<DocumentName> path=<path>");
Console.WriteLine
("\t<domain\\user> name of the user used for security purpose");
Console.WriteLine
("\t<password> password of the user");
Console.WriteLine
("\t<ServerName> Textmlserver name");
Console.WriteLine
("\t<DocBaseName> Document base name");
Console.WriteLine
("\t<DocumentName> Name of the document to retrieve");
Console.WriteLine
("\t<path> The path where the document will be saved");
}
// Save the <content> of the document to path\file <filename>
private static void WriteFile
(String fileName, IxiaDocument.BinaryContent content)
{
System.IO.StreamWriter f = new System.IO.StreamWriter(fileName);
try
{
IxiaDocument.MakeBinaryContent(fileName);
}
finally
{
f.Close();
}
}
// Save String <strContent> to file <filename>, a Unicode-16 file
private static void WriteFile (String fileName, String strContent)
{
System.IO.StreamWriter f =
new System.IO.StreamWriter(fileName, true,
System.Text.Encoding.Unicode);
try
{
f.Write("\ufeff"); // BOM for 16-bit little-endian Unicode file
f.Write(strContent);
}
finally
{
f.Close();
}
}
// Main routine
static void Main(string[] args)
{
String user = "";
String documentName = "";
// Parse the command line
Hashtable map = Extract(args);
// Validate the parameters
if (map.Count == 0)
{
Usage();
return;
}
if (map.ContainsKey(TOKEN_USER))
user = (String)map[TOKEN_USER];
if (user.IndexOf("\\") == -1)
{
Usage();
return;
}
if (map.ContainsKey(TOKEN_DOCNAME))
documentName = (String)map[TOKEN_DOCNAME];
String path = (String) map[TOKEN_PATH];
try
{
// Get the ClientServices object
IxiaClientServices cs =
Ixiasoft.TextmlServer45.ClientServicesFactory.getInstance();
// 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[TOKEN_PASSWORD]);
try
{
// Get the ServerServices for the server specified by the user
IxiaServerServices ss =
cs.ConnectServer((String)map[TOKEN_SERVER]);
try
{
// Get the DocbaseServices for the specified docbase
// on the server
IxiaDocBaseServices docbase =
ss.ConnectDocBase((String)map[TOKEN_DOCBASE]);
try
{
// Get the DocumentServices for the specified docbase
IxiaDocumentServices ds = docbase.DocumentServices;
try
{
// Prepare an array big enough to hold the names
// of the documents to be retrieved.
// We are only retrieving one document.
String [] documents = new String[1];
documents[0] = documentName;
// Get the specified documents.
// We want:
// -- the actual content of the document
// (normally in XML format).
// -- the document's properties
// The document is a "user document".
IxiaDocumentServices.Result [] result =
ds.GetDocuments(documents,
TextmlConstants.TEXTML_DOCUMENT_CONTENT |
TextmlConstants.TEXTML_DOCUMENT_PROPERTIES,
TextmlDocumentType.TextmlDocument);
// A Result object has two fields:
// -- a Document object.
// -- an Error object.
if (result == null) // Should not ever happen
{
Console.WriteLine
("An unexpected error occurred");
return;
}
// An error occurred for this document
else if (result[0].Error != null)
{
// Handle expected errors
if (result[0].Error.ErrorCode ==
IxiaTextmlServerError.TEXTML_E_TRANSACTION_LOG)
{
// Get a list of events for this document
IxiaTextmlServerError.Event[] events =
result[0].Error.Events;
// Display the list of events
for (int i=0; i<events.Length; i++)
{
Console.WriteLine(events[i].ToString());
}
}
}
if (result[0].Error != null)
{
System.Console.Error.WriteLine
("The following error occurred while getting " +
documentName);
System.Console.Error.WriteLine
(result[0].Error.Message);
return;
}
result[0].Document.Content.SaveTo
(path + "/" + documentName);
Console.WriteLine(documentName +
" successfully saved.") ;
WriteFile
(path + "/" + "P_" + documentName + ".xml",
result[0].Document.Properties);
Console.WriteLine(documentName +
"'s properties successfully saved.") ;
}
finally
{
ds.Release(); // Tidy up before you exit
}
}
finally
{
docbase.Release();
}
}
finally
{
ss.Release();
}
}
finally
{
// Don't forget to logout
cs.Logout();
Console.WriteLine();
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
catch (Exception e)
{
Console.WriteLine("**Exception GetDocument.main: " + e);
Console.WriteLine(e.StackTrace);
Console.WriteLine();
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}