|
| 1 | +/* |
| 2 | + * Copyright 2017-2021 Koordinierungsstelle für IT-Standards (KoSIT) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package de.kosit.validationtool.impl.xvrl; |
| 18 | + |
| 19 | +import de.kosit.validationtool.model.reportInput.XMLSyntaxError; |
| 20 | +import de.kosit.validationtool.model.xvrl.Location; |
| 21 | +import de.kosit.validationtool.model.xvrl.XVRLDetection; |
| 22 | +import de.kosit.validationtool.model.xvrl.XVRLMessage; |
| 23 | + |
| 24 | +import java.math.BigInteger; |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import static de.kosit.validationtool.api.XmlError.Severity.SEVERITY_FATAL_ERROR; |
| 29 | + |
| 30 | +public interface BaseDetection { |
| 31 | + |
| 32 | + List<XVRLMessage> getMessages(); |
| 33 | + |
| 34 | + List<Location> getLocations(); |
| 35 | + |
| 36 | + XVRLDetection.Severity getSeverity(); |
| 37 | + |
| 38 | + void setSeverity(XVRLDetection.Severity value); |
| 39 | + |
| 40 | + default List<String> getAllMessages() { |
| 41 | + return getMessages().stream().flatMap(message -> message.getMessageStrings().stream()).collect(Collectors.toList()); |
| 42 | + } |
| 43 | + |
| 44 | + default String getErrorMessage() { |
| 45 | + if (getMessages().isEmpty()) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + return getMessages().get(0).getContent().stream().map(Object::toString).collect(Collectors.joining()); |
| 49 | + } |
| 50 | + |
| 51 | + default Location getErrorLocation() { |
| 52 | + if (getLocations().isEmpty()) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return getLocations().get(0); |
| 56 | + } |
| 57 | + |
| 58 | + default boolean hasErrors() { |
| 59 | + return getSeverity() == XVRLDetection.Severity.ERROR || getSeverity() == XVRLDetection.Severity.FATAL_ERROR; |
| 60 | + } |
| 61 | + |
| 62 | + default void addMessageString(String message) { |
| 63 | + XVRLMessage messageObject = new XVRLMessage(); |
| 64 | + messageObject.getContent().add(message); |
| 65 | + getMessages().add(messageObject); |
| 66 | + } |
| 67 | + |
| 68 | + default void addLocation(int lineNumber, int columnNumber, String xpath) { |
| 69 | + Location location = new Location(); |
| 70 | + location.setLine(BigInteger.valueOf(lineNumber)); |
| 71 | + location.setColumn(BigInteger.valueOf(columnNumber)); |
| 72 | + location.setXpath(xpath); |
| 73 | + getLocations().add(location); |
| 74 | + } |
| 75 | + |
| 76 | + default void addSeverityFromXmlError(XMLSyntaxError xmlSyntaxError) { |
| 77 | + if (xmlSyntaxError.getSeverity() == SEVERITY_FATAL_ERROR) { |
| 78 | + setSeverity(XVRLDetection.Severity.FATAL_ERROR); |
| 79 | + } else { |
| 80 | + setSeverity(XVRLDetection.Severity.ERROR); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments