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

org.milyn.json.KeyMapDigester Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
package org.milyn.json;

import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.milyn.cdr.SmooksConfigurationException;
import org.milyn.xml.DomUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class KeyMapDigester {

	private static final String KEY_MAP_KEY_ELEMENT = "key";

	private static final String KEY_MAP_KEY_ELEMENT_FROM_ATTRIBUTE = "from";

	private static final String KEY_MAP_KEY_ELEMENT_TO_ATTRIBUTE = "to";

	public static HashMap digest(Element keyMapElement) {
		HashMap keyMap = new HashMap();

		NodeList keys = keyMapElement.getElementsByTagNameNS("*", KEY_MAP_KEY_ELEMENT);

        for (int i = 0; keys != null && i < keys.getLength(); i++) {
        	Element keyElement = (Element)keys.item(i);

        	String from = DomUtils.getAttributeValue(keyElement, KEY_MAP_KEY_ELEMENT_FROM_ATTRIBUTE);

        	if(StringUtils.isBlank(from)) {
        		throw new SmooksConfigurationException("The '"+ KEY_MAP_KEY_ELEMENT_FROM_ATTRIBUTE +"' attribute isn't defined or is empty for the key element: " + keyElement);
        	}
        	from = from.trim();

        	String value = DomUtils.getAttributeValue(keyElement, KEY_MAP_KEY_ELEMENT_TO_ATTRIBUTE);
        	if(value == null) {
        		value = DomUtils.getAllText(keyElement, true);
        		if(StringUtils.isBlank(value)) {
        			value = null;
        		}
        	}
        	keyMap.put(from, value);
        }

		return keyMap;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy