Skip to content

Commit

Permalink
Merge b7afb1f into 0703360
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Dec 19, 2021
2 parents 0703360 + b7afb1f commit 1973341
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,9 @@ public InputStream getBinaryData() {
public EDIReference getSchemaTypeReference() {
return delegate.getSchemaTypeReference();
}

@Override
public boolean hasText() {
return delegate.hasText();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public class StaEDIInputFactory extends EDIInputFactory {

private EDIInputErrorReporter reporter;

@SuppressWarnings("deprecation")
public StaEDIInputFactory() {
supportedProperties.add(EDI_VALIDATE_CONTROL_STRUCTURE);
supportedProperties.add(EDI_VALIDATE_CONTROL_CODE_VALUES);
supportedProperties.add(EDI_IGNORE_EXTRANEOUS_CHARACTERS);
supportedProperties.add(EDI_NEST_HIERARCHICAL_LOOPS);
supportedProperties.add(EDI_ENABLE_LOOP_TEXT);

supportedProperties.add(XML_DECLARE_TRANSACTION_XMLNS);
supportedProperties.add(XML_WRAP_TRANSACTION_CONTENTS);
Expand Down
67 changes: 57 additions & 10 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
public class StaEDIStreamReader implements EDIStreamReader, Configurable {

private static final Logger LOGGER = Logger.getLogger(StaEDIStreamReader.class.getName());
private static final CharBuffer GROUP_TEXT = CharBuffer.wrap(ProxyEventHandler.LOOP_CODE_GROUP);
private static final CharBuffer TRANSACTION_TEXT = CharBuffer.wrap(ProxyEventHandler.LOOP_CODE_TRANSACTION);

private Schema controlSchema;
private final Map<String, Object> properties;
Expand All @@ -57,6 +59,7 @@ public class StaEDIStreamReader implements EDIStreamReader, Configurable {

private boolean complete = false;
private boolean closed = false;
private boolean deprecationLogged = false;

public StaEDIStreamReader(
InputStream stream,
Expand Down Expand Up @@ -102,9 +105,38 @@ void requireEvent(String message, EDIStreamEvent... events) {
throw new IllegalStateException(message);
}

private void logDeprecation(EDIStreamEvent event) {
if (!deprecationLogged) {
deprecationLogged = true;

LOGGER.warning(() -> "DEPRECATION - Retrieving text for event " + event + " will not be supported in a future release. "
+ "Use `getReferenceCode` or `getSchemaTypeReference` to retrieve additional information for non-textual event types.");
}
}

private CharBuffer getBuffer() {
checkTextState();
return proxy.getCharacters();
EDIStreamEvent event = getEventType();

switch (event) {
case START_GROUP:
case END_GROUP:
logDeprecation(event);
return GROUP_TEXT;

case START_TRANSACTION:
case END_TRANSACTION:
logDeprecation(event);
return TRANSACTION_TEXT;

case START_LOOP:
case END_LOOP:
logDeprecation(event);
return CharBuffer.wrap(proxy.getSchemaTypeReference().getReferencedType().getCode());

default:
return proxy.getCharacters();
}
}

@Override
Expand Down Expand Up @@ -187,6 +219,7 @@ private EDIStreamEvent nextEvent() throws EDIStreamException {
try {
this.setBinaryDataLength(Long.parseLong(getText()));
} catch (NumberFormatException e) {
lexer.invalidate();
throw new EDIStreamException("Failed to parse binary element length", location, e);
}
}
Expand Down Expand Up @@ -339,28 +372,36 @@ public EDIStreamValidationError getErrorType() {
}

private void checkTextState() {
if (!hasText()) {
throw new IllegalStateException("not a valid text state [" + getEventType() + ']');
}
}

@Override
public boolean hasText() {
EDIStreamEvent event = getEventType();

if (event == null) {
throw new IllegalStateException("not a valid text state [" + event + ']');
return false;
}

switch (event) {
case START_GROUP:
case START_TRANSACTION:
case START_LOOP:
case START_SEGMENT:
case END_GROUP:
case END_TRANSACTION:
case END_LOOP:
case END_SEGMENT:
case ELEMENT_DATA:
case ELEMENT_DATA_ERROR:
case ELEMENT_OCCURRENCE_ERROR:
case SEGMENT_ERROR:
break;
return true;
case START_GROUP:
case START_TRANSACTION:
case START_LOOP:
case END_GROUP:
case END_TRANSACTION:
case END_LOOP:
return enableLoopText();
default:
throw new IllegalStateException("not a valid text state [" + event + ']');
return false;
}
}

Expand Down Expand Up @@ -477,4 +518,10 @@ boolean ignoreExtraneousCharacters() {
boolean nestHierarchicalLoops() {
return getProperty(EDIInputFactory.EDI_NEST_HIERARCHICAL_LOOPS, Boolean::parseBoolean, true);
}

@SuppressWarnings("deprecation")
boolean enableLoopText() {
return getProperty(EDIInputFactory.EDI_ENABLE_LOOP_TEXT, Boolean::parseBoolean, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import io.xlate.edi.internal.stream.tokenization.ProxyEventHandler;
import io.xlate.edi.schema.EDIComplexType;
import io.xlate.edi.schema.EDIReference;
import io.xlate.edi.stream.EDIInputFactory;
Expand All @@ -47,7 +48,7 @@ final class StaEDIXMLStreamReader implements XMLStreamReader {
private static final Logger LOGGER = Logger.getLogger(StaEDIXMLStreamReader.class.getName());
private static final QName DUMMY_QNAME = new QName("DUMMY");
private static final QName INTERCHANGE = new QName(EDINamespaces.LOOPS, "INTERCHANGE", prefixOf(EDINamespaces.LOOPS));
private static final QName TRANSACTION = new QName(EDINamespaces.LOOPS, "TRANSACTION", prefixOf(EDINamespaces.LOOPS));
private static final QName TRANSACTION = new QName(EDINamespaces.LOOPS, ProxyEventHandler.LOOP_CODE_TRANSACTION, prefixOf(EDINamespaces.LOOPS));

private final EDIStreamReader ediReader;
private final Map<String, Object> properties;
Expand Down Expand Up @@ -206,15 +207,14 @@ private void enqueueEvent(EDIStreamEvent ediEvent) throws XMLStreamException {

case START_TRANSACTION:
withinTransaction = true;
readerText = ediReader.getText();
name = buildName(elementStack.getFirst(), EDINamespaces.LOOPS, readerText);
name = buildName(elementStack.getFirst(), EDINamespaces.LOOPS, ediReader.getReferenceCode());
enqueueEvent(START_ELEMENT, name, true);
determineTransactionSegments();
break;

case START_GROUP:
case START_LOOP:
name = buildName(elementStack.getFirst(), EDINamespaces.LOOPS, ediReader.getText());
name = buildName(elementStack.getFirst(), EDINamespaces.LOOPS, ediReader.getReferenceCode());
enqueueEvent(START_ELEMENT, name, true);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void enqueueEvent(EDIStreamEvent ediEvent) {
case START_GROUP:
case START_TRANSACTION:
case START_LOOP:
enqueueStructureBegin("loop", ediReader.getText());
enqueueStructureBegin("loop", ediReader.getReferenceCode());
break;
case START_SEGMENT:
enqueueStructureBegin("segment", ediReader.getText());
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/xlate/edi/internal/stream/tokenization/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ public Dialect getDialect() {
return dialect;
}

public void invalidate() {
if (state != State.INVALID) {
previous = state;
state = State.INVALID;
}
}

public void setBinaryLength(long binaryLength) {
this.binaryRemain = binaryLength;

Expand Down Expand Up @@ -604,4 +611,14 @@ void popMode(Mode expected) throws EDIException {
throw error(EDIException.INVALID_STATE);
}
}

/* test */
State currentState() {
return state;
}

/* test */
State previousState() {
return previous;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

public class ProxyEventHandler implements EventHandler {

public static final String LOOP_CODE_GROUP = EDIType.Type.GROUP.toString();
public static final String LOOP_CODE_TRANSACTION = EDIType.Type.TRANSACTION.toString();

private final StaEDIStreamLocation location;
private final boolean nestHierarchicalLoops;

Expand Down Expand Up @@ -200,17 +203,17 @@ public void interchangeEnd() {
public void loopBegin(EDIReference typeReference) {
final String loopCode = typeReference.getReferencedType().getCode();

if (EDIType.Type.TRANSACTION.toString().equals(loopCode)) {
if (LOOP_CODE_TRANSACTION.equals(loopCode)) {
transaction = true;
transactionSchemaAllowed = true;
enqueueEvent(EDIStreamEvent.START_TRANSACTION, EDIStreamValidationError.NONE, loopCode, typeReference, location);
enqueueEvent(EDIStreamEvent.START_TRANSACTION, EDIStreamValidationError.NONE, null, typeReference, location);
if (transactionValidator != null) {
transactionValidator.reset();
}
} else if (EDIType.Type.GROUP.toString().equals(loopCode)) {
enqueueEvent(EDIStreamEvent.START_GROUP, EDIStreamValidationError.NONE, loopCode, typeReference, location);
} else if (LOOP_CODE_GROUP.equals(loopCode)) {
enqueueEvent(EDIStreamEvent.START_GROUP, EDIStreamValidationError.NONE, null, typeReference, location);
} else {
enqueueEvent(EDIStreamEvent.START_LOOP, EDIStreamValidationError.NONE, loopCode, typeReference, location);
enqueueEvent(EDIStreamEvent.START_LOOP, EDIStreamValidationError.NONE, null, typeReference, location);

if (nestHierarchicalLoops && isHierarchicalLoop(typeReference.getReferencedType())) {
EDILoopType loop = (EDILoopType) typeReference.getReferencedType();
Expand All @@ -229,17 +232,17 @@ public void loopEnd(EDIReference typeReference) {
// Validator can not be null when a loopEnd event has been signaled.
validator().validateLoopSyntax(this);

if (EDIType.Type.TRANSACTION.toString().equals(loopCode)) {
if (LOOP_CODE_TRANSACTION.equals(loopCode)) {
transaction = false;
dialect.transactionEnd();
enqueueEvent(EDIStreamEvent.END_TRANSACTION, EDIStreamValidationError.NONE, loopCode, typeReference, location);
} else if (EDIType.Type.GROUP.toString().equals(loopCode)) {
enqueueEvent(EDIStreamEvent.END_TRANSACTION, EDIStreamValidationError.NONE, null, typeReference, location);
} else if (LOOP_CODE_GROUP.equals(loopCode)) {
dialect.groupEnd();
enqueueEvent(EDIStreamEvent.END_GROUP, EDIStreamValidationError.NONE, loopCode, typeReference, location);
enqueueEvent(EDIStreamEvent.END_GROUP, EDIStreamValidationError.NONE, null, typeReference, location);
} else if (nestHierarchicalLoops && isHierarchicalLoop(typeReference.getReferencedType())) {
levelCheckPending = true;
} else {
enqueueEvent(EDIStreamEvent.END_LOOP, EDIStreamValidationError.NONE, loopCode, typeReference, location);
enqueueEvent(EDIStreamEvent.END_LOOP, EDIStreamValidationError.NONE, null, typeReference, location);
}
}

Expand Down
22 changes: 20 additions & 2 deletions src/main/java/io/xlate/edi/stream/EDIInputFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,33 @@ public abstract class EDIInputFactory extends PropertySupport {
* When set to true, hierarchical loops will be nested in the EDI input
* stream. The nesting structure is determined by the linkage specified by
* the EDI data itself using pointers given in the EDI schema for a loop.
*
*
* For example, the hierarchical information given by the X12 HL segment.
*
*
* Default value: true
*
* @since 1.18
*/
public static final String EDI_NEST_HIERARCHICAL_LOOPS = "io.xlate.edi.stream.EDI_NEST_HIERARCHICAL_LOOPS";

/**
* When set to true, functional group, transaction, and loop start/end
* events will allow for {@link EDIStreamReader#getText()} to be called,
* which is the legacy behavior.
*
* The default value is `true` and this property is deprecated. In the next
* major release, the property's default value will be `false`.
*
* Default value: true
*
* @since 1.20
* @deprecated use {@link EDIStreamReader#getReferenceCode()} and
* {@link EDIStreamReader#getSchemaTypeReference()} to retrieve
* additional information for non-textual event types.
*/
@Deprecated
public static final String EDI_ENABLE_LOOP_TEXT = "io.xlate.edi.stream.EDI_ENABLE_LOOP_TEXT"; //NOSONAR

/**
* When set to true, simple data elements not containing data will be
* represented via the JSON parsers as a <i>null</i> value.
Expand Down

0 comments on commit 1973341

Please sign in to comment.