All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.org.jafer.servlet.ZServlet Maven / Gradle / Ivy

The newest version!
/**
 * JAFER Toolkit Project.
 * Copyright (C) 2002, JAFER Toolkit Project, Oxford University.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

/**
 *  Title: JAFER Toolkit
 *  Description:
 *  Copyright: Copyright (c) 2001
 *  Company: Oxford University
 *
 *@author     Antony Corfield; Matthew Dovey; Colin Tatham
 *@version    1.0
 */

package org.jafer.servlet;

import org.jafer.zclient.*;
import org.jafer.util.xml.*;
import org.jafer.query.QueryBuilder;
import org.jafer.query.QueryException;
import org.jafer.exception.JaferException;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;
import java.net.URL;
import java.io.*;

import org.w3c.dom.*;

public class ZServlet extends HttpServlet {

  private ServletConfig servletConfig;
  private static final String[] initParams = {"target", "port", "databaseName", "maxHits"};
  private int maxHits;
  private URL recordXSLT, listXSLT, errorXSLT;
  private static Map attributeMap;

  public void doGet(HttpServletRequest request, HttpServletResponse response)
                                        throws ServletException, IOException {


    response.setContentType("text/html; charset=UTF-8");
    PrintWriter writer = response.getWriter();


    ZClient client = new ZClient();

  /* load required and optional initParameters, including stylesheets: */
    loadInitParams(client);

    client.setRemoteAddress(request.getRemoteAddr());
    client.setCheckRecordFormat(true);
    client.setParseQuery(false);

    Node root = client.getDocument().createElement("root");

    int totalResults;

    try {
      totalResults = submitQuery(request, client);
    } catch (JaferException j) {
      root.appendChild(createExceptionNode(client, j, "Connection or query error."));
      createErrorOutput(root, writer);
      close(client);
      return;
    }

    if (totalResults == 0)
      createOutput(root, listXSLT, writer, totalResults);

    else if (totalResults == 1) {
      try {
        client.setRecordCursor(1);
	root.appendChild(client.getCurrentRecord().getXML());
        createOutput(root, recordXSLT, writer, totalResults);
      } catch (JaferException j) {
        root.appendChild(createExceptionNode(client, j, "Error in record request/transformation."));
        createErrorOutput(root, writer);
      }
    }

    else {
      for (int i = 1; i <= ((totalResults > maxHits) ? maxHits : totalResults); i++) {
        try {
          client.setRecordCursor(i);
          root.appendChild(client.getCurrentRecord().getXML());
        } catch (JaferException j) {
        root.appendChild(createExceptionNode(client, j, "Error in record request/transformation."));
        }
      }
      createOutput(root, listXSLT, writer, totalResults);
    }
   close(client);
  }


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


  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    this.servletConfig = super.getServletConfig();
  }


  private void loadInitParams(ZClient client) throws ServletException {

    /* check for required initParameters: */
    for (int i=0; i 0) {

     // replace each item in the Vector with nodes made from each att/term:
        for (int i=0; i 0) {
      int previousAtt = 0;
      currentNode = null;

      for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy