Skip to content

Commit

Permalink
Merge pull request #14 from MikeEdgar/12_edireporter
Browse files Browse the repository at this point in the history
EDIReporter implementation
  • Loading branch information
MikeEdgar committed May 5, 2020
2 parents 9c7ca1b + df4a2f5 commit 59055c8
Show file tree
Hide file tree
Showing 19 changed files with 610 additions and 167 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>io.xlate</groupId>
<artifactId>staedi</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>

<name>StAEDI : Streaming API for EDI for Java</name>
<description>Streaming API for EDI for Java</description>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/xlate/edi/internal/stream/LocationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class LocationView implements Location {
protected int columnNumber;
protected int characterOffset;
protected int segmentPosition;
protected String segmentTag;
protected int elementPosition;
protected int componentPosition;
protected int elementOccurrence;
Expand All @@ -32,6 +33,7 @@ public LocationView(Location source) {
columnNumber = source.getColumnNumber();
characterOffset = source.getCharacterOffset();
segmentPosition = source.getSegmentPosition();
segmentTag = source.getSegmentTag();
elementPosition = source.getElementPosition();
componentPosition = source.getComponentPosition();
elementOccurrence = source.getElementOccurrence();
Expand Down Expand Up @@ -67,6 +69,11 @@ public int getSegmentPosition() {
return segmentPosition;
}

@Override
public String getSegmentTag() {
return segmentTag;
}

@Override
public int getElementPosition() {
return elementPosition;
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIInputFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.xlate.edi.schema.Schema;
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;
Expand All @@ -34,6 +35,7 @@ public class StaEDIInputFactory extends EDIInputFactory {
private static final String DEFAULT_ENCODING = "US-ASCII";

private final Set<String> supportedCharsets;
private EDIReporter reporter;

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

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

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

if (supportedCharsets.contains(encoding)) {
return new StaEDIStreamReader(stream, encoding, schema, properties);
return new StaEDIStreamReader(stream, encoding, schema, properties, getEDIReporter());
}

throw new EDIStreamException("Unsupported encoding: " + encoding);
Expand All @@ -78,4 +80,14 @@ public EDIStreamReader createFilteredReader(EDIStreamReader reader, EDIStreamFil
public XMLStreamReader createXMLStreamReader(EDIStreamReader reader) throws XMLStreamException {
return new StaEDIXMLStreamReader(reader);
}

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

@Override
public void setEDIReporter(EDIReporter reporter) {
this.reporter = reporter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public StaEDIStreamLocation copy() {
copy.columnNumber = this.columnNumber;
copy.characterOffset = this.characterOffset;
copy.segmentPosition = this.segmentPosition;
copy.segmentTag = this.segmentTag;
copy.elementPosition = this.elementPosition;
copy.elementOccurrence = this.elementOccurrence;
copy.componentPosition = this.componentPosition;
Expand All @@ -47,6 +48,7 @@ public void set(Location source) {
columnNumber = source.getColumnNumber();
characterOffset = source.getCharacterOffset();
segmentPosition = source.getSegmentPosition();
segmentTag = source.getSegmentTag();
elementPosition = source.getElementPosition();
componentPosition = source.getComponentPosition();
elementOccurrence = source.getElementOccurrence();
Expand All @@ -73,13 +75,15 @@ public void incrementOffset(int value) {
this.columnNumber++;
}

public void incrementSegmentPosition() {
public void incrementSegmentPosition(String segmentTag) {
if (this.segmentPosition < 0) {
this.segmentPosition = 1;
} else {
this.segmentPosition++;
}

this.segmentTag = segmentTag;

clearSegmentLocations();
}

Expand Down
27 changes: 24 additions & 3 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.xlate.edi.schema.EDISchemaException;
import io.xlate.edi.schema.Schema;
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 @@ -49,6 +50,7 @@ public class StaEDIStreamReader implements EDIStreamReader {
private final String encoding;
private Schema controlSchema;
private final Map<String, Object> properties;
private final EDIReporter reporter;
private final StaEDIStreamLocation location = new StaEDIStreamLocation();
private final ProxyEventHandler proxy;
private final Lexer lexer;
Expand All @@ -60,11 +62,13 @@ public StaEDIStreamReader(
InputStream stream,
String encoding,
Schema schema,
Map<String, Object> properties) {
Map<String, Object> properties,
EDIReporter reporter) {
this.stream = stream;
this.encoding = encoding;
this.controlSchema = schema;
this.properties = new HashMap<>(properties);
this.reporter = reporter;
this.proxy = new ProxyEventHandler(location, this.controlSchema);
this.lexer = new Lexer(this.stream, proxy, location);
}
Expand Down Expand Up @@ -119,8 +123,7 @@ public Map<String, Character> getDelimiters() {
return Collections.unmodifiableMap(delimiters);
}

@Override
public EDIStreamEvent next() throws EDIStreamException {
private EDIStreamEvent nextEvent() throws EDIStreamException {
ensureOpen();
ensureIncomplete();

Expand Down Expand Up @@ -165,6 +168,24 @@ public EDIStreamEvent next() throws EDIStreamException {
return event;
}

@Override
public EDIStreamEvent next() throws EDIStreamException {
EDIStreamEvent event = null;
boolean eventFound = false;

do {
event = nextEvent();

if (this.reporter != null && event.isError()) {
reporter.report(getErrorType(), this);
} else {
eventFound = true;
}
} while (!complete && !eventFound);

return event;
}

@Override
public EDIStreamEvent nextTag() throws EDIStreamException {
EDIStreamEvent event = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public EDIStreamWriter endInterchange() throws EDIStreamException {
@Override
public EDIStreamWriter writeStartSegment(String name) throws EDIStreamException {
ensureLevel(LEVEL_INTERCHANGE);
location.incrementSegmentPosition();
location.incrementSegmentPosition(name);
validate(validator -> validator.validateSegment(this, name));

if (exitTransaction(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,20 @@ private void enqueueEvent(EDIStreamEvent ediEvent) throws XMLStreamException {
case SEGMENT_ERROR:
throw new XMLStreamException(String.format("Segment %s has error %s",
ediReader.getText(),
ediReader.getErrorType()));
ediReader.getErrorType()),
this.location);

case ELEMENT_OCCURRENCE_ERROR:
throw new XMLStreamException(String.format("Element %s has error %s",
ediReader.getText(),
ediReader.getErrorType()));
ediReader.getErrorType()),
this.location);

case ELEMENT_DATA_ERROR:
throw new XMLStreamException(String.format("Element %s has error %s",
ediReader.getText(),
ediReader.getErrorType()));
ediReader.getErrorType()),
this.location);

default:
throw new IllegalStateException("Unknown state: " + ediEvent);
Expand All @@ -230,6 +233,8 @@ public int next() throws XMLStreamException {
if (eventQueue.isEmpty()) {
try {
enqueueEvent(ediReader.next());
} catch (XMLStreamException e) {
throw e;
} catch (Exception e) {
throw new XMLStreamException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface EventHandler extends ElementDataHandler, ValidationEventHandler

void interchangeEnd();

boolean segmentBegin(char[] text, int start, int length);
boolean segmentBegin(String tag);

boolean segmentEnd();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public Lexer(InputStream stream, EventHandler handler, StaEDIStreamLocation loca
};

ssn = (notifyState, start, length) -> {
location.incrementSegmentPosition();
return handler.segmentBegin(buffer.array(), start, length);
String segmentTag = new String(buffer.array(), start, length);
location.incrementSegmentPosition(segmentTag);
return handler.segmentBegin(segmentTag);
};

sen = (notifyState, start, length) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ProxyEventHandler implements EventHandler {
private boolean transaction = false;

private InputStream binary;
private CharArraySequence segmentHolder = new CharArraySequence();
private String segmentTag;
private CharArraySequence elementHolder = new CharArraySequence();

private StreamEvent[] events = new StreamEvent[99];
Expand Down Expand Up @@ -166,23 +166,23 @@ public void loopEnd(CharSequence id) {
}

@Override
public boolean segmentBegin(char[] text, int start, int length) {
segmentHolder.set(text, start, length);
public boolean segmentBegin(String segmentTag) {
this.segmentTag = segmentTag;

Validator validator = validator();
boolean eventsReady = true;

if (validator != null) {
validator.validateSegment(this, segmentHolder);
validator.validateSegment(this, segmentTag);
eventsReady = !validator.isPendingDiscrimination();
}

if (exitTransaction(segmentHolder)) {
if (exitTransaction(segmentTag)) {
transaction = false;
validator().validateSegment(this, segmentHolder);
validator().validateSegment(this, segmentTag);
}

enqueueEvent(EDIStreamEvent.START_SEGMENT, EDIStreamValidationError.NONE, segmentHolder, null, location);
enqueueEvent(EDIStreamEvent.START_SEGMENT, EDIStreamValidationError.NONE, segmentTag, null, location);
return eventsReady;
}

Expand All @@ -198,7 +198,7 @@ public boolean segmentEnd() {
}

location.clearSegmentLocations();
enqueueEvent(EDIStreamEvent.END_SEGMENT, EDIStreamValidationError.NONE, segmentHolder, null, location);
enqueueEvent(EDIStreamEvent.END_SEGMENT, EDIStreamValidationError.NONE, segmentTag, null, location);
transactionSchemaAllowed = false;
return true;
}
Expand Down Expand Up @@ -370,7 +370,7 @@ private Validator validator() {

private void enqueueEvent(EDIStreamEvent event,
EDIStreamValidationError error,
CharArraySequence holder,
CharSequence holder,
CharSequence code,
Location location) {

Expand Down
Loading

0 comments on commit 59055c8

Please sign in to comment.