|
20 | 20 | public class XmlMarshaller { |
21 | 21 |
|
22 | 22 | public static String toXml(Object obj, Boolean unescapeXML) throws JAXBException, UnsupportedEncodingException { |
23 | | - |
24 | | - JAXBContext context = JAXBContext.newInstance(obj.getClass()); |
25 | 23 |
|
26 | | - Marshaller m = context.createMarshaller(); |
27 | | - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
| 24 | + JAXBContext context = JAXBContext.newInstance(obj.getClass()); |
28 | 25 |
|
29 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
30 | | - m.marshal(obj, baos); |
| 26 | + Marshaller m = context.createMarshaller(); |
| 27 | + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
31 | 28 |
|
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 |
33 | 34 | // XML special character encoding (i.e. '"' to '"', so we have to unescape |
34 | 35 | // these character for the entire report here. |
35 | | - if(unescapeXML) { |
| 36 | + if (unescapeXML) { |
36 | 37 | return StringEscapeUtils.unescapeXml(baos.toString("UTF-8")); |
37 | 38 | } else { |
38 | 39 | return baos.toString("UTF-8"); |
39 | 40 | } |
40 | 41 |
|
41 | | - } |
42 | | - |
| 42 | + } |
| 43 | + |
43 | 44 | 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 | + }); |
54 | 62 | 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; |
58 | 67 |
|
59 | | - } |
| 68 | + } |
60 | 69 | } |
0 commit comments