/************************************************************************** /* Render a document which follows 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 org.apache.lucene.document.*; /** Render a document which follows the StandardDocument standard. @see StandardDocumentFactory @version $Revision: 1.8 $ @author $Author: bablokb $ */ public abstract class StandardDocumentRenderer implements DocumentRenderer { //////////////////////////////////////////////////////////////////////////// /** Render summary: if true, also render summary-field (if available) */ private boolean iShowSummary = true; //////////////////////////////////////////////////////////////////////////// /** Header-String (first-part of output-string). */ private String iHeader; //////////////////////////////////////////////////////////////////////////// /** Footer-String (last-part of output-string). */ private String iFooter; //////////////////////////////////////////////////////////////////////////// /** Prefix-String (added to every path). */ private String iPrefix; //////////////////////////////////////////////////////////////////////////// /** Set the header. @param header The header to use. */ public void setHeader(String header) { iHeader = header; } //////////////////////////////////////////////////////////////////////////// /** Set the footer. @param footer The footer to use. */ public void setFooter(String footer) { iFooter = footer; } //////////////////////////////////////////////////////////////////////////// /** Set the prefix. @param prefix The prefix to use. */ public void setPrefix(String prefix) { iPrefix = prefix; } //////////////////////////////////////////////////////////////////////////// /** Set the showSummary-attribute. @param flag if true, show the summary. */ public void setShowSummary(boolean flag) { iShowSummary = flag; } //////////////////////////////////////////////////////////////////////////// /** This method will render the PATH-field of the document. If the PATH-field is not set, an IllegalStateException is thrown. @throws IOException @throws IllegalStateException if document contains no PATH-field */ public String renderPath(Document doc) throws IOException, IllegalStateException { String path = doc.get(DocumentFactory.PATH); if (path != null) { if (iPrefix != null) path = iPrefix + path; } else throw new IllegalStateException("error: missing " + DocumentFactory.PATH + "-field in document!"); return path; } //////////////////////////////////////////////////////////////////////////// /** This method will render the Summary-field of the document. This is a noop and must be implemented by subclasses. @throws IOException @throws IllegalStateException if document contains no PATH-field */ public abstract String renderSummary(Document doc) throws IOException, IllegalStateException; //////////////////////////////////////////////////////////////////////////// /** {@inheritDoc} This method will render the hit. @throws IllegalStateException */ public String render(Document doc) throws IOException, IllegalStateException { StringBuffer buffer = new StringBuffer(); if (iHeader != null) buffer.append(iHeader); String path = renderPath(doc); if (path != null) buffer.append(path); String summary = renderSummary(doc); if (summary != null) buffer.append(summary); if (iFooter != null) buffer.append(iFooter); return buffer.toString(); } }