www.zeroonezero.com DDA CMT DDA Medical Dynamic Digital Advertising DDA Video DDA Apps DDA USA DDA SEM

I’m Rich!

Yesterday’s “good times” in the world of custom programming involved figuring out a new solution for an old “rich text” textbox.  In the 5 years I’ve been in website programming, I have used a handful of web-based rich text editors, fancy and not so fancy.  Eventually they all failed, as application programming normally does, either they didn’t keep up with new browsers or they had other fatal flaws, like not working under HTTPS.  Yesterday I transitioned to my latest friendly neighborhood editor, WYMEditor, based in jQuery (my new ”BFF”).   I was able to use that in IE 7 under HTTPS, which failed miserably in all the other cases for whatever reason.  Once I figured that out, I had the mission of revamping the code necessary to do the task at hand.  What was this task?  I’m glad you asked.

The reason a WYSIWYM (What You See is What You Mean) Web-based editor is necessary is that the output of the text editor is to be a rich text document that gets attached and sent via email.  It’s got placeholders that will be filled in with the appropriate person’s name via database, so I wasn’t going to hang myself on using Word.  So this html text needs to be formatted happily to RTF format.  At this time I use iText, which has always been good to me, whether I’m outputting to RTF or PDF (lets not talk about Coldfusion’s implementation).  This process is pretty straightforward.  I have to swap out the basic html that I allow in the text editor (<strong>,<i> <p> etc), replace it with the equivalent iText/RTF formatting and of course remove (or swap html characters with ascii) any other extraneous html.  Maybe some day I’ll share that code.

But we also need a preview window, one that isn’t just clicking on the downloadable document.  OK, well, we didn’t really need it, but I added it ages ago, and now I have to maintain it.  Here’s where the new code comes in, to read the RTF document and display it in a DIV on the page.  I found some source code snippet which uses JAVA, so this is not fully my idea, I knew what it was that I could do, but I found code to do it before I got through the JAVA API so it’s just my Coldfusion implementation.  It uses several nifty java components in very interesting ways.  Rather than other implementations I have seen, it requires no use of temporary .xml or other random files because of the StringWriter object - which makes a string out of a stream, fantastic.

  <cfset tempLocation = #location of my rich text file that I'm reading in, absolute path#>
<cfset htmlDoc = createObject("java", "java.io.StringWriter").init()>
<cfset fileStream = CreateObject("java","java.io.FileInputStream").init(javaCast("string", tempLocation))>

<cfset HTMLkit = createObject("java", "javax.swing.text.html.HTMLEditorKit")>
<cfset RTFkit = createObject("java", "javax.swing.text.rtf.RTFEditorKit")>
<cfset RTFDoc = RTFkit.createDefaultDocument()>
<cfset RTFkit.read(fileStream, RTFDoc, 0)>
<cfset htmlKit.write(htmlDoc, RTFDoc, 0, RTFDoc.getLength())>

<cfset formattedText = htmlDoc.toString()>
<cfset fileStream.close()>

So we do a standard filestream open of our rich text document.  Using the RTFEditor kit, we read it in to a default document object.  Then, using the stringwriter and HTMLEditorKit, we write the .rtf document into an html faux document and then output the text of that document to a variable that we use later on within the DIV.   Now this may be a ‘one-time use’ piece of custom programming, but it’s still interesting to work through.  Knowing what it is that you want to do, finding the tools you need,  and then executing the plan.  I love programming.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • e-mail
  • Reddit
  • StumbleUpon
  • Furl
  • Technorati

Entry by: amy


type and hit 'enter'