All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.milyn.edisax.unedifact.UNEdifactUtil Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2006 - 2010

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU Lesser General Public License for more details:
	http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.edisax.unedifact;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.milyn.edisax.util.EDIUtils;
import org.milyn.edisax.model.EdifactModel;
import org.milyn.edisax.model.internal.Delimiters;
import org.milyn.edisax.model.internal.Description;
import org.xml.sax.SAXException;

/**
 * UN/EDIFACT utility methods.
 * 
 * @author [email protected]
 */
public abstract class UNEdifactUtil {

	public static EdifactModel getMappingModel(String messageName, Delimiters delimiters, Map mappingModels) throws SAXException {
		Set> modelSet = mappingModels.entrySet();
		String[] nameComponents = EDIUtils.split(messageName, delimiters.getComponent(), delimiters.getEscape());
		StringBuilder lookupNameBuilder = new StringBuilder();
		
		// First 4 components are mandatory...we use those as the lookup...
		for(int i = 0; i < 4; i++) {
			if(i > 0) {
				lookupNameBuilder.append(':');
			}
			lookupNameBuilder.append(nameComponents[i]);
		}
		String lookupName = lookupNameBuilder.toString().trim();
		
		for(Entry mappingModel : modelSet) {
			Description description = mappingModel.getKey();
			String compoundName = description.getName() + ":" + description.getVersion();
			
			if(compoundName.equals(lookupName)) {
				return mappingModel.getValue();
			}
		}
		
		throw new SAXException("Mapping Model '" + messageName + "' not found in supplied set of Mapping model.");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy