• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

Richtext find and replace

Hi Guys
Anybody has a quick code to find and replace a text in richtext using NotesDXL . If yes, can it be shared :). I had to do some stuff quickly and i don't want the headache of writing the entire code. Any ideas, and oh i know about Midas, but i specifically need NotesDXL and NotesDomParser code.

TIA
Partha
0
p_partha
Asked:
p_partha
  • 6
  • 4
1 Solution
 
Sjef BosmanGroupware ConsultantCommented:
Hi Bro, yours is a difficult question... I'd be happy to develop something with you, but since you asked for a quicky I didn't respond. You suggest that, to write the code, you'll end up with a headache, but is it really that difficult? Let's dive into it!

First, let's have some design. How id you think to attack this interesting problem? Something like

    get document
    get richtext field
    dump richtext to file using Notes' DXL methods
    parse DXL and make alterations
    reload richtext from DXL
    save document
0
 
p_parthaAuthor Commented:
Bro, if you are with me on this, we can easily crack it  .....

But here is some code i got from notes.net, but couldn't make it to work, as it was throwing a error that Domparser is not initialized, i must be doing something silly , dont' know what , may be another pair of eyes would do :

      Dim session As New notessession
      Dim db As notesdatabase
      Set db = session.currentdatabase
      Dim ws As New notesuiworkspace
      Dim uidoc As notesuidocument
      Dim doc As notesdocument
      Set uidoc = ws.currentdocument
      Set doc = uidoc.document
      
      
      
      Dim exporter As NotesDXLExporter ' exports notes doc to XML code
      Dim domParser As NotesDOMParser ' transforms XML code to a DOM tree
      Dim importer As NotesDXLImporter ' imports XML code as notes document
      Dim itemList As NotesDOMNodeList ' a list of <item>-nodes
      Dim node As NotesDOMNode 'an <item> node
      Dim aNode As NotesDOMAttributeNode 'a "name" attribute
      
      
'create a temporary RichText field in the notes doc you want to work with
      
      Set rt = doc.createRichTextItem("TMPXML")
      
      
      
      
      Set exporter = session.CreateDXLExporter
      Set domParser = session.createDOMParser
      Set importer = session.CreateDXLImporter
      
'set the NotesDocument as input for the exporter:
      
      Call exporter.SetInput(doc)
      
'set the exporter as input and the rt as output for the domParser:
      
      Call domParser.setInput(exporter)
      Call domParser.SetOutput(rt)
      
'set some options for the importer:
      
      importer.ACLImportOption = DXLIMPORTOPTION_IGNORE
      importer.DesignImportOption = DXLIMPORTOPTION_IGNORE
      importer.DocumentImportOption = DXLIMPORTOPTION_REPLACE
      
'run the exporter:
      
      Call exporter.process
'---
'get the list of <item> elements:
      
      Set docNode = domParser.Document
      Set rootElement = domParser.Document.DocumentElement
      Set itemList = rootElement.GetElementsByTagName ("item")
      
'and search the itemList for your RichText field, f.g. "Body":
      
      For k = 1 To itemList.NumberOfEntries
            Set node = itemList.GetItem(k)
            Set aNode = node.Attributes.GetItem(1)
            If (Ucase(aNode.AttributeValue) = Ucase("BLASTEMAILBODY")) Then
' here we have the node which represents the BODY-field of the notes document
' and we're delegating the work to a sub-function
                  Call parseChild(node.FirstChild, doc)
            End If
      Next '<item> node
' the parseChild() has done all work, now we have a modifed tree of XML data in the
' domParser object
' now we order the domParser to put out his data to the temporary RichText field
      Call domParser.serialize()
' and set this RichText field as input for the importer
      Call importer.setInput(rt)
' the output is for sure this current database
      Call importer.setOutput(db)
' and tell the importer to do the import-process
      Call importer.process()
' finally we're removing the temporary RichText field
      Call doc.RemoveItem("TMPXML")
      
      
'the sub "parseChild" does the work (and does some recursion to walk down the DOM tree):
0
 
p_parthaAuthor Commented:
Oh, here is other functions :
Private Sub parseChild(node As NotesDOMNode, doc As NotesDocument)
      Dim child As notesdomnode
      Dim numChildNodes As Integer
      
      If Not (node.isNull) Then
            
            numChildNodes = node.NumberOfChildNodes
            
            If Not ( node.HasChildNodes) And (node.NodeType = DOMNODETYPE_TEXT_NODE) Then
                  Call doWork(node, doc)
            End If
            
            Set child = node.FirstChild
            While numChildNodes > 0
                  Call parseChild(child, doc)
                  Set child = child.NextSibling ' Get next node
                  numChildNodes = numChildNodes - 1
            Wend
      End If
End Sub


Private Sub doWork(node As NotesDOMTextNode, doc As NotesDocument)
      Dim s As String
      s = node.nodeValue
      l = Len(s)
      s = "Partha"
      Call node.replaceData(1, l, s)
End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sjef BosmanGroupware ConsultantCommented:
I left my eyes on the couch downstairs... Maybe later today. ;-)
0
 
Sjef BosmanGroupware ConsultantCommented:
Ah, oh, no, can't be true... Don't you use
    Option Declare
in your Options section?? Otherwise you would have found out that DXLIMPORTOPTION_REPLACE has no value. Unless of course your code above isn't complete, and there is an include-file missing. The same probaly goes for the other constants. Ah, no, it's the only one: it just doesn't exist, it's either DXLIMPORTOPTION_REPLACE_ELSE_IGNORE or DXLIMPORTOPTION_REPLACE_ELSE_CREATE

Tsk, tsk, tsk!
0
 
Sjef BosmanGroupware ConsultantCommented:
It stops on the line
    Call domParser.SetOutput(rt)
indeed with the error you mentioned, but there's more:
    DomParser output object is invalid, uninitialized, or not explicitly declared and strongly typed.

I assume therefore that you must do something with rt. What? That will be discussed in the episode after my dinner ;)
0
 
p_parthaAuthor Commented:
I did declare rt and it replaces kind of now... which is a good sign:) i will keep you posted abt the other learnings. This process has taken most of my time (although i don't have any :(( )


Partha
0
 
Sjef BosmanGroupware ConsultantCommented:
I tested with one of the test databases, and indeed it "replaces kind of". The big danger of the Importer is that it overwrites existing documents, so you might take some precautions. What I'd do:
- copy document
- change form name, so it won't appear anywhere
- save that copy
- put the TMP field in yet another temp document
- export, process and import the copy
- copy the rich text field back to the original document
- scrap anything you don't need

Be careful with the copy document, users may not have Delete permission
0
 
p_parthaAuthor Commented:
Thanks Bro
0
 
Sjef BosmanGroupware ConsultantCommented:
So it works (kind of)... ;)

Thanks for the grade, and you're always welcome!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now