Bug 1003: Restconf - remove whitespace on input
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / XmlMapper.java
1 /*
2  * Copyright (c) 2014 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.sal.rest.impl;
9
10 import javax.activation.UnsupportedDataTypeException;
11
12 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
13 import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
14 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
15 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
16 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
17 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.util.Leafref;
21 import org.w3c.dom.Document;
22
23 import com.google.common.base.Optional;
24
25 public class XmlMapper {
26     private static final LeafrefCodecImpl LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl(
27             Optional.<LeafrefTypeDefinition> absent());
28
29     private static class LeafrefCodecImpl extends TypeDefinitionAwareCodec<Object, LeafrefTypeDefinition> implements
30             LeafrefCodec<String> {
31
32         protected LeafrefCodecImpl(Optional<LeafrefTypeDefinition> typeDef) {
33             super(typeDef, Object.class);
34         }
35
36         @Override
37         public String serialize(Object data) {
38             return String.valueOf(data);
39         }
40
41         @Override
42         public Object deserialize(String data) {
43             return data;
44         }
45     }
46
47     private static class XmlCodecProviderImpl implements XmlCodecProvider {
48         @Override
49         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
50             TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = TypeDefinitionAwareCodec
51                     .from(baseType);
52
53             if (codec == null) {
54                 if (baseType instanceof Leafref) {
55                     return LEAFREF_DEFAULT_CODEC;
56                 }
57             }
58             return codec;
59         }
60     }
61
62     private static final XmlCodecProvider XML_CODEC_PROVIDER_IMPL = new XmlCodecProviderImpl();
63
64     public Document write(CompositeNode data, DataNodeContainer schema) throws UnsupportedDataTypeException {
65         return XmlDocumentUtils.toDocument(data, schema, XML_CODEC_PROVIDER_IMPL);
66     }
67 }