greenstone.org greenstone wiki greenstone trac planet greenstone

Changeset 16015

Show
Ignore:
Timestamp:
2008-06-16 11:58:51 (5 months ago)
Author:
davidb
Message:

Printing to standard out (used as the communication mechanism back to Perl script is now wrapped up in a UTF-8 PrintWriter?. Testing showed that it was important to 'flush' output each time a message is printed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • indexers/trunk/lucene-gs/src/org/greenstone/LuceneWrapper/GS2LuceneQuery.java

    r14559 r16015  
    7575    private Searcher searcher = null; 
    7676    private IndexReader reader = null; 
     77 
     78    static private PrintWriter utf8out = null; 
     79 
     80    static 
     81    { 
     82        try { 
     83            OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8"); 
     84            utf8out = new PrintWriter(osw, true); 
     85        } 
     86        catch (UnsupportedEncodingException e) {  
     87            System.out.println(e);  
     88        } 
     89    } 
     90 
    7791     
    7892    public GS2LuceneQuery() { 
     
    88102 
    89103        if (full_indexdir==null || full_indexdir.length()==-1){ 
    90             System.out.println("Index directory is not indicated "); 
     104            utf8out.println("Index directory is not indicated "); 
     105            utf8out.flush(); 
    91106            return false; 
    92107        } 
     
    107122         
    108123        if (query_string == null || query_string.equals("")) { 
    109             System.out.println("The query word is not indicated "); 
     124            utf8out.println("The query word is not indicated "); 
     125            utf8out.flush(); 
    110126            return null; 
    111127        } 
     
    407423    } 
    408424 
     425 
     426    protected void finalize() throws Throwable  
     427    { 
     428        try { 
     429            utf8out.flush();  
     430        } finally { 
     431            super.finalize(); 
     432        } 
     433    } 
     434 
    409435     
    410436    /** command line program and auxiliary methods */ 
     
    413439    static private boolean query_result_caching_enabled = false; 
    414440 
     441 
    415442    static public void main (String args[]) 
    416443    { 
     444 
     445 
    417446        if (args.length == 0) { 
    418447            System.out.println("Usage: GS2LuceneQuery <index directory> [-fuzziness value] [-filter filter_string] [-sort sort_field] [-dco AND|OR] [-startresults number -endresults number] [query]"); 
     
    495524                        break; 
    496525                    } 
     526 
    497527                    runQueryCaching(index_directory, queryer, query_string); 
    498528                     
     
    542572                String query_results_xml_string = query_results_xml.toString(); 
    543573                query_results_xml_string = query_results_xml_string.replaceFirst("cached=\"false\"", "cached=\"true\""); 
    544                 System.out.print(query_results_xml_string); 
     574 
     575                utf8out.print(query_results_xml_string); 
     576                utf8out.flush(); 
     577 
    545578                return; 
    546579            } 
     
    568601        query_results_xml.append("</ResultSet>\n"); 
    569602 
    570         System.out.print(query_results_xml); 
     603        utf8out.print(query_results_xml); 
     604        utf8out.flush(); 
     605 
     606        try { 
     607            /* 
     608            Writer output = null; 
     609            File file = new File("/tmp/lucenequery.txt"); 
     610            output = new BufferedWriter(new FileWriter(file,"UTF-8")); 
     611            output.write(query_results_xml.toString()); 
     612            output.close(); 
     613            */ 
     614 
     615            FileOutputStream fos = new FileOutputStream("/tmp/lucenequery.txt"); 
     616            
     617            OutputStreamWriter osw2 = new OutputStreamWriter(fos, "UTF-8"); 
     618 
     619            osw2.write("Query string = " + query_string + "\n"); 
     620            osw2.write(query_results_xml.toString()); 
     621            osw2.close(); 
     622        } 
     623        catch (Exception e) { 
     624            e.printStackTrace(); 
     625        } 
     626 
     627 
    571628 
    572629        // Cache this query result, if desired