
org.databene.edifatto.parser.EdifactParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edifatto Show documentation
Show all versions of edifatto Show documentation
'Edifatto' is an open source software library for parsing,
generating and verifying EDIFACT and X12 documents written by Volker Bergmann.
/*
* Copyright (C) 2013-2014 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.databene.edifatto.parser;
import java.text.ParsePosition;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.databene.edifatto.EdiFormatSymbols;
import org.databene.edifatto.EdifattoConfig;
import org.databene.edifatto.definition.MessageDefinition;
import org.databene.edifatto.model.Composite;
import org.databene.edifatto.model.EdiProtocol;
import org.databene.edifatto.model.Interchange;
import org.databene.edifatto.model.Message;
import org.databene.edifatto.model.Segment;
/**
* Parses EDI interchanges.
* Created: 10.10.2013 16:20:56
* @since 1.0
* @author Volker Bergmann
*/
public class EdifactParser extends AbstractEdiParser {
public EdifactParser() {
super(EdiProtocol.EDIFACT);
}
@Override
protected EdiFormatSymbols determineSymbols(String text) {
if (text.startsWith("UNA")) {
char componentSep = text.charAt(3);
char elementSep = text.charAt(4);
char decimalSep = text.charAt(5);
char escapeChar = text.charAt(6);
char reserved = text.charAt(7);
char segmentSep = text.charAt(8);
return new EdiFormatSymbols(componentSep, elementSep, decimalSep, escapeChar, reserved, segmentSep);
} else {
return EdiFormatSymbols.EDIFACT;
}
}
@Override
protected Interchange parseSegments(List segments, EdiFormatSymbols symbols) {
ParsePosition pos = new ParsePosition(0);
Interchange interchange = new Interchange(EdiProtocol.EDIFACT, symbols);
Segment una = parseOptionalSegment("UNA", segments, pos);
if (una != null)
interchange.addChild(una);
return parseInterchange(segments, pos, interchange, new HashMap());
}
@Override
protected Message parseRequiredMessage(List segments, ParsePosition pos, Map context) {
Segment currentSegment = segments.get(pos.getIndex());
AbstractEdiParser.requireTag(protocol.getMessageHeaderTag(), currentSegment);
Composite spec = (Composite) currentSegment.getChild(1);
String type = spec.getChild(0).getData();
String version = spec.getChild(1).getData();
String release = spec.getChild(2).getData();
MessageDefinition messageDefinition = EdifattoConfig.getInstance().getMessageDefinition(EdiProtocol.EDIFACT, type, version, release);
MessageParser messageParser = new MessageParser(messageDefinition);
Message message = messageParser.parseMessage(segments, pos);
return message;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy