06-FixCompilationWarningsMain

This commit is contained in:
Adrian-Devries 2025-04-24 12:53:55 +02:00
parent a1965e7079
commit adb4effcf9
17 changed files with 76 additions and 50 deletions

View file

@ -129,7 +129,7 @@ class InternalCheck extends DefaultCheck {
}
void printResults(final Map<String, Result> results) {
final PrintWriter writer = new PrintWriter(System.out);// NOSONAR
final PrintWriter writer = new PrintWriter(System.out); // NOSONAR
writer.write("Results:\n");
writer.write(createResultGrid(results).render());
writer.write(createStatusLine(results));

View file

@ -217,7 +217,7 @@ public class Grid {
* @param def {@link ColumnDefinition}s
*/
public Grid(final ColumnDefinition... def) {
Stream.of(def).forEach(this::addColumn);
this.definitions.addAll(Arrays.asList(def));
}
private String generateGridStart() {

View file

@ -114,7 +114,7 @@ public class ConfigurationLoader {
final XdmNode root = findRoot(doc);
final String frameworkVersion = root.getAttributeValue(new QName("frameworkVersion"));
return startsWith(frameworkVersion, SUPPORTED_MAJOR_VERSION)
&& root.getNodeName().getNamespaceURI().equals(SUPPORTED_MAJOR_VERSION_SCHEMA);
&& root.getNodeName().getNamespace().equals(SUPPORTED_MAJOR_VERSION_SCHEMA);
}
private static Scenario createFallback(final Scenarios scenarios, final ContentRepository repository) {

View file

@ -35,7 +35,7 @@ import lombok.Getter;
import de.kosit.validationtool.model.reportInput.XMLSyntaxError;
import de.kosit.validationtool.model.reportInput.XMLSyntaxErrorSeverity;
import net.sf.saxon.s9api.MessageListener2;
import net.sf.saxon.s9api.MessageListener;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.XdmNode;
@ -45,7 +45,7 @@ import net.sf.saxon.s9api.XdmNode;
* @author Andreas Penski
*/
@Getter
public class CollectingErrorEventHandler implements ValidationEventHandler, ErrorHandler, MessageListener2, ErrorListener {
public class CollectingErrorEventHandler implements ValidationEventHandler, ErrorHandler, MessageListener, ErrorListener {
private static final int DEFAULT_ABORT_COUNT = 50;
@ -132,14 +132,15 @@ public class CollectingErrorEventHandler implements ValidationEventHandler, Erro
}
@Override
public void message(final XdmNode content, final QName errorCode, final boolean terminate, final SourceLocator locator) {
public void message(final XdmNode content, final boolean terminate, final SourceLocator locator) {
final XMLSyntaxError e = new XMLSyntaxError();
if (locator != null) {
e.setColumnNumber(locator.getColumnNumber());
e.setRowNumber(locator.getLineNumber());
}
e.setMessage("Error procesing" + content.getStringValue());
e.setMessage("Error processing" + content.getStringValue());
e.setSeverityCode(terminate ? XMLSyntaxErrorSeverity.SEVERITY_FATAL_ERROR : XMLSyntaxErrorSeverity.SEVERITY_WARNING);
this.errors.add(e);
}
@Override

View file

@ -121,14 +121,16 @@ public class ContentRepository {
* @param uri die URI der XSL Definition
* @return ein XSLT Executable
*/
@SuppressWarnings("deprecation")
public XsltExecutable loadXsltScript(final URI uri) {
log.info("Loading XSLT script from {}", uri);
final XsltCompiler xsltCompiler = getProcessor().newXsltCompiler();
final CollectingErrorEventHandler listener = new CollectingErrorEventHandler();
try {
xsltCompiler.setErrorListener(listener);
xsltCompiler.setErrorReporter(er -> log.error(listener.getErrorDescription()));
if (getResolver() != null) {
// otherwise use default resolver
// TODO: Replace call to deprecated method.
xsltCompiler.setURIResolver(getResolver());
}

View file

@ -55,7 +55,9 @@ public class ConversionService {
/**
* Exception while serializing/deserializing with jaxb.
*/
public class ConversionExeption extends RuntimeException {
public class ConversionException extends RuntimeException {
private static final long serialVersionUID = 7950889507519996452L;
/**
* Constructor.
@ -63,7 +65,7 @@ public class ConversionService {
* @param message the message.
* @param cause the cause
*/
public ConversionExeption(final String message, final Exception cause) {
public ConversionException(final String message, final Exception cause) {
super(message, cause);
}
@ -72,7 +74,7 @@ public class ConversionService {
*
* @param message the message.
*/
public ConversionExeption(final String message) {
public ConversionException(final String message) {
super(message);
}
}
@ -97,13 +99,13 @@ public class ConversionService {
private void checkInputEmpty(final URI xml) {
if (xml == null) {
throw new ConversionExeption("Can not unmarshal from empty input");
throw new ConversionException("Can not unmarshal from empty input");
}
}
private <T> void checkTypeEmpty(final Class<T> type) {
if (type == null) {
throw new ConversionExeption("Can not unmarshal without type information. Need to specify a target type");
throw new ConversionException("Can not unmarshal without type information. Need to specify a target type");
}
}
@ -183,13 +185,13 @@ public class ConversionService {
u.setEventHandler(handler2Use);
final T value = u.unmarshal(xsr, type).getValue();
if (defaultHandler != null && defaultHandler.hasErrors()) {
throw new ConversionExeption(
throw new ConversionException(
String.format("Schema errors while reading content from %s: %s", xml, defaultHandler.getErrorDescription()));
}
return value;
} catch (final JAXBException | XMLStreamException e) {
throw new ConversionExeption(String.format("Can not unmarshal to type %s from %s", type.getSimpleName(), xml.toString()), e);
throw new ConversionException(String.format("Can not unmarshal to type %s from %s", type.getSimpleName(), xml.toString()), e);
}
}
@ -204,9 +206,10 @@ public class ConversionService {
return writeXml(model, null, null);
}
@SuppressWarnings("unchecked")
public <T> String writeXml(final T model, final Schema schema, final ValidationEventHandler handler) {
if (model == null) {
throw new ConversionExeption("Can not serialize null");
throw new ConversionException("Can not serialize null");
}
try ( final StringWriter w = new StringWriter() ) {
final JAXBIntrospector introspector = getJaxbContext().createJAXBIntrospector();
@ -219,7 +222,8 @@ public class ConversionService {
final XMLOutputFactory xof = XMLOutputFactory.newFactory();
final XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(w);
if (null == introspector.getElementName(model)) {
final JAXBElement jaxbElement = new JAXBElement(createQName(model), model.getClass(), model);
// TODO: Replace unchecked cast.
final JAXBElement<T> jaxbElement = new JAXBElement<>(createQName(model), (Class<T>) model.getClass(), model);
marshaller.marshal(jaxbElement, xmlStreamWriter);
} else {
marshaller.marshal(model, xmlStreamWriter);
@ -227,7 +231,7 @@ public class ConversionService {
xmlStreamWriter.flush();
return w.toString();
} catch (final JAXBException | IOException | XMLStreamException e) {
throw new ConversionExeption(String.format("Error serializing Object %s", model.getClass().getName()), e);
throw new ConversionException(String.format("Error serializing Object %s", model.getClass().getName()), e);
}
}
@ -238,7 +242,7 @@ public class ConversionService {
return u.unmarshal(source, type).getValue();
} catch (final JAXBException e) {
throw new ConversionExeption(String.format("Can not unmarshal to type %s: %s", type.getSimpleName(),
throw new ConversionException(String.format("Can not unmarshal to type %s: %s", type.getSimpleName(),
StringUtils.abbreviate(source.getSystemId(), MAX_LOG_CONTENT)), e);
}
}

View file

@ -21,6 +21,7 @@ import static de.kosit.validationtool.impl.DateFactory.createTimestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -145,8 +146,13 @@ public class DefaultCheck implements Check {
}
private static List<XmlError> convertErrors(final Collection<XMLSyntaxError> errors) {
// noinspection unchecked
return (List<XmlError>) (List<?>) errors;
List<XmlError> rv = new LinkedList<>();
for (XMLSyntaxError error : errors) {
if (error != null) {
rv.add(error);
}
}
return rv;
}
}

View file

@ -37,7 +37,7 @@ import net.sf.saxon.s9api.XdmNode;
*/
@Slf4j
public class ScenarioRepository {
public final class ScenarioRepository {
public static final String DEFAULT = "default";

View file

@ -58,7 +58,7 @@ public abstract class AbstractInput implements Input, LazyReadInput {
}
}
protected InputStream wrap(final InputStream stream) {
protected InputStream wrap(final InputStream stream) throws IOException {
InputStream result = stream;
if (!isHashcodeComputed()) {
result = StreamHelper.wrapDigesting(this, result, getDigestAlgorithm());

View file

@ -17,6 +17,7 @@
package de.kosit.validationtool.impl.input;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.Source;
@ -47,7 +48,7 @@ public class ByteArrayInput extends AbstractInput {
}
@Override
public Source getSource() {
public Source getSource() throws IOException {
final InputStream stream = wrap(new ByteArrayInputStream(this.content));
return new StreamSource(stream, getName());
}

View file

@ -53,7 +53,7 @@ import net.sf.saxon.om.TreeInfo;
*/
@Getter
@Slf4j
public class SourceInput extends AbstractInput {
public final class SourceInput extends AbstractInput {
private final Source source;
@ -133,14 +133,16 @@ public class SourceInput extends AbstractInput {
return this.source instanceof JAXBSource;
}
private Source wrappedSource() {
private Source wrappedSource() throws IOException {
Source result = this.source;
if (isStreamSource()) {
final StreamSource ss = (StreamSource) this.source;
if (ss.getInputStream() != null) {
result = new StreamSource(wrap(ss.getInputStream()), this.source.getSystemId());
} else if (ss.getReader() != null) {
result = new StreamSource(wrap(new ReaderInputStream(ss.getReader(), Charset.defaultCharset())), this.source.getSystemId());
result = new StreamSource(
wrap(ReaderInputStream.builder().setReader(ss.getReader()).setCharset(Charset.defaultCharset()).get()),
this.source.getSystemId());
}
}
return result;

View file

@ -26,7 +26,7 @@ import java.security.NoSuchAlgorithmException;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.io.input.BoundedInputStream;
import de.kosit.validationtool.api.Input;
@ -39,7 +39,7 @@ public class StreamHelper {
/**
* Helper class, which generates the hashcode while reading the stream e.g. for parsing the document. This allows
* generating the hashcode without an aditional reading step.
* generating the hashcode without an additional reading step.
*/
@SuppressWarnings("squid:S4929") // efficient read is done by internally used stream
private static class DigestingInputStream extends FilterInputStream {
@ -96,15 +96,15 @@ public class StreamHelper {
private final LazyReadInput reference;
public CountInputStream(final LazyReadInput input, final InputStream stream) {
super(new org.apache.commons.io.input.CountingInputStream(stream));
public CountInputStream(final LazyReadInput input, final InputStream stream) throws IOException {
super(BoundedInputStream.builder().setInputStream(stream).get());
this.reference = input;
}
@Override
public void close() throws IOException {
super.close();
this.reference.setLength(((CountingInputStream) this.in).getByteCount());
this.reference.setLength(((BoundedInputStream) this.in).getCount());
}
}
@ -134,7 +134,7 @@ public class StreamHelper {
* @param stream the stream
* @return a wrapped stream
*/
public static InputStream wrapCount(final LazyReadInput input, final InputStream stream) {
public static InputStream wrapCount(final LazyReadInput input, final InputStream stream) throws IOException {
return new CountInputStream(input, stream);
}

View file

@ -79,6 +79,7 @@ public class CreateReportAction implements CheckAction {
}
@Override
@SuppressWarnings("deprecation")
public void check(final Bag results) {
final DocumentBuilder documentBuilder = this.processor.newDocumentBuilder();
try {
@ -95,8 +96,9 @@ public class CreateReportAction implements CheckAction {
final XsltTransformer transformer = getTransformation(results).load();
transformer.setInitialContextNode(root);
final CollectingErrorEventHandler e = new CollectingErrorEventHandler();
transformer.setMessageListener(e);
transformer.setErrorReporter(er -> log.error(e.getErrorDescription()));
final Scenario scenario = results.getScenarioSelectionResult().getObject();
// TODO: Replace call to deprecated method.
transformer.setURIResolver(scenario.getUriResolver());
if (scenario.getUnparsedTextURIResolver() != null) {

View file

@ -55,16 +55,18 @@ public class SchematronValidationAction implements CheckAction {
return scenario.getSchematronValidations().stream().map(v -> validate(scenario, results, document, v)).collect(Collectors.toList());
}
@SuppressWarnings("deprecation")
private ValidationResultsSchematron validate(final Scenario scenario, final Bag results, final XdmNode document,
final Transformation validation) {
final ValidationResultsSchematron s = new ValidationResultsSchematron();
s.setResource(validation.getResourceType());
try {
final XsltTransformer transformer = validation.getExecutable().load();
// resolving nur relative zum Repository
// Resolving nur relativ zum Repository
// TODO: Replace call to deprecated method.
transformer.setURIResolver(scenario.getUriResolver());
final CollectingErrorEventHandler e = new CollectingErrorEventHandler();
transformer.setMessageListener(e);
transformer.setErrorReporter(er -> log.error(e.getErrorDescription()));
final XdmDestination result = new XdmDestination();
transformer.setDestination(result);

View file

@ -93,12 +93,14 @@ public class ProcessorProvider {
return processor;
}
@SuppressWarnings("deprecation")
private static Processor createProcessor() {
final Processor processor = new Processor(false);
// verhindere global im Prinzip alle resolving strategien
final SecureUriResolver resolver = new SecureUriResolver();
processor.getUnderlyingConfiguration().setCollectionFinder(resolver);
processor.getUnderlyingConfiguration().setOutputURIResolver(resolver);// NOSONAR
// TODO: Replace call to deprecated method.
processor.getUnderlyingConfiguration().setOutputURIResolver(resolver); // NOSONAR
processor.getUnderlyingConfiguration().setUnparsedTextURIResolver(resolver);
// grundsätzlich Feature-konfiguration:
@ -108,10 +110,14 @@ public class ProcessorProvider {
processor.setConfigurationProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS, false);
// Konfiguration des zu verwendenden Parsers, wenn Saxon selbst einen erzeugen muss, bspw. beim XSL parsen
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(FEATURE_SECURE_PROCESSING), true); // NOSONAR
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(DISSALLOW_DOCTYPE_DECL_FEATURE), true);// NOSONAR
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(LOAD_EXTERNAL_DTD_FEATURE), false);// NOSONAR
processor.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(XMLConstants.ACCESS_EXTERNAL_DTD), false);// NOSONAR
processor.getUnderlyingConfiguration().setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(FEATURE_SECURE_PROCESSING),
true); // NOSONAR
processor.getUnderlyingConfiguration()
.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(DISSALLOW_DOCTYPE_DECL_FEATURE), true); // NOSONAR
processor.getUnderlyingConfiguration().setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(LOAD_EXTERNAL_DTD_FEATURE),
false); // NOSONAR
processor.getUnderlyingConfiguration()
.setConfigurationProperty(FeatureKeys.XML_PARSER_FEATURE + encode(XMLConstants.ACCESS_EXTERNAL_DTD), false); // NOSONAR
return processor;
}
}

View file

@ -55,13 +55,13 @@ public class ConversionServiceTest {
@Test
public void testMarshalNull() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.writeXml(null);
}
@Test
public void testMarshalUnknown() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.writeXml(new Serializable() {
});
}
@ -82,31 +82,31 @@ public class ConversionServiceTest {
@Test
public void testUnmarshalInvalidXml() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(Invalid.SCENARIOS, Scenarios.class, this.repository.createSchema(SCHEMA));
}
@Test
public void testUnmarshalIllFormed() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(Invalid.SCENARIOS_ILLFORMED, Scenarios.class, this.repository.createSchema(SCHEMA));
}
@Test
public void testUnmarshalEmpty() {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(null, Scenarios.class);
}
@Test
public void testUnmarshalUnknownType() throws URISyntaxException {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(Simple.SCENARIOS, ConversionService.class);
}
@Test
public void testUnmarshalWithoutType() throws URISyntaxException {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(Simple.SCENARIOS, null);
}

View file

@ -71,13 +71,13 @@ public class VersioningTest {
@Test
public void testNewFeature() throws URISyntaxException {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(NEW_FEATURE.toURI(), Scenarios.class, SchemaProvider.getScenarioSchema());
}
@Test
public void testNewVersion() throws URISyntaxException {
this.exception.expect(ConversionService.ConversionExeption.class);
this.exception.expect(ConversionService.ConversionException.class);
this.service.readXml(NEW_VERSION.toURI(), Scenarios.class, SchemaProvider.getScenarioSchema());
}
}