home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / search-index.xsp < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  9.9 KB  |  259 lines

  1. <?xml version="1.0"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17.  
  18. <xsp:page language="java"
  19.           xmlns:xsp="http://apache.org/xsp"
  20.           xmlns:xsp-request="http://apache.org/xsp/request/2.0">
  21.  
  22.   <xsp:structure>
  23.     <xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
  24.     <xsp:include>org.apache.avalon.framework.component.ComponentException</xsp:include>
  25.     
  26.     <xsp:include>org.apache.cocoon.ProcessingException</xsp:include>
  27.     <xsp:include>org.apache.cocoon.components.language.markup.xsp.XSPUtil</xsp:include>
  28.     
  29.     <xsp:include>org.apache.cocoon.components.search.*</xsp:include>
  30.     
  31.     <xsp:include>org.apache.lucene.analysis.Analyzer</xsp:include>
  32.     <xsp:include>org.apache.lucene.store.*</xsp:include>
  33.     <xsp:include>org.apache.lucene.document.*</xsp:include>
  34.     <xsp:include>org.apache.lucene.search.Hits</xsp:include>
  35.     
  36.     <xsp:include>java.util.*</xsp:include>
  37.     
  38.   </xsp:structure>
  39.  
  40.   <xsp:logic>
  41.     File workDir = null;
  42.     /** Contextualize this class */
  43.     public void contextualize(Context context) throws ContextException {
  44.       super.contextualize( context );
  45.       workDir = (File) context.get(Constants.CONTEXT_WORK_DIR);
  46.     }
  47.     
  48.     LuceneCocoonSearcher lcs;
  49.     Directory directory;
  50.     Analyzer analyzer = LuceneCocoonHelper.getAnalyzer( "org.apache.lucene.analysis.standard.StandardAnalyzer" );
  51.   
  52.     Hits search( String query_string ) throws ProcessingException {
  53.       Hits hits = null;
  54.       try {
  55.         lcs = (LuceneCocoonSearcher)this.manager.lookup( LuceneCocoonSearcher.ROLE );
  56.         lcs.setAnalyzer( analyzer );
  57.         if (directory == null) {
  58.           directory = LuceneCocoonHelper.getDirectory( new File( workDir, "index" ), false );
  59.         }
  60.         lcs.setDirectory( directory );
  61.         hits = lcs.search( query_string, LuceneXMLIndexer.BODY_FIELD );
  62.         start = 0;
  63.         end = Math.min(hits.length(), start + hitsPerPage);
  64.       } catch (IOException ioe) {
  65.         // ignore ??
  66.         throw new ProcessingException( "IOException in search", ioe );
  67.       } catch (ProcessingException pe) {
  68.         // ignore ??
  69.         Throwable t = pe.getCause();
  70.         if (t instanceof org.apache.lucene.queryParser.ParseException) {
  71.           // ignore it or write info about reason
  72.         } else {
  73.           throw new ProcessingException( "ProcessingException in search", pe );
  74.         }
  75.       } catch (ComponentException ce) {
  76.         // ignore ??
  77.         throw new ProcessingException( "ComponentException in search", ce );
  78.       } finally {
  79.         if (lcs != null) {
  80.           this.manager.release( lcs );
  81.         }
  82.         lcs = null;
  83.       }
  84.       
  85.       return hits;
  86.     }
  87.     int hitsPerPage = 10;
  88.     int start = 0;
  89.     int end = 0; 
  90.     Hits hits;
  91.     LuceneCocoonPager luceneCocoonPager;
  92.   </xsp:logic>
  93.  
  94.   <page>
  95.     <xsp:logic>
  96.       String queryString = <xsp-request:get-parameter name="queryString" default=""/>;
  97.       boolean findIt = "Find!".equals( <xsp-request:get-parameter name="find"/> );
  98.       Integer startIndex = null;
  99.       Integer pageLength = null;
  100.       try {
  101.         if (<xsp-request:get-parameter name="previous"/> != null) {
  102.           startIndex = new Integer( <xsp-request:get-parameter name="startPreviousIndex" default="0"/> );
  103.         } else if (<xsp-request:get-parameter name="next"/> != null) {
  104.           startIndex = new Integer( <xsp-request:get-parameter name="startNextIndex"/> );
  105.         } else {
  106.           startIndex = new Integer( 0 );
  107.         }
  108.         pageLength = new Integer( <xsp-request:get-parameter name="pageLength" default="10"/> );
  109.       } catch (NumberFormatException nfe) {
  110.         // ignore it
  111.       }
  112.     </xsp:logic>
  113.     
  114.     <title>Cocoon XML Search Interface</title>
  115.     <content>
  116.       <a href="http://jakarta.apache.org/lucene/"><img border="0" alt="Lucene Logo" src="images/lucene_green_300.gif"/></a>
  117.       <para>
  118.         <font size="-1">
  119.           <a target="_blank" href="statistic">Index Statistics</a> |
  120.           <a href="welcome">Welcome</a>
  121.         </font>
  122.       </para>
  123.       <para>
  124.         <form action="search">
  125.           <input type="text" name="queryString" size="60">
  126.             <xsp:attribute name="value"><xsp:expr>queryString</xsp:expr></xsp:attribute>
  127.           </input>
  128.           <input type="submit" name="find" value="Find!"/>
  129.         </form>
  130.       </para>
  131.       <para>
  132.         Help by example (see also the
  133.         <a href="http://lucene.sourceforge.net/cgi-bin/faq/faqmanager.cgi">Lucene FAQ</a>)
  134.         <table cellspacing="2" cellpadding="2">
  135.           <tr bgcolor="#dddedd" valign="top">
  136.           <td width="50%"><font size="-2" >
  137.             <ul>
  138.               <li>free AND "text search"
  139.                 Search for documents which contain the word "free" and the 
  140.                 phrase "text search"
  141.               </li>
  142.               <li>+text search
  143.                 Search for documents which must contain the word "text" and 
  144.                 optionally contain the word "search".
  145.               </li>
  146.               <li>giants -football
  147.                 Search for "giants" but omit documents containing "football"
  148.               </li>
  149.             </ul>
  150.           </font></td>
  151.           
  152.           <td><font size="-2">
  153.             <ul>
  154.               <li>body:john
  155.                 Search for documents containing "john" in the  body field.
  156.                 The field "body" is used by default.
  157.                 Thus query "body:john" is equivalent to query "john".
  158.               </li>
  159.               <li>s1@title:cocoon
  160.                 Search for documents containing "cocoon" in the
  161.                 using field s1@title, ie searching in
  162.                 title attribute of s1 element of xml document.
  163.               </li>
  164.             </ul>
  165.           </font></td>
  166.           </tr>
  167.         </table>
  168.       </para>
  169.       <para>
  170.         SearchResult:
  171.         <xsp:logic>
  172.           if (queryString != null && queryString.length() != 0) {
  173.             
  174.             // do the search, search results are available in hits
  175.             hits = search( queryString );
  176.             luceneCocoonPager = new LuceneCocoonPager( hits );
  177.             if (startIndex != null && pageLength != null) {
  178.               luceneCocoonPager.setStartIndex( startIndex.intValue() );
  179.               luceneCocoonPager.setCountOfHitsPerPage( pageLength.intValue() );
  180.             }
  181.             <xsp:content>
  182.               Total Hits: <xsp:expr>hits.length()</xsp:expr>
  183.             </xsp:content>
  184.           }
  185.         </xsp:logic>
  186.       </para>
  187.       <para>
  188.         <table width="90%" cellpadding="4" border="1">
  189.           <tr>
  190.             <td>Score</td><td>Count</td><td>URL</td>
  191.           </tr>
  192.           <xsp:logic>
  193.             if (luceneCocoonPager!= null && luceneCocoonPager.hasNext()) {
  194.               int counter = luceneCocoonPager.getStartIndex();
  195.               List l = (List)luceneCocoonPager.next();
  196.               Iterator i = l.iterator();
  197.               for (; i.hasNext(); counter++) {
  198.                 LuceneCocoonPager.HitWrapper hw = (LuceneCocoonPager.HitWrapper)i.next();
  199.                 Document doc = hw.getDocument();
  200.                 float score = hw.getScore();
  201.                 String url = doc.get( LuceneXMLIndexer.URL_FIELD );
  202.                 
  203.                 <xsp:content>
  204.                   <tr>
  205.                     <td> <xsp:expr>String.valueOf((int)(score * 100.0f))</xsp:expr>% </td> 
  206.                     <td> <xsp:expr>String.valueOf(counter + 1)</xsp:expr> </td>
  207.                     <td>
  208.                       <a target="_blank">
  209.                         <xsp:attribute name="href"><xsp:expr>url</xsp:expr></xsp:attribute>
  210.                         <xsp:expr>url</xsp:expr>
  211.                       </a>
  212.                     </td>
  213.                   </tr>
  214.                 </xsp:content>
  215.               }
  216.             }
  217.           </xsp:logic>
  218.         </table>
  219.       </para>
  220.       <para>
  221.         <xsp:logic>
  222.           if (luceneCocoonPager!= null && luceneCocoonPager != null && 
  223.             (luceneCocoonPager.hasNext() || luceneCocoonPager.hasPrevious())) {
  224.             <xsp:content>
  225.               <form action="search">
  226.                 <input type="hidden" name="queryString">
  227.                   <xsp:attribute name="value"><xsp:expr>String.valueOf(queryString)</xsp:expr></xsp:attribute>
  228.                 </input>
  229.                 <input type="hidden" name="pageLength">
  230.                   <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.getCountOfHitsPerPage())</xsp:expr></xsp:attribute>
  231.                 </input>
  232.                 <xsp:logic>
  233.                   if (luceneCocoonPager.hasPrevious()) {
  234.                     <input type="hidden" name="startPreviousIndex">
  235.                       <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.previousIndex())</xsp:expr></xsp:attribute>
  236.                     </input>
  237.                     <input type="submit" name="previous" value="previous"/>
  238.                   }
  239.                 </xsp:logic>
  240.                  
  241.                 <xsp:logic>
  242.                   if (luceneCocoonPager.hasNext()) {
  243.                     <input type="hidden" name="startNextIndex">
  244.                       <xsp:attribute name="value"><xsp:expr>String.valueOf(luceneCocoonPager.nextIndex())</xsp:expr></xsp:attribute>
  245.                     </input>
  246.                     <input type="submit" name="next" value="next"/>
  247.                   }
  248.                 </xsp:logic>
  249.               </form>
  250.             </xsp:content>
  251.           }
  252.         </xsp:logic>
  253.       </para>
  254.     </content>
  255.   </page>
  256.  
  257. </xsp:page>
  258.  
  259.