Commit 114a8e60 authored by Luna Riegel's avatar Luna Riegel
Browse files

Refactor: Move functions

parent e07d5f03
......@@ -171,7 +171,6 @@ public class Checker {
if (pdfFile.getParentFile() != null && pdfFile.getParentFile().mkdirs()){
logger.trace("Pdf dir created");
}
Reporter reporter = new PdfReporter();
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pdfFile.getAbsolutePath()))) {
reporter.writeReport(checkConfig, bos, model, config);
......@@ -207,7 +206,7 @@ public class Checker {
}
AutoCloseable checkingContext = model.getCache().startCheckingMode();
try(checkingContext){
Callable<SvrlContentHandler> schematronThread = () -> executeSchematronValidationIfAvailable(config, model.getFile());
Callable<SvrlContentHandler> schematronThread = () -> SvrlContentHandler.executeSchematronValidationIfAvailable(config, model.getFile());
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<SvrlContentHandler> handlerFuture = executor.submit(schematronThread);
executor.shutdown();
......@@ -392,67 +391,7 @@ public class Checker {
return config;
}
public static SvrlContentHandler executeSchematronValidationIfAvailable(ValidationConfiguration config, File file) {
if (file == null || !file.exists()) {
return null;
}
try {
return executeSchematronValidationIfAvailable(config, new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new UncheckedIOException(e);
}
}
public static SvrlContentHandler executeSchematronValidationIfAvailable(ValidationConfiguration config,
InputStream in) {
if (config.getSchematronFilePath() != null && !config.getSchematronFilePath().isEmpty()) {
if (logger.isInfoEnabled()) {
logger.info(Localization.getText("Checker.schematronValidation"));
}
if (!Files.exists(Path.of(config.getSchematronFilePath()))) {
logger.warn(Localization.getText("Checker.schematronFileNotFound"), config.getSchematronFilePath());
return null;
}
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", Checker.class.getClassLoader());
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
transformerFactory.setURIResolver((href, base) -> new StreamSource(Checker.class.getResourceAsStream(href)));
Source dsdlXslSource = new StreamSource(Checker.class.getResourceAsStream("iso_dsdl_include.xsl"));
Transformer dsdlXslTransformer = transformerFactory.newTransformer(dsdlXslSource);
DOMResult dsdlXslResult = new DOMResult();
dsdlXslTransformer.transform(new StreamSource(new File(config.getSchematronFilePath())), dsdlXslResult);
Source abstractExpandXsl = new StreamSource(Checker.class.getResourceAsStream("iso_abstract_expand.xsl"));
Transformer abstractExpandTransformer = transformerFactory.newTransformer(abstractExpandXsl);
DOMResult abstractExpandResult = new DOMResult();
abstractExpandTransformer.transform(new DOMSource(dsdlXslResult.getNode()), abstractExpandResult);
Source svrlXslSource = new StreamSource(Checker.class.getResourceAsStream("iso_svrl_for_xslt2.xsl"));
Transformer svrlTransformer = transformerFactory.newTransformer(svrlXslSource);
DOMResult schematronXsltResult = new DOMResult();
svrlTransformer.transform(new DOMSource(abstractExpandResult.getNode()), schematronXsltResult);
Transformer schematronTransformer = transformerFactory.newTransformer(new DOMSource(schematronXsltResult.getNode()));
Source cityGmlSource = new StreamSource(in);
SvrlContentHandler handler = new SvrlContentHandler();
Result finalResult = new SAXResult(handler);
schematronTransformer.transform(cityGmlSource, finalResult);
return handler;
} catch (TransformerException e) {
logger.catching(e);
}
}
return null;
}
private void buildFilters() {
FilterConfiguration filterConfig = config.getFilter();
......@@ -804,7 +743,7 @@ public class Checker {
}
// execute schematron first
SvrlContentHandler handler = executeSchematronValidationIfAvailable(config, inputFile);
SvrlContentHandler handler = SvrlContentHandler.executeSchematronValidationIfAvailable(config, inputFile);
CityGmlConsumer con = new StreamCityGmlConsumer(c, xmlReporter, pdfReporter, handler, config, l);
// parse and validate
CityGmlParser.streamCityGml(inputFile, config.getParserConfiguration(), con, outputFile);
......
......@@ -18,11 +18,23 @@
*/
package de.hft.stuttgart.citydoctor2.checks;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.hft.stuttgart.citydoctor2.check.Checker;
import de.hft.stuttgart.citydoctor2.check.ValidationConfiguration;
import de.hft.stuttgart.citydoctor2.utils.Localization;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
......@@ -30,6 +42,17 @@ import org.xml.sax.SAXException;
import de.hft.stuttgart.citydoctor2.check.error.SchematronError;
import javax.xml.XMLConstants;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
/**
* This is the content handler for parsing the Svrl xml files created by a
* Schematron run. They are parsed and error objects are created and possibly
......@@ -40,6 +63,8 @@ import de.hft.stuttgart.citydoctor2.check.error.SchematronError;
*/
public class SvrlContentHandler implements ContentHandler {
private static final Logger logger = LogManager.getLogger(SvrlContentHandler.class);
private boolean nextElementIsErrorText = false;
private boolean nextIsTextContent = false;
......@@ -142,4 +167,67 @@ public class SvrlContentHandler implements ContentHandler {
// not needed
}
public static SvrlContentHandler executeSchematronValidationIfAvailable(ValidationConfiguration config, File file) {
if (file == null || !file.exists()) {
return null;
}
try {
return executeSchematronValidationIfAvailable(config, new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new UncheckedIOException(e);
}
}
public static SvrlContentHandler executeSchematronValidationIfAvailable(ValidationConfiguration config,
InputStream in) {
if (config.getSchematronFilePath() != null && !config.getSchematronFilePath().isEmpty()) {
if (logger.isInfoEnabled()) {
logger.info(Localization.getText("Checker.schematronValidation"));
}
if (!Files.exists(Path.of(config.getSchematronFilePath()))) {
logger.warn(Localization.getText("Checker.schematronFileNotFound"), config.getSchematronFilePath());
return null;
}
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", Checker.class.getClassLoader());
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
transformerFactory.setURIResolver((href, base) -> new StreamSource(Checker.class.getResourceAsStream(href)));
Source dsdlXslSource = new StreamSource(Checker.class.getResourceAsStream("iso_dsdl_include.xsl"));
Transformer dsdlXslTransformer = transformerFactory.newTransformer(dsdlXslSource);
DOMResult dsdlXslResult = new DOMResult();
dsdlXslTransformer.transform(new StreamSource(new File(config.getSchematronFilePath())), dsdlXslResult);
Source abstractExpandXsl = new StreamSource(Checker.class.getResourceAsStream("iso_abstract_expand.xsl"));
Transformer abstractExpandTransformer = transformerFactory.newTransformer(abstractExpandXsl);
DOMResult abstractExpandResult = new DOMResult();
abstractExpandTransformer.transform(new DOMSource(dsdlXslResult.getNode()), abstractExpandResult);
Source svrlXslSource = new StreamSource(Checker.class.getResourceAsStream("iso_svrl_for_xslt2.xsl"));
Transformer svrlTransformer = transformerFactory.newTransformer(svrlXslSource);
DOMResult schematronXsltResult = new DOMResult();
svrlTransformer.transform(new DOMSource(abstractExpandResult.getNode()), schematronXsltResult);
Transformer schematronTransformer = transformerFactory.newTransformer(new DOMSource(schematronXsltResult.getNode()));
Source cityGmlSource = new StreamSource(in);
SvrlContentHandler handler = new SvrlContentHandler();
Result finalResult = new SAXResult(handler);
schematronTransformer.transform(cityGmlSource, finalResult);
return handler;
} catch (TransformerException e) {
logger.catching(e);
}
}
return null;
}
}
......@@ -278,7 +278,7 @@ public class Healer {
byte[] gml = writeCityGml(co.getGmlObject());
// remove them again to save memory
co.clearGmlGeometries();
SvrlContentHandler handler = Checker.executeSchematronValidationIfAvailable(config,
SvrlContentHandler handler = SvrlContentHandler.executeSchematronValidationIfAvailable(config,
new ByteArrayInputStream(gml));
if (handler != null) {
handler.getFeatureErrors().computeIfPresent(co.getGmlId().getGmlString(), (key, list) -> {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment