Package webapp

Source Code of webapp.RestfulSpeller$CheckSpelling

/**
* Copyright (C) 2006 - present dabuTech Corporation
* All Rights Reserved.
*
* This file is part of Easier Java Websites.
*
* EJW is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the accompanying license
* for more details.
*
* You should have received a copy of the license along with EJW; if not,
* go to http://www.EasierJava.com and download the latest version.
*/

package webapp;

import com.swabunga.spell.engine.Word;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;
import ejw.RequestHandler;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import ejw.ServerInterface;

public class RestfulSpeller extends RequestHandler
  {
    Set formats = asSet(new String[] { "JSON", "XML", "PLAIN", "json", "xml", "plain" });
   
    public String word()
      throws RestfulSpellerException
      {
        Collection<Spelling> spellingList = new ArrayList<Spelling>();
        String args[] = getServerInterface().getPathArguments();
        String word = null, format = "PLAIN";

        if (args.length == 2)
          {
            if (formats.contains(args[0]))
              format = args[0];
           
            word = args[1];
          }
        else if (args.length == 1 && args[0] != null && args[0].length() > 0)
          word = args[0];
        else
          throw new RestfulSpellerException("Usage: /restfulSpeller/word[/xml or json or plain]/word");

        spellingList.add(new Spelling(word, AppUtils.getSpeller().isCorrect(word),
              formatSuggestions(AppUtils.getSpeller().getSuggestions(word, 10))));

        getServerInterface().addViewObject("format", format);
        getServerInterface().addViewObject("spellingList", spellingList);

        return "/WEB-INF/response.jsp";
      }

    public String text()
      throws RestfulSpellerException, IOException
      {
        String args[] = getServerInterface().getPathArguments();
        String format = "PLAIN";
       
        if (args.length > 0)
          if (args.length == 1)
            {
              if (formats.contains(args[0]))
                format = args[0];
            }
          else
            throw new RestfulSpellerException("Usage: /restfulSpeller/text[/xml or json or plain]");

        getServerInterface().setAttribute("format", format);
        getServerInterface().setAttribute("spellingList", new CheckSpelling(getServerInterface()).getSpellingErrors());

        return "/WEB-INF/response.jsp";
      }

    class CheckSpelling implements SpellCheckListener
      {
        Collection<Spelling> spellingErrors = new TreeSet<Spelling>();
       
        CheckSpelling(ServerInterface serverInterface) throws IOException
          {
            SpellChecker spellChecker = new SpellChecker(AppUtils.getSpeller(),100);

            spellChecker.addSpellCheckListener(this);
            spellChecker.checkSpelling(new StringWordTokenizer(getText(serverInterface)));
          }
       
        public void spellingError(SpellCheckEvent event)
          {
            spellingErrors.add(new Spelling(event.getInvalidWord(), false,
                             formatSuggestions(event.getSuggestions())));
          }
       
        Collection getSpellingErrors() { return spellingErrors; }
      }
   
    String getText(ServerInterface serverInterface) throws IOException
      {
        Reader r = serverInterface.getServletRequest().getReader();
        StringBuffer text = new StringBuffer();
        char[] buf = new char[2048];
        int numberRead = 0;

        while ((numberRead = r.read(buf)) != -1)
          text.append(buf, 0, numberRead);
       
        return text.toString();
      }
   
    List<String> formatSuggestions(List list)
      {
        List<String> suggestions = new ArrayList<String>();
       
        for (Iterator it = list.iterator(); it.hasNext();)
          suggestions.add(((Word)it.next()).getWord());
       
        return suggestions;
      }

    public class Spelling implements Comparable
      {
        String word;
        boolean isCorrect;
        List suggestions;
       
        Spelling(String word, boolean isCorrect, List suggestions)
          {
            this.word = word;
            this.isCorrect = isCorrect;
            this.suggestions = suggestions;
          }
       
        public boolean equals(Object obj) { return word.equals(((Spelling)obj).getWord()); }
        public int compareTo(Object s1) { return word.compareTo(((Spelling)s1).getWord()); }
        public String getWord() { return word; }
        public boolean isCorrect() { return isCorrect; }
        public List getSuggestions() { return suggestions; }
       
        public String getSuggestionsString()
          {
            return suggestions.toString().replaceAll("\\[", "").replaceAll("\\]", "");
          }
      }
   
    static Set<String> asSet(String[] array)
      {
        Set<String> set = new HashSet<String>();

        for (int i = 0; i < array.length; i++)
          set.add(array[i]);

        return set;
      }
   
    public static class RestfulSpellerException extends Exception
      {
        private static final long serialVersionUID = 100L;

        public RestfulSpellerException(String message) { super(message); }
      }
  }
TOP

Related Classes of webapp.RestfulSpeller$CheckSpelling

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.