001package org.maltparser.parser;
002
003import java.io.BufferedWriter;
004import java.io.FileWriter;
005import java.io.IOException;
006import java.io.OutputStreamWriter;
007
008import org.maltparser.core.exception.MaltChainedException;
009
010public class Diagnostics {
011//      protected final boolean diagnostics;
012        protected final BufferedWriter diaWriter;
013        
014        public Diagnostics(String fileName) throws MaltChainedException {
015                try {
016                        if (fileName.equals("stdout")) {
017                                diaWriter = new BufferedWriter(new OutputStreamWriter(System.out));
018                        } else if (fileName.equals("stderr")) {
019                                diaWriter = new BufferedWriter(new OutputStreamWriter(System.err));
020                        } else {
021                                diaWriter = new BufferedWriter(new FileWriter(fileName));
022                        }
023                } catch (IOException e) {
024                        throw new MaltChainedException("Could not open the diagnostic file. ", e);
025                }
026//              this.diagnostics = (Boolean)manager.getOptionValue("singlemalt", "diagnostics");
027//              openDiaWriter(manager.getOptionValue("singlemalt", "diafile").toString());
028        }
029        
030//      public boolean isDiagnostics() {
031//              return diagnostics;
032//      }
033
034        public BufferedWriter getDiaWriter() {
035                return diaWriter;
036        }
037        
038        public void writeToDiaFile(String message) throws MaltChainedException {
039                try {
040                        getDiaWriter().write(message);
041                } catch (IOException e) {
042                        throw new MaltChainedException("Could not write to the diagnostic file. ", e);
043                }
044        }
045        
046        public void closeDiaWriter() throws MaltChainedException {
047                if (diaWriter != null) {
048                        try {
049                                diaWriter.flush();
050                                diaWriter.close();
051                        } catch (IOException e) {
052                                throw new MaltChainedException("Could not close the diagnostic file. ", e);
053                        }
054                }
055        }
056        
057//      public void openDiaWriter(String fileName) throws MaltChainedException {
058//              if (diagnostics) {
059//                      try {
060//                              if (fileName.equals("stdout")) {
061//                                      diaWriter = new BufferedWriter(new OutputStreamWriter(System.out));
062//                              } else if (fileName.equals("stderr")) {
063//                                      diaWriter = new BufferedWriter(new OutputStreamWriter(System.err));
064//                              } else {
065//                                      diaWriter = new BufferedWriter(new FileWriter(fileName));
066//                              }
067//                      } catch (IOException e) {
068//                              throw new MaltChainedException("Could not open the diagnostic file. ", e);
069//                      }
070//              }
071//      }
072}