| 6 | import HighLevelEncoder from './HighLevelEncoder'; |
| 7 | |
| 8 | export class EdifactEncoder implements Encoder { |
| 9 | public getEncodingMode() { |
| 10 | return EDIFACT_ENCODATION; |
| 11 | } |
| 12 | |
| 13 | public encode(context: EncoderContext) { |
| 14 | // step F |
| 15 | const buffer = new StringBuilder(); |
| 16 | while (context.hasMoreCharacters()) { |
| 17 | const c = context.getCurrentChar(); |
| 18 | this.encodeChar(c, buffer); |
| 19 | context.pos++; |
| 20 | |
| 21 | const count = buffer.length(); |
| 22 | if (count >= 4) { |
| 23 | context.writeCodewords(this.encodeToCodewords(buffer.toString())); |
| 24 | |
| 25 | const test = buffer.toString().substring(4); |
| 26 | buffer.setLengthToZero(); |
| 27 | buffer.append(test); |
| 28 | |
| 29 | // buffer.delete(0, 4); |
| 30 | // for (let i = 0; i < 4; i++) { |
| 31 | // buffer.deleteCharAt(i); |
| 32 | // } |
| 33 | |
| 34 | const newMode = HighLevelEncoder.lookAheadTest( |
| 35 | context.getMessage(), |
| 36 | context.pos, |
| 37 | this.getEncodingMode() |
| 38 | ); |
| 39 | if (newMode !== this.getEncodingMode()) { |
| 40 | // Return to ASCII encodation, which will actually handle latch to new mode |
| 41 | context.signalEncoderChange(ASCII_ENCODATION); |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | buffer.append(StringUtils.getCharAt(31)); // Unlatch |
| 47 | this.handleEOD(context, buffer); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Handle "end of data" situations |
| 52 | * |
| 53 | * @param context the encoder context |
| 54 | * @param buffer the buffer with the remaining encoded characters |
| 55 | */ |
| 56 | private handleEOD(context: EncoderContext, buffer: StringBuilder) { |
| 57 | try { |
| 58 | const count = buffer.length(); |
| 59 | if (count === 0) { |
| 60 | return; // Already finished |
| 61 | } |
| 62 | if (count === 1) { |
| 63 | // Only an unlatch at the end |
| 64 | context.updateSymbolInfo(); |
| 65 | let available = |
nothing calls this directly
no outgoing calls
no test coverage detected