Ignore:
Timestamp:
2024-03-19T16:00:34+13:00 (2 months ago)
Author:
davidb
Message:

How this Java code was determining end-of-input was found to be incorrect; having read the docs more carefully, this is an updated version that works even if there is a delay in input arriving on standard-in

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/build-src/src/java/org/nzdl/gsdl/ApplyXSLT.java

    r28973 r38850  
    101101    }
    102102
     103    /*
     104    private boolean isOpen(BufferedReader br) {
     105    // The code used to rely on br.ready() to determine whether it should
     106    // continue looping to read input (from standard-in).  However on
     107    // closer read of the JavaDoc, br.ready() can return false even
     108    // when the input stream is still open -- it's just that there isn't
     109    // any input lines to read in at the moment.
     110
     111    // If the input stream has been closed (from the external calling Perl code),
     112    // then the only sure way to determine that this condition has arise is
     113    // to ask br.ready() and catch the Exception that occurs ... because the
     114    // input stream is not closed!
     115
     116    boolean is_open = true;
     117   
     118    try {
     119        boolean is_ready = br.ready();
     120    }
     121    catch (Exception e) {  // Could be more selective and go for IOException ??
     122        //System.err.println("ApplyXSLT::isOpen() encountered an exception => so input is closed");
     123        is_open = false;
     124    }
     125
     126    return is_open;
     127    }
     128    */
     129   
    103130  private boolean process()
    104131  {
     
    114141
    115142
    116       while (br.ready()) {
    117                            
    118     String this_line = br.readLine();
     143      String this_line;     
     144      while ((this_line = br.readLine()) != null) {
     145       
     146    //System.err.println("Read in from pipe, line: " + this_line);
     147   
    119148    if(system_status == BEFORE_READING){
    120149      if(this_line.compareTo(DOC_START) == 0){
     150          // If this_line is DOC_START then we require the next line of input to be
     151          // the filename
    121152        output_file = br.readLine(); // read the next line as the output file name
     153        if (output_file == null) {
     154        // A problem of some form occurred
     155        return false;
     156        }
     157        //System.err.println("Read in from pipe, next line: " + output_file);
    122158        system_status = IS_READING;
    123159        a_doc = new StringBuffer();
     
    127163      }
    128164      else{
    129         System.err.println("Undefined process status:" + this_line);
    130165        system_status = BEFORE_READING;
    131166      }
     
    143178           
    144179        if (!result){
    145           System.err.println("Translation Failed!!");
    146180          return false;
    147181        }
     
    161195      }
    162196
     197      return true;
     198     
    163199      //if(br != null) {
    164200      //  br.close();
    165201      //  br = null;
    166       //}
    167      
    168     }catch (Exception e)
    169       {
     202      //}     
     203    }
     204    catch (Exception e) {
    170205    System.err.println("Receiving piped data error!" + e.toString());
    171       }
     206    e.printStackTrace();
     207    }
    172208               
    173209    return false;
     
    206242      }catch (Exception e) {
    207243      System.err.println("Receiving piped data error!" + e.toString());
     244      e.printStackTrace();
    208245      }
    209246      return false;
     
    474511  public static void main(String[] args)
    475512  {
    476 
    477513    String xml_file="";
    478514    String xsl_file="";
     
    636672        ioe.printStackTrace(); 
    637673        } finally {
     674        System.err.println("ReadStreamGobbler:run() finished.  Closing resource");
    638675        closeResource(br);
    639676        }
Note: See TracChangeset for help on using the changeset viewer.