
org.databene.edifatto.util.EdiUtil 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.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.databene.commons.Assert;
import org.databene.commons.StringUtil;
import org.databene.commons.SyntaxError;
import org.databene.edifatto.ComparisonSettings;
import org.databene.edifatto.definition.MessageDefinition;
import org.databene.edifatto.model.EdiItem;
import org.databene.edifatto.model.EdiProtocol;
import org.databene.edifatto.model.EdifactMessage;
import org.databene.edifatto.model.Interchange;
import org.databene.edifatto.model.Message;
import org.databene.edifatto.model.X12Message;
/**
* Provides utility methods related to EDI documents.
* Created: 29.05.2014 07:11:44
* @since 1.2
* @author Volker Bergmann
*/
public class EdiUtil {
private EdiUtil() { }
public static String formatCardinalities(int min, int max) {
return min != max ? min + ".." + max : String.valueOf(min);
}
public static Message createMessage(MessageDefinition definition) {
Assert.notNull(definition, "definition");
EdiProtocol protocol = Assert.notNull(definition.getProtocol(), "protocol");
switch (protocol) {
case EDIFACT : return new EdifactMessage(null, definition);
case X12 : return new X12Message(null, definition);
default: throw new UnsupportedOperationException("Not a supported protocol: " + protocol);
}
}
public static ComparisonSettings settingsWithExclusionPaths(String... exclusionPaths) {
ComparisonSettings settings = new ComparisonSettings();
for (String exclusionPath : exclusionPaths)
settings.addToleratedDiff(null, exclusionPath);
return settings;
}
public static EdiProtocol protocolForContent(String text) {
if (text.startsWith("UN"))
return EdiProtocol.EDIFACT;
else if (text.startsWith("ISA"))
return EdiProtocol.X12;
else
throw new SyntaxError("No EDIFACT or X12 header found", StringUtil.limitLengthWithEllipsis(text, 30));
}
public static String getName(EdiItem item) {
return (item != null && item.getDefinition() != null? item.getDefinition().getName() : null);
}
public static EdiItem getRoot(EdiItem item) {
EdiItem root = item;
while (root.getParent() != null)
root = root.getParent();
return root;
}
public static Interchange copyOf(Interchange interchange) {
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(interchange);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
Interchange copy = (Interchange) in.readObject();
in.close();
return copy;
} catch (Exception e) {
throw new RuntimeException("Failure while copying interchange", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy