Changeset 21403


Ignore:
Timestamp:
01/04/10 17:40:03 (11 years ago)
Author:
davidb
Message:

Code was working for Ascii characters, but not for Unicode values > 128. More careful prescription of utf-8 used in code

Location:
main/trunk/greenstone2/common-src/src/jdbmedit
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/src/jdbmedit/Jdb2Txt.java

    r21395 r21403  
    3030
    3131import java.io.IOException;
     32import java.io.UnsupportedEncodingException;
     33import java.io.OutputStreamWriter;
     34import java.io.PrintWriter;
    3235import java.util.Properties;
    3336
     
    4043    HTree          hashtable_;
    4144
     45    static private PrintWriter utf8out = null;
     46
     47    static
     48    {
     49        try {
     50            OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
     51            utf8out = new PrintWriter(osw, true);
     52        }
     53        catch (UnsupportedEncodingException e) {
     54            System.out.println(e);
     55        }
     56    }
    4257
    4358    public Jdb2Txt(String db_filename)
    4459    throws IOException
    4560    {
     61    if (db_filename.endsWith(".jdb")) {
     62        // remove file extension as JDBM does not expect it
     63        db_filename = db_filename.substring(0,db_filename.length()-4);
     64    }
     65
    4666        // create or open a record manager
    4767        Properties props = new Properties();
     
    7191        while (key != null) {
    7292            val = (String) hashtable_.get(key);
    73             System.out.println("[" + key + "]");
    74             System.out.println(val);
     93            utf8out.println("[" + key + "]");
     94            utf8out.println(val);
    7595        // 70 hypens
    76         System.out.println("----------------------------------------------------------------------");
     96        utf8out.println("----------------------------------------------------------------------");
    7797            key = (String) iter.next();
    7898        }
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbGet.java

    r21402 r21403  
    3030
    3131import java.io.IOException;
     32import java.io.UnsupportedEncodingException;
     33import java.io.OutputStreamWriter;
     34import java.io.PrintWriter;
    3235import java.util.Properties;
    3336
     
    3538public class JdbGet
    3639{
     40    static private PrintWriter utf8out = null;
     41
     42    static
     43    {
     44        try {
     45            OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
     46            utf8out = new PrintWriter(osw, true);
     47        }
     48        catch (UnsupportedEncodingException e) {
     49            System.out.println(e);
     50        }
     51    }
    3752
    3853    public static void print_usage()
     
    5671            JdbmAPI jdbm_api = new JdbmAPI(dbname,true);
    5772            String val = jdbm_api.get(key);
    58         System.out.println(val);
     73        utf8out.println(val);
    5974
    6075        jdbm_api.close();
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbKeys.java

    r21402 r21403  
    2525
    2626import java.io.IOException;
     27import java.io.UnsupportedEncodingException;
     28import java.io.OutputStreamWriter;
     29import java.io.PrintWriter;
    2730import java.util.Properties;
    2831import java.util.ArrayList;
     
    3538public class JdbKeys
    3639{
     40    static private PrintWriter utf8out = null;
     41
     42    static
     43    {
     44        try {
     45            OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
     46            utf8out = new PrintWriter(osw, true);
     47        }
     48        catch (UnsupportedEncodingException e) {
     49            System.out.println(e);
     50        }
     51    }
     52
    3753    public static void print_usage()
    3854    {
     
    5167
    5268        try {
    53 
    5469            JdbmAPI jdbm_api = new JdbmAPI(dbname,true);
    55 
     70       
    5671        ArrayList keys = jdbm_api.get_keys();
    57            
     72       
    5873        int keys_len = keys.size();
    5974        for (int i=0; i<keys_len; i++) {
    6075        String key = (String) keys.get(i);
    61         System.out.println(key);
     76        utf8out.println(key);
    6277        }
    6378
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbmAPI.java

    r21402 r21403  
    2727import java.io.InputStreamReader;
    2828import java.io.IOException;
     29import java.io.UnsupportedEncodingException;
     30import java.io.OutputStreamWriter;
     31import java.io.PrintWriter;
    2932
    3033import java.util.Properties;
     
    4548    HTree          hashtable_;
    4649
     50    static private PrintWriter utf8out = null;
     51
     52    static
     53    {
     54        try {
     55            OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
     56            utf8out = new PrintWriter(osw, true);
     57        }
     58        catch (UnsupportedEncodingException e) {
     59            System.out.println(e);
     60        }
     61    }
     62
    4763    public JdbmAPI(String db_filename,boolean must_exist)
    4864    throws IOException
    4965    {
     66    if (db_filename.endsWith(".jdb")) {
     67        // remove file extension as JDBM does not expect it
     68        db_filename = db_filename.substring(0,db_filename.length()-4);
     69    }
     70
    5071        // create or open a record manager
    5172        Properties props = new Properties();
     
    197218        JdbmAPI jdbm_api = new JdbmAPI(dbname);
    198219
    199         InputStreamReader isr=new InputStreamReader(System.in);
     220        InputStreamReader isr=new InputStreamReader(System.in,"UTF-8");
    200221        BufferedReader brin = new BufferedReader(isr);
    201222
     
    208229
    209230            int keys_len = keys.size();
    210             System.out.println(keys_len);
     231            utf8out.println(keys_len);
    211232
    212233            for (int i=0; i<keys_len; i++) {
    213234            String key = (String) keys.get(i);
    214             System.out.println(key);
     235            utf8out.println(key);
    215236            }
    216237        }
     
    221242            if (cmd.equals("Get")) {
    222243                String val = jdbm_api.get(key);
    223                 System.out.println(val.length());
    224                 System.out.println(val);
     244                utf8out.println(val.length());
     245                utf8out.println(val);
    225246            }
    226247            else {
  • main/trunk/greenstone2/common-src/src/jdbmedit/Txt2Jdb.java

    r21395 r21403  
    2626import java.io.BufferedInputStream;
    2727import java.io.InputStream;
     28import java.io.IOException;
     29import java.io.InputStreamReader;
     30import java.io.BufferedReader;
     31import java.util.Properties;
    2832
    2933import jdbm.RecordManager;
     
    3236import jdbm.htree.HTree;
    3337
    34 import java.io.IOException;
    35 import java.util.Properties;
    36 
    3738
    3839public class Txt2Jdb
     
    4748    throws IOException
    4849    {
     50    if (db_filename.endsWith(".jdb")) {
     51        // remove file extension as JDBM does not expect it
     52        db_filename = db_filename.substring(0,db_filename.length()-4);
     53    }
     54
    4955        // create or open a record manager
    5056        Properties props = new Properties();
     
    7581    // Assumes InputStream is a file or standard-in 
    7682    // (no some other form of IO mapped device)
    77     public boolean eof(InputStream is)
     83    //public boolean eof(InputStream is)
     84    public boolean eof(int c)
    7885    throws IOException
    7986    {
    80     return (is.available() == 0);
     87    return (c == -1);
    8188    }
    8289
     
    95102    boolean delkey = false;
    96103
    97     BufferedInputStream bis = new BufferedInputStream(System.in);
    98    
     104
     105    InputStreamReader isr = new InputStreamReader(System.in,"UTF-8");
     106    BufferedReader bis = new BufferedReader(isr);
     107
    99108    int c = bis.read();
    100     while (!eof(bis)) {
     109    while (!eof(c)) {
    101110        num_dashes = 0;
    102111        key = new StringBuffer();
     
    106115       
    107116        // scan for first occurrence of [
    108         while (!eof(bis) && c != '[') {
    109         c = bis.read();
    110         }
    111 
    112        
    113         if (!eof(bis)) {
     117        while (!eof(c) && c != '[') {
     118        c = bis.read();
     119        }
     120
     121       
     122        if (!eof(c)) {
    114123        c = bis.read(); // skip [
    115124        }
     
    117126       
    118127        // now look for closing ], building up 'key' as we go
    119         while (!eof(bis) && c != ']') {
     128        while (!eof(c) && c != ']') {
    120129        key.append ((char)c);
    121130        c = bis.read();
    122131        }
    123132       
    124         if (!eof(bis)) {
     133        if (!eof(c)) {
    125134        // most likely an eol char, but if '-', then signifies record
    126135        // is to be deleted, not added
     
    134143        }
    135144
    136         while (!eof(bis) && (c == '\n' || c == '\r')) {
     145        while (!eof(c) && (c == '\n' || c == '\r')) {
    137146        c = bis.read();
    138147        }
     
    140149        // look for 70 dashes
    141150        tmp = new StringBuffer();
    142         while (!eof(bis) && (num_dashes < 70)) {
     151        while (!eof(c) && (num_dashes < 70)) {
    143152        if (c == '\n') {
    144153            tmp.append((char)c);
Note: See TracChangeset for help on using the changeset viewer.