Coverage Report - org.apache.johnzon.core.JsonParserFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonParserFactoryImpl
94%
35/37
92%
13/14
1,733
 
 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.Collections;
 26  
 import java.util.HashMap;
 27  
 import java.util.Locale;
 28  
 import java.util.Map;
 29  
 
 30  
 import javax.json.JsonArray;
 31  
 import javax.json.JsonObject;
 32  
 import javax.json.stream.JsonParser;
 33  
 import javax.json.stream.JsonParserFactory;
 34  
 
 35  
 class JsonParserFactoryImpl implements JsonParserFactory, Serializable {
 36  
     public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy";
 37  
     public static final String MAX_STRING_LENGTH = "org.apache.johnzon.max-string-length";
 38  
     public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer";
 39  1
     public static final int DEFAULT_MAX_SIZE = Integer.getInteger(MAX_STRING_LENGTH, 8192*32); //TODO check default string length/buffer size
 40  
 
 41  315
     private final Map<String, Object> internalConfig = new HashMap<String, Object>();
 42  1
     private static final String[] SUPPORTED_CONFIG_KEYS = new String[] {
 43  
         
 44  
         BUFFER_STRATEGY, MAX_STRING_LENGTH, BUFFER_LENGTH
 45  
         
 46  
     };
 47  
       
 48  
     private final int maxSize;
 49  
     private final BufferStrategy.BufferProvider<char[]> bufferProvider;
 50  
     private final BufferStrategy.BufferProvider<char[]> valueBufferProvider;
 51  
 
 52  315
     JsonParserFactoryImpl(final Map<String, ?> config) {
 53  
         
 54  
 
 55  315
         if(config != null) {
 56  
             
 57  1252
             for (String configKey : SUPPORTED_CONFIG_KEYS) {
 58  939
                 if(config.containsKey(configKey)) {
 59  313
                     internalConfig.put(configKey, config.get(configKey));
 60  
                 }
 61  
             }
 62  
         } 
 63  
         
 64  
 
 65  315
         final int bufferSize = getInt(BUFFER_LENGTH);
 66  315
         if (bufferSize <= 0) {
 67  1
             throw new IllegalArgumentException("buffer length must be greater than zero");
 68  
         }
 69  
 
 70  314
         this.maxSize = getInt(MAX_STRING_LENGTH);
 71  314
         this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
 72  314
         this.valueBufferProvider = getBufferProvider().newCharProvider(maxSize);
 73  314
     }
 74  
 
 75  
     private BufferStrategy getBufferProvider() {
 76  628
         final Object name = internalConfig.get(BUFFER_STRATEGY);
 77  628
         if (name != null) {
 78  0
             return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
 79  
         }
 80  628
         return BufferStrategy.QUEUE;
 81  
     }
 82  
 
 83  
     private int getInt(final String key) {
 84  629
         final Object maxStringSize = internalConfig.get(key);
 85  629
         if (maxStringSize == null) {
 86  316
             return DEFAULT_MAX_SIZE;
 87  313
         } else if (Number.class.isInstance(maxStringSize)) {
 88  4
             return Number.class.cast(maxStringSize).intValue();
 89  
         }
 90  309
         return Integer.parseInt(maxStringSize.toString());
 91  
     }
 92  
 
 93  
     private JsonParser getDefaultJsonParserImpl(final InputStream in) {
 94  
         //UTF Auto detection RFC 4627
 95  278
         return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
 96  
     }
 97  
 
 98  
     private JsonParser getDefaultJsonParserImpl(final InputStream in, final Charset charset) {
 99  
         //use provided charset
 100  55
         return new JsonStreamParserImpl(in, charset, maxSize, bufferProvider, valueBufferProvider);
 101  
     }
 102  
 
 103  
     private JsonParser getDefaultJsonParserImpl(final Reader in) {
 104  
         //no charset necessary
 105  126
         return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider);
 106  
     }
 107  
 
 108  
     @Override
 109  
     public JsonParser createParser(final Reader reader) {
 110  119
         return getDefaultJsonParserImpl(reader);
 111  
     }
 112  
 
 113  
     @Override
 114  
     public JsonParser createParser(final InputStream in) {
 115  194
         return getDefaultJsonParserImpl(in);
 116  
     }
 117  
 
 118  
     @Override
 119  
     public JsonParser createParser(final InputStream in, final Charset charset) {
 120  1
         return getDefaultJsonParserImpl(in, charset);
 121  
     }
 122  
 
 123  
     @Override
 124  
     public JsonParser createParser(final JsonObject obj) {
 125  1
         return new JsonInMemoryParser(obj);
 126  
     }
 127  
 
 128  
     @Override
 129  
     public JsonParser createParser(final JsonArray array) {
 130  1
         return new JsonInMemoryParser(array);
 131  
     }
 132  
 
 133  
     @Override
 134  
     public Map<String, ?> getConfigInUse() {
 135  0
         return Collections.unmodifiableMap(internalConfig);
 136  
     }
 137  
 
 138  
     public JsonParser createInternalParser(final InputStream in) {
 139  84
         return getDefaultJsonParserImpl(in);
 140  
     }
 141  
     
 142  
     public JsonParser createInternalParser(final InputStream in, final Charset charset) {
 143  54
         return getDefaultJsonParserImpl(in, charset);
 144  
     }
 145  
 
 146  
     public JsonParser createInternalParser(final Reader reader) {
 147  7
         return getDefaultJsonParserImpl(reader);
 148  
     }
 149  
 }