2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
23 import org.dom4j.Document;
24 import org.dom4j.DocumentException;
25 import org.dom4j.DocumentHelper;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
28 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
29 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker;
31 import java.io.Writer;
32 import java.util.List;
35 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat.XML;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_LIS_ERR;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_TREE_ERR;
40 * Representation of XML serializer which encodes properties to XML and
41 * decodes properties from XML with the data format serializer.
43 public class XmlSerializer extends DataFormatSerializer {
46 * Creates an instance of XML serializer.
48 * @param serializerContext data format serializer context
50 protected XmlSerializer(DataFormatSerializerContext serializerContext) {
51 super(XML, serializerContext);
55 public String encode(Map<String, String> param,
56 Map<String, List<Annotation>> annotations)
57 throws SvcLogicException {
58 PropertiesNode propNode = serializerContext().getPropNodeSerializer()
60 PropertiesNodeWalker nodeWalker = new DefaultPropertiesNodeWalker<>();
61 PropertiesNodeXmlListener xmlListener = new PropertiesNodeXmlListener();
62 nodeWalker.walk(xmlListener, propNode);
63 Writer writer = xmlListener.getWriter();
64 return writer.toString();
68 public Map<String, String> decode(String dataFormatBody)
69 throws SvcLogicException {
70 if (!(serializerContext().listener() instanceof XmlListener)) {
71 throw new SvcLogicException(XML_LIS_ERR);
74 XmlListener listener = (XmlListener) serializerContext().listener();
75 XmlWalker walker = new DefaultXmlWalker();
79 document = DocumentHelper.parseText(dataFormatBody);
80 } catch (DocumentException e) {
81 throw new SvcLogicException(XML_TREE_ERR, e);
83 walker.walk(listener, document.getRootElement());
85 return serializerContext().getPropNodeSerializer().decode(
86 listener.serializerHelper().getPropertiesNode());