/************************************************************************** /* Render a document with a text-only header. The document must follow /* the StandardDocument standard. /* /* Copyright (c) 2003-2004 by Bernhard Bablok (mail@bablokb.de) /* /* 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 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; see the file COPYING.LESSER. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.luala.lib; import java.io.*; import java.util.*; import org.apache.lucene.document.*; /** Render a document with text-only header. The document must follow the StandardDocument standard. @see StandardDocumentFactory @version $Revision: 1.6 $ @author $Author: bablokb $ */ public class StandardDocumentTextRenderer extends StandardDocumentRenderer { //////////////////////////////////////////////////////////////////////////// /** Line length of summary. */ private final static int SUMMARY_LINE_LENGTH = 60; //////////////////////////////////////////////////////////////////////////// /** Counter for the sequence of hits. */ private int iCounter = 0; //////////////////////////////////////////////////////////////////////////// /** Constructor. This just sets the header and footer fields of the base class. */ public StandardDocumentTextRenderer() { setFooter("\n"); } //////////////////////////////////////////////////////////////////////////// /** {@inheritDoc} */ public synchronized String render(Document doc) throws IOException, IllegalStateException { iCounter++; setHeader(iCounter+": "); return super.render(doc); } //////////////////////////////////////////////////////////////////////////// /** This method will render the Summary-field of the document. Implements {@link StandardDocumentRenderer#renderSummary}. @throws IOException @throws IllegalStateException if document contains no SUMMARY-field */ public String renderSummary(Document doc) throws IOException, IllegalStateException { String summary = doc.get(DocumentFactory.SUMMARY); if (summary == null) throw new IllegalStateException("error: missing " + DocumentFactory.SUMMARY + "-field in document!"); StringBuffer buffer = new StringBuffer(summary.length()); buffer.append('\n'); StringTokenizer tokenizer = new StringTokenizer(summary," \t\n\r\f",true); StringBuffer line = new StringBuffer(SUMMARY_LINE_LENGTH); while (true) { line.setLength(0); line.append("\t"); String token = ""; while (tokenizer.hasMoreTokens() && line.length() < SUMMARY_LINE_LENGTH) { token = tokenizer.nextToken(); line.append(token); if (token.equals("\n")) break; } buffer.append(line); if (!token.equals("\n")) buffer.append('\n'); if (!tokenizer.hasMoreTokens()) break; } return buffer.toString(); } }