Debian Codegraph @

src/org/eclipse/parsson/NodeReference.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package org.eclipse.parsson;
18
19 import jakarta.json.JsonArray;
20 import jakarta.json.JsonArrayBuilder;
21 import jakarta.json.JsonException;
22 import jakarta.json.JsonObject;
23 import jakarta.json.JsonStructure;
24 import jakarta.json.JsonValue;
25
26 /**
27 * This class is a helper class for JsonPointer implementation,
28 * and is not part of the API.
29 *
30 * This class encapsulates a reference to a JSON node.
31 * There are three types of references.
32 * <ol><li>a reference to the root of a JSON tree.</li>
33 * <li>a reference to a name/value (possibly non-existing) pair of a JSON object, identified by a name.</li>
34 * <li>a reference to a member value of a JSON array, identified by an index.</li>
35 * </ol>
36 * Static factory methods are provided for creating these references.
37 *
38 * <p>A referenced value can be retrieved or replaced.
39 * The value of a JSON object or JSON array can be
40 * removed. A new value can be added to a JSON object or
41 * inserted into a JSON array</p>
42 *
43 * <p>Since a {@code JsonObject} or {@code JsonArray} is immutable, these operations
44 * must not modify the referenced JSON object or array. The methods {@link #add},
45 * {@link #replace}, and {@link #remove} returns a new
46 * JSON object or array after the execution of the operation.</p>
47 */
48 abstract class NodeReference {
49
50 /**
51 * Return {@code true} if a reference points to a valid value, {@code false} otherwise.
52 *
53 * @return {@code true} if a reference points to a value
54 */
55 abstract public boolean contains();
56
57 /**
58 * Get the value at the referenced location.
59 *
60 * @return the JSON value referenced
61 * @throws JsonException if the referenced value does not exist
62 */
63 abstract public JsonValue get();
64
65 /**
66 * Add or replace a value at the referenced location.
67 * If the reference is the root of a JSON tree, the added value must be
68 * a JSON object or array, which becomes the referenced JSON value.
69 * If the reference is an index of a JSON array, the value is inserted
70 * into the array at the index. If the index is -1, the value is
71 * appended to the array.
72 * If the reference is a name of a JSON object, the name/value pair is added
73 * to the object, replacing any pair with the same name.
74 *
75 * @param value the value to be added
76 * @return the JsonStructure after the operation
77 * @throws JsonException if the index to the array is not -1 or is out of range
78 */
79 abstract public JsonStructure add(JsonValue value);
80
81 /**
82 * Remove the name/value pair from the JSON object, or the value in a JSON array, as specified by the reference
83 *
84 * @return the JsonStructure after the operation
85 * @throws JsonException if the name/value pair of the referenced JSON object
86 * does not exist, or if the index of the referenced JSON array is
87 * out of range, or if the reference is a root reference
88 */
89 abstract public JsonStructure remove();
90
91 /**
92 * Replace the referenced value with the specified value.
93 *
94 * @param value the JSON value to be stored at the referenced location
95 * @return the JsonStructure after the operation
96 * @throws JsonException if the name/value pair of the referenced JSON object
97 * does not exist, or if the index of the referenced JSON array is
98 * out of range, or if the reference is a root reference
99 */
100 abstract public JsonStructure replace(JsonValue value);
101
102 /**
103 * Returns a {@code NodeReference} for a {@code JsonStructure}.
104 *
105 * @param structure the {@code JsonStructure} referenced
106 * @return the {@code NodeReference}
107 */
108 static NodeReference of(JsonStructure structure) {
109 return new RootReference(structure);
110 }
111
112 /**
113 * Returns a {@code NodeReference} for a name/value pair in a
114 * JSON object.
115 *
116 * @param object the referenced JSON object
117 * @param name the name of the name/pair
118 * @return the {@code NodeReference}
119 */
120 static NodeReference of(JsonObject object, String name, JsonContext jsonContext) {
121 return new ObjectReference(object, name, jsonContext);
122 }
123
124 /**
125 * Returns a {@code NodeReference} for a member value in a
126 * JSON array.
127 *
128 * @param array the referenced JSON array
129 * @param index the index of the member value in the JSON array
130 * @return the {@code NodeReference}
131 */
132 static NodeReference of(JsonArray array, int index, JsonContext jsonContext) {
133 return new ArrayReference(array, index, jsonContext);
134 }
135
136 static class RootReference extends NodeReference {
137
138 private JsonStructure root;
139
140 RootReference(JsonStructure root) {
141 this.root = root;
142 }
143
144 @Override
145 public boolean contains() {
146 return root != null;
147 }
148
149 @Override
150 public JsonValue get() {
151 return root;
152 }
153
154 @Override
155 public JsonStructure add(JsonValue value) {
156 switch (value.getValueType() ) {
157 case OBJECT:
158 case ARRAY:
159 this.root = (JsonStructure) value;
160 break;
161 default:
162 throw new JsonException(JsonMessages.NODEREF_VALUE_ADD_ERR());
163 }
164 return root;
165 }
166
167 @Override
168 public JsonStructure remove() {
169 throw new JsonException(JsonMessages.NODEREF_VALUE_CANNOT_REMOVE());
170 }
171
172 @Override
173 public JsonStructure replace(JsonValue value) {
174 return add(value);
175 }
176 }
177
178 static class ObjectReference extends NodeReference {
179
180 private final JsonObject object;
181 private final String key;
182 private final JsonContext jsonContext;
183
184 ObjectReference(JsonObject object, String key, JsonContext jsonContext) {
185 this.object = object;
186 this.key = key;
187 this.jsonContext = jsonContext;
188 }
189
190 @Override
191 public boolean contains() {
192 return object != null && object.containsKey(key);
193 }
194
195 @Override
196 public JsonValue get() {
197 if (!contains()) {
198 throw new JsonException(JsonMessages.NODEREF_OBJECT_MISSING(key));
199 }
200 return object.get(key);
201 }
202
203 @Override
204 public JsonObject add(JsonValue value) {
205 return new JsonObjectBuilderImpl(object, jsonContext).add(key, value).build();
206 }
207
208 @Override
209 public JsonObject remove() {
210 if (!contains()) {
211 throw new JsonException(JsonMessages.NODEREF_OBJECT_MISSING(key));
212 }
213 return new JsonObjectBuilderImpl(object, jsonContext).remove(key).build();
214 }
215
216 @Override
217 public JsonObject replace(JsonValue value) {
218 if (!contains()) {
219 throw new JsonException(JsonMessages.NODEREF_OBJECT_MISSING(key));
220 }
221 return add(value);
222 }
223 }
224
225 static class ArrayReference extends NodeReference {
226
227 private final JsonArray array;
228 private final int index; // -1 means "-" in JSON Pointer
229 private final JsonContext jsonContext;
230
231 ArrayReference(JsonArray array, int index, JsonContext jsonContext) {
232 this.array = array;
233 this.index = index;
234 this.jsonContext = jsonContext;
235 }
236
237 @Override
238 public boolean contains() {
239 return array != null && index > -1 && index < array.size();
240 }
241
242 @Override
243 public JsonValue get() {
244 if (!contains()) {
245 throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size()));
246 }
247 return array.get(index);
248 }
249
250 @Override
251 public JsonArray add(JsonValue value) {
252 //TODO should we check for arrayoutofbounds?
253 // The spec seems to say index = array.size() is allowed. This is handled as append
254 JsonArrayBuilder builder = new JsonArrayBuilderImpl(this.array, jsonContext);
255 if (index == -1 || index == array.size()) {
256 builder.add(value);
257 } else {
258 if(index < array.size()) {
259 builder.add(index, value);
260 } else {
261 throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size()));
262 }
263 }
264 return builder.build();
265 }
266
267 @Override
268 public JsonArray remove() {
269 if (!contains()) {
270 throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size()));
271 }
272 JsonArrayBuilder builder = new JsonArrayBuilderImpl(this.array, jsonContext);
273 return builder.remove(index).build();
274 }
275
276 @Override
277 public JsonArray replace(JsonValue value) {
278 if (!contains()) {
279 throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size()));
280 }
281 JsonArrayBuilder builder = new JsonArrayBuilderImpl(this.array, jsonContext);
282 return builder.set(index, value).build();
283 }
284 }
285 }
286