Archive for the ‘LotusScript’ Category

 
Jan
26
Posted (Johan Känngård) in LotusScript on January-26-2006

A useful class for you to use at your own risk. An implementation of a last-in-first-out data structure. Can be used like:


Dim stack As New Stack()
Call stack.push("Domino")
Call stack.push("Notes")
Call stack.push("R7")
 
Print "Stack is: " & stack.toString()
 
Print "First: " & stack.pop()
Print "Second: " & stack.pop()
Print "Third: " & stack.pop()
' This will produce the output:
' R7
' Notes
' Domino


Thanks to Vitezsla Vit VLCEK and Chad “Smiley” Schelfhout for pointing out bugs in the original class.



 
Jan
17
Posted (Johan Känngård) in LotusScript, XML on January-17-2006

I am not Microsoft’s biggest fan, since I was a Macintosh fanatic in the old days, but had to convert into the Wintel platform for about 10 years ago. :-) But I have to admit though, when comparing Microsoft’s XML parser with the ones that are built into Notes/Domino, that their products sometimes are great!
When comparing Microsoft’s XML parser with the other two, I created an agent that parsed a big (over 700 KB) and complex XML document and read an element’s value. I did this 25 times per parser, and came up with this interesting result (average execution time per parser) when executed in the Notes client:

  1. Microsoft XML Parser: 0.2 seconds
  2. NotesSAXParser: 2.2 seconds
  3. NotesDOMParser: 2.5 seconds

When executed on a Domino server, the result was a bit different, since the server is a bit old:

  1. Microsoft XML Parser: 1.0 seconds
  2. NotesSAXParser: 5.0 seconds
  3. NotesDOMParser: 7.7 seconds


 
Jan
11
Posted (Johan Känngård) in LotusScript, XML on January-11-2006

In my earlier post today, I described a problem I have, where I wanted to get the XML string of a DOM tree. I’ve started writing my own routine, this is what I have right now. It’s far from complete, since it can not handle namespaces, CDATA etc. yet:
This is a new version that handles siblings better, and not getting out of stack space :-)


Public Sub serializeNode(node As NotesDOMNode_
, outStream As NotesStream)
  On Error Goto catch
  
  If node.IsNull Then Exit Sub
  
  Dim currentNode As NotesDOMNode
  Set currentNode = node
  
  Do Until currentNode.isNull
    Select Case currentNode.nodeType
    Case DOMNODETYPE_DOCUMENT_NODE
    Case DOMNODETYPE_XMLDECL_NODE
      Dim xmlDecl As NotesDOMXMLDeclNode
      Set xmlDecl = currentNode
      Call outStream.WriteText("< ?xml version=""" _
+ xmlDecl.version + """ encoding=""" + xmldecl.encoding + """?>")
    Case DOMNODETYPE_ELEMENT_NODE
      Call outStream.writeText("< " + currentNode.nodeName + "")
      Dim nodeMap As NotesDOMNamedNodeMap
      Set nodeMap = currentNode.attributes
      Dim attribute As NotesDOMNode
      Dim i As Integer
      
      For i = 1 To nodeMap.numberOfEntries
        Set attribute = nodeMap.getItem(i)
        Call outStream.WriteText(" " + attribute.nodeName + "=")
        Call outStream.WriteText("""" + attribute.NodeValue + """")
      Next i
      Call outStream.writeText(">")
    Case DOMNODETYPE_TEXT_NODE
      Call outStream.writeText(currentNode.NodeValue)
    Case DOMNODETYPE_COMMENT_NODE
      Call outStream.writeText("<!--" _
+ currentNode.NodeValue + "-->")
    Case Else
      Error 2000, "Unhandled node type [" _
& currentNode.nodeType & "]"
    End Select
    
    Dim endWritten As Boolean
    
    If currentNode.hasChildNodes Then
      Call serializenode(currentNode.firstChild, outStream)
    End If
    
    If currentNode.NodeType = DOMNODETYPE_ELEMENT_NODE Then
      Call outStream.writeText("")
      endWritten = True
    End If
    
    Set currentNode = currentNode.nextSibling
  Loop
  
  If node.NodeType = DOMNODETYPE_ELEMENT_NODE _
And Not endWritten Then
    Call outStream.writeText("")
  End If
  Exit Sub
catch:
  Error Err, Err & Lsi_Info(2) & ":" & Erl & "|" & Error
End Sub



 
Jan
11
Posted (Johan Känngård) in LotusScript, XML on January-11-2006

I have a problem, that nor I nor any of my colleagues can’t solve easily. I have several classes that are passing objects/variables between them. On of them is handling Web Service request, and parsing the responses to a NotesDOMDocumentNode via the NotesDOMParser. The NotesDOMDocumentNode is passed returned, and are used in several places to extract information. But, I need to cache the response, so concurrent request with the same parameters are sped up.
To cache the response, I need to serialize the NotesDOMDocumentNode. At first, I thought this was easy, since there is a Serialize method on the NotesDOMParser. But no… You have to keep the original NotesDOMParser that parsed the raw XML data. The only solutions I can figure out are:

  • pass both the NotesDOMDocumentNode AND the original NotesDOMParser at all times
  • write my own Serialize method
  • pass the XML string only, and do the parsing at each method. I think this would be really slow…

To further illustrate what I want, here is an example:

Public Sub testDOMParser()
  Dim parser As NotesDOMParser
  Dim inStream As NotesStream
  Dim node As NotesDOMDocumentNode
  Set inStream = session.createStream()
  Call inStream.open("c:\temp\test.xml")
  Set parser = session.createDOMParser(inStream)
  Call parser.process()
  Set node = parser.document
  Call serializeDocument(node)
End Sub
Public Sub serializeDocument(node As NotesDOMDocumentNode)
  ' Here, I want to do something like this:
  Dim outStream As NotesStream
  Dim parser As NotesDOMParser
  Set outStream = session.createStream()
  Set parser = session.createDOMParser(node, outStream)
  parser.exitOnFirstFatalError = True
  Call parser.serialize()
  Print "Serialized tree: " + outStream.readText()
End Sub

Anyone who have had any experience with this and are willing to share their thoughts? We are talking R6 here…



 
Dec
16
Posted (Johan Känngård) in LotusScript, XML on December-16-2005

So I don’t forget the next time I want to do this, and trust me, I got a short memory!

Dim xml As String
Dim rootElementNode As NotesDOMElementNode
xml = "YOUR XML HERE"
Set rootElementNode = parseXML(xml)._
getElementsByTagName("THE ROOTNODE").getItem(1)
Public Function parseXML(xml As String) _
As NotesDOMDocumentNode
  ' Parses the specified xml.
  On Error Goto catch
  Dim session As New NotesSession()
  Dim stream As NotesStream
  Set stream = session.createStream()
  Call stream.writeText(xml)
  Dim domparser As NotesDOMParser
  Set domparser = session.createDOMParser(stream)
  domParser.exitOnFirstFatalError = True
  Call domParser.parse()
  Set parseXML = domparser.Document
  Exit Function
catch:
  Error Err, _
Err & "|" & Error & "|" & Lsi_info(2) & ":" & Erl
End Function



 
Dec
13
Posted (Johan Känngård) in LotusScript on December-13-2005

To send HTML mails in R6, you have to use the setContentFromText method in the NotesMIMEEntity class. To add inline images, use the setContentFromBytes method in the same class, like this:

Dim session as New NotesSession()
session.convertMIME = False ' Do not convert to rich text
Dim currentDb As NotesDatabase
Set currentDb = session.currentDatabase
Dim path As String
path = "c:\temp\test.html"
Dim message As NotesDocument
Set message = currentDb.createDocument()
Call message.replaceItemValue("Form", "memo")
Call message.replaceItemValue("From", "your@email.com")
Call message.replaceItemValue("Subject", _
"HTML email via MIME")
Call message.replaceItemValue("SendTo", "another@email.com")
Dim body As NotesMIMEEntity
Set body = message.CreateMIMEEntity
Dim mh As NotesMimeHeader
Set mh = body.CreateHeader({MIME-Version})
Call mh.SetHeaderVal("1.0")
Set mh = body.CreateHeader("Content-Type")
Call mh.SetHeaderValAndParams( _
{multipart/related;boundary="=NextPart_="})
Dim mc As NotesMIMEEntity
Set mc = body.createChildEntity()
Dim stream As NotesStream
Set stream = session.createStream()
If Not stream.open(path, "ISO-8859-1") Then
  Error 2000, "Could not open file"
End If
Call mc.setContentFromText(stream, _
{text/html;charset="iso-8859-1"}, ENC_NONE)
Call stream.close()
Call addImage(body, "C:\temp\image1.gif")
Call addImage(body, "C:\temp\image2.gif")
Call message.send(False)


The addImage method looks like this:

Public Sub addImage(body As NotesMimeEntity, _
imagePath As String)
  Dim id As String
  Dim stream As NotesStream
  Dim mh As NotesMimeHeader
  Dim mc As NotesMIMEEntity
  id = Strrightback(imagePath, "\")
  Set stream = session.createStream()
  Set mc = body.createChildEntity()
  Set mh = mc.createHeader({Content-ID})
  Call mh.setHeaderVal(id)
  Call stream.open(imagePath)
  Call mc.setContentFromBytes(stream, _
"image/gif;name=""" + id + """", ENC_IDENTITY_BINARY)
  Call stream.close
End Sub

The HTML code must refer to the images via URL:s containing cid and the “path” to the image, like this:

&lt;img src="cid:imagename.gif" /&gt;

Found this great tip via LDD, posted by Raymond Neeves.



 
Nov
11
Posted (Johan Känngård) in LotusScript, Visual Basic, Windows on November-11-2005

I have tried to search for the literal values of some of the options to WinHTTP that handles SSL server certificates errors. In my case I have a development server that has a certificate with the wrong name in it, and I want to ignore the usual “The host name in the certificate is invalid or does not match” error. Since I am using LotusScript to access the COM class WinHTTP.WinHTTPRequest, I can’t simply use:

Dim http As Variant
Set http = CreateObject("WinHTTP.WinHTTPRequest.5.1")
http.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = 13056

This of course generates an error, since the WinHttpRequestOption_SslErrorIgnoreFlags constant can not be found in LotusScript.
The answer couldn’t be found anywhere on MSDN, but some searching on Google led me to a page at fastbugtrack.com with some code that contains the constant I needed! So here they are for future reference:

' WinHTTPRequest options:
Const WinHttpRequestOption_UserAgentString = 0
Const WinHttpRequestOption_URL = 1
Const WinHttpRequestOption_URLCodePage = 2
Const WinHttpRequestOption_EscapePercentInURL = 3
Const WinHttpRequestOption_SslErrorIgnoreFlags = 4
Const WinHttpRequestOption_SelectCertificate = 5
Const WinHttpRequestOption_EnableRedirects = 6
Const WinHttpRequestOption_UrlEscapeDisable = 7
Const WinHttpRequestOption_UrlEscapeDisableQuery = 8
Const WinHttpRequestOption_SecureProtocols = 9
Const WinHttpRequestOption_EnableTracing = 10

The actual value for http.Option(WinHttpRequestOption_SslErrorIgnoreFlags) that I have set to 13056 (hexadecimal 0×3300) is an addition of all switches found at the WinHttpRequestOption documentation at WinHttpRequestOption_SslErrorIgnoreFlags.



 
May
06
Posted (Johan Känngård) in LotusScript, Programming on May-6-2002

Here is my Vector class, that makes handling of arrays easier. You also have to use the Enumeration and VectorEnumeration classes that are attached. Use it like this:


Dim v As New Vector()
Call v.addElement("Domino")
Call v.addElement("Notes")
Call v.addElement("R5")
Print "Elements are: " & v.toString()

Results in the text “Elements are: Domino, Notes, R5″

You may download and use the code at your own risk.


On request, I have changed the license from GPL to LGPL on these files.

I have updated the Vector class with a bug fix that Mikkel Heisterberg kindly supplied. The code is now also GPL:ed.



 
Apr
25
Posted (Johan Känngård) in LotusScript, Programming on April-25-2002

The LotusScript language in Domino is very powerful once you find out that you can create custom classes with it. I´m going to write some articles about creating great helper classes, and start off with the basics of creating user classes. I am NOT going to explain why object oriented programming is better than procedure programming…

The definition of a class looks like this:

Public Class Car
End Class

The Public tells that this class will be seen outside the agent or scriptlibrary, where the class is created. You can also make a Private class, that only the agent or script library can see, where the class is defined:

Private Class Car
End Class

To create an instance of a class, you use the New keyword:


Dim myCar as New Car()

or:


Dim myCar as Car
Set myCar = New Car()

To add functionality to a class, you can create functions, subroutines, property getters and property getters. I will not explain the last ones, as they easily can be transformed into functions and subs. To add a function, that returns a value, you write like:


Public Class Car
 
   Public Function getTopSpeed() as Integer
   End Function
 
End Class

To add a subroutine, that does not return a value, you write:


Public Class Car
 
   Public Sub start()
   End Sub
 
End Class

There are two special subroutines, delete and new:


Public Class Car
 
   Public Sub new()
   End Sub
 
   Public Sub delete()
   End Sub
 
End Class

The new subroutine, also called constructor, is called whenever you use the New keyword to create a new instance of the class. The delete subroutine is called when there are no references to the object.

To add arguments that can be passed to subroutines and functions, just add them like you do to any ordinary function or subroutine:


Public Class Car
 
   Public Sub new(topSpeed As Integer)
   End Sub
 
End Class

To add instance member variables, declare them inside the class, without the usual Dim:


Public Class Car
 
   topSpeed As Integer
 
End Class

To use instance member variables, you handle them like any other variable. A special case is show below, when a function has an argument variable, that has the same name as the instance variable. The keyword Me is used to denote the current object, and when saying Me.topSpeed, we mean the instance member variable topSpeed, and not the function argument variable :


Public Class Car
 
   topSpeed As Integer
 
   Public Sub new(topSpeed as Integer)
      Me.topSpeed = topSpeed
   End Sub
 
   Public Function getTopSpeed() as Integer
      getTopSpeed = topSpeed
' Can also be written as:
' Me.getTopSpeed = Me.topSpeed
   End Function
 
End Class

To remove an object from memory, you use the Delete keyword. This removes the actual object from memory, and all references that are using the object can not use it any more.


Dim myCar as New Car(120)
Delete myCar

If you just want to relase a reference to an object, you use the Nothing keyword. This will only remove the specified reference, but the object is still in memory, until ALL references are removed.


Dim myCar as New Car(120)
Set myCar = Nothing

You can test if an object reference is still valid, by comparing the object reference to Nothing:


Dim myCar as New Car(120)
' Do some work...
If myCar Is Nothing _
Then Error 2000, "Object reference not valid"

To check what type an object reference is, you can use the IsA keyword. This is useful, when you don´t know at compile time what type of object you handle:


Dim myVehicle as Variant
 
If userName = "Donald" Then
   Set myVehicle = New Helicopter()
Else
   Set myVehicle = New Bicycle()
End If
If myVehicle IsA "Helicopter" _
Then Print "Fly away!"

The problem with the above is, that if you want to assign non-objects to the same variable, you must test if the variable is an object, before trying to use IsA or testing if it is Nothing:


Dim myVehicle as Variant
 
If userName = "Donald" Then
   Set myVehicle = New Helicopter()
Else If userName = "Mickey" Then
   Set myVehicle = New Bicycle()
Else
   myVehicle = "Illegal user?"
End If
 
If IsObject(myVehicle) Then
   If myVehicle IsA "Helicopter" _
   Then Print "Fly away!"
Else
   Print "No object, probably strange user..."
End If

To get the name of the class that was used to create an object instance, use the keyword Typename:


Dim myCar As New Car(120)
' Will print "Car":
Print "The type of object is: " & Typename(myCar)...

Ok, let´s do some useful with our new knowledge, let´s create a working Car class. The start sub must be called before the accelerate() method is called.


Public Class Car
 
   topSpeed As Integer
   currentSpeed As Integer
   started As Intger
 
   Public Sub new(topSpeed As Integer)
      Me.topSpeed = topSpeed
   End Sub
 
   Public Sub start()
      started = True
   End Sub
 
   Public Sub stop()
      currentSpeed = 0
      started = False
   End Sub
 
   Public Function accelerate() As Integer
      If Not started _
      Then Error 2000, "Start the before driving"
      If currentSpeed => topSpeed _
      Then Error 2000, "Top speed reached"
      currentSpeed = currentSpeed + 10
      accelerate = currentSpeed
   End Function
 
   Public Function getTopSpeed() As Integer
      getTopSpeed = topSpeed
   End Function
 
   Public Function getCurrentSpeed() As Integer
      getCurrentSpeed = currentSpeed
   End Function
End Class

To use our class, we can write:


Dim car As New Car(120)
Call car.start()
Do While car.getCurrentSpeed() < car.getTopSpeed()
   Print "Current speed is: " & car.accelerate()
Loop
Print "Top speed " & car.getTopSpeed() & " reached!"
Call car.stop()
Print "Car parked!"
Set car = Nothing

There is more to learn about classes in LotusScript, so stay tuned for the next article! Meanwhile, check the following links:

“Using the object-oriented features of LotusScript” on Lotus Developer Domain, great explanation of all (?) advantages!
“User-defined classes” in the Domino Designer Help 5.0.3
The Unfinished LotusScript Book by Julian Robichaux
Bill Buchan’s Lotus Notes programming tips