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

org.hibernate.cfg.reveng.MetaAttributeBinder Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
package org.hibernate.cfg.reveng;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
import org.dom4j.Element;
import org.hibernate.mapping.MetaAttribute;
import org.hibernate.tool.hbm2x.MetaAttributeHelper;

public class MetaAttributeBinder {

	/**
	 * Merges a Multimap with inherited maps.
	 * Values specified always overrules/replaces the inherited values.
	 * 
	 * @param specific
	 * @param general
	 * @return a MultiMap with all values from local and extra values
	 * from inherited
	 */
	public static MultiValuedMap mergeMetaMaps(
			MultiValuedMap specific, 
			MultiValuedMap general) {
		MultiValuedMap result = new HashSetValuedHashMap();
		MetaAttributeHelper.copyMultiMap(result, specific);
		if (general != null) {
			for (Iterator iter = general.keySet().iterator();iter.hasNext();) {
				String key = iter.next();
				if (!specific.containsKey(key) ) {
					// inheriting a meta attribute only if it is inheritable
					Collection ml = general.get(key);
					for (Iterator iterator = ml.iterator(); iterator.hasNext();) {
						SimpleMetaAttribute element = iterator.next();
						if (element.inheritable) {
							result.put(key, element);
						}
					}
				}
			}
		}
	
		return result;
	
	}

	public static MetaAttribute toRealMetaAttribute(String name, Collection values) {
		MetaAttribute attribute = new MetaAttribute(name);
		for (Iterator iter = values.iterator(); iter.hasNext();) {
			SimpleMetaAttribute element = iter.next();
			attribute.addValue(element.value);
		}
		
		return attribute;
	}

	/**
	 * Method loadAndMergeMetaMap.
	 * @param classElement
	 * @param inheritedMeta
	 * @return MultiMap
	 */
	public static MultiValuedMap loadAndMergeMetaMap(
		Element classElement,
		MultiValuedMap inheritedMeta) {
		return MetaAttributeBinder.mergeMetaMaps(loadMetaMap(classElement), inheritedMeta);
	}


	/**
	 * Load meta attributes from jdom element into a MultiMap.
	 * 
	 * @param element
	 * @return MultiMap
	 */
	 protected static MultiValuedMap loadMetaMap(Element element) {
		MultiValuedMap result = new HashSetValuedHashMap();
		List metaAttributeList = new ArrayList();
		for (Object obj : element.elements("meta")) {
			metaAttributeList.add((Element)obj);
		}

		for (Iterator iter = metaAttributeList.iterator(); iter.hasNext();) {
			Element metaAttrib = iter.next();
			// does not use getTextNormalize() or getTextTrim() as that would remove the formatting in new lines in items like description for javadocs.
			String attribute = metaAttrib.attributeValue("attribute");
			String value = metaAttrib.getText();
			String inheritStr= metaAttrib.attributeValue("inherit");
			boolean inherit = true;
			if(inheritStr!=null) {
				inherit = Boolean.valueOf(inheritStr).booleanValue(); 
			}			
			
			SimpleMetaAttribute ma = new SimpleMetaAttribute(value, inherit);
			result.put(attribute, ma);
		}
		return result;

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy