Skip to content
Snippets Groups Projects
Adapter.java 11.9 KiB
Newer Older
marvin's avatar
marvin committed
import java.io.*;
import java.util.*;

import org.jdom2.*;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.*;



/**
 * Takes a featuremodel.xml file generated using FeatureModelConfigurator and a given ROS/ Gazebo .gazebo.xacro file.
 * The .gazebo.xacro file is modified to support the features specified in the featuremodel.xml file.
marvin's avatar
marvin committed
 */
public class Adapter {
	
	/**
	 * Reads an .xml file from file path and returns a JDOM document.
	 * 
	 * @param src			The .xml file path
	 * @return JDOM document
	 * @throws JDOMException
	 * @throws IOException
	 */
	public static Document readXml(String src) throws JDOMException, IOException {
		File file = new File(src);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document doc = saxBuilder.build(file);

		return doc;
	}
	
	/**
	 * Saves the given Document as XML to the given path.
	 * 
	 * @param oDoc			The JDOM document
	 * @param sPath			The output path
	 * @throws IOException
	 */
	public static void saveAsXml(Document oDoc, String sPath) throws IOException {
		FileOutputStream dest = new FileOutputStream(sPath);
		XMLOutputter xmlOutput = new XMLOutputter();		
		xmlOutput.output(oDoc, dest);
	}
	
marvin's avatar
marvin committed
	/**
	 * Takes a gives directory path and lists all files directly in it.
	 * (Does not visit sub-directories recursively)
	 * 
	 * @param path			Path to the directory
	 * @return ArrayList with all file names
	 */
	public static ArrayList<String> getFileNames(String path) {
		ArrayList<String> fileNames = new ArrayList<String>();
		File directory = new File(path);
		File[] contentList = directory.listFiles();

		for (File file : contentList) {
			if (file.isFile()) fileNames.add(file.getName());
		}
		
		return fileNames;
	}
	
marvin's avatar
marvin committed
	/**
	 * Executes given XPath expression for elements with JDOM Document as source.
	 * 
	 * @param doc			The JDOM Document
	 * @param xPath			XPath expression
	 * @return List with all elements found or empty list if no match found
	 */
	public static List<Element> xQueryElements(Document doc, String xPath) {
		XPathFactory xFactory = XPathFactory.instance();
		XPathExpression<Element> expr = xFactory.compile(xPath, Filters.element());
		List<Element> result = expr.evaluate(doc);
		
		return result;
	}
	
	/**
	 * Executes given XPath expression for elements with Element as source.
	 * 
	 * @param elem			The Element
	 * @param xPath			XPath expression
	 * @return List with all elements found or empty list if no match found
	 */
	public static List<Element> xQueryElements(Element elem, String xPath) {
		XPathFactory xFactory = XPathFactory.instance();
		XPathExpression<Element> expr = xFactory.compile(xPath, Filters.element());
		List<Element> result = expr.evaluate(elem);
		
		return result;
	}
	
	/**
	 * Executes given XPath expression for attributes.
	 * 
	 * @param doc			The JDOM Document
	 * @param xPath			XPath expression
	 * @return List with all attributes found or empty list if no match found
	 */
	public static List<Attribute> xQueryAttributes(Document doc, String xPath) {
		XPathFactory xFactory = XPathFactory.instance();
		XPathExpression<Attribute> expr = xFactory.compile(xPath, Filters.attribute());
		List<Attribute> result = expr.evaluate(doc);
		
		return result;
	}
	
	/**
	 * Searches a document for the first occurrence of an xml tag.
	 * 
	 * @param doc			The JDOM Document
	 * @param elementName	Name of the xml tag
	 * @return First element found or NULL if no match found
	 */
	public static Element searchFirstElement(Document doc, String elementName) {
		//XPath index starts with 1; [] has a higher precedence than //
		List<Element> query= xQueryElements(doc, "(//"+elementName+")[1]");
		
		if(query.isEmpty()) return null;
		return query.get(0);
	}
	
	/**
	 * Searches a document for all occurrences of an xml tag.
	 * 
	 * @param doc			The JDOM Document
	 * @param elementName	Name of the xml tag
	 * @return List with all elements found or empty list if no match found
	 */
	public static List<Element> searchAllElements(Document doc, String elementName) {
		return xQueryElements(doc, "//"+elementName);
	}
	
