Skip to content

Commit 39bff55

Browse files
author
Dennis Labordus
committed
Added some documentation.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 28fe773 commit 39bff55

File tree

1 file changed

+74
-4
lines changed

1 file changed

+74
-4
lines changed

repository/src/main/java/org/lfenergy/compas/scl/data/util/SclElementProcessor.java

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,42 @@
1414
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.HEADER_NOT_FOUND_ERROR_CODE;
1515
import static org.w3c.dom.Node.ELEMENT_NODE;
1616

17+
/**
18+
* Support class to work with the SCL XML in a generic way as W3C Element/Node class.
19+
* This way multiple versions of the SCL XSD can easily be supported.
20+
*/
1721
public class SclElementProcessor {
22+
/**
23+
* Make sure that the SCL XSD Namespace will be the default namespace used on the passed
24+
* Node and all it's child nodes. Attributes aren't fixed, because these use the "null" namespace
25+
* as they are marked as "unqualified" in the XSD (attributeFormDefault="unqualified").
26+
*
27+
* @param root The root node from which to start, will mostly be the SCL XML Element.
28+
*/
1829
public void fixDefaultPrefix(Node root) {
1930
var oldNamespacePrefixes = new HashSet<String>();
31+
// Use a stack to walk through all child nodes.
2032
var nodes = new Stack<Node>();
33+
// Start with the root node.
2134
nodes.push(root);
2235

2336
while (!nodes.isEmpty()) {
2437
var node = nodes.pop();
25-
if (node.getNodeType() == ELEMENT_NODE && SCL_NS_URI.equals(node.getNamespaceURI())
38+
if (SCL_NS_URI.equals(node.getNamespaceURI())
2639
&& node.getPrefix() != null && !node.getPrefix().isBlank()) {
40+
// The namespace is the SCL XSD Namespace, but with a prefix, so we will fix that.
2741
oldNamespacePrefixes.add(node.getPrefix());
2842
node.setPrefix("");
2943
}
3044

31-
// For child nodes of this node
45+
// Push all the Child Nodes (Type Element) on the Stack and continue with these.
3246
NodeList childNodes = node.getChildNodes();
3347
if (childNodes != null) {
3448
for (int i = 0, count = childNodes.getLength(); i < count; ++i) {
35-
nodes.push(childNodes.item(i));
49+
var childNode = childNodes.item(i);
50+
if (childNode.getNodeType() == ELEMENT_NODE) {
51+
nodes.push(childNode);
52+
}
3653
}
3754
}
3855
}
@@ -42,25 +59,50 @@ public void fixDefaultPrefix(Node root) {
4259
);
4360
}
4461

62+
/**
63+
* Search for the SCL Header in the SCL Root Element and return that.
64+
*
65+
* @param scl The SCL Root Element
66+
* @return The Header Element if found or empty() if not.
67+
*/
4568
public Optional<Element> getSclHeader(Element scl) {
4669
return getChildNodesByName(scl, SCL_HEADER_ELEMENT_NAME).stream()
4770
.findFirst();
4871
}
4972

73+
/**
74+
* Add the SCL Header ot the SCL Root Element, because that element is important for the SCL Data Service
75+
* we want to make sure it's there.
76+
*
77+
* @param scl The SCL Root Element
78+
* @return The new created Header Element.
79+
*/
5080
public Element addSclHeader(Element scl) {
5181
var header = scl.getOwnerDocument().createElementNS(SCL_NS_URI, SCL_HEADER_ELEMENT_NAME);
5282
header.setPrefix(SCL_NS_PREFIX);
5383
scl.insertBefore(header, scl.getFirstChild());
5484
return header;
5585
}
5686

87+
/**
88+
* Search for the private element with type "#Constants.COMPAS_SCL_EXTENSION_TYPE" on the SCL Root Element.
89+
*
90+
* @param scl The SCL Root Element
91+
* @return The Private Element with the correct type if found or empty() if not.
92+
*/
5793
public Optional<Element> getCompasPrivate(Element scl) {
5894
return getChildNodesByName(scl, SCL_PRIVATE_ELEMENT_NAME).stream()
5995
.filter(element -> element.hasAttribute(SCL_PRIVATE_TYPE_ATTR))
6096
.filter(element -> element.getAttribute(SCL_PRIVATE_TYPE_ATTR).equals(COMPAS_SCL_EXTENSION_TYPE))
6197
.findFirst();
6298
}
6399

100+
/**
101+
* Create a Private Element with type "#COMPAS_SCL_EXTENSION_TYPE" on the SCL Root Element.
102+
*
103+
* @param scl The SCL Root Element
104+
* @return The new created Private Element with the correct type.
105+
*/
64106
public Element addCompasPrivate(Element scl) {
65107
scl.setAttribute("xmlns:" + COMPAS_EXTENSION_NS_PREFIX, COMPAS_EXTENSION_NS_URI);
66108

@@ -74,7 +116,14 @@ public Element addCompasPrivate(Element scl) {
74116
return tPrivate;
75117
}
76118

77-
119+
/**
120+
* Add a new element with namespace "#COMPAS_EXTENSION_NS_URI" to the Private element of CoMPAS.
121+
*
122+
* @param compasPrivate The Private Element on which to add the new Element.
123+
* @param localName The name of the Element to create.
124+
* @param value The value set on the new Element.
125+
* @return The new created Element.
126+
*/
78127
public Element addCompasElement(Element compasPrivate, String localName, String value) {
79128
Element element = compasPrivate.getOwnerDocument().createElementNS(COMPAS_EXTENSION_NS_URI, localName);
80129
element.setPrefix(COMPAS_EXTENSION_NS_PREFIX);
@@ -83,17 +132,38 @@ public Element addCompasElement(Element compasPrivate, String localName, String
83132
return element;
84133
}
85134

135+
/**
136+
* Returns the value of a specific attribute from the passed Element.
137+
*
138+
* @param element The Element to search for the attribute.
139+
* @param attributeName The name of the Attribute to search for.
140+
* @return The value if found or empty() if not.
141+
*/
86142
public Optional<String> getAttributeValue(Element element, String attributeName) {
87143
var value = element.getAttribute(attributeName);
88144
return (value != null && !value.isBlank()) ? Optional.of(value) : Optional.empty();
89145
}
90146

147+
/**
148+
* Search for a Child Node on the passed Element.
149+
*
150+
* @param root The element on which to search for a child Node.
151+
* @param localName The name of the Child Node.
152+
* @return The Child Node if found or empty() if not.
153+
*/
91154
public Optional<Element> getChildNodeByName(Element root, String localName) {
92155
return getChildNodesByName(root, localName)
93156
.stream()
94157
.findFirst();
95158
}
96159

160+
/**
161+
* Search for all Child Node on the passed Element.
162+
*
163+
* @param root The element on which to search for all child Node.
164+
* @param localName The name of the Child Node.
165+
* @return The list of Child Nodes found.
166+
*/
97167
public List<Element> getChildNodesByName(Element root, String localName) {
98168
var foundElements = new ArrayList<Element>();
99169
var childNodes = root.getChildNodes();

0 commit comments

Comments
 (0)