blob: b5f2911953c5b0cc23c9315f6e777fd7d4371ef1 [file] [log] [blame]
chengfanc58d4be2016-09-20 10:33:12 +08001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.protocol.restconf.server.utils.parser.json;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.JsonNodeType;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.protocol.restconf.server.utils.exceptions.JsonParseException;
23import org.onosproject.protocol.restconf.server.utils.parser.api.JsonBuilder;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.io.IOException;
28import java.util.Set;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static com.google.common.base.Strings.isNullOrEmpty;
32
33/**
34 * Represents implementation of interfaces to build and obtain JSON data tree.
35 */
36public class DefaultJsonBuilder implements JsonBuilder {
37
38 private static final String LEFT_BRACE = "{";
39 private static final String RIGHT_BRACE = "}";
40 private static final String LEFT_BRACKET = "[";
41 private static final String RIGHT_BRACKET = "]";
42 private static final String COMMA = ",";
43 private static final String COLON = ":";
44 private static final String QUOTE = "\"";
45 private static final String E_UNSUP_TYPE = "Unsupported node type %s " +
46 "field name is %s fieldName";
47
48 private Logger log = LoggerFactory.getLogger(getClass());
49
50 private StringBuilder treeString;
51
52 /**
53 * Creates a Default Json Builder with a specific root name.
54 *
55 * @param rootName the start string of the Json builder
56 */
57 public DefaultJsonBuilder(String rootName) {
58 checkNotNull(rootName);
59 treeString = new StringBuilder(rootName);
60 }
61
62 /**
63 * Creates a Default Json Builder with a default root name.
64 */
65 public DefaultJsonBuilder() {
66 treeString = new StringBuilder(LEFT_BRACE);
67 }
68
69 @Override
70 public void addNodeTopHalf(String fieldName, JsonNodeType nodeType) {
71
72 appendField(fieldName);
73
74 switch (nodeType) {
75 case OBJECT:
76 treeString.append(LEFT_BRACE);
77 break;
78 case ARRAY:
79 treeString.append(LEFT_BRACKET);
80 break;
81 default:
82 throw new JsonParseException(String.format(E_UNSUP_TYPE,
83 nodeType, fieldName));
84 }
85 }
86
87 @Override
88 public void addNodeWithValueTopHalf(String fieldName, String value) {
89 if (isNullOrEmpty(fieldName)) {
90 return;
91 }
92 appendField(fieldName);
93 if (value.isEmpty()) {
94 return;
95 }
96 treeString.append(QUOTE)
97 .append(value)
98 .append(QUOTE + COMMA);
99 }
100
101 @Override
102 public void addNodeWithSetTopHalf(String fieldName, Set<String> sets) {
103 if (isNullOrEmpty(fieldName)) {
104 return;
105 }
106 appendField(fieldName);
107 treeString.append(LEFT_BRACKET);
108 for (String el : sets) {
109 treeString.append(QUOTE)
110 .append(el)
111 .append(QUOTE + COMMA);
112 }
113 }
114
115 @Override
116 public void addNodeBottomHalf(JsonNodeType nodeType) {
117
118 switch (nodeType) {
119 case OBJECT:
120 removeCommaIfExist();
121 treeString.append(RIGHT_BRACE + COMMA);
122 break;
123
124 case ARRAY:
125 removeCommaIfExist();
126 treeString.append(RIGHT_BRACKET + COMMA);
127 break;
128
129 case BINARY:
130 case BOOLEAN:
131 case MISSING:
132 case NULL:
133 case NUMBER:
134 case POJO:
135 case STRING:
136 log.debug("Unimplemented node type {}", nodeType);
137 break;
138
139 default:
140 throw new JsonParseException("Unsupported json node type " +
141 nodeType);
142 }
143 }
144
145 @Override
146 public String getTreeString() {
147 removeCommaIfExist();
148 return treeString.append(RIGHT_BRACE).toString();
149 }
150
151 @Override
152 public ObjectNode getTreeNode() {
153 ObjectNode node = null;
154 try {
155 node = (ObjectNode) (new ObjectMapper()).readTree(getTreeString());
156 } catch (IOException e) {
157 log.error("Parse json string failed {}", e.getMessage());
158 }
159 return node;
160 }
161
162
163 private void appendField(String fieldName) {
164 if (!isNullOrEmpty(fieldName)) {
165 treeString.append(QUOTE)
166 .append(fieldName)
167 .append(QUOTE + COLON);
168 }
169 }
170
171 private void removeCommaIfExist() {
172 int lastIndex = treeString.length() - 1;
173 if (treeString.charAt(lastIndex) == COMMA.charAt(0)) {
174 treeString.deleteCharAt(lastIndex);
175 }
176 }
177}