|
| 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.tasks; |
| 18 | + |
| 19 | +import de.kosit.validationtool.impl.CollectingErrorEventHandler; |
| 20 | +import de.kosit.validationtool.impl.ConversionService; |
| 21 | +import de.kosit.validationtool.impl.EngineInformation; |
| 22 | +import de.kosit.validationtool.impl.Scenario; |
| 23 | +import de.kosit.validationtool.model.reportInput.XMLSyntaxError; |
| 24 | +import lombok.RequiredArgsConstructor; |
| 25 | +import lombok.extern.slf4j.Slf4j; |
| 26 | +import net.sf.saxon.s9api.*; |
| 27 | +import org.xml.sax.*; |
| 28 | +import org.xml.sax.helpers.AttributesImpl; |
| 29 | + |
| 30 | +import javax.xml.bind.JAXBException; |
| 31 | +import javax.xml.bind.Marshaller; |
| 32 | +import javax.xml.bind.util.JAXBSource; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.stream.Collectors; |
| 36 | + |
| 37 | +/** |
| 38 | + * Erzeugt den Report auf Basis der gesammelten Informationen über den Prüfling. Sollte kein Szenario identifiziert |
| 39 | + * worden sein, so wird ein das Fallback-Szenario verwendet und ein default report} erzeugt. |
| 40 | + * |
| 41 | + * @author Andreas Penski |
| 42 | + */ |
| 43 | +@RequiredArgsConstructor |
| 44 | +@Slf4j |
| 45 | +public class CreateXVRLReportAction implements CheckAction { |
| 46 | + |
| 47 | + /** |
| 48 | + * Wrapper to fix some inconsistencies between sax and saxon. Saxon tries to set some properties which has no effect |
| 49 | + * on {@link JAXBSource}'s XMLReader, but it throws exceptions on unknown properties. This just drops this |
| 50 | + * exceptions. |
| 51 | + */ |
| 52 | + private static class ReaderWrapper implements XMLReader { |
| 53 | + |
| 54 | + private static final String SAX_FEATURES_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; |
| 55 | + |
| 56 | + private static final String SAX_FEATURES_NAMESPACES = "http://xml.org/sax/features/namespaces"; |
| 57 | + |
| 58 | + private final XMLReader delegate; |
| 59 | + |
| 60 | + public ReaderWrapper(final XMLReader xmlReader) { |
| 61 | + this.delegate = xmlReader; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean getFeature(final String name) { |
| 66 | + if (SAX_FEATURES_NAMESPACES.equals(name)) { |
| 67 | + return true; |
| 68 | + } else if (SAX_FEATURES_NAMESPACE_PREFIXES.equals(name)) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + // just return false on unknown properties |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException { |
| 77 | + // this inverts the logic from JaxbSource pseudo parser |
| 78 | + if (name.equals(SAX_FEATURES_NAMESPACES) && !value) { |
| 79 | + throw new SAXNotRecognizedException(name); |
| 80 | + } |
| 81 | + if (name.equals(SAX_FEATURES_NAMESPACE_PREFIXES) && value) { |
| 82 | + throw new SAXNotRecognizedException(name); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 88 | + return this.delegate.getProperty(name); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 93 | + this.delegate.setProperty(name, value); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void setEntityResolver(final EntityResolver resolver) { |
| 98 | + this.delegate.setEntityResolver(resolver); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public EntityResolver getEntityResolver() { |
| 103 | + return this.delegate.getEntityResolver(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void setDTDHandler(final DTDHandler handler) { |
| 108 | + this.delegate.setDTDHandler(handler); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public DTDHandler getDTDHandler() { |
| 113 | + return this.delegate.getDTDHandler(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void setContentHandler(final ContentHandler handler) { |
| 118 | + this.delegate.setContentHandler(handler); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public ContentHandler getContentHandler() { |
| 123 | + return this.delegate.getContentHandler(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void setErrorHandler(final ErrorHandler handler) { |
| 128 | + this.delegate.setErrorHandler(handler); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public ErrorHandler getErrorHandler() { |
| 133 | + return this.delegate.getErrorHandler(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void parse(final InputSource input) throws IOException, SAXException { |
| 138 | + this.delegate.parse(input); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void parse(final String systemId) throws IOException, SAXException { |
| 143 | + this.delegate.parse(systemId); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static final String ERROR_MESSAGE_ELEMENT = "error-message"; |
| 148 | + |
| 149 | + private final Processor processor; |
| 150 | + |
| 151 | + private final ConversionService conversionService; |
| 152 | + |
| 153 | + private static XsltExecutable loadFromScenario(final Scenario object) { |
| 154 | + return object.getReportTransformation().getExecutable(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void check(final Process results) { |
| 159 | + final DocumentBuilder documentBuilder = this.processor.newDocumentBuilder(); |
| 160 | + try { |
| 161 | + |
| 162 | + final XdmNode parsedDocument = results.getParserResult().isValid() ? results.getParserResult().getObject() |
| 163 | + : createErrorInformation(results.getParserResult().getErrors()); |
| 164 | + |
| 165 | + final Marshaller marshaller = this.conversionService.getJaxbContext().createMarshaller(); |
| 166 | + final JAXBSource source = new JAXBSource(marshaller, results.getXvrlReportSummary()); |
| 167 | + // wrap to circumvent inconsistency between sax and saxon |
| 168 | + source.setXMLReader(new ReaderWrapper(source.getXMLReader())); |
| 169 | + |
| 170 | + final XdmNode root = documentBuilder.build(source); |
| 171 | + final XsltTransformer transformer = getTransformation(results).load(); |
| 172 | + transformer.setInitialContextNode(root); |
| 173 | + final CollectingErrorEventHandler e = new CollectingErrorEventHandler(); |
| 174 | + transformer.setMessageListener(e); |
| 175 | + final Scenario scenario = results.getScenarioSelectionResult().getObject(); |
| 176 | + transformer.setURIResolver(scenario.getUriResolver()); |
| 177 | + |
| 178 | + if (scenario.getUnparsedTextURIResolver() != null) { |
| 179 | + transformer.getUnderlyingController().setUnparsedTextURIResolver(scenario.getUnparsedTextURIResolver()); |
| 180 | + } |
| 181 | + if (parsedDocument != null) { |
| 182 | + transformer.setParameter(new QName("input-document"), parsedDocument); |
| 183 | + } |
| 184 | + final XdmDestination destination = new XdmDestination(); |
| 185 | + transformer.setDestination(destination); |
| 186 | + transformer.transform(); |
| 187 | + results.setXvrlFinalReport(destination.getXdmNode()); |
| 188 | + |
| 189 | + } catch (final SaxonApiException | SAXException | JAXBException e) { |
| 190 | + log.error("Error creating final report", e); |
| 191 | + results.stopProcessing("Can not create final report: " + e.getMessage()); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private XdmNode createErrorInformation(final Collection<XMLSyntaxError> errors) throws SaxonApiException, SAXException { |
| 196 | + final BuildingContentHandler contentHandler = this.processor.newDocumentBuilder().newBuildingContentHandler(); |
| 197 | + contentHandler.startDocument(); |
| 198 | + contentHandler.startElement(EngineInformation.getFrameworkNamespace(), ERROR_MESSAGE_ELEMENT, ERROR_MESSAGE_ELEMENT, |
| 199 | + new AttributesImpl()); |
| 200 | + final String message = errors.stream().map(XMLSyntaxError::getMessage).collect(Collectors.joining()); |
| 201 | + contentHandler.characters(message.toCharArray(), 0, message.length()); |
| 202 | + contentHandler.endElement(EngineInformation.getFrameworkNamespace(), ERROR_MESSAGE_ELEMENT, ERROR_MESSAGE_ELEMENT); |
| 203 | + return contentHandler.getDocumentNode(); |
| 204 | + } |
| 205 | + |
| 206 | + private static XsltExecutable getTransformation(final Process results) { |
| 207 | + return loadFromScenario(results.getScenarioSelectionResult().getObject()); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments