[daisy] Javascript API & htmlcleaner

Bruno Dumon bruno at outerthought.org
Mon Sep 17 06:18:39 CDT 2007


Hi,

On Mon, 2007-09-17 at 11:32 +0200, Jano Kula wrote:
> Greetings!
> 
> Is daisy-util.js API also meant for use in Javascript API with "daisy-js 
> <name-of-scriptfile>"?

No, definitely not.

> 
> I try to write a script to import data. After some struggle I'm able to 
> write byte array in document.setPart with
> 
> new java.lang.String("<html><body><p>" + text +
>                       "</p></body></html>").getBytes());
> 
> but I have no luck in using htmlcleaner. daisy-util.js library works in 
> cocoon flow.js, but in a javascript script even the "var daisy = 
> getDaisy();" doesn't work and with a small java and javascript (both 
> mixed well togheter) experience I don't know whether I need to 
> "importPackage(somepackage)" or just use Dasiy API to get htmlceaner 
> working for Javascript scripting (and how). If you could modify 
> following function (simplified guestbook example) it would be a big step 
> in Daisy & Java & Javascript learning to me on how to use Dasiy API 
> methods in Javascript in general.
> 
> Thanks.
> 
> Jano
> 
> 
> meant to run with <DAISY_HOME>/bin/daisy-js
> 
> // ??? importPackage(somepackage)
> 
> function buildContent(message) {
>      message = encodeToHTML(message);
>      var content =
> 	"<html><body><p>" +  message + "</p></body></html>";
> 
>      var htmlcleaner = getDaisy().getHTMLCleaner();
>      return htmlcleaner.cleanToByteArray(content);
> }
> 

I didn't test this, but something like this should do it:

// Read the htmlcleaner into a 'template' object
// This part needs to be executed just once
var configfile = "/path/to/htmlcleaner.xml";
var factory = new Packages.org.outerj.daisy.htmlcleaner.HtmlCleanerFactory();
var is = new Packages.org.xml.sax.InputSource(configfile);
var template = factory.buildTemplate(is);

// For each clean operator, do this
var htmlcleaner = template.newHtmlCleaner();
var result = htmlcleaner.cleanToByteArray("<html>foo</html>");

Let me know if it works, then I'll add it to the samples.

-- 
Bruno Dumon                             http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno at outerthought.org                          bruno at apache.org



More information about the daisy mailing list