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

org.eclipse.epsilon.flexmi.templates.XmlTemplate Maven / Gradle / Ivy

package org.eclipse.epsilon.flexmi.templates;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.text.StrLookup;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.eclipse.epsilon.flexmi.xml.Xml;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class XmlTemplate extends Template {

	public XmlTemplate(Element element, URI uri) {
		super(element, uri);
	}

	public List apply(Element call) {
		
		List application = getApplication(call);
		
		for (Element applicationElement : application) {
			for (String attributeName : Xml.getAttributeNames(call)) {
				if (!attributeName.startsWith(Template.PREFIX)) {
					applicationElement.setAttribute(attributeName, call.getAttribute(attributeName));
				}
			}
			replaceParameters(applicationElement, call);
		}
		
		return application;
	}
	
	public List getApplication(Element call) {
		List application = new ArrayList();
		for (Element contentChild : Xml.getChildren(content)) {
			application.add((Element) contentChild.cloneNode(true));
		}
		return application;
	}
	
	protected void replaceParameters(Element element, Element call) {
		
		StrSubstitutor substitutor = new StrSubstitutor(new StrLookup() {
			@Override
			public String lookup(String name) {
				return call.getAttribute("_" + name);
			}
		});
		
		for (Node attribute : Xml.getAttributes(element)) {
			if (attribute.getNodeValue().indexOf("$") > -1) {
				attribute.setNodeValue(substitutor.replace(attribute.getNodeValue()));
			}
		}
		
		for (Element child : Xml.getChildren(element)) {
			replaceParameters(child, call);
		}
		
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy