Skip to content

Commit c40384b

Browse files
author
Dennis Labordus
committed
Some refactoring of classes.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent 060e92f commit c40384b

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

service/src/main/java/org/lfenergy/compas/scl/validator/common/NsdocInfo.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55

66
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
77

8-
import javax.xml.namespace.QName;
98
import javax.xml.stream.XMLEventReader;
109
import javax.xml.stream.XMLInputFactory;
1110
import javax.xml.stream.XMLStreamException;
12-
import javax.xml.stream.events.Attribute;
1311
import javax.xml.stream.events.StartElement;
1412
import javax.xml.stream.events.XMLEvent;
1513
import java.io.File;
1614
import java.io.FileInputStream;
1715
import java.io.IOException;
1816

1917
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.DETERMINING_ID_FAILED;
18+
import static org.lfenergy.compas.scl.validator.util.StaxUtil.getAttributeValue;
19+
import static org.lfenergy.compas.scl.validator.util.StaxUtil.isElement;
2020

2121
public class NsdocInfo {
2222
private static final String NSDOC_ELEMENT_NAME = "NSDoc";
@@ -29,16 +29,7 @@ public NsdocInfo(File file) {
2929
XMLEventReader reader = xmlInputFactory.createXMLEventReader(fis);
3030

3131
while (id == null && reader.hasNext()) {
32-
XMLEvent nextEvent = reader.nextEvent();
33-
if (nextEvent.isStartElement()) {
34-
StartElement startElement = nextEvent.asStartElement();
35-
if (NSDOC_ELEMENT_NAME.equals(startElement.getName().getLocalPart())) {
36-
Attribute attribute = startElement.getAttributeByName(new QName("id"));
37-
if (attribute != null) {
38-
id = attribute.getValue();
39-
}
40-
}
41-
}
32+
processEvent(reader.nextEvent());
4233
}
4334
} catch (IOException | XMLStreamException exp) {
4435
throw new SclValidatorException(DETERMINING_ID_FAILED, "Error loading NSDoc File " + file.getName() + "'.", exp);
@@ -49,6 +40,18 @@ public NsdocInfo(File file) {
4940
}
5041
}
5142

43+
private void processEvent(XMLEvent nextEvent) {
44+
if (nextEvent.isStartElement()) {
45+
processStartElement(nextEvent.asStartElement());
46+
}
47+
}
48+
49+
private void processStartElement(StartElement element) {
50+
if (isElement(element, NSDOC_ELEMENT_NAME)) {
51+
id = getAttributeValue(element, "id");
52+
}
53+
}
54+
5255
public String getId() {
5356
return id;
5457
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-FileCopyrightText: 2022 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.validator.util;
5+
6+
import javax.xml.namespace.QName;
7+
import javax.xml.stream.events.Attribute;
8+
import javax.xml.stream.events.StartElement;
9+
10+
public class StaxUtil {
11+
StaxUtil() {
12+
throw new UnsupportedOperationException("StaxUtil class");
13+
}
14+
15+
public static boolean isElement(StartElement element, String elementName) {
16+
return elementName.equals(element.getName().getLocalPart());
17+
}
18+
19+
public static String getAttributeValue(StartElement element, String attributeName) {
20+
Attribute attribute = element.getAttributeByName(new QName(attributeName));
21+
if (attribute != null) {
22+
return attribute.getValue();
23+
}
24+
return null;
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: 2022 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.validator.util;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
class StaxUtilTest {
11+
@Test
12+
void constructor_WhenConstructorCalled_ThenShouldThrowExceptionCauseForbidden() {
13+
assertThrows(UnsupportedOperationException.class, StaxUtil::new);
14+
}
15+
16+
}

0 commit comments

Comments
 (0)