A Couple Handy Domino Java String Utils
Wed Jan 25 11:08:30 EST 2012
As most developers probably do, I have a grab bag of "utility" functions/methods I use across various projects, and I figured I'd post a few of the handier ones.
The first is a basic XML-encoding function that just takes a string and returns something suitable for putting into an XML or HTML file. I've been too lazy to figure out if the standard Java library has an equivalent that doesn't involve creating an actual XML document in memory, so I just took the one I use for LotusScript and ported it over. It passes standard letter and number characters through as-is and then just encodes all others as Unicode entities. The string size increases accordingly, but it's presumably fast and it gets the job done, even if you have oddball characters.
The next set are just quick-and-dirty Java equivalents to the StrLeft, StrRight, etc. functions from LotusScript. They do more or less the same thing as in LS and make it so you don't have to worry about string indexes all the time.
The last is potentially the most useful, since it's the most esoteric: a special-text decoder. I've had a couple occasions where I want to spit out the contents of a view for the web, but also want to preserve things like child counts on categories. Fortunately, special text is stored in the view column in a workable format: a special non-alphanumeric character, a single letter indicating which function it corresponds to, a number representing the parameter count for the function, and then a delimited list of those parameters with character counts. Using that, I wrote a routine to process all of the special text functions I could think of other than @IsExpandable (since that's meant for the Notes UI). It even reproduces the weirdo behavior you get when you pass a multi-character string to @DocLevel.
Paul Withers - Wed Jan 25 15:55:30 EST 2012
A couple of months ago I hit the same issues that a number of string functions were standard in LotusScript but there was nothing in Java. Nathan Freeman pointed me towards one of the many Java packages that come natively with Domino and the Domino server. I blogged about it here http://www.intec.co.uk/why-reinvent-the-wheel-when-there-are-1500-already/ and followed up on adding them to XPages http://www.intec.co.uk/how-to-add-in-built-java-packages/
One of the real benefits of the StringUtils package from Apache is that it is null safe, so you don't need to check if a value is null when comparing values.