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

org.databene.edifatto.gui.ParameterizationHelper Maven / Gradle / Ivy

Go to download

'Edifatto' is an open source software library for parsing, generating and verifying EDIFACT and X12 documents written by Volker Bergmann.

The newest version!
/*
 * Copyright (C) 2013-2015 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.gui;

import org.databene.edifatto.EdifattoConfig;
import org.databene.edifatto.definition.Definition;
import org.databene.edifatto.model.Component;
import org.databene.edifatto.model.EdiGroup;
import org.databene.edifatto.model.EdiItem;
import org.databene.edifatto.model.Interchange;
import org.databene.edifatto.template.DataModel;
import org.databene.edifatto.template.DataModelBuilder;
import org.databene.edifatto.util.EdiUtil;
import org.databene.edifatto.util.Parent;

import freemarker.template.utility.StringUtil;

/**
 * Provides utility methods for the parameterization of EDIFACT documents.
 * Created: 23.07.2014 11:57:52
 * @since 2.0.0
 * @author Volker Bergmann
 */

public class ParameterizationHelper {
	
	private static final String PARAM_NAME = "parameter.name";
	private static final String ITEM_NAME = "item.name";
	private static final String BEANCLASS_NAME = "beanclass.name";
	
	public ParameterizationHelper() {
	}

	public static void togglePropertyName(EdiItem item) {
		String name = getPropertyName(item);
		if (name == null) {
			setPropertyName(createPropertyNameFor(item), item);
			if ((item instanceof EdiGroup)) {
				setItemName(defaultItemNameFor(item), item);
				setBeanClassName(defaultBeanClassNameFor(item), item);
			}
		} else {
			setPropertyName(null, item);
			setItemName(null, item);
			setBeanClassName(null, item);
		}
	}

	private static String defaultBeanClassNameFor(EdiItem item) {
		return StringUtil.capitalize(defaultItemNameFor(item));
	}

	private static String createPropertyNameFor(EdiItem item) {
		String defaultName = defaultPropertyNameFor(item);
		int n = 0;
		String name;
		do {
			name = defaultName;
			n++;
			if (n > 1)
				name += "_" + n;
		} while (nameUsed(name, EdiUtil.getRoot(item)));
		return name;
	}

	@SuppressWarnings("unchecked")
	private static boolean nameUsed(String name, EdiItem item) {
		if (name.equals(getPropertyName(item)))
			return true;
		if (item instanceof Parent) {
			Parent parent = (Parent) item;
			int n = parent.getChildCount();
			for (int i = 0; i < n; i++) {
				EdiItem child = parent.getChild(i);
				if (nameUsed(name, child))
					return true;
			}
		}
		return false;
	}
	
	private static String defaultPropertyNameFor(EdiItem item) {
		if ((item instanceof EdiGroup))
			return formatName(item, true);
		else if (item instanceof Component)
			return formatName(item, false);
		else
			throw new UnsupportedOperationException("Cannot provide default parameter name for " + item.getClass());
	}

	private static String defaultItemNameFor(EdiItem item) {
		return formatName(item, false);
	}

	private static String formatName(EdiItem item, boolean plural) {
		Definition definition = item.getDefinition();
		StringBuilder builder = new StringBuilder();
		if (definition != null) {
			String description = definition.getDocumentation();
			if (description.length() < 100) {
				appendNameToken(description, true, builder);
				if (plural)
					builder.append("s");
			}
			appendNameToken(definition.getName(), false, builder);
		}
		return builder.toString();
	}

	private static void appendNameToken(String token, boolean lowerCase, StringBuilder builder) {
		if (token != null && !token.trim().isEmpty()) {
			if (builder.length() > 0)
				builder.append('_');
			token = token.trim();
			for (int i = 0; i < token.length(); i++) {
				char c = token.charAt(i);
				c = (Character.isLetterOrDigit(c) ? (lowerCase ? Character.toLowerCase(c) : c) : '_');
				builder.append(c);
			}
		}
	}

	public static String getPropertyName(EdiItem item) {
		return (String) item.getTag(PARAM_NAME);
	}

	public static void setPropertyName(String parameterName, EdiItem item) {
		item.setTag(PARAM_NAME, parameterName);
	}

	public static boolean isParameterized(EdiItem item) {
		return (getPropertyName(item) != null);
	}

	public static String getItemName(EdiItem item) {
		return (String) item.getTag(ITEM_NAME);
	}

	public static void setItemName(String itemName, EdiItem item) {
		item.setTag(ITEM_NAME, itemName);
	}

	public static String getBeanClassName(EdiItem item) {
		return (String) item.getTag(BEANCLASS_NAME);
	}

	public static void setBeanClassName(String beanClassName, EdiItem item) {
		item.setTag(BEANCLASS_NAME, beanClassName);
	}

	public static DataModel createDataModel(Interchange interchange) {
		EdifattoConfig cfg = EdifattoConfig.getInstance();
		DataModelBuilder builder = new DataModelBuilder(cfg.getDataClassPackage(), cfg.getDataClassParent());
		return builder.buildModel(interchange);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy