import lotus.domino.*;
import java.io.*;
import sun.misc.BASE64Decoder;
public class JavaAgent extends AgentBase {
private final String exportDir = "c:\\dxl\\";
private final String serverName = "your_server_name";
private final String databaseName = "path_to_your_db";
private final boolean includeAcl = true;
private final boolean includeActions = true;
private final boolean includeAgents = true;
private final boolean includeDbScript = true;
private final boolean includeDocuments = true;
private final boolean includeFolders = true;
private final boolean includeForms = true;
private final boolean includeFramesets = true;
private final boolean includeHelpAbout = true;
private final boolean includeHelpIndex = true;
private final boolean includeHelpUsing = true;
private final boolean includeIcon = true;
private final boolean includeImageResources = true;
private final boolean includeJavaResources = true;
private final boolean includeMiscCode = true;
private final boolean includeMiscFormatElements = true;
private final boolean includeMiscIndexElements = true;
private final boolean includeNavigators = true;
private final boolean includeOutlines = true;
private final boolean includePages = true;
private final boolean includeProfiles = true;
private final boolean includeReplicationFormulas = true;
private final boolean includeSharedFields = true;
private final boolean includeStyleSheetResources = true;
private final boolean includeSubforms = true;
private final boolean includeScriptLibraries = true;
private final boolean includeViews = true;
private final String ELEMENT_TYPE_ACL = "Acl";
private final String ELEMENT_TYPE_ACTION = "Actions";
private final String ELEMENT_TYPE_AGENT = "Agents";
private final String ELEMENT_TYPE_DB_SCRIPT = "DatabaseScript";
private final String ELEMENT_TYPE_DOCUMENT = "Documents";
private final String ELEMENT_TYPE_FOLDER = "Folders";
private final String ELEMENT_TYPE_FORM = "Forms";
private final String ELEMENT_TYPE_FRAMESET = "Framsets";
private final String ELEMENT_TYPE_HELP_ABOUT = "HelpAbout";
private final String ELEMENT_TYPE_HELP_INDEX = "HelpIndex";
private final String ELEMENT_TYPE_HELP_USING = "HelpUsing";
private final String ELEMENT_TYPE_ICON = "Icon";
private final String ELEMENT_TYPE_IMAGE_RESOURCE = "ImageResources";
private final String ELEMENT_TYPE_JAVA_RESOURCE = "JavaResources";
private final String ELEMENT_TYPE_MISC_CODE = "MiscCode";
private final String ELEMENT_TYPE_MISC_FORMAT = "MiscFormat";
private final String ELEMENT_TYPE_MISC_INDEX = "MiscIndex";
private final String ELEMENT_TYPE_NAVIGATOR = "Navigator";
private final String ELEMENT_TYPE_OUTLINE = "Outlines";
private final String ELEMENT_TYPE_PAGE = "Pages";
private final String ELEMENT_TYPE_PROFILE = "Profiles";
private final String ELEMENT_TYPE_REP_FORMULA = "ReplicationFormulas";
private final String ELEMENT_TYPE_SHARED_FIELD = "SharedFields";
private final String ELEMENT_TYPE_STYLESHEET = "StyleSheets";
private final String ELEMENT_TYPE_SUBFORM = "Subforms";
private final String ELEMENT_TYPE_SCRIPT_LIBRARY = "ScriptLibraries";
private final String ELEMENT_TYPE_VIEW = "Views";
private Session session;
private Database db;
private DxlExporter exporter;
public void NotesMain() {
try {
session = getSession();
db = session.getDatabase(serverName, databaseName);
if (db == null) {
System.out.println("Can't open remote database");
return;
}
exporter = session.createDxlExporter();
exporter.setForceNoteFormat(true);
if (includeAcl) exportDesignElements(ELEMENT_TYPE_ACL);
if (includeActions) exportDesignElements(ELEMENT_TYPE_ACTION);
if (includeAgents) exportDesignElements(ELEMENT_TYPE_AGENT);
if (includeDbScript) exportDesignElements(ELEMENT_TYPE_DB_SCRIPT);
if (includeDocuments) exportDesignElements(ELEMENT_TYPE_DOCUMENT);
if (includeFolders) exportDesignElements(ELEMENT_TYPE_FOLDER);
if (includeForms) exportDesignElements(ELEMENT_TYPE_FORM);
if (includeFramesets) exportDesignElements(ELEMENT_TYPE_FRAMESET);
if (includeHelpAbout) exportDesignElements(ELEMENT_TYPE_HELP_ABOUT);
if (includeHelpIndex) exportDesignElements(ELEMENT_TYPE_HELP_INDEX);
if (includeHelpUsing) exportDesignElements(ELEMENT_TYPE_HELP_USING);
if (includeIcon) exportDesignElements(ELEMENT_TYPE_ICON);
if (includeImageResources) exportDesignElements(ELEMENT_TYPE_IMAGE_RESOURCE);
if (includeJavaResources) exportDesignElements(ELEMENT_TYPE_JAVA_RESOURCE);
if (includeMiscCode) exportDesignElements(ELEMENT_TYPE_MISC_CODE);
if (includeMiscFormatElements) exportDesignElements(ELEMENT_TYPE_MISC_FORMAT);
if (includeMiscIndexElements) exportDesignElements(ELEMENT_TYPE_MISC_INDEX);
if (includeNavigators) exportDesignElements(ELEMENT_TYPE_NAVIGATOR);
if (includeOutlines) exportDesignElements(ELEMENT_TYPE_OUTLINE);
if (includePages) exportDesignElements(ELEMENT_TYPE_PAGE);
if (includeProfiles) exportDesignElements(ELEMENT_TYPE_PROFILE);
if (includeReplicationFormulas) exportDesignElements(ELEMENT_TYPE_REP_FORMULA);
if (includeSharedFields) exportDesignElements(ELEMENT_TYPE_SHARED_FIELD);
if (includeStyleSheetResources) exportDesignElements(ELEMENT_TYPE_STYLESHEET);
if (includeScriptLibraries) exportDesignElements(ELEMENT_TYPE_SCRIPT_LIBRARY);
if (includeSubforms) exportDesignElements(ELEMENT_TYPE_SUBFORM);
if (includeViews) exportDesignElements(ELEMENT_TYPE_VIEW);
} catch(Exception e) {
e.printStackTrace();
}
}
// main export stub
public void exportDesignElements(String designElementType)
{
NoteCollection nc = null;
try {
nc = db.createNoteCollection(false);
if (designElementType == ELEMENT_TYPE_ACL) nc.setSelectAcl(true);
if (designElementType == ELEMENT_TYPE_ACTION) nc.setSelectActions(true);
if (designElementType == ELEMENT_TYPE_AGENT) nc.setSelectAgents(true);
if (designElementType == ELEMENT_TYPE_DB_SCRIPT) nc.setSelectDatabaseScript(true);
if (designElementType == ELEMENT_TYPE_DOCUMENT) nc.setSelectDocuments(true);
if (designElementType == ELEMENT_TYPE_FOLDER) nc.setSelectFolders(true);
if (designElementType == ELEMENT_TYPE_FORM) nc.setSelectForms(true);
if (designElementType == ELEMENT_TYPE_FRAMESET) nc.setSelectFramesets(true);
if (designElementType == ELEMENT_TYPE_HELP_ABOUT) nc.setSelectHelpAbout(true);
if (designElementType == ELEMENT_TYPE_HELP_INDEX) nc.setSelectHelpIndex(true);
if (designElementType == ELEMENT_TYPE_HELP_USING) nc.setSelectHelpUsing(true);
if (designElementType == ELEMENT_TYPE_ICON) nc.setSelectIcon(true);
if (designElementType == ELEMENT_TYPE_IMAGE_RESOURCE) nc.setSelectImageResources(true);
if (designElementType == ELEMENT_TYPE_JAVA_RESOURCE) nc.setSelectJavaResources(true);
if (designElementType == ELEMENT_TYPE_MISC_CODE) nc.setSelectMiscCodeElements(true);
if (designElementType == ELEMENT_TYPE_MISC_FORMAT) nc.setSelectMiscFormatElements(true);
if (designElementType == ELEMENT_TYPE_MISC_INDEX) nc.setSelectMiscIndexElements(true);
if (designElementType == ELEMENT_TYPE_NAVIGATOR) nc.setSelectNavigators(true);
if (designElementType == ELEMENT_TYPE_OUTLINE) nc.setSelectOutlines(true);
if (designElementType == ELEMENT_TYPE_PAGE) nc.setSelectPages(true);
if (designElementType == ELEMENT_TYPE_PROFILE) nc.setSelectProfiles(true);
if (designElementType == ELEMENT_TYPE_REP_FORMULA) nc.setSelectReplicationFormulas(true);
if (designElementType == ELEMENT_TYPE_SHARED_FIELD) nc.setSelectSharedFields(true);
if (designElementType == ELEMENT_TYPE_STYLESHEET) nc.setSelectStylesheetResources(true);
if (designElementType == ELEMENT_TYPE_SCRIPT_LIBRARY) nc.setSelectScriptLibraries(true);
if (designElementType == ELEMENT_TYPE_SUBFORM) nc.setSelectSubforms(true);
if (designElementType == ELEMENT_TYPE_VIEW) nc.setSelectViews(true);
nc.buildCollection();
int nDesignElements = nc.getCount();
System.out.println("Exporting " + designElementType + " (" + nDesignElements + " element(s))...");
int nNote = 1;
String noteId = nc.getFirstNoteID();
while (nNote <= nDesignElements)
{
System.out.print("Processing doc " + nNote + " of " + nDesignElements + " (NoteId: " + noteId + ")... ");
Document noteDoc = db.getDocumentByID(noteId);
if (noteDoc != null) {
String designElementName = getDesignElementName(noteDoc);
String originalFile = exportDir + "original\\" + designElementType + "\\" + designElementName + ".xml";
String decodedFile = exportDir + "decoded\\" + designElementType + "\\" + designElementName + ".txt";
String dxlData = exporter.exportDxl(noteDoc);
String decodedDxlData = decodeRawItemData(dxlData);
exportDataToFile(originalFile, dxlData);
exportDataToFile(decodedFile, decodedDxlData);
System.out.println("Done");
} else {
System.out.println("ERROR: Can't get document with NoteID " + noteId);
}
noteDoc.recycle();
nNote++;
noteId = nc.getNextNoteID(noteId);
}
nc.recycle();
} catch(Exception e) {
e.printStackTrace();
}
}
// finds and decodes text between <rawitemdata ...> and </rawitemdata>
public String decodeRawItemData(String fileContents)
{
int startPos = 0;
try {
while (fileContents.indexOf("<rawitemdata", startPos) != -1)
{
int rawItem_Start = fileContents.indexOf("<rawitemdata", startPos);
int encodedData_Start = fileContents.indexOf(">", rawItem_Start) + 1;
int encodedData_End = fileContents.indexOf("</rawitemdata", encodedData_Start);
String rawData = fileContents.substring(encodedData_Start, encodedData_End);
BASE64Decoder decoder = new BASE64Decoder();
byte[] bDecodedData = decoder.decodeBuffer(rawData);
String strDecodedData = new String(bDecodedData);
String strStart = fileContents.substring(0, encodedData_Start);
String strEnd = fileContents.substring(encodedData_End);
fileContents = strStart + strDecodedData + strEnd;
startPos = rawItem_Start + 1;
}
} catch (IOException e) {
e.printStackTrace();
}
return fileContents;
}
// create file name from title or document's UNID
public String getDesignElementName(Document noteDoc)
{
String elementName = "";
try {
if (noteDoc.hasItem("$Title")) {
// if title is available - use it as file name
elementName = noteDoc.getItemValueString("$Title");
// replace characters disallowed in file name
elementName = elementName.replaceAll("[\\\\|\\/:\"*?<>+|\n\r\t\f\0]", "");
} else {
// if title is unavailable - use UNID
elementName = noteDoc.getUniversalID();
}
} catch (NotesException e) {
e.printStackTrace();
}
return elementName;
}
// save data to file
public int exportDataToFile(String pathToFile, String dataToExport)
{
createDirectories(pathToFile);
int nWrittenBytes = 0;
Stream stream;
try {
stream = session.createStream();
if (stream.open(pathToFile)) {
stream.truncate();
nWrittenBytes = stream.writeText(dataToExport);
stream.close();
stream.recycle();
if (nWrittenBytes == 0) {
System.out.println("ERRROR: Zero bytes were written to file");
}
} else {
System.out.println("ERROR: Can't create file " + pathToFile);
}
} catch (NotesException e) {
e.printStackTrace();
}
return nWrittenBytes;
}
public void createDirectories(String pathToDir)
{
// get full path without file name
int lastSlashPos = pathToDir.lastIndexOf("\\");
if (lastSlashPos != -1)
{
String dirPath = pathToDir.substring(0, lastSlashPos);
File file = new File(dirPath);
if (!file.exists()) {
boolean isCreated = new File(dirPath).mkdirs();
if (isCreated == false) {
System.out.println("ERROR: Can't create required directory: " + dirPath);
}
}
}
}
}