Skip to content

Commit

Permalink
Merge 89713de into d4a3733
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Jul 10, 2020
2 parents d4a3733 + 89713de commit d9bc432
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 43 deletions.
32 changes: 26 additions & 6 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIInputFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
import javax.xml.stream.XMLStreamReader;

import io.xlate.edi.schema.Schema;
import io.xlate.edi.stream.EDIInputErrorReporter;
import io.xlate.edi.stream.EDIInputFactory;
import io.xlate.edi.stream.EDIReporter;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamFilter;
import io.xlate.edi.stream.EDIStreamReader;

public class StaEDIInputFactory extends EDIInputFactory {

private EDIReporter reporter;
private EDIInputErrorReporter reporter;

public StaEDIInputFactory() {
supportedProperties.add(EDI_VALIDATE_CONTROL_STRUCTURE);
Expand All @@ -52,7 +52,7 @@ public EDIStreamReader createEDIStreamReader(InputStream stream, String encoding

@Override
public EDIStreamReader createEDIStreamReader(InputStream stream, Schema schema) {
return new StaEDIStreamReader(stream, StandardCharsets.UTF_8, schema, properties, getEDIReporter());
return new StaEDIStreamReader(stream, StandardCharsets.UTF_8, schema, properties, getErrorReporter());
}

@SuppressWarnings("resource")
Expand All @@ -61,7 +61,7 @@ public EDIStreamReader createEDIStreamReader(InputStream stream, String encoding
Objects.requireNonNull(stream);

if (Charset.isSupported(encoding)) {
return new StaEDIStreamReader(stream, Charset.forName(encoding), schema, properties, getEDIReporter());
return new StaEDIStreamReader(stream, Charset.forName(encoding), schema, properties, getErrorReporter());
}

throw new EDIStreamException("Unsupported encoding: " + encoding);
Expand All @@ -78,12 +78,32 @@ public XMLStreamReader createXMLStreamReader(EDIStreamReader reader) throws XMLS
}

@Override
public EDIReporter getEDIReporter() {
public EDIInputErrorReporter getErrorReporter() {
return reporter;
}

@Override
public void setEDIReporter(EDIReporter reporter) {
public void setErrorReporter(EDIInputErrorReporter reporter) {
this.reporter = reporter;
}

@Override
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Deprecated /*(forRemoval = true, since = "1.9")*/
public io.xlate.edi.stream.EDIReporter getEDIReporter() {
EDIInputErrorReporter errorReporter = getErrorReporter();

if (errorReporter instanceof io.xlate.edi.stream.EDIReporter) {
return (io.xlate.edi.stream.EDIReporter) errorReporter;
}

throw new ClassCastException("Can not cast reporter to EDIReporter; did you mean to call getErrorReporter() ?");
}

@Override
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Deprecated /*(forRemoval = true, since = "1.9")*/
public void setEDIReporter(io.xlate.edi.stream.EDIReporter reporter) {
setErrorReporter(reporter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

import javax.xml.stream.XMLStreamWriter;

import io.xlate.edi.stream.EDIOutputErrorReporter;
import io.xlate.edi.stream.EDIOutputFactory;
import io.xlate.edi.stream.EDIStreamConstants;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamWriter;

public class StaEDIOutputFactory extends EDIOutputFactory {

private EDIOutputErrorReporter reporter;

public StaEDIOutputFactory() {
supportedProperties.add(EDIStreamConstants.Delimiters.SEGMENT);
supportedProperties.add(EDIStreamConstants.Delimiters.DATA_ELEMENT);
Expand All @@ -43,13 +46,13 @@ public StaEDIOutputFactory() {

@Override
public EDIStreamWriter createEDIStreamWriter(OutputStream stream) {
return new StaEDIStreamWriter(stream, StandardCharsets.UTF_8, properties);
return new StaEDIStreamWriter(stream, StandardCharsets.UTF_8, properties, reporter);
}

@Override
public EDIStreamWriter createEDIStreamWriter(OutputStream stream, String encoding) throws EDIStreamException {
if (Charset.isSupported(encoding)) {
return new StaEDIStreamWriter(stream, Charset.forName(encoding), properties);
return new StaEDIStreamWriter(stream, Charset.forName(encoding), properties, reporter);
}
throw new EDIStreamException("Unsupported encoding: " + encoding);
}
Expand All @@ -58,4 +61,14 @@ public EDIStreamWriter createEDIStreamWriter(OutputStream stream, String encodin
public XMLStreamWriter createXMLStreamWriter(EDIStreamWriter writer) {
return new StaEDIXMLStreamWriter(writer);
}

@Override
public EDIOutputErrorReporter getErrorReporter() {
return this.reporter;
}

@Override
public void setErrorReporter(EDIOutputErrorReporter reporter) {
this.reporter = reporter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import io.xlate.edi.internal.stream.tokenization.ProxyEventHandler;
import io.xlate.edi.schema.EDISchemaException;
import io.xlate.edi.schema.Schema;
import io.xlate.edi.stream.EDIInputErrorReporter;
import io.xlate.edi.stream.EDIInputFactory;
import io.xlate.edi.stream.EDIReporter;
import io.xlate.edi.stream.EDIStreamEvent;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamReader;
Expand All @@ -48,7 +48,7 @@ public class StaEDIStreamReader implements EDIStreamReader {

private Schema controlSchema;
private final Map<String, Object> properties;
private final EDIReporter reporter;
private final EDIInputErrorReporter reporter;
private final StaEDIStreamLocation location = new StaEDIStreamLocation();
private final ProxyEventHandler proxy;
private final Lexer lexer;
Expand All @@ -61,7 +61,7 @@ public StaEDIStreamReader(
Charset charset,
Schema schema,
Map<String, Object> properties,
EDIReporter reporter) {
EDIInputErrorReporter reporter) {

this.controlSchema = schema;
this.properties = new HashMap<>(properties);
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIStreamWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.xlate.edi.internal.stream.validation.Validator;
import io.xlate.edi.schema.EDIType;
import io.xlate.edi.schema.Schema;
import io.xlate.edi.stream.EDIOutputErrorReporter;
import io.xlate.edi.stream.EDIOutputFactory;
import io.xlate.edi.stream.EDIStreamConstants.Delimiters;
import io.xlate.edi.stream.EDIStreamEvent;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class StaEDIStreamWriter implements EDIStreamWriter, ElementDataHandler,
private final OutputStream stream;
private final OutputStreamWriter writer;
private final Map<String, Object> properties;
private final EDIOutputErrorReporter reporter;
private Dialect dialect;

private final StaEDIStreamLocation location;
Expand Down Expand Up @@ -107,10 +109,11 @@ public class StaEDIStreamWriter implements EDIStreamWriter, ElementDataHandler,
private int emptyComponents = 0;
private boolean unterminatedComponent = false;

public StaEDIStreamWriter(OutputStream stream, Charset charset, Map<String, Object> properties) {
public StaEDIStreamWriter(OutputStream stream, Charset charset, Map<String, Object> properties, EDIOutputErrorReporter reporter) {
this.stream = stream;
this.writer = new OutputStreamWriter(stream, charset);
this.properties = new HashMap<>(properties);
this.reporter = reporter;
this.emptyElementTruncation = booleanValue(properties.get(EDIOutputFactory.TRUNCATE_EMPTY_ELEMENTS));
this.prettyPrint = booleanValue(properties.get(EDIOutputFactory.PRETTY_PRINT));

Expand Down Expand Up @@ -799,12 +802,20 @@ public void elementError(EDIStreamEvent event,
copy.setElementOccurrence(repetition);
copy.setComponentPosition(component);

errors.add(new EDIValidationException(event, error, copy, data));
if (this.reporter != null) {
this.reporter.report(error, this, copy, data, referenceCode);
} else {
errors.add(new EDIValidationException(event, error, copy, data));
}
}

@Override
public void segmentError(CharSequence token, EDIStreamValidationError error) {
errors.add(new EDIValidationException(EDIStreamEvent.SEGMENT_ERROR, error, location, token));
if (this.reporter != null) {
this.reporter.report(error, this, this.getLocation(), token, token);
} else {
errors.add(new EDIValidationException(EDIStreamEvent.SEGMENT_ERROR, error, location, token));
}
}

private void validate(Consumer<Validator> command) {
Expand Down Expand Up @@ -837,7 +848,9 @@ private void validateElement(Runnable setupCommand, CharSequence data) {
location.getComponentPosition(),
location.getElementOccurrence());
}
}

if (!errors.isEmpty()) {
throw validationExceptionChain(errors);
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/io/xlate/edi/stream/EDIInputErrorReporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.xlate.edi.stream;

/**
* This interface is used to report non-fatal errors detected in an EDI input.
*
* @since 1.9
*/
public interface EDIInputErrorReporter {
/**
* Report the desired message in an application specific format. Only
* warnings and non-fatal errors should be reported through this interface.
*
* Fatal errors will be thrown as EDIStreamException.
*
* @param errorType
* the type of error detected
* @param reader
* the EDIStreamReader that encountered the error
* @throws EDIStreamException
* when errors occur calling the reader
*/
void report(EDIStreamValidationError errorType, EDIStreamReader reader) throws EDIStreamException;
}
35 changes: 35 additions & 0 deletions src/main/java/io/xlate/edi/stream/EDIInputFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ public abstract EDIStreamReader createEDIStreamReader(InputStream stream,
* @return the reporter that will be set on any EDIStreamReader created by
* this factory instance
*
* @throws ClassCastException
* when reporter is not an instance of EDIReporter
*
* @since 1.4
* @deprecated use {@link #getErrorReporter()} instead
*/
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Deprecated /*(forRemoval = true, since = "1.9")*/
public abstract EDIReporter getEDIReporter();

/**
Expand All @@ -231,6 +237,35 @@ public abstract EDIStreamReader createEDIStreamReader(InputStream stream,
* the resolver to use to report non fatal errors
*
* @since 1.4
* @deprecated use {@link #setErrorReporter(EDIInputErrorReporter)} instead
*/
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Deprecated /*(forRemoval = true, since = "1.9")*/
public abstract void setEDIReporter(EDIReporter reporter);

/**
* Retrieves the reporter that will be set on any EDIStreamReader created by
* this factory instance.
*
* @return the reporter that will be set on any EDIStreamReader created by
* this factory instance
*
* @since 1.9
*/
public abstract EDIInputErrorReporter getErrorReporter();

/**
* The reporter that will be set on any EDIStreamReader created by this
* factory instance.
*
* NOTE: When using an EDIReporter, errors found in the data stream that are
* reported to the reporter will not appear in the stream of events returned
* by the EDIStreamReader.
*
* @param reporter
* the resolver to use to report non fatal errors
*
* @since 1.9
*/
public abstract void setErrorReporter(EDIInputErrorReporter reporter);
}
30 changes: 30 additions & 0 deletions src/main/java/io/xlate/edi/stream/EDIOutputErrorReporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.xlate.edi.stream;

/**
* This interface is used to report non-fatal errors detected in an EDI input.
*
* @since 1.9
*/
public interface EDIOutputErrorReporter {
/**
* Report the desired message in an application specific format. Only
* warnings and non-fatal errors should be reported through this interface.
*
* Fatal errors will be thrown as {@link EDIStreamException}s.
*
* @param errorType
* the type of error detected
* @param writer
* the EDIStreamWriter that encountered the error
* @param location
* the location of the error, may be different than the location
* returned by the writer (e.g. for derived element positions)
* @param data
* the invalid data, may be null (e.g. for missing required
* element errors)
* @param code
* the reference code for the invalid data, if available from the
* current schema used for validation
*/
void report(EDIStreamValidationError errorType, EDIStreamWriter writer, Location location, CharSequence data, CharSequence code);
}
38 changes: 34 additions & 4 deletions src/main/java/io/xlate/edi/stream/EDIOutputFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@
public abstract class EDIOutputFactory extends PropertySupport {

/**
* <p>When set to true, the EDI output will have a platform specific line
* <p>
* When set to true, the EDI output will have a platform specific line
* separator written after each segment terminator.
*
* <p>Default value is false.
* <p>
* Default value is false.
*/
public static final String PRETTY_PRINT = "io.xlate.edi.stream.PRETTY_PRINT";

/**
* <p>When set to true, empty trailing elements in a segment and empty trailing
* <p>
* When set to true, empty trailing elements in a segment and empty trailing
* components in a composite element will be truncated. I.e, they will not
* be written to the output.
*
* <p>Default value is false.
* <p>
* Default value is false.
*/
public static final String TRUNCATE_EMPTY_ELEMENTS = "io.xlate.edi.stream.TRUNCATE_EMPTY_ELEMENTS";

Expand Down Expand Up @@ -90,4 +94,30 @@ public abstract EDIStreamWriter createEDIStreamWriter(OutputStream stream,
* @return a new {@link XMLStreamWriter}
*/
public abstract XMLStreamWriter createXMLStreamWriter(EDIStreamWriter writer);

/**
* Retrieves the reporter that will be set on any EDIStreamWriter created by
* this factory instance.
*
* @return the reporter that will be set on any EDIStreamWriter created by
* this factory instance
*
* @since 1.9
*/
public abstract EDIOutputErrorReporter getErrorReporter();

/**
* The reporter that will be set on any EDIStreamWriter created by this
* factory instance.
*
* NOTE: When using an EDIOutputErrorReporter, errors found in the data
* stream that are reported to the reporter will not be throw as instances
* of {@link EDIValidationException}.
*
* @param reporter
* the resolver to use to report non fatal errors
*
* @since 1.9
*/
public abstract void setErrorReporter(EDIOutputErrorReporter reporter);
}
20 changes: 5 additions & 15 deletions src/main/java/io/xlate/edi/stream/EDIReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@
* This interface is used to report non-fatal errors detected in an EDI input.
*
* @since 1.4
* @deprecated implement EDIInputErrorReporter instead. This interface will be
* removed in a future version of StAEDI.
*/
public interface EDIReporter {
/**
* Report the desired message in an application specific format. Only
* warnings and non-fatal errors should be reported through this interface.
*
* Fatal errors will be thrown as EDIStreamException.
*
* @param errorType
* the type of error detected
* @param reader
* the EDIStreamReader that encountered the error
* @throws EDIStreamException
* when errors occur calling the reader
*/
void report(EDIStreamValidationError errorType, EDIStreamReader reader) throws EDIStreamException;
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Deprecated/*(forRemoval = true, since = "1.9")*/
public interface EDIReporter extends EDIInputErrorReporter {
}
Loading

0 comments on commit d9bc432

Please sign in to comment.