Skip to content

Commit 79af210

Browse files
committed
support both schemas
1 parent ee1c951 commit 79af210

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/main/java/edu/ucsb/nceas/mdqengine/serialize/XmlMarshaller.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,50 @@
2020
public class XmlMarshaller {
2121

2222
public static String toXml(Object obj, Boolean unescapeXML) throws JAXBException, UnsupportedEncodingException {
23-
24-
JAXBContext context = JAXBContext.newInstance(obj.getClass());
2523

26-
Marshaller m = context.createMarshaller();
27-
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
24+
JAXBContext context = JAXBContext.newInstance(obj.getClass());
2825

29-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
30-
m.marshal(obj, baos);
26+
Marshaller m = context.createMarshaller();
27+
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
3128

32-
// JAXB annotations (e.g. in Check.java) are unable to prevent marshalling from performing
29+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
30+
m.marshal(obj, baos);
31+
32+
// JAXB annotations (e.g. in Check.java) are unable to prevent marshalling from
33+
// performing
3334
// XML special character encoding (i.e. '"' to '&quot', so we have to unescape
3435
// these character for the entire report here.
35-
if(unescapeXML) {
36+
if (unescapeXML) {
3637
return StringEscapeUtils.unescapeXml(baos.toString("UTF-8"));
3738
} else {
3839
return baos.toString("UTF-8");
3940
}
4041

41-
}
42-
42+
}
43+
4344
public static Object fromXml(String xml, Class clazz) throws JAXBException, IOException, SAXException {
44-
45-
JAXBContext context = JAXBContext.newInstance(clazz);
46-
Unmarshaller u = context.createUnmarshaller();
47-
48-
// TODO: include the schema in jar
49-
InputStream schemaStream = XmlMarshaller.class.getResourceAsStream("/schemas/schema1.1.xsd");
50-
//InputStream schemaStream = new FileInputStream("/Users/leinfelder/git/mdqengine/target/schemas/schema1.xsd");
51-
52-
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
53-
Schema schema = factory.newSchema(new StreamSource(schemaStream));
45+
46+
JAXBContext context = JAXBContext.newInstance(clazz);
47+
Unmarshaller u = context.createUnmarshaller();
48+
49+
// TODO: include the schema in jar
50+
InputStream schemaStreamV1 = XmlMarshaller.class.getResourceAsStream("/schemas/schema1.xsd");
51+
InputStream schemaStreamV11 = XmlMarshaller.class.getResourceAsStream("/schemas/schema1.1.xsd");
52+
53+
if (schemaStreamV1 == null || schemaStreamV11 == null) {
54+
throw new IOException("One or more schema files not found");
55+
}
56+
57+
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
58+
Schema schema = factory.newSchema(new StreamSource[] {
59+
new StreamSource(schemaStreamV1),
60+
new StreamSource(schemaStreamV11)
61+
});
5462
u.setSchema(schema);
55-
56-
Object obj = u.unmarshal(IOUtils.toInputStream(xml, "UTF-8"));
57-
return obj;
63+
u.setSchema(schema);
64+
65+
Object obj = u.unmarshal(IOUtils.toInputStream(xml, "UTF-8"));
66+
return obj;
5867

59-
}
68+
}
6069
}

0 commit comments

Comments
 (0)