source: josm/trunk/src/org/glassfish/json/JsonPointerImpl.java@ 15903

Last change on this file since 15903 was 13231, checked in by Don-vip, 7 years ago

see #15682 - upgrade to JSR 374 (JSON Processing) API 1.1.2

File size: 13.2 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12 * or LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41package org.glassfish.json;
42
43import javax.json.JsonArray;
44import javax.json.JsonException;
45import javax.json.JsonObject;
46import javax.json.JsonPointer;
47import javax.json.JsonStructure;
48import javax.json.JsonValue;
49import java.io.Serializable;
50import java.util.function.BiFunction;
51
52/**
53 * <p>This class is an immutable representation of a JSON Pointer as specified in
54 * <a href="http://tools.ietf.org/html/rfc6901">RFC 6901</a>.
55 * </p>
56 * <p> A JSON Pointer, when applied to a target {@link JsonValue},
57 * defines a reference location in the target.</p>
58 * <p> An empty JSON Pointer string defines a reference to the target itself.</p>
59 * <p> If the JSON Pointer string is non-empty, it must be a sequence
60 * of '/' prefixed tokens, and the target must either be a {@link JsonArray}
61 * or {@link JsonObject}. If the target is a {@code JsonArray}, the pointer
62 * defines a reference to an array element, and the last token specifies the index.
63 * If the target is a {@link JsonObject}, the pointer defines a reference to a
64 * name/value pair, and the last token specifies the name.
65 * </p>
66 * <p> The method {@link #getValue getValue()} returns the referenced value.
67 * The methods {@link #add add()}, {@link #replace replace()},
68 * and {@link #remove remove()} executes the operations specified in
69 * <a href="http://tools.ietf.org/html/rfc6902">RFC 6902</a>. </p>
70 *
71 * @since 1.1
72 */
73
74public final class JsonPointerImpl implements JsonPointer, Serializable {
75
76 private static final long serialVersionUID = -8123110179640843141L;
77 private final String[] tokens;
78 private final String jsonPointer;
79
80 /**
81 * Constructs and initializes a JsonPointerImpl.
82 * @param jsonPointer the JSON Pointer string
83 * @throws NullPointerException if {@code jsonPointer} is {@code null}
84 * @throws JsonException if {@code jsonPointer} is not a valid JSON Pointer
85 */
86 public JsonPointerImpl(String jsonPointer) {
87 this.jsonPointer = jsonPointer;
88 tokens = jsonPointer.split("/", -1); // keep the trailing blanks
89 if (! "".equals(tokens[0])) {
90 throw new JsonException(JsonMessages.POINTER_FORMAT_INVALID());
91 }
92 for (int i = 1; i < tokens.length; i++) {
93 String token = tokens[i];
94 StringBuilder reftoken = new StringBuilder();
95 for (int j = 0; j < token.length(); j++) {
96 char ch = token.charAt(j);
97 if (ch == '~' && j < token.length() - 1) {
98 char ch1 = token.charAt(j+1);
99 if (ch1 == '0') {
100 ch = '~'; j++;
101 } else if (ch1 == '1') {
102 ch = '/'; j++;
103 }
104 }
105 reftoken.append(ch);
106 }
107 tokens[i] = reftoken.toString();
108 }
109 }
110
111 /**
112 * Compares this {@code JsonPointer} with another object.
113 * @param obj the object to compare this {@code JsonPointer} against
114 * @return true if the given object is a {@code JsonPointer} with the same
115 * reference tokens as this one, false otherwise.
116 */
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj)
120 return true;
121 if (obj == null || obj.getClass() != JsonPointerImpl.class)
122 return false;
123 return jsonPointer.equals(((JsonPointerImpl)obj).jsonPointer);
124 }
125
126 /**
127 * Returns the hash code value for this {@code JsonPointer} object.
128 * The hash code of this object is defined by the hash codes of it's reference tokens.
129 *
130 * @return the hash code value for this {@code JsonPointer} object
131 */
132 @Override
133 public int hashCode() {
134 return jsonPointer.hashCode();
135 }
136
137 /**
138 * Returns {@code true} if there is a value at the referenced location in the specified {@code target}.
139 *
140 * @param target the target referenced by this {@code JsonPointer}
141 * @return {@code true} if this pointer points to a value in a specified {@code target}.
142 */
143 @Override
144 public boolean containsValue(JsonStructure target) {
145 NodeReference[] refs = getReferences(target);
146 return refs[0].contains();
147 }
148
149 /**
150 * Returns the value at the referenced location in the specified {@code target}
151 *
152 * @param target the target referenced by this {@code JsonPointer}
153 * @return the referenced value in the target.
154 * @throws NullPointerException if {@code target} is null
155 * @throws JsonException if the referenced value does not exist
156 */
157 @Override
158 public JsonValue getValue(JsonStructure target) {
159 NodeReference[] refs = getReferences(target);
160 return refs[0].get();
161 }
162
163 /**
164 * Adds or replaces a value at the referenced location in the specified
165 * {@code target} with the specified {@code value}.
166 * <ol>
167 * <li>If the reference is the target (empty JSON Pointer string),
168 * the specified {@code value}, which must be the same type as
169 * specified {@code target}, is returned.</li>
170 * <li>If the reference is an array element, the specified {@code value} is inserted
171 * into the array, at the referenced index. The value currently at that location, and
172 * any subsequent values, are shifted to the right (adds one to the indices).
173 * Index starts with 0. If the reference is specified with a "-", or if the
174 * index is equal to the size of the array, the value is appended to the array.</li>
175 * <li>If the reference is a name/value pair of a {@code JsonObject}, and the
176 * referenced value exists, the value is replaced by the specified {@code value}.
177 * If the value does not exist, a new name/value pair is added to the object.</li>
178 * </ol>
179 *
180 * @param target the target referenced by this {@code JsonPointer}
181 * @param value the value to be added
182 * @return the transformed {@code target} after the value is added.
183 * @throws NullPointerException if {@code target} is {@code null}
184 * @throws JsonException if the reference is an array element and
185 * the index is out of range ({@code index < 0 || index > array size}),
186 * or if the pointer contains references to non-existing objects or arrays.
187 */
188 @Override
189 public JsonStructure add(JsonStructure target, JsonValue value) {
190 return execute(NodeReference::add, target, value);
191 }
192
193 /**
194 * Replaces the value at the referenced location in the specified
195 * {@code target} with the specified {@code value}.
196 *
197 * @param target the target referenced by this {@code JsonPointer}
198 * @param value the value to be stored at the referenced location
199 * @return the transformed {@code target} after the value is replaced.
200 * @throws NullPointerException if {@code target} is {@code null}
201 * @throws JsonException if the referenced value does not exist,
202 * or if the reference is the target.
203 */
204 @Override
205 public JsonStructure replace(JsonStructure target, JsonValue value) {
206 return execute(NodeReference::replace, target, value);
207 }
208
209 /**
210 * Removes the value at the reference location in the specified {@code target}
211 *
212 * @param target the target referenced by this {@code JsonPointer}
213 * @return the transformed {@code target} after the value is removed.
214 * @throws NullPointerException if {@code target} is {@code null}
215 * @throws JsonException if the referenced value does not exist,
216 * or if the reference is the target.
217 */
218 @Override
219 public JsonStructure remove(JsonStructure target) {
220 return execute((r,v)->r.remove(), target, null);
221 }
222
223 /**
224 * Executes the operation
225 * @param op a {code BiFunction} used to specify the operation to execute on
226 * the leaf node of the Json Pointer
227 * @param target the target JsonStructure for this JsonPointer
228 * @param value the JsonValue for add and replace, can be null for getvalue and remove
229 */
230 private JsonStructure execute(BiFunction<NodeReference, JsonValue, JsonStructure> op,
231 JsonStructure target, JsonValue value) {
232
233 NodeReference[] refs = getReferences(target);
234 JsonStructure result = op.apply(refs[0], value);
235 for (int i = 1; i < refs.length; i++) {
236 result = refs[i].replace(result);
237 }
238 return result;
239 }
240
241 /**
242 * Computes the {@code NodeReference}s for each node on the path of
243 * the JSON Pointer, in reverse order, starting from the leaf node
244 */
245 private NodeReference[] getReferences(JsonStructure target) {
246 NodeReference[] references;
247 // First check if this is a reference to a JSON value tree
248 if (tokens.length == 1) {
249 references = new NodeReference[1];
250 references[0] = NodeReference.of(target);
251 return references;
252 }
253
254 references = new NodeReference[tokens.length-1];
255 JsonValue value = target;
256 int s = tokens.length;
257 for (int i = 1; i < s; i++) {
258 // Start with index 1, skipping the "" token
259 switch (value.getValueType()) {
260 case OBJECT:
261 JsonObject object = (JsonObject) value;
262 references[s-i-1] = NodeReference.of(object, tokens[i]);
263 if (i < s-1) {
264 value = object.get(tokens[i]);
265 if (value == null) {
266 // Except for the last name, the mapping must exist
267 throw new JsonException(JsonMessages.POINTER_MAPPING_MISSING(object, tokens[i]));
268 }
269 }
270 break;
271 case ARRAY:
272 int index = getIndex(tokens[i]);
273 JsonArray array = (JsonArray) value;
274 references[s-i-1] = NodeReference.of(array, index);
275 if (i < s-1 && index != -1) {
276 if (index >= array.size()) {
277 throw new JsonException(JsonMessages.NODEREF_ARRAY_INDEX_ERR(index, array.size()));
278 }
279 // The last array index in the path can have index value of -1
280 // ("-" in the JSON pointer)
281 value = array.get(index);
282 }
283 break;
284 default:
285 throw new JsonException(JsonMessages.POINTER_REFERENCE_INVALID(value.getValueType()));
286 }
287 }
288 return references;
289 }
290
291 /**
292 * Computes the array index
293 * @param token the input string token
294 * @return the array index. -1 if the token is "-"
295 * @throws JsonException if the string token is not in correct format
296 */
297 static private int getIndex(String token) {
298 if (token == null || token.length() == 0) {
299 throw new JsonException(JsonMessages.POINTER_ARRAY_INDEX_ERR(token));
300 }
301 if (token.equals("-")) {
302 return -1;
303 }
304 if (token.equals("0")) {
305 return 0;
306 }
307 if (token.charAt(0) == '+' || token.charAt(0) == '-') {
308 throw new JsonException(JsonMessages.POINTER_ARRAY_INDEX_ERR(token));
309 }
310 try {
311 return Integer.parseInt(token);
312 } catch (NumberFormatException ex) {
313 throw new JsonException(JsonMessages.POINTER_ARRAY_INDEX_ILLEGAL(token), ex);
314 }
315 }
316}
Note: See TracBrowser for help on using the repository browser.