<!-- 
     Server side spell checking based on aspell.
     Used by kupu. This is an implementation in JSPX
     TODO: Perhaps MMBase depenency can be removed, and this can be a more general tool, for JSP/Kupu combo's.

     @author Michiel Meeuwissen
     @version $Id: spellcheck.jspx,v 1.4 2005/09/27 18:26:52 michiel Exp $
-->
<mm:content type="text/xml" expires="-1"
            xmlns:jsp="http://java.sun.com/JSP/Page" 
            xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
            xmlns:mm="http://www.mmbase.org/mmbase-taglib-2.0"            
            >
  <jsp:output omit-xml-declaration="true" />
  <jsp:directive.page import="java.io.*,org.mmbase.util.externalprocess.CommandLauncher" session="true" errorPage="error.jspx" />

  <mm:import externid="text" jspvar="text" vartype="string" />
  <spellcheck_result>
    <jsp:scriptlet>      
      CommandLauncher launcher = new CommandLauncher("SpellChecker");
      ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
      InputStream inputStream = new ByteArrayInputStream(text.getBytes("UTF-8"));

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // XXXX TODO should use piping here, and perhaps even pooling
      launcher.execute(new String[]{"aspell", "-a", "-H", "--lang=nl", "--encoding=utf-8"});
      launcher.waitAndWrite(inputStream, outputStream, errorStream);

      BufferedReader output = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray()), "UTF-8"));
      String line = output.readLine();
      while (line != null) {
        if (line.startsWith("&amp; ")) {
          int colon = line.indexOf(":");
          int wordEnd = line.substring(2).indexOf(" ");
    </jsp:scriptlet>
    <incorrect>
      <word><jsp:expression>line.substring(2, wordEnd + 2)</jsp:expression></word>
      <replacements><jsp:expression>line.substring(colon + 1)</jsp:expression></replacements>
    </incorrect>    
    <jsp:scriptlet>
        }
        line = output.readLine();
      }
    </jsp:scriptlet>
    <error><jsp:expression>new String(errorStream.toByteArray(), "UTF-8")</jsp:expression></error>
  </spellcheck_result> 
</mm:content>