- The following agent reads XML data from an InputSource, InputStream, and Reader, parses the XML data, and transforms the data using the specified style sheet.
import lotus.domino.*;
import java.io.*;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session s = getSession();
AgentContext agentContext = s.getAgentContext();
Database db = agentContext.getCurrentDatabase();
if (db == null)
db = s.getDatabase("", "xml.nsf", true);
if (db == null){
System.out.println("db xml.nsf is not found!");
return;
}
// (Your code goes here)
dopart1(s, db);
dopart2(s, db);
} catch(Exception e) {
e.printStackTrace();
}
}
public void dopart2(Session s, Database db)
throws Exception
{
for (int i=0; i<2; i++)
{
if (i==0) s.setConvertMIME(false);
else if (i==1) s.setConvertMIME(true);
if (s.isConvertMIME())
System.out.println("isConvertMIME = true");
else System.out.println("isConvertMIME = false");
View view = db.getView("MIMEData");
Document doc = view.getFirstDocument();
while (doc != null)
{
String name = doc.getFirstItem("Subject").getText();
System.out.println(name);
try
{
System.out.println(" Body");
Item it = doc.getFirstItem("Body");
MIMEEntity me = it.getMIMEEntity();
if (me != null)
{
java.io.InputStream is = me.getInputStream();
if (is != null)
{
DataInputStream dis = new DataInputStream(is);
System.out.println(
" MIMEEntity Stream from InputStream - "
+ dis.readLine());
}
else
System.out.println(" No MIMEEntity InputStream ");
org.w3c.dom.Document domdoc = me.parseXML(false);
if (domdoc == null)
System.out.println("MIMEEntity.parseXML failed.");
else
System.out.println(
"MIMEEntity.parseXML succeeded.");
me.recycle();
}
else if (it instanceof RichTextItem)
{
RichTextItem rit = (RichTextItem) it;
java.util.Vector objs = rit.getEmbeddedObjects();
if ((objs != null) && (objs.size() > 0))
{
EmbeddedObject em = (EmbeddedObject)
objs.firstElement();
org.w3c.dom.Document domdoc =
em.parseXML(false);
em.recycle();
}
else
{
// Getting reader from RichTextItem
Reader rd = rit.getReader();
try {
char[] buff = new char [50];
rd.read(buff,0,40);
System.out.println(
"GetReader from RTItem: " + buff );
}catch (Exception e) {
System.out.println(
"Exception while opening items InputStream"
+ e);
}
org.w3c.dom.Document domdoc =
it.parseXML(false);
}
}
else
{
org.w3c.dom.Document domdoc = it.parseXML(false);
}
it.recycle();
}catch (Exception e){
e.printStackTrace();
}
{
Document tmpdoc = view.getNextDocument(doc);
doc.recycle();
doc = tmpdoc;
}
}
}
}
public void dopart1(Session s, Database db)
throws Exception
{
View view = db.getView("OtherData");
Document doc = view.getFirstDocument();
while (doc != null)
{
String name = doc.getFirstItem("Name").getText();
System.out.println(name);
try {
System.out.println(" TextBody");
Item it = doc.getFirstItem("TextBody");
org.w3c.dom.Document domdoc = it.parseXML(false);
} catch (Exception e){
e.printStackTrace();
}
try {
System.out.println(" RichTextBody");
Item it = doc.getFirstItem("RichTextBody");
org.w3c.dom.Document domdoc = it.parseXML(false);
it.recycle();
} catch (Exception e){
e.printStackTrace();
}
try {
System.out.println(" EmbBody");
Item it = doc.getFirstItem("EmbBody");
if (it instanceof RichTextItem)
{
RichTextItem rit = (RichTextItem) it;
EmbeddedObject em = rit.getEmbeddedObject(name);
org.w3c.dom.Document domdoc = em.parseXML(false);
em.recycle();
rit.recycle();
}
else
System.out.println("Not richtext!");
it.recycle();
} catch (Exception e){
e.printStackTrace();
}
try {
System.out.println(" Attachment");
EmbeddedObject em = doc.getAttachment(name);
org.xml.sax.InputSource is = em.getInputSource();
if (is != null)
System.out.println(
" System ID from Attachment InputSource - "
+ is.getSystemId());
else
System.out.println(" No Attachment InputSource! " );
org.w3c.dom.Document domdoc = em.parseXML(false);
em.recycle();
} catch (Exception e){
e.printStackTrace();
}
try {
Item StyleName = doc.getFirstItem("UseStyle");
if ((StyleName != null) &&
(! (StyleName.getText().equals(""))))
{
System.out.println(
" UseStyle(" + StyleName.getText() + ")");
Item xml = doc.getFirstItem("TextBody");
if (xml == null) System.out.println(
"xml is null!");
Document StyleDoc =
view.getDocumentByKey(StyleName.getText());
if (StyleDoc == null)
System.out.println("Style doc not found!");
else
{
Item xsl = StyleDoc.getFirstItem("TextBody");
if (xsl == null) System.out.println(
"xsl is null!");
XSLTResultTarget out = new XSLTResultTarget();
out.setFileName(name + "_" +
StyleName.getText() + ".out");
xml.transformXML(xsl, out);
}
xml.recycle();
StyleDoc.recycle();
StyleName.recycle();
}
} catch (Exception e){
e.printStackTrace();
}
doc = view.getNextDocument(doc);
}
}
}
- This WebQuerySave agent takes the current document, uses the generateXML method to create an XML document, styles it with the included XSL stylesheet, and sends a mail message to a mail-in database named "Book Orders" with the resulting XML.
import lotus.domino.*;
import org.xml.sax.InputSource;
import java.io.*;
public class JavaAgent extends AgentBase {
public void NotesMain()
{
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db = agentContext.getCurrentDatabase();
Document order = agentContext.getDocumentContext();
Document msg = db.createDocument();
msg.appendItemValue("Form", "Memo");
msg.appendItemValue("Subject", "Got an order!");
RichTextItem body = msg.createRichTextItem("Body");
InputSource style = new
InputSource(new StringReader(promote_xsl));
StringWriter resultWriter = new StringWriter();
XSLTResultTarget result =
new XSLTResultTarget(resultWriter);
order.generateXML(style, result);
body.appendText(resultWriter.toString());
msg.send("Book Orders");
// This agent is run at the end of handling a form, so we
// have to specify the HTML to display next.
getAgentOutput().println(
"<head><title>Thank you</title><h2>" +
"Thank you for your order!</h2>");
getAgentOutput().println(
"<p><b><a onClick='self.close()' "href='#'>" +
"Click here to close this window.</a></b></p>");
} catch(Exception e) {
e.printStackTrace();
}
}
// Promote.xsl:
// Turn DXL document into simple XML
// A document is turned into an XML element named after the Form.
// Each item becomes a child element named after the item.
private String promote_xsl =
"<?xml version='1.0'?>"+
"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"+
"version='1.0'>"+
"<xsl:template match='document'>"+
" <xsl:element name='{translate(@form,\" \", \"\")}'>"+
" <xsl:for-each select='item'>"+
" <xsl:choose>"+
" <!-- Items whose names have dollars are skipped -->"+
" <xsl:when test='contains(@name, \"$\")'/>"+
" <!-- Items named SaveOptions are skipped -->"+
" <xsl:when test='@name=\"SaveOptions\"'/>"+
" <!-- Items starting with 'dsp' are skipped -->"+
" <xsl:when test='starts-with(@name, \"dsp\")'/>"+
" <xsl:otherwise>"+
" <xsl:element name='{@name}'>"+
" <xsl:value-of select='*'/>"+
" </xsl:element>"+
" </xsl:otherwise>"+
" </xsl:choose>"+
" </xsl:for-each>"+
" </xsl:element>"+
"</xsl:template>"+
"</xsl:stylesheet>\n";
}
- This agent runs when new mail arrives. It parses the body of the mail message to create a document and items from the XML data.
import lotus.domino.*;
import org.w3c.dom.*;
public class JavaAgent extends AgentBase {
public void NotesMain()
{
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
lotus.domino.Document inDoc =
agentContext.getDocumentContext();
// Parse the XML body of the mail message.
RichTextItem inBody = (RichTextItem)
inDoc.getFirstItem("Body");
org.w3c.dom.Document inDom = inBody.parseXML(false);
// Call the item shredder and Save the result
shredXML(inDom.getDocumentElement(), inDoc);
inDoc.save();
} catch (Exception e) {
if (e instanceof NotesException)
{
System.err.println(((NotesException)e).text);
}
e.printStackTrace();
}
}
//Convert an XML DOM tree into a set of items on a document.
// Each child of the given Node is turned into an item named
// after the element.
private void shredXML(Node n, lotus.domino.Document doc)
throws lotus.domino.NotesException
{
for(Children cTop = new Children(n); cTop.hasMoreNodes(); ) {
Node node = cTop.nextChild();
if (node.getNodeType() == Node.ELEMENT_NODE) {
doc.appendItemValue(node.getNodeName(),
getChildrenText(node));
}
}
}
// A DOM helper method to collect up all of the text content
// for a node. All of the children (and children of children,
// and ...) are examined, and all of the text is concatenated
// together.
private String getChildrenText(Node n)
{
String text = "";
for(Children c = new Children(n); c.hasMoreNodes(); ) {
Node child = c.nextChild();
switch (child.getNodeType()) {
case Node.DOCUMENT_NODE:
case Node.ELEMENT_NODE:
// For an element or document root, recur on the children
// of the node.
text += getChildrenText(child);
break;
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
// For text content or CDATA nodes, append the content.
text += child.getNodeValue();
break;
}
}
return text;
}
}
// A helper class for the DOM: an iterator over the children
// of a node.
import org.w3c.dom.*;
public class Children {
private NodeList nodelist;
private int i;
private int numChildren;
// Construct a Children from a Node.
public Children(Node n)
{
nodelist = n.getChildNodes();
if (nodelist == null) {
numChildren = 0;
}
else {
numChildren = nodelist.getLength();
}
i = 0;
}
// Are there any more children, or have we used them all?
public boolean hasMoreNodes()
{
return i < numChildren;
}
// Return the next child.
public Node nextChild()
{
if (i >= numChildren) {
return null;
}
return nodelist.item(i++);
}
}