Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create New Release #138

Merged
merged 14 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/automate-projects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ jobs:
steps:
- name: add-new-issues-to-organization-based-project-column
if: github.event_name == 'issues' && github.event.action == 'opened'
uses: alex-page/[email protected].1
uses: alex-page/[email protected].2
with:
project: CoMPAS Issues Overview Board
column: To do
repo-token: ${{ secrets.ORG_GITHUB_ACTION_SECRET }}
- name: add-new-pull-request-to-organization-based-project-column
if: (github.event_name == 'pull_request' || github.event_name == 'pull_request_target') && github.event.action == 'opened'
uses: alex-page/[email protected].1
uses: alex-page/[email protected].2
with:
project: CoMPAS Pull Request Overview Board
column: To do
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
java-version: '17'

- name: Create custom Maven Settings.xml
uses: whelk-io/maven-settings-xml-action@v20
uses: whelk-io/maven-settings-xml-action@v21
with:
output_file: custom_maven_settings.xml
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
password: ${{ secrets.DOCKER_HUB_TOKEN }}

- name: Create custom Maven Settings.xml
uses: whelk-io/maven-settings-xml-action@v20
uses: whelk-io/maven-settings-xml-action@v21
with:
output_file: custom_maven_settings.xml
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
-Dsonar.projectKey=com-pas_compas-scl-validator \
-Dsonar.organization=com-pas )"
- name: Create custom Maven Settings.xml
uses: whelk-io/maven-settings-xml-action@v20
uses: whelk-io/maven-settings-xml-action@v21
with:
output_file: custom_maven_settings.xml
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SPDX-License-Identifier: Apache-2.0
<compas.scl.xsd.version>0.0.4</compas.scl.xsd.version>
<compas.core.version>0.9.3</compas.core.version>

<quarkus.platform.version>2.13.0.Final</quarkus.platform.version>
<quarkus.platform.version>2.13.3.Final</quarkus.platform.version>
<slf4j.version>2.0.3</slf4j.version>
<openpojo.version>0.9.1</openpojo.version>
</properties>
Expand Down Expand Up @@ -105,6 +105,17 @@ SPDX-License-Identifier: Apache-2.0
<version>${compas.core.version}</version>
</dependency>

<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.2</version>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.openapi</groupId>
<artifactId>microprofile-openapi-api</artifactId>
Expand Down
14 changes: 14 additions & 0 deletions validator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>microprofile-openapi-api</artifactId>
</dependency>

