Skip to content

Commit

Permalink
Allow to use custom instances of XMLInputFactory and XMLOutputFactory.
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
kenwenzel committed Feb 24, 2023
1 parent 25f88ee commit 23e4132
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AASXDeserializer {
private static final String XML_TYPE = "http://www.admin-shell.io/aasx/relationships/aas-spec";
private static final String AASX_ORIGIN = "/aasx/aasx-origin";

private XmlDeserializer deserializer = new XmlDeserializer();
private final XmlDeserializer deserializer;

private Environment environment;
private final OPCPackage aasxRoot;
Expand All @@ -61,6 +61,7 @@ public class AASXDeserializer {
*/
public AASXDeserializer(InputStream inputStream) throws InvalidFormatException, IOException {
aasxRoot = OPCPackage.open(inputStream);
this.deserializer = new XmlDeserializer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ public class AASXSerializer {

private static final String AASSUPPL_RELTYPE = "http://www.admin-shell.io/aasx/relationships/aas-suppl";

private Serializer xmlSerializer = new XmlSerializer();
private final Serializer xmlSerializer;

/**
* Default constructor
*/
public AASXSerializer() {
this.xmlSerializer = new XmlSerializer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,31 @@
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class XmlDeserializer implements Deserializer {

protected final XmlFactory xmlFactory;
protected XmlMapper mapper;
protected SimpleAbstractTypeResolver typeResolver;
@SuppressWarnings("rawtypes")
protected static Map<Class<?>, JsonDeserializer> customDeserializers = Map.of(
SubmodelElement.class, new SubmodelElementDeserializer());

public XmlDeserializer() {
this(new XmlFactory());
}

public XmlDeserializer(XmlFactory xmlFactory) {
this.xmlFactory = xmlFactory;
initTypeResolver();
buildMapper();
}

protected void buildMapper() {
mapper = XmlMapper.builder().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
mapper = XmlMapper.builder(xmlFactory)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.annotationIntrospector(new XmlDataformatAnnotationIntrospector())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;


public class XmlSerializer implements Serializer {
protected final XmlFactory xmlFactory;
protected XmlMapper mapper;
protected Map<String, String> namespacePrefixes;

Expand All @@ -49,12 +51,17 @@ public XmlSerializer() {
}

public XmlSerializer(Map<String, String> namespacePrefixes) {
this(new XmlFactory(), namespacePrefixes);
}

public XmlSerializer(XmlFactory xmlFactory, Map<String, String> namespacePrefixes) {
this.xmlFactory = xmlFactory;
this.namespacePrefixes = namespacePrefixes;
buildMapper();
}

protected void buildMapper() {
mapper = XmlMapper.builder()
mapper = XmlMapper.builder(xmlFactory)
.enable(SerializationFeature.INDENT_OUTPUT)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.serializationInclusion(JsonInclude.Include.NON_NULL)
Expand Down

0 comments on commit 23e4132

Please sign in to comment.