00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 package main.ausgabe;
00019
00020 import java.io.BufferedWriter;
00021 import java.io.File;
00022 import java.io.FileNotFoundException;
00023 import java.io.FileReader;
00024 import java.io.IOException;
00025 import java.io.LineNumberReader;
00026
00027 import main.Main;
00028 import main.NoInputException;
00029 import main.WrongParameterException;
00030 import main.eingabe.TooFewCharactersException;
00031 import main.eingabe.TooFewSperrfelderException;
00032 import main.eingabe.UnexpectedCharacterException;
00033 import main.eingabe.UnexpectedLineException;
00034 import main.eingabe.UnknownCharacterException;
00035 import main.tools.file.OutputFileExistsException;
00036 import main.verarbeitung.IsSperrfeldException;
00037 import main.verarbeitung.IsStartpunktException;
00038 import main.verarbeitung.OutOfMatrixException;
00039 import main.verarbeitung.WrongDimensionException;
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 public class FehlerAusgabe
00085 {
00086
00087 String eingabeDateiName;
00088
00089
00090 BufferedWriter bw;
00091
00092
00093 Exception exception;
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 public FehlerAusgabe(
00104 BufferedWriter bw,
00105 Exception exception,
00106 String dateiName)
00107 {
00108
00109 this.bw = bw;
00110
00111
00112 this.exception = exception;
00113
00114
00115 eingabeDateiName = dateiName;
00116
00117
00118 printError();
00119 }
00120
00121
00122
00123
00124
00125
00126
00127 private void printError()
00128 {
00129
00130 if (exception instanceof UnexpectedCharacterException)
00131 {
00132
00133 schreibeFehler(
00134 "Zuviel Eingaben in Zeile " + exception.getMessage());
00135 }
00136 else if (exception instanceof UnexpectedLineException)
00137 {
00138
00139 schreibeFehler(
00140 "Es folgen noch Zeichen nach der Eingabe in Zeile "
00141 + exception.getMessage());
00142 }
00143 else if (exception instanceof UnknownCharacterException)
00144 {
00145
00146 schreibeFehler(
00147 "Unbekanntes Zeichen in der Eingabe in Zeile "
00148 + exception.getMessage());
00149 }
00150 else if (exception instanceof TooFewCharactersException)
00151 {
00152
00153 schreibeFehler(
00154 "Zu wenig Zeichen für Eingabe in Zeile "
00155 + exception.getMessage());
00156 }
00157 else if (exception instanceof TooFewSperrfelderException)
00158 {
00159
00160 schreibeFehler("Es sind zu wenige Sperrfelder angegeben");
00161 }
00162 else if (exception instanceof WrongDimensionException)
00163 {
00164
00165 schreibeFehler("Falsche Dimensionsangaben für Matrix");
00166 }
00167 else if (exception instanceof OutOfMatrixException)
00168 {
00169
00170 schreibeFehler(
00171 exception.getMessage() + " außerhalb der Matrix definiert");
00172 }
00173 else if (exception instanceof IsStartpunktException)
00174 {
00175
00176 schreibeFehler(
00177 "Sperrfeld auf Startpunkt"
00178 + exception.getMessage()
00179 + " definiert");
00180 }
00181 else if (exception instanceof IsSperrfeldException)
00182 {
00183
00184 schreibeFehler(
00185 "Sperrfeld " + exception.getMessage() + " doppelt definiert");
00186 }
00187
00188 else if (exception instanceof FileNotFoundException)
00189 {
00190
00191 System.out.println("Die Eingabedatei wurde nicht gefunden");
00192 }
00193 else if (exception instanceof OutputFileExistsException)
00194 {
00195
00196
00197 System.out.println(
00198 "Die Datei " + exception.getMessage() + " existiert schon");
00199 }
00200 else if (exception instanceof NoInputException)
00201 {
00202 System.out.println(
00203 "Es wurde kein Parameter für das Programm übergeben.");
00204 hilfeAusgabe();
00205 }
00206 else if (exception instanceof WrongParameterException)
00207 {
00208 System.out.println(exception.getMessage());
00209 hilfeAusgabe();
00210 }
00211
00212 else if (exception instanceof IOException)
00213 {
00214
00215 schreibeFehler(
00216 "Es ist ein Fehler bei der Ausgabe aufgetreten:"
00217 + exception.getMessage());
00218 }
00219 else
00220 {
00221
00222 schreibeFehler(
00223 "Es ist ein Fehler aufgetreten: " + exception.getMessage());
00224 }
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 private void schreibeFehler(String s)
00236 {
00237
00238 StringBuffer eingabe = new StringBuffer("");
00239
00240
00241 String lineEnd = System.getProperty("line.separator");
00242
00243
00244 if (lineEnd == null)
00245 {
00246 lineEnd = "\n";
00247 }
00248
00249
00250 String tabulator = " ";
00251
00252
00253 try
00254 {
00255
00256 LineNumberReader line =
00257 new LineNumberReader(
00258 new FileReader(
00259 new File(eingabeDateiName)));
00260
00261
00262 eingabe.append(lineEnd + tabulator + "Eingabedaten: " + lineEnd);
00263 eingabe.append(tabulator + "*************" + lineEnd);
00264
00265
00266 String zeile;
00267 while ((zeile = line.readLine()) != null)
00268 {
00269
00270 String whitespace = " ";
00271 if (line.getLineNumber() >= 10)
00272 {
00273 whitespace = " ";
00274 }
00275
00276
00277 eingabe.append(
00278 whitespace
00279 + line.getLineNumber()
00280 + ":"
00281 + tabulator
00282 + zeile
00283 + lineEnd);
00284 }
00285 }
00286
00287 catch (Exception e)
00288 {
00289
00290
00291
00292 }
00293
00294
00295 try
00296 {
00297
00298
00299 if (Main.debug)
00300 {
00301 System.out.println(eingabe + lineEnd + lineEnd);
00302 System.out.println(tabulator + s);
00303 }
00304
00305 bw.write(eingabe + lineEnd + lineEnd);
00306 bw.write(tabulator + s);
00307 }
00308
00309 catch (Exception e)
00310 {
00311
00312 System.err.println(
00313 "Die Ausgabedatei konnte nicht beschrieben werden.");
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322
00323 private static void hilfeAusgabe()
00324 {
00325
00326 System.out.println(
00327 "Aufruf:\n"
00328 + "java -jar programm.jar -d Datei [-h][-debug][-overwrite]"
00329 + "\n\n"
00330 + "-h zeigt diese Hilfe an\n"
00331 + "-debug die Fehlerausgabe wird zusätzlich auf dem"
00332 + " Bildschirm ausgeben\n"
00333 + "-overwrite eine vorhandene Ausgabedatei mit gleichem Namen "
00334 + " wird überschrieben\n");
00335 }
00336 }