*/
package org.opendaylight.controller.netconf.monitoring.xml;
-import org.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.dom.DOMResult;
+import org.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
public class JaxBSerializer {
+ private static final JAXBContext JAXB_CONTEXT;
- public Element toXml(NetconfState monitoringModel) {
- DOMResult res = null;
+ static {
try {
- JAXBContext jaxbContext = JAXBContext.newInstance(NetconfState.class);
- Marshaller marshaller = jaxbContext.createMarshaller();
+ JAXB_CONTEXT = JAXBContext.newInstance(NetconfState.class);
+ } catch (JAXBException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ }
+
+ public Element toXml(final NetconfState monitoringModel) {
+ final DOMResult res;
+ try {
+ final Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
res = new DOMResult();
marshaller.marshal(monitoringModel, res);
- } catch (JAXBException e) {
- throw new RuntimeException("Unable to serialize netconf state " + monitoringModel, e);
+ } catch (final JAXBException e) {
+ throw new RuntimeException("Unable to serialize netconf state " + monitoringModel, e);
}
return ((Document)res.getNode()).getDocumentElement();
}