2 * ============LICENSE_START=======================================================
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
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.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;
30 * Created by 10036837 on 16-7-21.
32 public class NormalizedNodePrinter implements NormalizedNodeVisitor {
34 private static final String CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
35 public NormalizedNodePrinter(StringBuilder result) {
38 private final static String endl=System.getProperty("line.separator");
39 public final static String getEndl(){
42 private static String spaces(int n) {
43 StringBuilder builder = new StringBuilder();
44 for (int i = 0; i < n; i++) {
47 return builder.toString();
51 public void visitNode(int level, String parentPath, NormalizedNode<?, ?> normalizedNode, boolean start) {
52 if(normalizedNode == null)
56 if(normalizedNode.getNodeType() == null)
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);
67 String svalue=normalizedNode.getValue().toString();
68 if(normalizedNode.getValue() instanceof QName){
69 QName qn=(QName)normalizedNode.getValue();
70 svalue= qn.getLocalName();
72 result.append(spaces((level - 1) * 4) + "<" + localName + ">" + (normalizedNode.getValue() == null ? "" :svalue) + "</" + localName + ">"+endl);
77 result.append(spaces((level-1) * 4) + "<" + localName + " xmlns=\"" + normalizedNode.getNodeType().getNamespace() + "\">"+endl);
79 result.append(spaces((level-1) * 4) + "<" + localName + ">"+endl);
82 result.append(spaces((level-1) * 4) + "</" + localName + ">"+endl);
86 private String base64Encode(byte[] in) {
87 StringBuilder out = new StringBuilder((in.length * 4) / 3);
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));
103 out.append(CODES.charAt(b));
107 out.append(CODES.charAt(b));
111 return out.toString();