Skip to content
Snippets Groups Projects
Commit 0ea92258 authored by marvin's avatar marvin
Browse files

Refactoring, New XPath methods

parent d776b771
No related merge requests found
......@@ -18,35 +18,61 @@ import org.jdom2.xpath.*;
public class Adapter {
/**
* Searches a document for the first occurrence of an xml tag using XPath.
* Executes given XPath expression for elements.
*
* @param doc The JDOM Document
* @param elementName Name of the xml tag
* @return First element found or NULL if no match found
* @param xPath XPath expression
* @return List with all elements found or empty list if no match found
*/
public static Element queryFirstElement(Document doc, String elementName) {
public static List<Element> xQueryElements(Document doc, String xPath) {
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Element> expr = xFactory.compile("//"+elementName, Filters.element());
Element result = expr.evaluateFirst(doc);
XPathExpression<Element> expr = xFactory.compile(xPath, Filters.element());
List<Element> result = expr.evaluate(doc);
return result;
}
/**
* Searches a document for all occurrences of an xml tag using XPath.
* Executes given XPath expression for attributes.
*
* @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
* @param xPath XPath expression
* @return List with all attributes found or empty list if no match found
*/
public static List<Element> queryAllElements(Document doc, String elementName) {
public static List<Attribute> xQueryAttributes(Document doc, String xPath) {
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Element> expr = xFactory.compile("//"+elementName, Filters.element());
List<Element> result = expr.evaluate(doc);
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);
}
/**
* @param args
*/
......@@ -62,34 +88,46 @@ public class Adapter {
//######### Operate on featuremodel.xml #########
Element fmRoot = FeatureModel.getRootElement();
List<Element> modules = fmRoot.getChildren("module");
//###############################################
//TODO
//######### Operate on .gazebo.xacro ############
Element gmRoot = GazeboModel.getRootElement();
//XPathExamples
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Element> expr = xFactory.compile("robot/gazebo/sensor/ray/range/min", Filters.element());
Element test = expr.evaluateFirst(GazeboModel);
List<Element> test2 = queryAllElements(GazeboModel, "min");
List<Element> test3 = queryAllElements(GazeboModel, "max");
//###############################################
FileOutputStream dest = new FileOutputStream("resources/output/turtlebot3_burger.gazebo.xacro");
XMLOutputter xmlOutput = new XMLOutputter();
//TODO
//######### Output result #######################
xmlOutput.output(GazeboModel, dest);
//###############################################
//XPathExamples
XPathFactory xFactory = XPathFactory.instance();
XPathExpression<Element> xExpr = xFactory.compile("robot/gazebo/sensor/ray/range/min", Filters.element());
Element test = xExpr.evaluateFirst(GazeboModel);
List<Element> test2 = searchAllElements(GazeboModel, "min");
List<Element> test3 = searchAllElements(GazeboModel, "max");
Element test4 = searchFirstElement(GazeboModel, "plugin");
List<Attribute> test5 = xQueryAttributes(GazeboModel, "(//plugin)[1]/@*");
if(test != null) xmlOutput.output(test, System.out);
System.out.println();
System.out.println("\n");
xmlOutput.output(test2, System.out);
System.out.println();
System.out.println("\n");
xmlOutput.output(test3, System.out);
//###############################################
System.out.println("\n");
if(test4 != null) xmlOutput.output(test4, System.out);
System.out.println("\n");
for(Attribute a : test5) System.out.println(a.getName()+"="+a.getValue());
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
......
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