<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -52,6 +61,11 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.openpojo</groupId>
<artifactId>openpojo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SclValidatorErrorCode {
public static final String NO_SCL_ELEMENT_FOUND_ERROR_CODE = "SVS-0001";
public static final String LOADING_SCL_FILE_ERROR_CODE = "SVS-0002";
public static final String LOADING_XSD_FILE_ERROR_CODE = "SVS-0003";
public static final String CREATE_XPATH_ELEMENT_ERROR_CODE = "SVS-0004";

public static final String WEBSOCKET_DECODER_ERROR_CODE = "SVS-0100";
public static final String WEBSOCKET_ENCODER_ERROR_CODE = "SVS-0101";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class ValidationError {
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI)
private Integer columnNumber;

@Schema(description = "The XPath expression to find the element where the validation error occurred",
example = "/SCL/Substation[1]/VoltageLevel[1]/Bay[5]")
@XmlElement(name = "XPath",
namespace = SCL_VALIDATOR_SERVICE_V1_NS_URI)
private String xpath;

public String getMessage() {
return message;
}
Expand Down Expand Up @@ -70,4 +76,12 @@ public Integer getColumnNumber() {
public void setColumnNumber(Integer columnNumber) {
this.columnNumber = columnNumber;
}

public String getXpath() {
return xpath;
}

public void setXpath(String xPath) {
this.xpath = xPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,18 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.validator.xsd;

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

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.LOADING_SCL_FILE_ERROR_CODE;
import static org.lfenergy.compas.scl.validator.util.StaxUtil.getAttributeValue;
import static org.lfenergy.compas.scl.validator.util.StaxUtil.isElement;
import org.w3c.dom.Document;

public class SclInfo {
private static final String SCL_ELEMENT_NAME = "SCL";

private String version = null;
private String revision = null;
private String release = null;

public SclInfo(String sclData) {
try (var fis = new ByteArrayInputStream(sclData.getBytes(StandardCharsets.UTF_8))) {
var xmlInputFactory = getXMLInputFactory();
var reader = xmlInputFactory.createXMLEventReader(fis);

while (reader.hasNext()) {
processEvent(reader.nextEvent());
}
} catch (IOException | XMLStreamException exp) {
throw new SclValidatorException(LOADING_SCL_FILE_ERROR_CODE, "Error loading SCL File", exp);
}
}

private void processEvent(XMLEvent nextEvent) {
if (nextEvent.isStartElement()) {
processStartElement(nextEvent.asStartElement());
}
}

private void processStartElement(StartElement element) {
if (isElement(element, SCL_ELEMENT_NAME)) {
version = getAttributeValue(element, "version");
revision = getAttributeValue(element, "revision");
release = getAttributeValue(element, "release");
}
}

private XMLInputFactory getXMLInputFactory() {
var xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
return xmlInputFactory;
private String version;
private String revision;
private String release;

public SclInfo(Document doc) {
var sclElement = doc.getDocumentElement();
version = sclElement.getAttribute("version");
revision = sclElement.getAttribute("revision");
release = sclElement.getAttribute("release");
}

public String getSclVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,71 @@
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.scl.validator.xsd;

import org.apache.xerces.impl.Constants;
import org.lfenergy.compas.scl.validator.exception.SclValidatorException;
import org.lfenergy.compas.scl.validator.model.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.validation.Validator;
import java.util.List;

import static org.lfenergy.compas.scl.validator.exception.SclValidatorErrorCode.CREATE_XPATH_ELEMENT_ERROR_CODE;

public class XSDErrorHandler implements ErrorHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(XSDErrorHandler.class);

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

private List<ValidationError> errorList;
private final Validator validator;
private final List<ValidationError> errorList;

public XSDErrorHandler(List<ValidationError> errorList) {
public XSDErrorHandler(Validator validator, List<ValidationError> errorList) {
this.validator = validator;
this.errorList = errorList;
}

@Override
public void warning(SAXParseException exception) {
public void warning(SAXParseException exception) throws SAXException {
var validationError = createValidationError(exception);
errorList.add(validationError);

LOGGER.debug("XSD Validation - warning: '{}' (Line number {}, Column number {})",
LOGGER.debug("XSD Validation - warning: '{}' (XPath {})",
validationError.getMessage(),
validationError.getLineNumber(),
validationError.getColumnNumber());
validationError.getXpath());
}

@Override
public void error(SAXParseException exception) {
public void error(SAXParseException exception) throws SAXException {
var validationError = createValidationError(exception);
errorList.add(validationError);

LOGGER.debug("XSD Validation - error: '{}' (Line number {}, Column number {})",
LOGGER.debug("XSD Validation - error: '{}' (XPath {})",
validationError.getMessage(),
validationError.getLineNumber(),
validationError.getColumnNumber());
validationError.getXpath());
}

@Override
public void fatalError(SAXParseException exception) {
public void fatalError(SAXParseException exception) throws SAXException {
var validationError = createValidationError(exception);
errorList.add(validationError);

LOGGER.debug("XSD Validation - fatal error, stopping: '{}' (Line number {}, Column number {})",
LOGGER.debug("XSD Validation - fatal error, stopping: '{}' (XPath {})",
validationError.getMessage(),
validationError.getLineNumber(),
validationError.getColumnNumber());
validationError.getXpath());
}

private ValidationError createValidationError(SAXParseException exception) {
private ValidationError createValidationError(SAXParseException exception) throws SAXException {
var validationError = new ValidationError();
var xsdMessage = exception.getMessage();
validationError.setMessage(getMessage(xsdMessage));
validationError.setRuleName(getRuleName(xsdMessage));
validationError.setLineNumber(exception.getLineNumber());
validationError.setColumnNumber(exception.getColumnNumber());
validationError.setXpath(getXPath(getCurrentNode()));
return validationError;
}

Expand Down Expand Up @@ -93,4 +98,36 @@ String getMessage(String xsdMessage) {
}
return message;
}

String getXPath(Node node) {
if (node != null) {
var parent = node.getParentNode();
if (parent != null && parent != node.getOwnerDocument()) {
return getXPath(parent) + "/" + node.getNodeName() + "[" + getIndex(parent, node) + "]";
}
return "/" + node.getNodeName();
}
return null;
}

int getIndex(Node parent, Node child) {
var children = parent.getChildNodes();
var index = 0;
for (int i = 0; i < children.getLength(); i++) {
var listItem = children.item(i);
if (listItem.getNodeName().equals(child.getNodeName())) {
index++;
if (listItem == child) {
return index;
}
}
}
throw new SclValidatorException(CREATE_XPATH_ELEMENT_ERROR_CODE, "Error determining index of child element");
}

private Node getCurrentNode() throws SAXException {
// Get prop "http://apache.org/xml/properties/dom/current-element-node"
// See https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
return (Node) validator.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY);
}
}
Loading