	/**
	 * Checks whether two elements have an equal xml tag.
	 * (Name of the xml tag and all attributes including their order must be equal)
	 * 
	 * @param element1		Element1
	 * @param element2		Element2
	 * @return true if of equal tag or false if not
	 */
	public static boolean equalTag(Element element1, Element element2) {
		List<Attribute> attributeList1 = element1.getAttributes();
		List<Attribute> attributeList2 = element2.getAttributes();
		
		if(!element1.getName().equals(element2.getName())) return false;
		if(attributeList1.size()!=attributeList2.size()) return false;
		for(int i=0; i<attributeList1.size(); i++) {
			if(!attributeList1.get(i).getName().equals(attributeList2.get(i).getName())) return false;
			if(!attributeList1.get(i).getValue().equals(attributeList2.get(i).getValue())) return false;
		}
		
		return true;
	}
	
	/**
	 * Recursive replacement of all text values in destElements with their corresponding 
	 * new ones from srcElements.
	 * (srcElements and destElements must be matching tags!)
	 * 
	 * @param srcElements		Element list with new values
	 * @param destElements		Element list with values to be replaced
	 * @return true if reached deepest Element or false if not
	 */
	public static boolean replace(List<Element> srcElements, List<Element> destElements) {
		if(srcElements.isEmpty()&&destElements.isEmpty()) return true;
		if(srcElements.isEmpty()^destElements.isEmpty()) {
marvin's avatar
marvin committed
			System.err.println("Error during value replacement: Sensor xml trees have mismatching deph (maybe tags with no value)");
marvin's avatar
marvin committed
			System.exit(-1);
		}
		for(Element src : srcElements) {
			for(Element dest : destElements) {		
				if(equalTag(src, dest)) {
					boolean deadEnd = replace(src.getChildren(), dest.getChildren());
					if(deadEnd) dest.setText(src.getText());
					break;
				}
			}
		}
marvin's avatar
marvin committed
		
marvin's avatar
marvin committed
		return false;
	}
	
marvin's avatar
marvin committed
	/**
	 * Obtains an attribute value of the input/ output entries specified in adapterconfig.xml.
marvin's avatar
marvin committed
	 * 
	 * @param doc		The JDOM Document (adapterconfig.xml)
	 * @param entry		String formed as "{input or output}/{entity}"
	 * @param val		Attribute name as string
marvin's avatar
marvin committed
	 * @return path as specified in adapterconfig.xml
	 */
	private static String getConfigVal(Document doc, String str, String val) {
		List<Attribute> valList = xQueryAttributes(doc, "//"+str+"/@"+val);
		if(valList.size()!=1) {
			System.err.println("Error while reading input/ output "+val+" value from adapterconfig.xml");
marvin's avatar
marvin committed
			System.exit(-1);
		}

		return valList.get(0).getValue();
marvin's avatar
marvin committed
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Adapter started.");
		
		try {
			//######### Input ###############################			
			Document adapterConfig = readXml("./adapterconfig.xml");
			
			String in_fmPath = getConfigVal(adapterConfig, "input/featuremodel", "path");
			String in_scPath = getConfigVal(adapterConfig, "input/sensorconfig", "path");
			String in_gmPath = getConfigVal(adapterConfig, "input/gazebomodel", "path");
			String out_gmPath = getConfigVal(adapterConfig, "output/gazebomodel", "path");
			String out_bnPath = getConfigVal(adapterConfig, "output/botname", "path");
			String in_fmName = getConfigVal(adapterConfig, "input/featuremodel", "name");
			String in_gmName = "";
			String out_gmName = getConfigVal(adapterConfig, "output/gazebomodel", "name");
			String out_bnName = getConfigVal(adapterConfig, "output/botname", "name");
			
			Document featureModel = readXml(in_fmPath+in_fmName);
			List<Document> sensorConfigList = new ArrayList<>();
			Document gazeboModel = null;
			
			//Get the selected mode (either NO_CHANGE or Custom) as String
			String mode = "";
			List<Attribute> modeList = xQueryAttributes(featureModel, "//mode/@param");
			if(modeList.size()==1) {
				mode = modeList.get(0).getValue();
			}
			else {
				System.err.println("Invalid input in \'"+in_fmName+"\': NO_CHANGE/ Custom parameter could not be parsed.");
				System.exit(-1);
			}
			
			//Get all selected sensorconfig files as List<Document>
			switch(mode) {
				case "NO_CHANGE":
					break;
				case "Custom":
					List<Attribute> sensorList = xQueryAttributes(featureModel, "//sensor/@name");
					
					List<String> scList = getFileNames(in_scPath);
					for(Attribute sensor : sensorList) {
						boolean inDirectory = false;
						for(String fileName : scList) {
							if(fileName.equals(sensor.getValue()+".xml")) inDirectory = true;
						}
						if(!inDirectory) {
							System.err.println("Cannot handle \'"+sensor.getValue()+"\': No input file known.");
							System.exit(-1);
						}
					}
					
					for(Attribute sensor : sensorList) {
						sensorConfigList.add(readXml(in_scPath+sensor.getValue()+".xml")); 
					}
					break;
				default:
					System.err.println("Invalid input in \'"+in_fmName+"\': NO_CHANGE/ Custom parameter could not be parsed.");
					System.exit(-1);
			}
			
			//Get the selected .gazebo.xacro file as Document
			String botName = "";
			List<Attribute> botList = xQueryAttributes(featureModel, "//templates/@name");
			if(botList.size()==1) {
				botName = botList.get(0).getValue();
			}
			else {
				System.err.println("Invalid input in \'"+in_fmName+"\': No or multiple bots selected.");
				System.exit(-1);
			}

			in_gmName = botName+".gazebo.xacro";
			
			List<String> gmList = getFileNames(in_gmPath);
			boolean inDirectory = false;
			for(String fileName : gmList) {
				if(fileName.equals(in_gmName)) inDirectory = true;
			}
			if(!inDirectory) {
				System.err.println("Cannot handle \'"+botName+"\': No input file known.");
				System.exit(-1);
			}
			
			gazeboModel = readXml(in_gmPath+in_gmName);
			//###############################################
			
			
			//######### Operate on .gazebo.xacro ############
			switch(mode) {
				case "NO_CHANGE":
					break;
				case "Custom":
					//Remove all existing sensors in template
					List<Element> removableSensorsList = xQueryElements(gazeboModel, "//sensor");
					
					for(Element sensor : removableSensorsList) {	
						Element entry = sensor.getParentElement();
						if(entry.getName().equals("gazebo")) {
							entry.detach();
						}
						else {
							System.out.println("Found a <sensor> element in \'"+in_gmName+
								"\' that does not have a <gazebo> element as immediate parent. It was not removed, as this input is unexpected.");	
						}
					}
					
					//Insert selected sensors into template
					for(Document sensorConfig : sensorConfigList) {	
						List<Element> sensorList = xQueryElements(sensorConfig, "//gazebo");
						if(sensorList.size()==1) {
							Element sensor = sensorList.get(0);
							
							//Warn if a sensor still contains $(arg ...) values
							List<Element> tagList = xQueryElements(sensor, "//*");
							for(Element tag : tagList) {	
								String text = tag.getText();
								if(text.contains("$")) {
									System.out.println("Warning: a sensorconfig file still contains an abstract value: \'"+text+
										"\'. It should be replaced with a set value, or the output .gazebo.xacro will likely be broken.");	
								}
							}
							
							sensor.detach();
							gazeboModel.getRootElement().addContent(sensor);
						}
						else {
							System.err.println("Invalid input. At least one sensorconfig file contains no or multiple <gazebo> entries.");
							System.exit(-1);
						}
					}
					break;
				default:
					System.err.println("Invalid input in \'"+in_fmName+"\': NO_CHANGE/ Custom parameter could not be parsed.");
					System.exit(-1);
			}
			//###############################################
			
			
			//######### Output result #######################
			saveAsXml(gazeboModel, out_gmPath+out_gmName);
			
			BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(out_bnPath+out_bnName));
			bufferedWriter.write(botName);
			bufferedWriter.close();
			//###############################################
		}
		catch (JDOMException | IOException e) {
			e.printStackTrace();
		}

		System.out.println("Adapter finished.");
marvin's avatar
marvin committed
	}
}