Search in sources :

Example 1 with ErrorDescription

use of com.amazon.tools.errorReport.ErrorDescription in project ion-java by amzn.

the class IonJavaCli method processFiles.

// 
// 
// functions for processing
// 
// 
private static void processFiles(IonWriter ionWriterForErrorReport, CommandArgs args, ProcessContext processContext) throws IOException {
    boolean finish = false;
    for (String path : args.getInputFiles()) {
        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(path));
            IonReader ionReader = IonReaderBuilder.standard().build(inputStream)) {
            processContext.setFileName(path);
            ReadContext readContext = new ReadContext(new ArrayList<>());
            try {
                getEventStream(ionReader, CommandType.PROCESS, readContext);
            } catch (IonException | NullPointerException e) {
                new ErrorDescription(readContext.getState(), e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
                finish = true;
            } catch (Exception e) {
                new ErrorDescription(ErrorType.STATE, e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
                finish = true;
            }
            processContext.setEventStream(readContext.getEventStream());
            processContext.setEventIndex(0);
            if (args.getOutputFormat() == OutputFormat.EVENTS) {
                processContext.getIonWriter().writeSymbol(EVENT_STREAM);
                processToEventStream(ionWriterForErrorReport, processContext);
            } else {
                processToIonStream(processContext, args);
            }
            if (finish)
                System.exit(IO_ERROR_EXIT_CODE);
            processContext.getIonWriter().finish();
            ionWriterForErrorReport.finish();
        } catch (IonException | NullPointerException e) {
            new ErrorDescription(processContext.getState(), e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
            System.exit(IO_ERROR_EXIT_CODE);
        } catch (Exception e) {
            new ErrorDescription(ErrorType.STATE, e.getMessage(), processContext.getFileName(), processContext.getEventIndex()).writeOutput(ionWriterForErrorReport);
            System.exit(IO_ERROR_EXIT_CODE);
        }
    }
}
Also used : ErrorDescription(com.amazon.tools.errorReport.ErrorDescription) IonException(com.amazon.ion.IonException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IonException(com.amazon.ion.IonException) CmdLineException(org.kohsuke.args4j.CmdLineException) BufferedInputStream(java.io.BufferedInputStream) IonReader(com.amazon.ion.IonReader)

Example 2 with ErrorDescription

use of com.amazon.tools.errorReport.ErrorDescription in project ion-java by amzn.

the class IonJavaCli method compareFiles.

// 
// 
// functions for comparing
// 
// 
private static void compareFiles(IonWriter ionWriterForOutput, IonWriter ionWriterForErrorReport, CommandArgs args, ComparisonType comparisonType) throws IOException {
    List<String> files = args.getInputFiles();
    CompareContext compareContext = new CompareContext(null, null);
    compareContext.setType(args.getComparisonType());
    for (String path : files) {
        for (String compareToPath : files) {
            compareContext.reset(path, compareToPath);
            try (InputStream inputFirst = new BufferedInputStream(new FileInputStream(path));
                IonReader ionReaderFirst = IonReaderBuilder.standard().build(inputFirst);
                InputStream inputSecond = new BufferedInputStream(new FileInputStream(compareToPath));
                IonReader ionReaderSecond = IonReaderBuilder.standard().build(inputSecond)) {
                if (comparisonType == ComparisonType.BASIC) {
                    if (path.equals(compareToPath)) {
                        continue;
                    }
                }
                ReadContext readContextFirst = new ReadContext(new ArrayList<>());
                ReadContext readContextSecond = new ReadContext(new ArrayList<>());
                getEventStream(ionReaderFirst, CommandType.COMPARE, readContextFirst);
                getEventStream(ionReaderSecond, CommandType.COMPARE, readContextSecond);
                compareContext.setEventStreamFirst(readContextFirst.getEventStream());
                compareContext.setEventStreamSecond(readContextSecond.getEventStream());
                if (comparisonType != ComparisonType.BASIC) {
                    if (compareEquivs(compareContext) ^ (comparisonType == ComparisonType.EQUIVS || comparisonType == ComparisonType.EQUIVS_TIMELINE)) {
                        ComparisonResultType type = comparisonType == ComparisonType.NON_EQUIVS ? ComparisonResultType.EQUAL : ComparisonResultType.NOT_EQUAL;
                        writeReport(compareContext, ionWriterForOutput, type);
                    }
                } else {
                    if (!compare(compareContext, 0, readContextFirst.getEventStream().size() - 1, 0, readContextSecond.getEventStream().size() - 1)) {
                        writeReport(compareContext, ionWriterForOutput, ComparisonResultType.NOT_EQUAL);
                    }
                }
            } catch (Exception e) {
                new ErrorDescription(ErrorType.STATE, e.getMessage(), path + ';' + compareToPath, -1).writeOutput(ionWriterForErrorReport);
                System.exit(IO_ERROR_EXIT_CODE);
            }
        }
    }
}
Also used : ErrorDescription(com.amazon.tools.errorReport.ErrorDescription) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IonReader(com.amazon.ion.IonReader) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) IonException(com.amazon.ion.IonException) CmdLineException(org.kohsuke.args4j.CmdLineException) ComparisonResultType(com.amazon.tools.comparisonReport.ComparisonResultType)

Aggregations

IonException (com.amazon.ion.IonException)2 IonReader (com.amazon.ion.IonReader)2 ErrorDescription (com.amazon.tools.errorReport.ErrorDescription)2 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2 ComparisonResultType (com.amazon.tools.comparisonReport.ComparisonResultType)1