Coverage Report - org.apache.johnzon.core.JsonParserFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonParserFactoryImpl
88%
47/53
79%
19/24
2,312
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.core;
 20  
 
 21  
 import java.io.InputStream;
 22  
 import java.io.Reader;
 23  
 import java.io.Serializable;
 24  
 import java.nio.charset.Charset;
 25  
 import java.util.Collection;
 26  
 import java.util.Collections;
 27  
 import java.util.HashMap;
 28  
 import java.util.Locale;
 29  
 import java.util.Map;
 30  
 import java.util.logging.Logger;
 31  
 
 32  
 import javax.json.JsonArray;
 33  
 import javax.json.JsonObject;
 34  
 import javax.json.stream.JsonParser;
 35  
 import javax.json.stream.JsonParserFactory;
 36  
 
 37  
 import static java.util.Arrays.asList;
 38  
 
 39  
 class JsonParserFactoryImpl implements JsonParserFactory, Serializable {
 40  1
     private static final Logger LOGGER = Logger.getLogger(JsonParserFactoryImpl.class.getName());
 41  
 
 42  
     public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy";
 43  
     public static final String MAX_STRING_LENGTH = "org.apache.johnzon.max-string-length";
 44  
     public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer";
 45  
     public static final String SUPPORTS_COMMENTS = "org.apache.johnzon.supports-comments";
 46  1
     public static final int DEFAULT_MAX_SIZE = Integer.getInteger(MAX_STRING_LENGTH, 8192*32); //TODO check default string length/buffer size
 47  
     public static final boolean DEFAULT_SUPPORTS_COMMENT = false;
 48  
 
 49  316
     private final Map<String, Object> internalConfig = new HashMap<String, Object>();
 50  1
     private static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
 51  
         BUFFER_STRATEGY, MAX_STRING_LENGTH, BUFFER_LENGTH, SUPPORTS_COMMENTS
 52  
     );
 53  
       
 54  
     private final int maxSize;
 55  
     private final BufferStrategy.BufferProvider<char[]> bufferProvider;
 56  
     private final BufferStrategy.BufferProvider<char[]> valueBufferProvider;
 57  
     private final boolean supportsComments;
 58  
 
 59  316
     JsonParserFactoryImpl(final Map<String, ?> config) {
 60  316
         if(config != null) {
 61  314
             for (String configKey : config.keySet()) {
 62  314
                 if(SUPPORTED_CONFIG_KEYS.contains(configKey)) {
 63  314
                     internalConfig.put(configKey, config.get(configKey));
 64  
                 } else {
 65  0
                     LOGGER.warning(configKey + " is not supported by " + getClass().getName());
 66  
                 }
 67  314
             }
 68  
         } 
 69  
         
 70  
 
 71  316
         final int bufferSize = getInt(BUFFER_LENGTH);
 72  316
         if (bufferSize <= 0) {
 73  1
             throw new IllegalArgumentException("buffer length must be greater than zero");
 74  
         }
 75  
 
 76  315
         this.maxSize = getInt(MAX_STRING_LENGTH);
 77  315
         this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
 78  315
         this.valueBufferProvider = getBufferProvider().newCharProvider(maxSize);
 79  315
         this.supportsComments = getBool(SUPPORTS_COMMENTS);
 80  315
     }
 81  
 
 82  
     private BufferStrategy getBufferProvider() {
 83  630
         final Object name = internalConfig.get(BUFFER_STRATEGY);
 84  630
         if (name != null) {
 85  0
             return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
 86  
         }
 87  630
         return BufferStrategy.QUEUE;
 88  
     }
 89  
 
 90  
     private int getInt(final String key) {
 91  631
         final Object maxStringSize = internalConfig.get(key);
 92  631
         if (maxStringSize == null) {
 93  318
             return DEFAULT_MAX_SIZE;
 94  313
         } else if (Number.class.isInstance(maxStringSize)) {
 95  4
             return Number.class.cast(maxStringSize).intValue();
 96  
         }
 97  309
         return Integer.parseInt(maxStringSize.toString());
 98  
     }
 99  
 
 100  
     private boolean getBool(final String key) {
 101  315
         final Object maxStringSize = internalConfig.get(key);
 102  315
         if (maxStringSize == null) {
 103  314
             return DEFAULT_SUPPORTS_COMMENT;
 104  1
         } else if (Boolean.class.isInstance(maxStringSize)) {
 105  1
             return Boolean.class.cast(maxStringSize);
 106  
         }
 107  0
         return Boolean.parseBoolean(maxStringSize.toString());
 108  
     }
 109  
 
 110  
     private JsonParser getDefaultJsonParserImpl(final InputStream in) {
 111  279
         if (supportsComments) {
 112  1
             return new CommentsJsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
 113  
         }
 114  
         //UTF Auto detection RFC 4627
 115  278
         return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
 116  
     }
 117  
 
 118  
     private JsonParser getDefaultJsonParserImpl(final InputStream in, final Charset charset) {
 119  55
         if (supportsComments) {
 120  0
             return new CommentsJsonStreamParserImpl(in, charset, maxSize, bufferProvider, valueBufferProvider);
 121  
         }
 122  
         //use provided charset
 123  55
         return new JsonStreamParserImpl(in, charset, maxSize, bufferProvider, valueBufferProvider);
 124  
     }
 125  
 
 126  
     private JsonParser getDefaultJsonParserImpl(final Reader in) {
 127  126
         if (supportsComments) {
 128  0
             return new CommentsJsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
 129  
         }
 130  
         //no charset necessary
 131  126
         return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
 132  
     }
 133  
 
 134  
     @Override
 135  
     public JsonParser createParser(final Reader reader) {
 136  119
         return getDefaultJsonParserImpl(reader);
 137  
     }
 138  
 
 139  
     @Override
 140  
     public JsonParser createParser(final InputStream in) {
 141  194
         return getDefaultJsonParserImpl(in);
 142  
     }
 143  
 
 144  
     @Override
 145  
     public JsonParser createParser(final InputStream in, final Charset charset) {
 146  1
         return getDefaultJsonParserImpl(in, charset);
 147  
     }
 148  
 
 149  
     @Override
 150  
     public JsonParser createParser(final JsonObject obj) {
 151  
         // no need of a comment version since JsonObject has no comment event
 152  1
         return new JsonInMemoryParser(obj);
 153  
     }
 154  
 
 155  
     @Override
 156  
     public JsonParser createParser(final JsonArray array) {
 157  
         // no need of a comment version since JsonObject has no comment event
 158  1
         return new JsonInMemoryParser(array);
 159  
     }
 160  
 
 161  
     @Override
 162  
     public Map<String, ?> getConfigInUse() {
 163  0
         return Collections.unmodifiableMap(internalConfig);
 164  
     }
 165  
 
 166  
     public JsonParser createInternalParser(final InputStream in) {
 167  85
         return getDefaultJsonParserImpl(in);
 168  
     }
 169  
     
 170  
     public JsonParser createInternalParser(final InputStream in, final Charset charset) {
 171  54
         return getDefaultJsonParserImpl(in, charset);
 172  
     }
 173  
 
 174  
     public JsonParser createInternalParser(final Reader reader) {
 175  7
         return getDefaultJsonParserImpl(reader);
 176  
     }
 177  
 }