Update POM to inherit from oparent
[so.git] / common / src / main / java / org / openecomp / mso / yangDecoder / transform / impl / NormalizedNodePrinter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.mso.yangDecoder.transform.impl;
22 import org.openecomp.mso.yangDecoder.transform.api.NormalizedNodeVisitor;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28
29 /**
30  * Created by 10036837 on 16-7-21.
31  */
32 public class NormalizedNodePrinter implements NormalizedNodeVisitor {
33     StringBuilder result;
34     private static final String CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
35     public NormalizedNodePrinter(StringBuilder result) {
36         this.result = result;
37     }
38     private final static String endl=System.getProperty("line.separator");
39     public final static String getEndl(){
40         return endl;
41     }
42     private static String spaces(int n) {
43         StringBuilder builder = new StringBuilder();
44         for (int i = 0; i < n; i++) {
45             builder.append(' ');
46         }
47         return builder.toString();
48     }
49
50     @Override
51     public void visitNode(int level, String parentPath, NormalizedNode<?, ?> normalizedNode, boolean start) {
52         if(normalizedNode == null)
53         {
54             return;
55         }
56         if(normalizedNode.getNodeType() == null)
57         {
58             return;
59         }
60
61         String localName = normalizedNode.getNodeType().getLocalName();
62         if (normalizedNode instanceof LeafNode || normalizedNode instanceof LeafSetEntryNode) {
63             if(normalizedNode.getValue() instanceof  byte[]){
64                 result.append(spaces((level-1) * 4) + "<" + localName + ">" + (normalizedNode.getValue() == null ? "" : base64Encode((byte[]) normalizedNode.getValue())) + "</" + localName + ">"+endl);
65             }
66             else {
67                String svalue=normalizedNode.getValue().toString();
68                 if(normalizedNode.getValue() instanceof QName){
69                     QName qn=(QName)normalizedNode.getValue();
70                     svalue= qn.getLocalName();
71                 }
72                 result.append(spaces((level - 1) * 4) + "<" + localName + ">" + (normalizedNode.getValue() == null ? "" :svalue) + "</" + localName + ">"+endl);
73             }
74         } else {
75             if (start) {
76                 if (level == 1) {
77                     result.append(spaces((level-1) * 4) + "<" + localName + " xmlns=\"" + normalizedNode.getNodeType().getNamespace() + "\">"+endl);
78                 } else {
79                     result.append(spaces((level-1) * 4) + "<" + localName + ">"+endl);
80                 }
81             } else {
82                 result.append(spaces((level-1) * 4) + "</" + localName + ">"+endl);
83             }
84         }
85     }
86     private String base64Encode(byte[] in) {
87         StringBuilder out = new StringBuilder((in.length * 4) / 3);
88         int b;
89         for (int i = 0; i < in.length; i += 3) {
90             b = (in[i] & 0xFC) >> 2;
91             out.append(CODES.charAt(b));
92             b = (in[i] & 0x03) << 4;
93             if (i + 1 < in.length) {
94                 b |= (in[i + 1] & 0xF0) >> 4;
95                 out.append(CODES.charAt(b));
96                 b = (in[i + 1] & 0x0F) << 2;
97                 if (i + 2 < in.length) {
98                     b |= (in[i + 2] & 0xC0) >> 6;
99                     out.append(CODES.charAt(b));
100                     b = in[i + 2] & 0x3F;
101                     out.append(CODES.charAt(b));
102                 } else {
103                     out.append(CODES.charAt(b));
104                     out.append('=');
105                 }
106             } else {
107                 out.append(CODES.charAt(b));
108                 out.append("==");
109             }
110         }
111         return out.toString();
112     }
113 }