BUG-1521 Netconf-monitoring test coverage raise
[controller.git] / opendaylight / netconf / netconf-monitoring / src / main / java / org / opendaylight / controller / netconf / monitoring / xml / 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.controller.netconf.monitoring.xml;
9
10 import org.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element;
13
14 import javax.xml.bind.JAXBContext;
15 import javax.xml.bind.JAXBException;
16 import javax.xml.bind.Marshaller;
17 import javax.xml.transform.dom.DOMResult;
18
19 public class JaxBSerializer {
20
21     public Element toXml(final NetconfState monitoringModel) {
22         final DOMResult res;
23         try {
24             final JAXBContext jaxbContext = JAXBContext.newInstance(NetconfState.class);
25             final Marshaller marshaller = jaxbContext.createMarshaller();
26
27             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
28
29             res = new DOMResult();
30             marshaller.marshal(monitoringModel, res);
31         } catch (final JAXBException e) {
32            throw new RuntimeException("Unable to serialize netconf state " + monitoringModel, e);
33         }
34         return ((Document)res.getNode()).getDocumentElement();
35     }
36 }