Bump versions to 7.0.6-SNAPSHOT
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / monitoring / JaxBSerializer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.test.tool.monitoring;
9
10 import javax.xml.bind.JAXBContext;
11 import javax.xml.bind.JAXBException;
12 import javax.xml.bind.Marshaller;
13 import javax.xml.transform.dom.DOMResult;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16
17 // FIXME: this is a rather ugly class
18 public final class JaxBSerializer {
19     private static final JAXBContext JAXB_CONTEXT;
20
21     static {
22         try {
23             JAXB_CONTEXT = JAXBContext.newInstance(NetconfState.class);
24         } catch (final JAXBException e) {
25             throw new ExceptionInInitializerError(e);
26         }
27     }
28
29     private JaxBSerializer() {
30         // Hidden on purpose
31     }
32
33     public static Element toXml(final NetconfState monitoringModel) {
34         final var res = new DOMResult();
35         try {
36             final var marshaller = JAXB_CONTEXT.createMarshaller();
37             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
38             marshaller.marshal(monitoringModel, res);
39         } catch (JAXBException e) {
40             throw new IllegalStateException("Unable to serialize netconf state " + monitoringModel, e);
41         }
42         return ((Document) res.getNode()).getDocumentElement();
43     }
44 }