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 / statistic-index.xsp < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  6.6 KB  |  217 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" xmlns:xsp="http://apache.org/xsp">
  19.  
  20.   <xsp:structure>
  21.     <xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
  22.     <xsp:include>org.apache.cocoon.components.search.*</xsp:include>
  23.     
  24.     <xsp:include>org.apache.lucene.document.Document</xsp:include>
  25.     <xsp:include>org.apache.lucene.index.*</xsp:include>
  26.     <xsp:include>org.apache.lucene.document.*</xsp:include>
  27.     <xsp:include>org.apache.lucene.store.*</xsp:include>
  28.     
  29.     <xsp:include>java.io.*</xsp:include>
  30.     <xsp:include>java.util.*</xsp:include>
  31.     <xsp:include>java.net.*</xsp:include>
  32.   </xsp:structure>
  33.   
  34.   <xsp:logic>
  35.     File workDir = null;
  36.     /** Contextualize this class */
  37.     public void contextualize(Context context) throws ContextException {
  38.       super.contextualize( context );
  39.       workDir = (File) context.get(Constants.CONTEXT_WORK_DIR);
  40.     }
  41.     
  42.     Directory directory;
  43.     
  44.     void init(String indexName) throws ProcessingException {
  45.       try {
  46.         directory = LuceneCocoonHelper.getDirectory( new File( workDir, indexName ), false );
  47.       } catch (Exception e) {
  48.         throw new ProcessingException( "Exception in init()!", e );
  49.       }
  50.     }
  51.  
  52.     void closeReader( IndexReader reader ) {
  53.       if (reader != null) {
  54.         try {
  55.           reader.close();
  56.         } catch (IOException ioe) {
  57.         }
  58.       }
  59.     }
  60.     
  61.     int numDocs() throws ProcessingException {
  62.       IndexReader reader = null;
  63.       try {
  64.         reader = IndexReader.open( directory );
  65.         int num_docs;
  66.         num_docs = reader.numDocs();
  67.         return num_docs;
  68.       } catch (Exception e) {
  69.         throw new ProcessingException( "Exception in numDocs()!", e );
  70.       } finally {
  71.         closeReader(reader);
  72.       }
  73.     }
  74.     
  75.     Map allDocuments() throws ProcessingException {
  76.       IndexReader reader = null;
  77.       try {
  78.         reader = IndexReader.open( directory );
  79.         
  80.         HashMap fieldsStatistic = new HashMap();
  81.         
  82.         for (int i = 0; i < reader.maxDoc(); i++ ) {
  83.           if (!reader.isDeleted(i)) {
  84.             Document document = reader.document(i);
  85.             Enumeration fields = document.fields();
  86.             while (fields.hasMoreElements()) {
  87.               Field f = (Field)fields.nextElement();
  88.               String name = f.name();
  89.               String value = f.stringValue();
  90.               if (value == null) value = "--void--";
  91.               
  92.               String fieldStatistic = name + "/" + value;
  93.               if (fieldsStatistic.get( fieldStatistic ) == null) {
  94.                 fieldsStatistic.put( fieldStatistic, new Integer(1) );
  95.               } else {
  96.                 Integer sum = (Integer)fieldsStatistic.get( fieldStatistic );
  97.                 int sum_plus = sum.intValue() + 1;
  98.                 fieldsStatistic.put( fieldStatistic, new Integer( sum_plus ) );
  99.               }
  100.             }
  101.           }
  102.         }
  103.         return fieldsStatistic;
  104.         //map.keySet();
  105.       } catch (Exception e) {
  106.         throw new ProcessingException( "Exception allDocuments()!", e );
  107.       } finally {
  108.         closeReader(reader);
  109.       }
  110.     }
  111.  
  112.     Map allTerms() throws ProcessingException {
  113.       IndexReader reader = null;
  114.       try {
  115.         reader = IndexReader.open( directory );
  116.         
  117.         TermEnum te = reader.terms();
  118.         HashMap all_fields = new HashMap();
  119.         while (te.next()) {
  120.           Term term = te.term();
  121.           int docfreq = te.docFreq();
  122.           String field = term.field();
  123.           if (field != null) {
  124.             if (all_fields.containsKey( field )) {
  125.               Integer sum = (Integer)all_fields.get( field );
  126.               int sum_plus = sum.intValue() + docfreq;
  127.               all_fields.put( field, new Integer( sum_plus ) );
  128.             } else {
  129.               all_fields.put( field, new Integer( docfreq ) );
  130.             }
  131.           }
  132.         }
  133.         te.close();
  134.         return all_fields;
  135.       } catch (Exception e) {
  136.         throw new ProcessingException( "Exception allDocuments()!", e );
  137.       } finally {
  138.         closeReader(reader);
  139.       }
  140.     }
  141.     Map sort( Map map ) {
  142.       TreeMap treeMap = new TreeMap( map );
  143.       return treeMap;
  144.     }
  145.   </xsp:logic>
  146.   
  147.   <page>
  148.     <xsp:logic>
  149.       String indexName = request.getParameter("indexName");
  150.       if (indexName == null) indexName = "index";
  151.       init(indexName);
  152.     </xsp:logic>
  153.     <title>Index Statistics</title>
  154.     <content>
  155.       <para>
  156.         <font size="-1">
  157.           <a href="welcome">Welcome</a>
  158.         </font>
  159.       </para>
  160.       <para>
  161.         Statistics:
  162.       </para>
  163.       <para>
  164.         Total Count Of Documents
  165.         <xsp:expr>String.valueOf(numDocs())</xsp:expr>
  166.       </para>
  167.       <para>
  168.         <table>
  169.           <tr>
  170.             <td>Count Of Terms</td><td>Fieldname/Fieldvalue</td>
  171.           </tr>
  172.           <xsp:logic>
  173.             Map all_docs = sort(allDocuments());
  174.             Iterator it1 = all_docs.keySet().iterator();
  175.             while (it1.hasNext()) {
  176.               String k = (String)it1.next();
  177.               Integer v = (Integer)all_docs.get( k );
  178.               <xsp:content>
  179.                 <tr>
  180.                   <td> <xsp:expr>v.toString()</xsp:expr> </td>
  181.                   <td> <xsp:expr>k</xsp:expr> </td>
  182.                 </tr>
  183.               </xsp:content>
  184.             }
  185.           </xsp:logic>
  186.         </table>
  187.       </para>
  188.       <para>
  189.         All Terms
  190.       </para>
  191.       <para>
  192.         <table>
  193.           <tr>
  194.             <td>Count Of Terms</td><td>Term</td>
  195.           </tr>
  196.           <xsp:logic>
  197.             Map all_terms = sort(allTerms());
  198.             Iterator it2 = all_terms.keySet().iterator();
  199.             while (it2.hasNext()) {
  200.               String k = (String)it2.next();
  201.               Integer v = (Integer)all_terms.get( k );
  202.               <xsp:content>
  203.                 <tr>
  204.                   <td> <xsp:expr>v.toString()</xsp:expr> </td>
  205.                   <td> <xsp:expr>k</xsp:expr> </td> 
  206.                 </tr>
  207.               </xsp:content>
  208.             }
  209.           </xsp:logic>
  210.         </table>
  211.       </para>
  212.     </content>
  213.   </page>
  214.  
  215. </xsp:page>
  216.  
  217.