view software/mpdl-services/mpiwg-mpdl-cms-web/src/de/mpg/mpiwg/berlin/mpdl/servlets/lt/Normalize.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents
children
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.servlets.lt;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
import de.mpg.mpiwg.berlin.mpdl.lt.text.norm.Normalizer;

public class Normalize extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public Normalize() {
    super();
  }

  public void init(ServletConfig config) throws ServletException  {
    super.init(config);
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    String inputString = request.getParameter("inputString");
    String language = request.getParameter("language");
    String method = request.getParameter("method");
    String typeStr = request.getParameter("type");
    if (language == null)
      language = "eng";
    if (method == null)
      method = "norm";
    String[] methods = method.split(" ");
    if (typeStr == null)
      typeStr = "display";
    int type = Normalizer.DISPLAY;
    if (typeStr.equals("dictionary"))
      type = Normalizer.DICTIONARY;
    else if (typeStr.equals("search"))
      type = Normalizer.SEARCH;
    String result = null;
    try {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      if (inputString == null || inputString.isEmpty()) {
        out.print("request parameter \"inputString\" is empty. Please specify \"inputString\"");
        out.close();
        return;
      }
      Normalizer normalizer = new Normalizer(methods, language);
      normalizer.setNormMode(type);
      result = normalizer.normalize(inputString);
      if (result != null)
        out.print(result);
      out.close();
    } catch (ApplicationException e) { 
      throw new ServletException(e);
    }
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }  
}