45f02af78994d1e87b8dc1c86c7b81e7e57d9f1a
[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 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.opendaylight.controller.netconf.monitoring.xml.model.NetconfState;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 public class JaxBSerializer {
19
20     public Element toXml(final NetconfState monitoringModel) {
21         final DOMResult res;
22         try {
23             final JAXBContext jaxbContext = JAXBContext.newInstance(NetconfState.class);
24             final Marshaller marshaller = jaxbContext.createMarshaller();
25
26             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
27
28             res = new DOMResult();
29             marshaller.marshal(monitoringModel, res);
30         } catch (final JAXBException e) {
31             throw new RuntimeException("Unable to serialize netconf state " + monitoringModel, e);
32         }
33         return ((Document)res.getNode()).getDocumentElement();
34     }
35 }