Skip to content

Commit dfaffc7

Browse files
authored
Merge pull request #138 from com-pas/develop
Create New Release
2 parents ad76af3 + 672a170 commit dfaffc7

File tree

14 files changed

+314
-138
lines changed

14 files changed

+314
-138
lines changed

.github/workflows/automate-projects.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ jobs:
1414
steps:
1515
- name: add-new-issues-to-organization-based-project-column
1616
if: github.event_name == 'issues' && github.event.action == 'opened'
17-
uses: alex-page/[email protected].1
17+
uses: alex-page/[email protected].2
1818
with:
1919
project: CoMPAS Issues Overview Board
2020
column: To do
2121
repo-token: ${{ secrets.ORG_GITHUB_ACTION_SECRET }}
2222
- name: add-new-pull-request-to-organization-based-project-column
2323
if: (github.event_name == 'pull_request' || github.event_name == 'pull_request_target') && github.event.action == 'opened'
24-
uses: alex-page/[email protected].1
24+
uses: alex-page/[email protected].2
2525
with:
2626
project: CoMPAS Pull Request Overview Board
2727
column: To do

.github/workflows/build-project.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
java-version: '17'
4848

4949
- name: Create custom Maven Settings.xml
50-
uses: whelk-io/maven-settings-xml-action@v20
50+
uses: whelk-io/maven-settings-xml-action@v21
5151
with:
5252
output_file: custom_maven_settings.xml
5353
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'

.github/workflows/release-project.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
password: ${{ secrets.DOCKER_HUB_TOKEN }}
5454

5555
- name: Create custom Maven Settings.xml
56-
uses: whelk-io/maven-settings-xml-action@v20
56+
uses: whelk-io/maven-settings-xml-action@v21
5757
with:
5858
output_file: custom_maven_settings.xml
5959
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'

.github/workflows/sonarcloud-analysis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
-Dsonar.projectKey=com-pas_compas-scl-validator \
5858
-Dsonar.organization=com-pas )"
5959
- name: Create custom Maven Settings.xml
60-
uses: whelk-io/maven-settings-xml-action@v20
60+
uses: whelk-io/maven-settings-xml-action@v21
6161
with:
6262
output_file: custom_maven_settings.xml
6363
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'

pom.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SPDX-License-Identifier: Apache-2.0
2626
<compas.scl.xsd.version>0.0.4</compas.scl.xsd.version>
2727
<compas.core.version>0.9.3</compas.core.version>
2828

29-
<quarkus.platform.version>2.13.0.Final</quarkus.platform.version>
29+
<quarkus.platform.version>2.13.3.Final</quarkus.platform.version>
3030
<slf4j.version>2.0.3</slf4j.version>
3131
<openpojo.version>0.9.1</openpojo.version>
3232
</properties>
@@ -105,6 +105,17 @@ SPDX-License-Identifier: Apache-2.0
105105
<version>${compas.core.version}</version>
106106
</dependency>
107107

108+
<dependency>
109+
<groupId>xerces</groupId>
110+
<artifactId>xerces</artifactId>
111+
<version>2.4.0</version>
112+
</dependency>
113+
<dependency>
114+
<groupId>xerces</groupId>
115+
<artifactId>xercesImpl</artifactId>
116+
<version>2.12.2</version>
117+
</dependency>
118+
108119
<dependency>
109120
<groupId>org.eclipse.microprofile.openapi</groupId>
110121
<artifactId>microprofile-openapi-api</artifactId>

validator/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ SPDX-License-Identifier: Apache-2.0
3636
<artifactId>microprofile-openapi-api</artifactId>
3737
</dependency>
3838

39+
<dependency>
40+
<groupId>xerces</groupId>
41+
<artifactId>xerces</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>xerces</groupId>
45+
<artifactId>xercesImpl</artifactId>
46+
</dependency>
47+
3948
<dependency>
4049
<groupId>org.slf4j</groupId>
4150
<artifactId>slf4j-api</artifactId>
@@ -52,6 +61,11 @@ SPDX-License-Identifier: Apache-2.0
5261
<artifactId>junit-jupiter-engine</artifactId>
5362
<scope>test</scope>
5463
</dependency>
64+
<dependency>
65+
<groupId>org.mockito</groupId>
66+
<artifactId>mockito-junit-jupiter</artifactId>
67+
<scope>test</scope>
68+
</dependency>
5569
<dependency>
5670
<groupId>com.openpojo</groupId>
5771
<artifactId>openpojo</artifactId>

validator/src/main/java/org/lfenergy/compas/scl/validator/exception/SclValidatorErrorCode.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class SclValidatorErrorCode {
1111
public static final String NO_SCL_ELEMENT_FOUND_ERROR_CODE = "SVS-0001";
1212
public static final String LOADING_SCL_FILE_ERROR_CODE = "SVS-0002";
1313
public static final String LOADING_XSD_FILE_ERROR_CODE = "SVS-0003";
14+
public static final String CREATE_XPATH_ELEMENT_ERROR_CODE = "SVS-0004";
1415

1516
public static final String WEBSOCKET_DECODER_ERROR_CODE = "SVS-0100";
1617
public static final String WEBSOCKET_ENCODER_ERROR_CODE = "SVS-0101";

validator/src/main/java/org/lfenergy/compas/scl/validator/model/ValidationError.java

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public class ValidationError {
3939
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI)
4040
private Integer columnNumber;
4141

42+
@Schema(description = "The XPath expression to find the element where the validation error occurred",
43+
example = "/SCL/Substation[1]/VoltageLevel[1]/Bay[5]")
44+
@XmlElement(name = "XPath",
45+
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI)
46+
private String xpath;
47+
4248
public String getMessage() {
4349
return message;
4450
}
@@ -70,4 +76,12 @@ public Integer getColumnNumber() {
7076
public void setColumnNumber(Integer columnNumber) {
7177
this.columnNumber = columnNumber;
7278
}
79+
80+
public String getXpath() {
81+
return xpath;
82+
}
83+
84+
public void setXpath(String xPath) {
85+
this.xpath = xPath;
86+
}
7387
}

validator/src/main/java/org/lfenergy/compas/scl/validator/xsd/SclInfo.java

+10-50
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,18 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package org.lfenergy.compas.scl.validator.xsd;
55

6-
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
7-
8-
import javax.xml.stream.XMLInputFactory;
9-
import javax.xml.stream.XMLStreamException;
10-
import javax.xml.stream.events.StartElement;
11-
import javax.xml.stream.events.XMLEvent;
12-
import java.io.ByteArrayInputStream;
13-
import java.io.IOException;
14-
import java.nio.charset.StandardCharsets;
15-
16-
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.LOADING_SCL_FILE_ERROR_CODE;
17-
import static org.lfenergy.compas.scl.validator.util.StaxUtil.getAttributeValue;
18-
import static org.lfenergy.compas.scl.validator.util.StaxUtil.isElement;
6+
import org.w3c.dom.Document;
197

208
public class SclInfo {
21-
private static final String SCL_ELEMENT_NAME = "SCL";
22-
23-
private String version = null;
24-
private String revision = null;
25-
private String release = null;
26-
27-
public SclInfo(String sclData) {
28-
try (var fis = new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8))) {
29-
var xmlInputFactory = getXMLInputFactory();
30-
var reader = xmlInputFactory.createXMLEventReader(fis);
31-
32-
while (reader.hasNext()) {
33-
processEvent(reader.nextEvent());
34-
}
35-
} catch (IOException | XMLStreamException exp) {
36-
throw new SclValidatorException(LOADING_SCL_FILE_ERROR_CODE, "Error loading SCL File", exp);
37-
}
38-
}
39-
40-
private void processEvent(XMLEvent nextEvent) {
41-
if (nextEvent.isStartElement()) {
42-
processStartElement(nextEvent.asStartElement());
43-
}
44-
}
45-
46-
private void processStartElement(StartElement element) {
47-
if (isElement(element, SCL_ELEMENT_NAME)) {
48-
version = getAttributeValue(element, "version");
49-
revision = getAttributeValue(element, "revision");
50-
release = getAttributeValue(element, "release");
51-
}
52-
}
53-
54-
private XMLInputFactory getXMLInputFactory() {
55-
var xmlInputFactory = XMLInputFactory.newInstance();
56-
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
57-
return xmlInputFactory;
9+
private String version;
10+
private String revision;
11+
private String release;
12+
13+
public SclInfo(Document doc) {
14+
var sclElement = doc.getDocumentElement();
15+
version = sclElement.getAttribute("version");
16+
revision = sclElement.getAttribute("revision");
17+
release = sclElement.getAttribute("release");
5818
}
5919

6020
public String getSclVersion() {

validator/src/main/java/org/lfenergy/compas/scl/validator/xsd/XSDErrorHandler.java

+54-17
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,71 @@
33
// SPDX-License-Identifier: Apache-2.0
44
package org.lfenergy.compas.scl.validator.xsd;
55

6+
import org.apache.xerces.impl.Constants;
7+
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
68
import org.lfenergy.compas.scl.validator.model.ValidationError;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
11+
import org.w3c.dom.Node;
912
import org.xml.sax.ErrorHandler;
13+
import org.xml.sax.SAXException;
1014
import org.xml.sax.SAXParseException;
1115

16+
import javax.xml.validation.Validator;
1217
import java.util.List;
1318

19+
import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.CREATE_XPATH_ELEMENT_ERROR_CODE;
20+
1421
public class XSDErrorHandler implements ErrorHandler {
1522
private static final Logger LOGGER = LoggerFactory.getLogger(XSDErrorHandler.class);
1623

1724
public static final String DEFAULT_PREFIX = "XSD/";
1825
public static final String DEFAULT_RULE_NAME = DEFAULT_PREFIX + "general";
1926

20-
private List<ValidationError> errorList;
27+
private final Validator validator;
28+
private final List<ValidationError> errorList;
2129

22-
public XSDErrorHandler(List<ValidationError> errorList) {
30+
public XSDErrorHandler(Validator validator, List<ValidationError> errorList) {
31+
this.validator = validator;
2332
this.errorList = errorList;
2433
}
2534

2635
@Override
27-
public void warning(SAXParseException exception) {
36+
public void warning(SAXParseException exception) throws SAXException {
2837
var validationError = createValidationError(exception);
2938
errorList.add(validationError);
3039

31-
LOGGER.debug("XSD Validation - warning: '{}' (Line number {}, Column number {})",
40+
LOGGER.debug("XSD Validation - warning: '{}' (XPath {})",
3241
validationError.getMessage(),
33-
validationError.getLineNumber(),
34-
validationError.getColumnNumber());
42+
validationError.getXpath());
3543
}
3644

3745
@Override
38-
public void error(SAXParseException exception) {
46+
public void error(SAXParseException exception) throws SAXException {
3947
var validationError = createValidationError(exception);
4048
errorList.add(validationError);
4149

42-
LOGGER.debug("XSD Validation - error: '{}' (Line number {}, Column number {})",
50+
LOGGER.debug("XSD Validation - error: '{}' (XPath {})",
4351
validationError.getMessage(),
44-
validationError.getLineNumber(),
45-
validationError.getColumnNumber());
52+
validationError.getXpath());
4653
}
4754

4855
@Override
49-
public void fatalError(SAXParseException exception) {
56+
public void fatalError(SAXParseException exception) throws SAXException {
5057
var validationError = createValidationError(exception);
5158
errorList.add(validationError);
5259

53-
LOGGER.debug("XSD Validation - fatal error, stopping: '{}' (Line number {}, Column number {})",
60+
LOGGER.debug("XSD Validation - fatal error, stopping: '{}' (XPath {})",
5461
validationError.getMessage(),
55-
validationError.getLineNumber(),
56-
validationError.getColumnNumber());
62+
validationError.getXpath());
5763
}
5864

59-
private ValidationError createValidationError(SAXParseException exception) {
65+
private ValidationError createValidationError(SAXParseException exception) throws SAXException {
6066
var validationError = new ValidationError();
6167
var xsdMessage = exception.getMessage();
6268
validationError.setMessage(getMessage(xsdMessage));
6369
validationError.setRuleName(getRuleName(xsdMessage));
64-
validationError.setLineNumber(exception.getLineNumber());
65-
validationError.setColumnNumber(exception.getColumnNumber());
70+
validationError.setXpath(getXPath(getCurrentNode()));
6671
return validationError;
6772
}
6873

@@ -93,4 +98,36 @@ String getMessage(String xsdMessage) {
9398
}
9499
return message;
95100
}
101+
102+
String getXPath(Node node) {
103+
if (node != null) {
104+
var parent = node.getParentNode();
105+
if (parent != null && parent != node.getOwnerDocument()) {
106+
return getXPath(parent) + "/" + node.getNodeName() + "[" + getIndex(parent, node) + "]";
107+
}
108+
return "/" + node.getNodeName();
109+
}
110+
return null;
111+
}
112+
113+
int getIndex(Node parent, Node child) {
114+
var children = parent.getChildNodes();
115+
var index = 0;
116+
for (int i = 0; i < children.getLength(); i++) {
117+
var listItem = children.item(i);
118+
if (listItem.getNodeName().equals(child.getNodeName())) {
119+
index++;
120+
if (listItem == child) {
121+
return index;
122+
}
123+
}
124+
}
125+
throw new SclValidatorException(CREATE_XPATH_ELEMENT_ERROR_CODE, "Error determining index of child element");
126+
}
127+
128+
private Node getCurrentNode() throws SAXException {
129+
// Get prop "http://apache.org/xml/properties/dom/current-element-node"
130+
// See https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
131+
return (Node) validator.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY);
132+
}
96133
}

0 commit comments

Comments
 (0)