chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.protocol.restconf.server.utils.parser.json; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.JsonNodeType; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onosproject.protocol.restconf.server.utils.exceptions.JsonParseException; |
| 23 | import org.onosproject.protocol.restconf.server.utils.parser.api.JsonBuilder; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 32 | |
| 33 | /** |
| 34 | * Represents implementation of interfaces to build and obtain JSON data tree. |
| 35 | */ |
| 36 | public 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); |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame] | 93 | |
| 94 | // If the value is null, then it's a empty leaf-node |
| 95 | if (value == null) { |
| 96 | treeString.append(QUOTE) |
| 97 | .append(QUOTE + COMMA); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // If the value is empty, then it's a non-leaf node |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 102 | if (value.isEmpty()) { |
| 103 | return; |
| 104 | } |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame] | 105 | |
| 106 | // It's a non-empty leaf node |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 107 | treeString.append(QUOTE) |
| 108 | .append(value) |
| 109 | .append(QUOTE + COMMA); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void addNodeWithSetTopHalf(String fieldName, Set<String> sets) { |
| 114 | if (isNullOrEmpty(fieldName)) { |
| 115 | return; |
| 116 | } |
| 117 | appendField(fieldName); |
| 118 | treeString.append(LEFT_BRACKET); |
| 119 | for (String el : sets) { |
| 120 | treeString.append(QUOTE) |
| 121 | .append(el) |
| 122 | .append(QUOTE + COMMA); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void addNodeBottomHalf(JsonNodeType nodeType) { |
| 128 | |
| 129 | switch (nodeType) { |
| 130 | case OBJECT: |
| 131 | removeCommaIfExist(); |
| 132 | treeString.append(RIGHT_BRACE + COMMA); |
| 133 | break; |
| 134 | |
| 135 | case ARRAY: |
| 136 | removeCommaIfExist(); |
| 137 | treeString.append(RIGHT_BRACKET + COMMA); |
| 138 | break; |
| 139 | |
| 140 | case BINARY: |
| 141 | case BOOLEAN: |
| 142 | case MISSING: |
| 143 | case NULL: |
| 144 | case NUMBER: |
| 145 | case POJO: |
| 146 | case STRING: |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 147 | log.trace("Unimplemented node type {}", nodeType); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 148 | break; |
| 149 | |
| 150 | default: |
| 151 | throw new JsonParseException("Unsupported json node type " + |
| 152 | nodeType); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public String getTreeString() { |
| 158 | removeCommaIfExist(); |
| 159 | return treeString.append(RIGHT_BRACE).toString(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public ObjectNode getTreeNode() { |
| 164 | ObjectNode node = null; |
| 165 | try { |
| 166 | node = (ObjectNode) (new ObjectMapper()).readTree(getTreeString()); |
| 167 | } catch (IOException e) { |
| 168 | log.error("Parse json string failed {}", e.getMessage()); |
| 169 | } |
| 170 | return node; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | private void appendField(String fieldName) { |
| 175 | if (!isNullOrEmpty(fieldName)) { |
| 176 | treeString.append(QUOTE) |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame] | 177 | .append(fieldName) |
| 178 | .append(QUOTE + COLON); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | private void removeCommaIfExist() { |
| 183 | int lastIndex = treeString.length() - 1; |
| 184 | if (treeString.charAt(lastIndex) == COMMA.charAt(0)) { |
| 185 | treeString.deleteCharAt(lastIndex); |
| 186 | } |
| 187 | } |
| 188 | } |