1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013-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 |
|
---|
41 | package org.glassfish.json;
|
---|
42 |
|
---|
43 |
|
---|
44 | import javax.json.stream.JsonLocation;
|
---|
45 | import javax.json.stream.JsonParser;
|
---|
46 | import java.text.MessageFormat;
|
---|
47 | import java.util.ResourceBundle;
|
---|
48 | import javax.json.JsonObject;
|
---|
49 | import javax.json.JsonValue;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Defines string formatting method for each constant in the resource file
|
---|
53 | *
|
---|
54 | * @author Jitendra Kotamraju
|
---|
55 | */
|
---|
56 | final class JsonMessages {
|
---|
57 | private static final ResourceBundle BUNDLE =
|
---|
58 | ResourceBundle.getBundle("org.glassfish.json.messages");
|
---|
59 |
|
---|
60 | // global/shared messages
|
---|
61 | static String INTERNAL_ERROR() {
|
---|
62 | return localize("internal.error");
|
---|
63 | }
|
---|
64 |
|
---|
65 | // tokenizer messages
|
---|
66 | static String TOKENIZER_UNEXPECTED_CHAR(int unexpected, JsonLocation location) {
|
---|
67 | return localize("tokenizer.unexpected.char", unexpected, location);
|
---|
68 | }
|
---|
69 |
|
---|
70 | static String TOKENIZER_EXPECTED_CHAR(int unexpected, JsonLocation location, char expected) {
|
---|
71 | return localize("tokenizer.expected.char", unexpected, location, expected);
|
---|
72 | }
|
---|
73 |
|
---|
74 | static String TOKENIZER_IO_ERR() {
|
---|
75 | return localize("tokenizer.io.err");
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | // parser messages
|
---|
80 | static String PARSER_GETSTRING_ERR(JsonParser.Event event) {
|
---|
81 | return localize("parser.getString.err", event);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static String PARSER_ISINTEGRALNUMBER_ERR(JsonParser.Event event) {
|
---|
85 | return localize("parser.isIntegralNumber.err", event);
|
---|
86 | }
|
---|
87 |
|
---|
88 | static String PARSER_GETINT_ERR(JsonParser.Event event) {
|
---|
89 | return localize("parser.getInt.err", event);
|
---|
90 | }
|
---|
91 |
|
---|
92 | static String PARSER_GETLONG_ERR(JsonParser.Event event) {
|
---|
93 | return localize("parser.getLong.err", event);
|
---|
94 | }
|
---|
95 |
|
---|
96 | static String PARSER_GETBIGDECIMAL_ERR(JsonParser.Event event) {
|
---|
97 | return localize("parser.getBigDecimal.err", event);
|
---|
98 | }
|
---|
99 |
|
---|
100 | static String PARSER_GETARRAY_ERR(JsonParser.Event event) {
|
---|
101 | return localize("parser.getArray.err", event);
|
---|
102 | }
|
---|
103 |
|
---|
104 | static String PARSER_GETOBJECT_ERR(JsonParser.Event event) {
|
---|
105 | return localize("parser.getObject.err", event);
|
---|
106 | }
|
---|
107 |
|
---|
108 | static String PARSER_GETVALUE_ERR(JsonParser.Event event) {
|
---|
109 | return localize("parser.getValue.err", event);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static String PARSER_GETVALUESTREAM_ERR() {
|
---|
113 | return localize("parser.getValueStream.err");
|
---|
114 | }
|
---|
115 |
|
---|
116 | static String PARSER_EXPECTED_EOF(JsonTokenizer.JsonToken token) {
|
---|
117 | return localize("parser.expected.eof", token);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static String PARSER_TOKENIZER_CLOSE_IO() {
|
---|
121 | return localize("parser.tokenizer.close.io");
|
---|
122 | }
|
---|
123 |
|
---|
124 | static String PARSER_INVALID_TOKEN(JsonTokenizer.JsonToken token, JsonLocation location, String expectedTokens) {
|
---|
125 | return localize("parser.invalid.token", token, location, expectedTokens);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static String PARSER_STATE_ERR(JsonValue.ValueType type) {
|
---|
129 | return localize("parser.state.err", type);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static String PARSER_SCOPE_ERR(JsonValue value) {
|
---|
133 | return localize("parser.scope.err", value);
|
---|
134 | }
|
---|
135 |
|
---|
136 | static String PARSER_INPUT_ENC_DETECT_FAILED() {
|
---|
137 | return localize("parser.input.enc.detect.failed");
|
---|
138 | }
|
---|
139 |
|
---|
140 | static String PARSER_INPUT_ENC_DETECT_IOERR() {
|
---|
141 | return localize("parser.input.enc.detect.ioerr");
|
---|
142 | }
|
---|
143 |
|
---|
144 | // generator messages
|
---|
145 | static String GENERATOR_FLUSH_IO_ERR() {
|
---|
146 | return localize("generator.flush.io.err");
|
---|
147 | }
|
---|
148 |
|
---|
149 | static String GENERATOR_CLOSE_IO_ERR() {
|
---|
150 | return localize("generator.close.io.err");
|
---|
151 | }
|
---|
152 |
|
---|
153 | static String GENERATOR_WRITE_IO_ERR() {
|
---|
154 | return localize("generator.write.io.err");
|
---|
155 | }
|
---|
156 |
|
---|
157 | static String GENERATOR_ILLEGAL_METHOD(Object scope) {
|
---|
158 | return localize("generator.illegal.method", scope);
|
---|
159 | }
|
---|
160 |
|
---|
161 | static String GENERATOR_DOUBLE_INFINITE_NAN() {
|
---|
162 | return localize("generator.double.infinite.nan");
|
---|
163 | }
|
---|
164 |
|
---|
165 | static String GENERATOR_INCOMPLETE_JSON() {
|
---|
166 | return localize("generator.incomplete.json");
|
---|
167 | }
|
---|
168 |
|
---|
169 | static String GENERATOR_ILLEGAL_MULTIPLE_TEXT() {
|
---|
170 | return localize("generator.illegal.multiple.text");
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 | // writer messages
|
---|
176 | static String WRITER_WRITE_ALREADY_CALLED() {
|
---|
177 | return localize("writer.write.already.called");
|
---|
178 | }
|
---|
179 |
|
---|
180 | // reader messages
|
---|
181 | static String READER_READ_ALREADY_CALLED() {
|
---|
182 | return localize("reader.read.already.called");
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | // obj builder messages
|
---|
187 | static String OBJBUILDER_NAME_NULL() {
|
---|
188 | return localize("objbuilder.name.null");
|
---|
189 | }
|
---|
190 |
|
---|
191 | static String OBJBUILDER_VALUE_NULL() {
|
---|
192 | return localize("objbuilder.value.null");
|
---|
193 | }
|
---|
194 |
|
---|
195 | static String OBJBUILDER_OBJECT_BUILDER_NULL() {
|
---|
196 | return localize("objbuilder.object.builder.null");
|
---|
197 | }
|
---|
198 |
|
---|
199 | static String OBJBUILDER_ARRAY_BUILDER_NULL() {
|
---|
200 | return localize("objbuilder.array.builder.null");
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | // array builder messages
|
---|
205 | static String ARRBUILDER_VALUE_NULL() {
|
---|
206 | return localize("arrbuilder.value.null");
|
---|
207 | }
|
---|
208 |
|
---|
209 | static String ARRBUILDER_OBJECT_BUILDER_NULL() {
|
---|
210 | return localize("arrbuilder.object.builder.null");
|
---|
211 | }
|
---|
212 |
|
---|
213 | static String ARRBUILDER_ARRAY_BUILDER_NULL() {
|
---|
214 | return localize("arrbuilder.array.builder.null");
|
---|
215 | }
|
---|
216 |
|
---|
217 | static String ARRBUILDER_VALUELIST_NULL(int index, int size) {
|
---|
218 | return localize("arrbuilder.valuelist.null", index, size);
|
---|
219 | }
|
---|
220 |
|
---|
221 | // json pointer messages
|
---|
222 | static String POINTER_FORMAT_INVALID() {
|
---|
223 | return localize("pointer.format.invalid");
|
---|
224 | }
|
---|
225 |
|
---|
226 | static String POINTER_MAPPING_MISSING(JsonObject object, String key) {
|
---|
227 | return localize("pointer.mapping.missing", object, key);
|
---|
228 | }
|
---|
229 |
|
---|
230 | static String POINTER_REFERENCE_INVALID(JsonValue.ValueType type) {
|
---|
231 | return localize("pointer.reference.invalid", type.name());
|
---|
232 | }
|
---|
233 |
|
---|
234 | static String POINTER_ARRAY_INDEX_ERR(String token) {
|
---|
235 | return localize("pointer.array.index.err", token);
|
---|
236 | }
|
---|
237 |
|
---|
238 | static String POINTER_ARRAY_INDEX_ILLEGAL(String token) {
|
---|
239 | return localize("pointer.array.index.illegal", token);
|
---|
240 | }
|
---|
241 |
|
---|
242 | // nodereference messages
|
---|
243 | static String NODEREF_VALUE_ADD_ERR() {
|
---|
244 | return localize("noderef.value.add.err");
|
---|
245 | }
|
---|
246 |
|
---|
247 | static String NODEREF_VALUE_CANNOT_REMOVE() {
|
---|
248 | return localize("noderef.value.cannot.remove");
|
---|
249 | }
|
---|
250 |
|
---|
251 | static String NODEREF_OBJECT_MISSING(String key) {
|
---|
252 | return localize("noderef.object.missing", key);
|
---|
253 | }
|
---|
254 |
|
---|
255 | static String NODEREF_ARRAY_INDEX_ERR(int index, int size) {
|
---|
256 | return localize("noderef.array.index.err", index, size);
|
---|
257 | }
|
---|
258 |
|
---|
259 | // json patch messages
|
---|
260 | static String PATCH_MUST_BE_ARRAY() {
|
---|
261 | return localize("patch.must.be.array");
|
---|
262 | }
|
---|
263 |
|
---|
264 | static String PATCH_MOVE_PROPER_PREFIX(String from, String path) {
|
---|
265 | return localize("patch.move.proper.prefix", from, path);
|
---|
266 | }
|
---|
267 |
|
---|
268 | static String PATCH_MOVE_TARGET_NULL(String from) {
|
---|
269 | return localize("patch.move.target.null", from);
|
---|
270 | }
|
---|
271 |
|
---|
272 | static String PATCH_TEST_FAILED(String path, String value) {
|
---|
273 | return localize("patch.test.failed", path, value);
|
---|
274 | }
|
---|
275 |
|
---|
276 | static String PATCH_ILLEGAL_OPERATION(String operation) {
|
---|
277 | return localize("patch.illegal.operation", operation);
|
---|
278 | }
|
---|
279 |
|
---|
280 | static String PATCH_MEMBER_MISSING(String operation, String member) {
|
---|
281 | return localize("patch.member.missing", operation, member);
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | private static String localize(String key, Object ... args) {
|
---|
286 | try {
|
---|
287 | String msg = BUNDLE.getString(key);
|
---|
288 | return MessageFormat.format(msg, args);
|
---|
289 | } catch (Exception e) {
|
---|
290 | return getDefaultMessage(key, args);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | private static String getDefaultMessage(String key, Object ... args) {
|
---|
295 | StringBuilder sb = new StringBuilder();
|
---|
296 | sb.append("[failed to localize] ");
|
---|
297 | sb.append(key);
|
---|
298 | if (args != null) {
|
---|
299 | sb.append('(');
|
---|
300 | for (int i = 0; i < args.length; ++i) {
|
---|
301 | if (i != 0)
|
---|
302 | sb.append(", ");
|
---|
303 | sb.append(String.valueOf(args[i]));
|
---|
304 | }
|
---|
305 | sb.append(')');
|
---|
306 | }
|
---|
307 | return sb.toString();
|
---|
308 | }
|
---|
309 |
|
---|
310 | }
|
---|