Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Semesterprojekt Modulbasiertes Testen
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonas Trappe
Semesterprojekt Modulbasiertes Testen
Commits
0ea92258
There was an error fetching the commit references. Please try again later.
Commit
0ea92258
authored
5 years ago
by
marvin
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring, New XPath methods
parent
d776b771
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Varianten/AdapterFIDEROS/src/Adapter.java
+61
-23
61 additions, 23 deletions
Varianten/AdapterFIDEROS/src/Adapter.java
with
61 additions
and
23 deletions
Varianten/AdapterFIDEROS/src/Adapter.java
+
61
−
23
View file @
0ea92258
...
...
@@ -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
element
s
found or
empty list
if no match found
*/
public
static
Element
queryFirst
Element
(
Document
doc
,
String
elementName
)
{
public
static
List
<
Element
>
xQuery
Element
s
(
Document
doc
,
String
xPath
)
{
XPathFactory
xFactory
=
XPathFactory
.
instance
();
XPathExpression
<
Element
>
expr
=
xFactory
.
compile
(
"//"
+
elementName
,
Filters
.
element
());
Element
result
=
expr
.
evaluate
First
(
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
element
s found or empty
L
ist if no match found
* @param
xPath XPath expression
* @return List with all
attribute
s found or empty
l
ist if no match found
*/
public
static
List
<
Element
>
queryAllElement
s
(
Document
doc
,
String
elementName
)
{
public
static
List
<
Attribute
>
xQueryAttribute
s
(
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
();
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment