Help Retaining RichText Format

Greetings,

I’m writing an agent to read the body of the document and search for “U.S.” and when it finds it I want to replace it with “United States”. My search and replace is working but when I write the body back out and save it the formatting is lost and everything is strung together.

If anyone can shed light/direction on how to retain the RichText format it would be appreciated.

Here’s my agent code:

import lotus.domino.*;

import java.lang.String;

import java.util.*;

import java.io.*;

public class JavaAgent extends AgentBase

{

public void NotesMain() 

 {

    try 

    {

        Session session = getSession();

        AgentContext agentContext = session.getAgentContext();

        // (Your code goes here)           	     

        Database db = agentContext.getCurrentDatabase();     

       //View view = db.getView("($Inbox)");    

        View view = db.getView("IDDE");

        Document doc = view.getFirstDocument();

        while (doc != null) 

        {

           //   System.out.println(doc.getItemValueString("Subject"));

           Enumeration items = doc.getItems().elements();

  

           while (items.hasMoreElements()) 

           {

              Item item = (Item)items.nextElement();

              //      if (item.getName().equals("Subject")) 

              //      {

              //         System.out.println("Subject Text:\n" + item.getText()); 

              //      }

              if (item.getName().equals( "Body" )) 

              {

                 //         System.out.println("Body Text:\n" + item.getText()); 

                 String oldBody = doc.getItemValueString("Body");

                 String oldText = "UNITED STATES";

     			 String newText = "UNITED STATES AND IRELAND";         

     			 StringBuffer result = new StringBuffer();

     			 int startIdx = 0;

     			 int idxOld = 0;

     			 while ((idxOld = oldBody.indexOf(oldText, startIdx)) >= 0) 

     			 {

        			result.append(oldBody.substring(startIdx, idxOld));

        			result.append(newText);

        			startIdx = idxOld + oldText.length();

     			 }

     			result.append(oldBody.substring(startIdx));

     			String newBody = result.toString();

			//         System.out.println("New Body Text:\n" + newBody);

     			oldBody = newBody;         

     			oldText = "U.S.";

     			newText = "U.S. AND IRISH";         

     			result = new StringBuffer();

     			startIdx = 0;

     			idxOld = 0;

     			while ((idxOld = oldBody.indexOf(oldText, startIdx)) >= 0) 

     			{

       		 	result.append(oldBody.substring(startIdx, idxOld));

        			result.append(newText);

        			startIdx = idxOld + oldText.length();

     			}

     			result.append(oldBody.substring(startIdx));

     			newBody = result.toString();     

     			doc.replaceItemValue("Body", newBody);

     			System.out.println("New Body Text:\n" + newBody); 

     			System.out.println("rich text:\n" + getRichTextItem());

     			doc.save();

     			System.out.println("bodyText:\n" + item.getText());          

  		 }

	 }                        

 doc = view.getNextDocument(doc); 

 }     

	} 

      catch(Exception e) 

      {

		e.printStackTrace();

	}

}

}

If there is an easier way I’m all for it. I tried the @ReplaceSubString function but had problems getting it to work.

Cheers,

Andrew

Subject: Help Retaining RichText Format

You need to treat Rich Text as Rich Text. That is, you need to cast Body to RichTextItem, then create a Range or Navigator, and replace text within TextRuns. The code’s going to be a lot more verbose than it is now, by quite a bit. The Java examples in Help aren’t quite what they could be – the LotusScript equivalents will fill in the blanks.

Subject: RE: Help Retaining RichText Format

Thanks Stan for the reply.I figured this wasn’t an easy thing to do.

Would it be easier or better to get this to work in LotusScript or to use Formulas? I’ve not used either of them before.

Cheers,

Andrew

Subject: RE: Help Retaining RichText Format

It has to be LotusScript or Java. Formula doesn’t have the needed hooks. (Java will do the job – it’s just that the Help is a little weak. Using the cross-language reference links to get the LotusScript help examples for the equivalent Java classes is a good way to get additional help.) Come to think of it, though, you could do the same thing with DXL (Domino XML), and using Java’s XML stuff, you could do a database-wide search’n’replace in one fell swoop.