Skip to content
Snippets Groups Projects
Commit 2554084e authored by marvin's avatar marvin
Browse files

merge

parent 63f7a4be
No related merge requests found
File added
File added
File added
File added
import java.util.List;
import java.util.ArrayList;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Document.OutputSettings.Syntax;
import org.jsoup.parser.Parser;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
SoUrCe RooT ConfiguratorMain "../features/ConfiguratorMain/Configurator.jak";
......@@ -35,16 +30,12 @@ abstract class Configurator$$ConfiguratorMain {
//module list
private List m_aModuleList;
//output path
private String m_sOutputPath = "./test.xml";
private String m_sOutputPath = "resources/output/featuremodel.xml";
//TODO: set this
//path for test case selector, which has to be launched from this application
private String m_sTestCaseSelectorPath;
//TODO: set this
//path for the Gazebo adapter, which has to be launched from this application
private String m_sGazeboAdapterPath;
//TODO: properly set this
//path to the feature model xml
private String m_sFeatureModelPath = "./model.xml";
private String m_sFeatureModelPath = "model.xml";
//document representation of the feature model
private Document m_oFeatureModelDoc;
//representation of a single module with all of its types
......@@ -59,10 +50,9 @@ abstract class Configurator$$ConfiguratorMain {
}
public void save() {
//transform list to XML document using jsoup
Document oDoc = Jsoup.parse("<?xml version=\"1.0\"?>\n", "", Parser.xmlParser());
//create empty xml
Element oStruct = new Element("specification");
oDoc.appendChild(oStruct);
Document oDoc = new Document(oStruct);
//if the module list was initialized
if(m_aModuleList!=null) {
......@@ -71,7 +61,7 @@ abstract class Configurator$$ConfiguratorMain {
Element oModuleElement = new Element("module");
//Add name
Module oModule = (Module)m_aModuleList.get(i);
oModuleElement.attr("name", oModule.sModuleName);
oModuleElement.setAttribute("name", oModule.sModuleName);
//Create type String
ArrayList<String> aTypes = oModule.aModuleTypes;
String sTypesString = "";
......@@ -82,59 +72,59 @@ abstract class Configurator$$ConfiguratorMain {
if(j<aTypes.size()-1)
sTypesString += ", ";
}
oModuleElement.attr("types", sTypesString);
oStruct.appendChild(oModuleElement);
oModuleElement.setAttribute("types", sTypesString);
oStruct.addContent(oModuleElement);
}
}
//generate xml String
OutputSettings oOutputSettings = new OutputSettings();
oOutputSettings.syntax(Syntax.xml);
oDoc.outputSettings(oOutputSettings);
String sDoc = oDoc.toString();
System.out.println(sDoc);
//save to file
//generate xml String and save it to a file
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(m_sOutputPath));
writer.write(sDoc);
writer.close();
}
catch (IOException oEx) {
Adapter.saveAsXml(oDoc, m_sOutputPath);
}
catch (Exception oEx) {
System.out.println(oEx);
return;
}
//create and launch Adapter
new Adapter();
}
//get all parents of a given module from the Feature Model file
private Module GetModuleParents(String sModuleName) throws IOException {
private Module GetModuleParents(String sModuleName) throws IOException, JDOMException {
Module oModule = new Module();
//set name
oModule.sModuleName = sModuleName;
//init Doc if not done
if(m_oFeatureModelDoc==null) {
File oModel = new File(m_sFeatureModelPath);
m_oFeatureModelDoc = Jsoup.parse(oModel, "UTF-8");
m_oFeatureModelDoc = Adapter.readXml(m_sFeatureModelPath);
System.out.println(m_oFeatureModelDoc.toString());
}
//get element
String sQuery = "//*[@name= '"+sModuleName+"']";
List<Element> aModules = Adapter.xQueryElements(m_oFeatureModelDoc, sQuery);
Element oModuleElement;
try {
oModuleElement = aModules.get(0);
}
//should have size 1
org.jsoup.select.Elements aModuleBuffer = m_oFeatureModelDoc.getElementsByAttributeValue("name", sModuleName);
if(aModuleBuffer.size()>1) {
//somethings wrong
//TODO: properly log this
System.out.println("Found more than one module with the name " + sModuleName);
catch (Exception oEx) {
System.out.println(oEx.toString() + "\nList empty for query "+ sQuery);
return null;
}
Element oModuleElement = aModuleBuffer.first();
//get all anchestors
aModuleBuffer = oModuleElement.parents();
Element oParent = oModuleElement.getParentElement();
ArrayList<String> aTypes = new ArrayList();
//itterate through parents of module
for(int i=0; i<aModuleBuffer.size(); i++) {
Element oElement = aModuleBuffer.get(i);
String sElementName = oElement.attr("name");
while(true) {
String sElementName = oParent.getAttribute("name").getValue();
//break if we reach the Main class, as there are no types further up
if(sElementName.equals("ConfiguratorMain")) {
break;
}
aTypes.add(sElementName);
//iterate further trough List
oModuleElement = oParent;
oParent = oModuleElement.getParentElement();
}
oModule.aModuleTypes = aTypes;
return oModule;
......@@ -151,7 +141,7 @@ abstract class Configurator$$ConfiguratorMain {
try {
oModule = GetModuleParents(sModuleName);
}
catch (IOException oEx) {
catch (Exception oEx) {
System.out.print(oEx);
return;
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment