All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.neutron.spi.NeutronResourceMapPropertyAdapter Maven / Gradle / Ivy

There is a newer version: 0.16.4
Show newest version
/*
 * Copyright (c) 2016 Intel Corporation and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.neutron.spi;

import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public final class NeutronResourceMapPropertyAdapter extends XmlAdapter> {
    private static final Logger LOG = LoggerFactory.getLogger(NeutronResourceMapPropertyAdapter.class);

    @Override
    public Map unmarshal(Object domTree) {
        Map map = new HashMap<>();
        Element content = (Element) domTree;
        NodeList childNodes = content.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            String key = child.getNodeName();
            String value = ((Text) child.getChildNodes().item(0)).getWholeText();
            map.put(key, value);
        }
        return map;
    }

    @Override
    public Object marshal(Map map) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            Element customXml = doc.createElement("Map");
            if (map != null) {
                for (Map.Entry entry : map.entrySet()) {
                    Element keyValuePair = doc.createElement(entry.getKey());
                    keyValuePair.appendChild(doc.createTextNode(entry.getValue()));
                    customXml.appendChild(keyValuePair);
                }
            }
            return customXml;
        } catch (javax.xml.parsers.ParserConfigurationException e) {
            LOG.error("ParserConfigurationException", e);
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy