1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2011-2013 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://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
|
---|
12 | * or packager/legal/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 packager/legal/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 javax.json.spi;
|
---|
42 |
|
---|
43 | import javax.json.*;
|
---|
44 | import javax.json.stream.JsonGenerator;
|
---|
45 | import javax.json.stream.JsonGeneratorFactory;
|
---|
46 | import javax.json.stream.JsonParser;
|
---|
47 | import javax.json.stream.JsonParserFactory;
|
---|
48 | import java.io.InputStream;
|
---|
49 | import java.io.OutputStream;
|
---|
50 | import java.io.Reader;
|
---|
51 | import java.io.Writer;
|
---|
52 | import java.util.Iterator;
|
---|
53 | import java.util.Map;
|
---|
54 | import java.util.ServiceLoader;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Service provider for JSON processing objects.
|
---|
58 | *
|
---|
59 | * <p>All the methods in this class are safe for use by multiple concurrent
|
---|
60 | * threads.
|
---|
61 | *
|
---|
62 | * @see ServiceLoader
|
---|
63 | * @author Jitendra Kotamraju
|
---|
64 | */
|
---|
65 | public abstract class JsonProvider {
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * A constant representing the name of the default
|
---|
69 | * {@code JsonProvider} implementation class.
|
---|
70 | */
|
---|
71 | private static final String DEFAULT_PROVIDER
|
---|
72 | = "org.glassfish.json.JsonProviderImpl";
|
---|
73 |
|
---|
74 | protected JsonProvider() {
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | *
|
---|
79 | * Creates a JSON provider object. The provider is loaded using the
|
---|
80 | * {@link ServiceLoader#load(Class)} method. If there are no available
|
---|
81 | * service providers, this method returns the default service provider.
|
---|
82 | *
|
---|
83 | * @see ServiceLoader
|
---|
84 | * @return a JSON provider
|
---|
85 | */
|
---|
86 | public static JsonProvider provider() {
|
---|
87 | ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
|
---|
88 | Iterator<JsonProvider> it = loader.iterator();
|
---|
89 | if (it.hasNext()) {
|
---|
90 | return it.next();
|
---|
91 | }
|
---|
92 |
|
---|
93 | try {
|
---|
94 | Class<?> clazz = Class.forName(DEFAULT_PROVIDER);
|
---|
95 | return (JsonProvider)clazz.newInstance();
|
---|
96 | } catch (ClassNotFoundException x) {
|
---|
97 | throw new JsonException(
|
---|
98 | "Provider " + DEFAULT_PROVIDER + " not found", x);
|
---|
99 | } catch (Exception x) {
|
---|
100 | throw new JsonException(
|
---|
101 | "Provider " + DEFAULT_PROVIDER + " could not be instantiated: " + x,
|
---|
102 | x);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Creates a JSON parser from a character stream.
|
---|
108 | *
|
---|
109 | * @param reader i/o reader from which JSON is to be read
|
---|
110 | * @return a JSON parser
|
---|
111 | */
|
---|
112 | public abstract JsonParser createParser(Reader reader);
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Creates a JSON parser from the specified byte stream.
|
---|
116 | * The character encoding of the stream is determined
|
---|
117 | * as defined in <a href="http://tools.ietf.org/rfc/rfc4627.txt">RFC 4627
|
---|
118 | * </a>.
|
---|
119 | *
|
---|
120 | * @param in i/o stream from which JSON is to be read
|
---|
121 | * @throws JsonException if encoding cannot be determined
|
---|
122 | * or i/o error (IOException would be cause of JsonException)
|
---|
123 | * @return a JSON parser
|
---|
124 | */
|
---|
125 | public abstract JsonParser createParser(InputStream in);
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Creates a parser factory for creating {@link JsonParser} instances.
|
---|
129 | *
|
---|
130 | * @return a JSON parser factory
|
---|
131 | *
|
---|
132 | public abstract JsonParserFactory createParserFactory();
|
---|
133 | */
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Creates a parser factory for creating {@link JsonParser} instances.
|
---|
137 | * The factory is configured with the specified map of
|
---|
138 | * provider specific configuration properties. Provider implementations
|
---|
139 | * should ignore any unsupported configuration properties specified in
|
---|
140 | * the map.
|
---|
141 | *
|
---|
142 | * @param config a map of provider specific properties to configure the
|
---|
143 | * JSON parsers. The map may be empty or null
|
---|
144 | * @return a JSON parser factory
|
---|
145 | */
|
---|
146 | public abstract JsonParserFactory createParserFactory(Map<String, ?> config);
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Creates a JSON generator for writing JSON text to a character stream.
|
---|
150 | *
|
---|
151 | * @param writer a i/o writer to which JSON is written
|
---|
152 | * @return a JSON generator
|
---|
153 | */
|
---|
154 | public abstract JsonGenerator createGenerator(Writer writer);
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Creates a JSON generator for writing JSON text to a byte stream.
|
---|
158 | *
|
---|
159 | * @param out i/o stream to which JSON is written
|
---|
160 | * @return a JSON generator
|
---|
161 | */
|
---|
162 | public abstract JsonGenerator createGenerator(OutputStream out);
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Creates a generator factory for creating {@link JsonGenerator} instances.
|
---|
166 | *
|
---|
167 | * @return a JSON generator factory
|
---|
168 | *
|
---|
169 | public abstract JsonGeneratorFactory createGeneratorFactory();
|
---|
170 | */
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Creates a generator factory for creating {@link JsonGenerator} instances.
|
---|
174 | * The factory is configured with the specified map of provider specific
|
---|
175 | * configuration properties. Provider implementations should
|
---|
176 | * ignore any unsupported configuration properties specified in the map.
|
---|
177 | *
|
---|
178 | * @param config a map of provider specific properties to configure the
|
---|
179 | * JSON generators. The map may be empty or null
|
---|
180 | * @return a JSON generator factory
|
---|
181 | */
|
---|
182 | public abstract JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config);
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Creates a JSON reader from a character stream.
|
---|
186 | *
|
---|
187 | * @param reader a reader from which JSON is to be read
|
---|
188 | * @return a JSON reader
|
---|
189 | */
|
---|
190 | public abstract JsonReader createReader(Reader reader);
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Creates a JSON reader from a byte stream. The character encoding of
|
---|
194 | * the stream is determined as described in
|
---|
195 | * <a href="http://tools.ietf.org/rfc/rfc4627.txt">RFC 4627</a>.
|
---|
196 | *
|
---|
197 | * @param in a byte stream from which JSON is to be read
|
---|
198 | * @return a JSON reader
|
---|
199 | */
|
---|
200 | public abstract JsonReader createReader(InputStream in);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Creates a JSON writer to write a
|
---|
204 | * JSON {@link JsonObject object} or {@link JsonArray array}
|
---|
205 | * structure to the specified character stream.
|
---|
206 | *
|
---|
207 | * @param writer to which JSON object or array is written
|
---|
208 | * @return a JSON writer
|
---|
209 | */
|
---|
210 | public abstract JsonWriter createWriter(Writer writer);
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Creates a JSON writer to write a
|
---|
214 | * JSON {@link JsonObject object} or {@link JsonArray array}
|
---|
215 | * structure to the specified byte stream. Characters written to
|
---|
216 | * the stream are encoded into bytes using UTF-8 encoding.
|
---|
217 | *
|
---|
218 | * @param out to which JSON object or array is written
|
---|
219 | * @return a JSON writer
|
---|
220 | */
|
---|
221 | public abstract JsonWriter createWriter(OutputStream out);
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Creates a writer factory for creating {@link JsonWriter} objects.
|
---|
225 | * The factory is configured with the specified map of provider specific
|
---|
226 | * configuration properties. Provider implementations should ignore any
|
---|
227 | * unsupported configuration properties specified in the map.
|
---|
228 | *
|
---|
229 | * @param config a map of provider specific properties to configure the
|
---|
230 | * JSON writers. The map may be empty or null
|
---|
231 | * @return a JSON writer factory
|
---|
232 | */
|
---|
233 | public abstract JsonWriterFactory createWriterFactory(Map<String,?> config);
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Creates a reader factory for creating {@link JsonReader} objects.
|
---|
237 | * The factory is configured with the specified map of provider specific
|
---|
238 | * configuration properties. Provider implementations should ignore any
|
---|
239 | * unsupported configuration properties specified in the map.
|
---|
240 | *
|
---|
241 | * @param config a map of provider specific properties to configure the
|
---|
242 | * JSON readers. The map may be empty or null
|
---|
243 | * @return a JSON reader factory
|
---|
244 | */
|
---|
245 | public abstract JsonReaderFactory createReaderFactory(Map<String,?> config);
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Creates a JSON object builder
|
---|
249 | *
|
---|
250 | * @return a JSON object builder
|
---|
251 | */
|
---|
252 | public abstract JsonObjectBuilder createObjectBuilder();
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Creates a JSON array builder
|
---|
256 | *
|
---|
257 | * @return a JSON array builder
|
---|
258 | */
|
---|
259 | public abstract JsonArrayBuilder createArrayBuilder();
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Creates a builder factory for creating {@link JsonArrayBuilder}
|
---|
263 | * and {@link JsonObjectBuilder} objects.
|
---|
264 | * The factory is configured with the specified map of provider specific
|
---|
265 | * configuration properties. Provider implementations should ignore any
|
---|
266 | * unsupported configuration properties specified in the map.
|
---|
267 | *
|
---|
268 | * @param config a map of provider specific properties to configure the
|
---|
269 | * JSON builders. The map may be empty or null
|
---|
270 | * @return a JSON builder factory
|
---|
271 | */
|
---|
272 | public abstract JsonBuilderFactory createBuilderFactory(Map<String,?> config);
|
---|
273 |
|
---|
274 | }
|
---|