[jsword-svn] jsword-web/java/limbo/org/crosswire/jsword/view/web s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Thu Apr 28 15:42:39 MST 2005


Update of /cvs/jsword/jsword-web/java/limbo/org/crosswire/jsword/view/web
In directory www.crosswire.org:/tmp/cvs-serv17129/java/limbo/org/crosswire/jsword/view/web

Added Files:
	SimpleWebConverter.java DemoServlet.java 
Log Message:
Moving broken code to limbo

--- NEW FILE: SimpleWebConverter.java ---
package org.crosswire.jsword.view.web;

import java.net.URL;
import java.util.MissingResourceException;

import javax.xml.transform.TransformerException;

import org.crosswire.common.util.ResourceUtil;
import org.crosswire.common.xml.Converter;
import org.crosswire.common.xml.SAXEventProvider;
import org.crosswire.common.xml.TransformingSAXEventProvider;

/**
 * Turn XML from a Bible into HTML according to a Display style.
 * 
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 *
 * Distribution Licence:<br />
 * JSword is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License,
 * version 2 as published by the Free Software Foundation.<br />
 * This program 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
 * General Public License for more details.<br />
 * The License is available on the internet
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA<br />
 * The copyright to this program is held by it's authors.
 * </font></td></tr></table>
 * @see gnu.gpl.Licence
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id: SimpleWebConverter.java,v 1.1 2005/04/28 22:42:37 dmsmith Exp $
 */
public class SimpleWebConverter implements Converter
{
    /* (non-Javadoc)
     * @see org.crosswire.common.xml.Converter#convert(org.crosswire.common.xml.SAXEventProvider)
     */
    public SAXEventProvider convert(SAXEventProvider xmlsep) throws TransformerException
    {
        try
        {
            URL xslurl = ResourceUtil.getResource("xsl/web/simple.xsl"); //$NON-NLS-1$

            // We used to do:
            // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            // however for various reasons, now we don't but nothing seems to be broken ...
            return new TransformingSAXEventProvider(xslurl, xmlsep);
        }
        catch (MissingResourceException ex)
        {
            throw new TransformerException(ex);
        }
    }
}

--- NEW FILE: DemoServlet.java ---
package org.crosswire.jsword.view.web;

import java.io.IOException;
import java.net.URLEncoder;

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

import org.crosswire.common.util.Logger;
import org.crosswire.common.xml.SAXEventProvider;
import org.crosswire.common.xml.XMLUtil;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookData;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.search.parse.IndexSearcher;
import org.crosswire.jsword.book.search.parse.PhraseParamWord;
import org.crosswire.jsword.passage.Key;
import org.crosswire.jsword.passage.Passage;
import org.crosswire.jsword.passage.PassageTally;
import org.crosswire.jsword.passage.RestrictionType;

/**
 * A quick demo of how easy it is to write new front-ends to JSword.
 * 
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 *
 * Distribution Licence:<br />
 * JSword is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License,
 * version 2 as published by the Free Software Foundation.<br />
 * This program 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
 * General Public License for more details.<br />
 * The License is available on the internet
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA<br />
 * The copyright to this program is held by it's authors.
 * </font></td></tr></table>
 * @see gnu.gpl.Licence
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id: DemoServlet.java,v 1.1 2005/04/28 22:42:37 dmsmith Exp $
 */
public class DemoServlet extends HttpServlet
{
    /**
     * @see javax.servlet.Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);

        try
        {
            String bookname = config.getInitParameter("book-name"); //$NON-NLS-1$
            book = Books.installed().getBook(bookname);
        }
        catch (Exception ex)
        {
            throw new ServletException(Msg.INIT_FAILED.toString(), ex);
        }
    }

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            Key key = null;

            String search = request.getParameter(FIELD_SEARCH);
            if (search != null)
            {
                request.setAttribute(FIELD_SEARCH, search);
                key = book.find(search);
            }

            String match = request.getParameter(FIELD_MATCH);
            if (match != null)
            {
                request.setAttribute(FIELD_MATCH, match);
                String quote = IndexSearcher.getPreferredSyntax(PhraseParamWord.class);
                PassageTally tally = (PassageTally) book.find(quote + match + quote);
                tally.setOrdering(PassageTally.ORDER_TALLY);
                tally.trimRanges(tallyTrim, RestrictionType.NONE);
                key = tally;
            }

            String view = request.getParameter(FIELD_VIEW);
            if (view != null)
            {
                request.setAttribute(FIELD_VIEW, view);
                key = book.getKey(view);
            }

            if (key instanceof Passage)
            {
                Passage ref = (Passage) key;

                // Do we need multiple pages
                if (ref.countVerses() > pageSize)
                {
                    Passage waiting = ref.trimVerses(pageSize);

                    // JDK: A deprecation error if you don't, won't build or run on java < 1.4 if you do.
                    //String link = URLEncoder.encode(waiting.getName());
                    String link = URLEncoder.encode(waiting.getName(), "UTF-8"); //$NON-NLS-1$

                    request.setAttribute("next-link", link); //$NON-NLS-1$
                    request.setAttribute("next-name", waiting.getName()); //$NON-NLS-1$
                    request.setAttribute("next-overview", waiting.getOverview()); //$NON-NLS-1$
                }

                BookData data = book.getData(ref);
                SAXEventProvider osissep = data.getSAXEventProvider();
                SAXEventProvider htmlsep = style.convert(osissep);
                String text = XMLUtil.writeToString(htmlsep);

                request.setAttribute("reply", text); //$NON-NLS-1$
            }
        }
        catch (Exception ex)
        {
            log.error("Failed view", ex); //$NON-NLS-1$
            throw new ServletException("Failed view", ex); //$NON-NLS-1$
        }

        getServletContext().getRequestDispatcher("/demo.jsp").forward(request, response); //$NON-NLS-1$
    }

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }

    private static int tallyTrim = 50;
    private static int pageSize = 150;
    private Book book;
    private SimpleWebConverter style = new SimpleWebConverter();

    private static final String FIELD_VIEW = "view"; //$NON-NLS-1$
    private static final String FIELD_MATCH = "match"; //$NON-NLS-1$
    private static final String FIELD_SEARCH = "search"; //$NON-NLS-1$

    /**
     * The log stream
     */
    private static final Logger log = Logger.getLogger(DemoServlet.class);

    /**
     * Serialization ID
     */
    private static final long serialVersionUID = 3257006549032777012L;
}



More information about the jsword-svn mailing list