[6756] | 1 | /*
|
---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2012-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 org.glassfish.json;
|
---|
| 42 |
|
---|
| 43 | import org.glassfish.json.api.BufferPool;
|
---|
| 44 |
|
---|
| 45 | import javax.json.*;
|
---|
| 46 | import javax.json.stream.JsonGenerator;
|
---|
| 47 | import javax.json.stream.JsonGeneratorFactory;
|
---|
| 48 | import javax.json.stream.JsonParser;
|
---|
| 49 | import javax.json.stream.JsonParserFactory;
|
---|
| 50 | import javax.json.spi.JsonProvider;
|
---|
| 51 | import java.io.InputStream;
|
---|
| 52 | import java.io.OutputStream;
|
---|
| 53 | import java.io.Reader;
|
---|
| 54 | import java.io.Writer;
|
---|
| 55 | import java.util.Collections;
|
---|
| 56 | import java.util.HashMap;
|
---|
| 57 | import java.util.Map;
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * @author Jitendra Kotamraju
|
---|
| 61 | */
|
---|
| 62 | public class JsonProviderImpl extends JsonProvider {
|
---|
| 63 |
|
---|
| 64 | private final BufferPool bufferPool = new BufferPoolImpl();
|
---|
| 65 |
|
---|
| 66 | @Override
|
---|
| 67 | public JsonGenerator createGenerator(Writer writer) {
|
---|
| 68 | return new JsonGeneratorImpl(writer, bufferPool);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public JsonGenerator createGenerator(OutputStream out) {
|
---|
| 73 | return new JsonGeneratorImpl(out, bufferPool);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | public JsonParser createParser(Reader reader) {
|
---|
| 78 | return new JsonParserImpl(reader, bufferPool);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | public JsonParser createParser(InputStream in) {
|
---|
| 83 | return new JsonParserImpl(in, bufferPool);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | @Override
|
---|
| 87 | public JsonParserFactory createParserFactory(Map<String, ?> config) {
|
---|
| 88 | BufferPool pool = null;
|
---|
| 89 | if (config != null && config.containsKey(BufferPool.class.getName())) {
|
---|
| 90 | pool = (BufferPool)config.get(BufferPool.class.getName());
|
---|
| 91 | }
|
---|
| 92 | if (pool == null) {
|
---|
| 93 | pool = bufferPool;
|
---|
| 94 | }
|
---|
| 95 | return new JsonParserFactoryImpl(pool);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config) {
|
---|
| 100 | Map<String, Object> providerConfig;
|
---|
| 101 | boolean prettyPrinting;
|
---|
| 102 | BufferPool pool;
|
---|
| 103 | if (config == null) {
|
---|
| 104 | providerConfig = Collections.emptyMap();
|
---|
| 105 | prettyPrinting = false;
|
---|
| 106 | pool = bufferPool;
|
---|
| 107 | } else {
|
---|
| 108 | providerConfig = new HashMap<String, Object>();
|
---|
| 109 | if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) {
|
---|
| 110 | providerConfig.put(JsonGenerator.PRETTY_PRINTING, true);
|
---|
| 111 | }
|
---|
| 112 | pool = (BufferPool)config.get(BufferPool.class.getName());
|
---|
| 113 | if (pool != null) {
|
---|
| 114 | providerConfig.put(BufferPool.class.getName(), pool);
|
---|
| 115 | } else {
|
---|
| 116 | pool = bufferPool;
|
---|
| 117 | }
|
---|
| 118 | providerConfig = Collections.unmodifiableMap(providerConfig);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return new JsonGeneratorFactoryImpl(providerConfig, prettyPrinting, pool);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | @Override
|
---|
| 125 | public JsonReader createReader(Reader reader) {
|
---|
| 126 | return new JsonReaderImpl(reader, bufferPool);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | @Override
|
---|
| 130 | public JsonReader createReader(InputStream in) {
|
---|
| 131 | return new JsonReaderImpl(in, bufferPool);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | @Override
|
---|
| 135 | public JsonWriter createWriter(Writer writer) {
|
---|
| 136 | return new JsonWriterImpl(writer, bufferPool);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | @Override
|
---|
| 140 | public JsonWriter createWriter(OutputStream out) {
|
---|
| 141 | return new JsonWriterImpl(out, bufferPool);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | @Override
|
---|
| 145 | public JsonWriterFactory createWriterFactory(Map<String, ?> config) {
|
---|
| 146 | Map<String, Object> providerConfig;
|
---|
| 147 | boolean prettyPrinting;
|
---|
| 148 | BufferPool pool;
|
---|
| 149 | if (config == null) {
|
---|
| 150 | providerConfig = Collections.emptyMap();
|
---|
| 151 | prettyPrinting = false;
|
---|
| 152 | pool = bufferPool;
|
---|
| 153 | } else {
|
---|
| 154 | providerConfig = new HashMap<String, Object>();
|
---|
| 155 | if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) {
|
---|
| 156 | providerConfig.put(JsonGenerator.PRETTY_PRINTING, true);
|
---|
| 157 | }
|
---|
| 158 | pool = (BufferPool)config.get(BufferPool.class.getName());
|
---|
| 159 | if (pool != null) {
|
---|
| 160 | providerConfig.put(BufferPool.class.getName(), pool);
|
---|
| 161 | } else {
|
---|
| 162 | pool = bufferPool;
|
---|
| 163 | }
|
---|
| 164 | providerConfig = Collections.unmodifiableMap(providerConfig);
|
---|
| 165 | }
|
---|
| 166 | return new JsonWriterFactoryImpl(providerConfig, prettyPrinting, pool);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | @Override
|
---|
| 170 | public JsonReaderFactory createReaderFactory(Map<String, ?> config) {
|
---|
| 171 | BufferPool pool = null;
|
---|
| 172 | if (config != null && config.containsKey(BufferPool.class.getName())) {
|
---|
| 173 | pool = (BufferPool)config.get(BufferPool.class.getName());
|
---|
| 174 | }
|
---|
| 175 | if (pool == null) {
|
---|
| 176 | pool = bufferPool;
|
---|
| 177 | }
|
---|
| 178 | return new JsonReaderFactoryImpl(pool);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | @Override
|
---|
| 182 | public JsonObjectBuilder createObjectBuilder() {
|
---|
| 183 | return new JsonObjectBuilderImpl(bufferPool);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | @Override
|
---|
| 187 | public JsonArrayBuilder createArrayBuilder() {
|
---|
| 188 | return new JsonArrayBuilderImpl(bufferPool);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | @Override
|
---|
| 192 | public JsonBuilderFactory createBuilderFactory(Map<String,?> config) {
|
---|
| 193 | BufferPool pool = null ;
|
---|
| 194 | if (config != null && config.containsKey(BufferPool.class.getName())) {
|
---|
| 195 | pool = (BufferPool)config.get(BufferPool.class.getName());
|
---|
| 196 | }
|
---|
| 197 | if (pool == null) {
|
---|
| 198 | pool = bufferPool;
|
---|
| 199 | }
|
---|
| 200 | return new JsonBuilderFactoryImpl(pool);
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | static boolean isPrettyPrintingEnabled(Map<String, ?> config) {
|
---|
| 204 | return config.containsKey(JsonGenerator.PRETTY_PRINTING);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|