mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
(chore) Rückgabe der Schematron-Ergebnisse als komplexes Objekt in der API
This commit is contained in:
parent
b3e9d3a244
commit
2069c0d04e
11 changed files with 354 additions and 59 deletions
|
|
@ -27,14 +27,26 @@ import java.util.Arrays;
|
|||
import java.util.Collection;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import javax.xml.bind.*;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.JAXBIntrospector;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.ValidationEventHandler;
|
||||
import javax.xml.bind.annotation.XmlRegistry;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.stream.*;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import javax.xml.validation.Schema;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
|
@ -45,6 +57,32 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
public class ConversionService {
|
||||
|
||||
/**
|
||||
* Exception while serializing/deserializing with jaxb.
|
||||
*/
|
||||
public class ConversionExeption extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param message the message.
|
||||
* @param cause the cause
|
||||
*/
|
||||
public ConversionExeption(final String message, final Exception cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param message the message.
|
||||
*/
|
||||
public ConversionExeption(final String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static final int MAX_LOG_CONTENT = 50;
|
||||
// context setup
|
||||
private JAXBContext jaxbContext;
|
||||
|
||||
|
|
@ -68,7 +106,7 @@ public class ConversionService {
|
|||
* Initialisiert den default context; Alle Packages mit {@link XmlRegistry XmlRegistries}.
|
||||
*/
|
||||
public void initialize() {
|
||||
Collection<Package> p = new ArrayList<>();
|
||||
final Collection<Package> p = new ArrayList<>();
|
||||
p.add(de.kosit.validationtool.model.reportInput.ObjectFactory.class.getPackage());
|
||||
p.add(de.kosit.validationtool.model.scenarios.ObjectFactory.class.getPackage());
|
||||
initialize(p);
|
||||
|
|
@ -77,6 +115,7 @@ public class ConversionService {
|
|||
public void initialize(final Package... context) {
|
||||
initialize(Arrays.asList(context));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialisiert den conversion service mit den angegegebenen Packages.
|
||||
*
|
||||
|
|
@ -84,7 +123,7 @@ public class ConversionService {
|
|||
*/
|
||||
public void initialize(final Collection<Package> context) {
|
||||
final String[] packages = context != null ? context.stream().map(Package::getName).toArray(String[]::new) : new String[0];
|
||||
StringJoiner joiner = new StringJoiner(":");
|
||||
final StringJoiner joiner = new StringJoiner(":");
|
||||
Arrays.stream(packages).forEach(p -> joiner.add(p));
|
||||
initialize(joiner.toString());
|
||||
}
|
||||
|
|
@ -121,11 +160,11 @@ public class ConversionService {
|
|||
return readXml(xml, type, null, null);
|
||||
}
|
||||
|
||||
public <T> T readXml(final URI xml, final Class<T> type, Schema schema) {
|
||||
public <T> T readXml(final URI xml, final Class<T> type, final Schema schema) {
|
||||
return readXml(xml, type, schema, null);
|
||||
}
|
||||
|
||||
public <T> T readXml(final URI xml, final Class<T> type, Schema schema, ValidationEventHandler handler) {
|
||||
public <T> T readXml(final URI xml, final Class<T> type, final Schema schema, final ValidationEventHandler handler) {
|
||||
checkInputEmpty(xml);
|
||||
checkTypeEmpty(type);
|
||||
CollectingErrorEventHandler defaultHandler = null;
|
||||
|
|
@ -167,11 +206,11 @@ public class ConversionService {
|
|||
return writeXml(model, null, null);
|
||||
}
|
||||
|
||||
public <T> String writeXml(final T model, Schema schema, ValidationEventHandler handler) {
|
||||
public <T> String writeXml(final T model, final Schema schema, final ValidationEventHandler handler) {
|
||||
if (model == null) {
|
||||
throw new ConversionExeption("Can not serialize null");
|
||||
}
|
||||
try ( StringWriter w = new StringWriter() ) {
|
||||
try ( final StringWriter w = new StringWriter() ) {
|
||||
final JAXBIntrospector introspector = getJaxbContext().createJAXBIntrospector();
|
||||
final Marshaller marshaller = getJaxbContext().createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
|
|
@ -189,17 +228,17 @@ public class ConversionService {
|
|||
}
|
||||
xmlStreamWriter.flush();
|
||||
return w.toString();
|
||||
} catch (JAXBException | IOException | XMLStreamException e) {
|
||||
} catch (final JAXBException | IOException | XMLStreamException e) {
|
||||
throw new ConversionExeption(String.format("Error serializing Object %s", model.getClass().getName()), e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> Document writeDocument(T input) {
|
||||
public <T> Document writeDocument(final T input) {
|
||||
if (input == null) {
|
||||
throw new ConversionExeption("Can not serialize null");
|
||||
}
|
||||
DocumentBuilder builder = ObjectFactory.createDocumentBuilder(false);
|
||||
Document document = builder.newDocument();
|
||||
final DocumentBuilder builder = ObjectFactory.createDocumentBuilder(false);
|
||||
final Document document = builder.newDocument();
|
||||
|
||||
// Marshal the Object to a Document
|
||||
Marshaller marshaller = null;
|
||||
|
|
@ -208,34 +247,20 @@ public class ConversionService {
|
|||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
marshaller.marshal(input, document);
|
||||
return document;
|
||||
} catch (JAXBException e) {
|
||||
} catch (final JAXBException e) {
|
||||
throw new ConversionExeption(String.format("Error serializing Object %s to document", input.getClass().getName()), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception while serializing/deserializing with jaxb.
|
||||
*/
|
||||
public class ConversionExeption extends RuntimeException {
|
||||
public <T> T readDocument(final Source source, final Class<T> type) {
|
||||
try {
|
||||
final Unmarshaller u = getJaxbContext().createUnmarshaller();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param message the message.
|
||||
* @param cause the cause
|
||||
*/
|
||||
public ConversionExeption(final String message, final Exception cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
return u.unmarshal(source, type).getValue();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param message the message.
|
||||
*/
|
||||
public ConversionExeption(final String message) {
|
||||
super(message);
|
||||
} catch (final JAXBException e) {
|
||||
throw new ConversionExeption(String.format("Can not unmarshal to type %s: %s", type.getSimpleName(),
|
||||
StringUtils.abbreviate(source.getSystemId(), MAX_LOG_CONTENT)), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue