Coverage Report - org.apache.johnzon.core.JsonGeneratorFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonGeneratorFactoryImpl
73%
28/38
53%
14/26
3,714
 
 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.OutputStream;
 22  
 import java.io.Serializable;
 23  
 import java.io.Writer;
 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.concurrent.ConcurrentMap;
 31  
 
 32  
 import javax.json.stream.JsonGenerator;
 33  
 import javax.json.stream.JsonGeneratorFactory;
 34  
 
 35  
 import static java.util.Arrays.asList;
 36  
 
 37  
 public class JsonGeneratorFactoryImpl implements JsonGeneratorFactory, Serializable {    
 38  
     public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer-generator";
 39  1
     public static final int DEFAULT_BUFFER_LENGTH = Integer.getInteger(BUFFER_LENGTH, 1024); //TODO check default string length/buffer size
 40  3
     private final Map<String, Object> internalConfig = new HashMap<String, Object>();
 41  1
     private static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
 42  
         JsonGenerator.PRETTY_PRINTING, BUFFER_LENGTH, JsonParserFactoryImpl.BUFFER_STRATEGY
 43  
     );
 44  
     //key caching currently disabled
 45  3
     private final ConcurrentMap<String, String> cache = null;//new ConcurrentHashMap<String, String>();
 46  
     private final boolean pretty;
 47  
     private final BufferStrategy.BufferProvider<char[]> bufferProvider;
 48  
 
 49  3
     public JsonGeneratorFactoryImpl(final Map<String, ?> config) {
 50  
         
 51  3
           if(config != null) {
 52  
           
 53  1
               for (String configKey : config.keySet()) {
 54  1
                   if(SUPPORTED_CONFIG_KEYS.contains(configKey)) {
 55  1
                       internalConfig.put(configKey, config.get(configKey));
 56  
                   }
 57  1
               }
 58  
           } 
 59  
 
 60  3
           if(internalConfig.containsKey(JsonGenerator.PRETTY_PRINTING)) {
 61  1
               this.pretty = Boolean.TRUE.equals(internalConfig.get(JsonGenerator.PRETTY_PRINTING)) || "true".equals(internalConfig.get(JsonGenerator.PRETTY_PRINTING));
 62  
           } else {
 63  2
               this.pretty = false;
 64  
           }
 65  
           
 66  3
           final int bufferSize = getInt(BUFFER_LENGTH);
 67  3
           if (bufferSize <= 0) {
 68  0
               throw new IllegalArgumentException("buffer length must be greater than zero");
 69  
           }
 70  
 
 71  3
           this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
 72  3
     }
 73  
     
 74  
     private BufferStrategy getBufferProvider() {
 75  3
         final Object name = internalConfig.get(JsonParserFactoryImpl.BUFFER_STRATEGY);
 76  3
         if (name != null) {
 77  0
             return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
 78  
         }
 79  3
         return BufferStrategy.QUEUE;
 80  
     }
 81  
 
 82  
     private int getInt(final String key) {
 83  3
         final Object maxStringSize = internalConfig.get(key);
 84  3
         if (maxStringSize == null) {
 85  3
             return DEFAULT_BUFFER_LENGTH;
 86  0
         } else if (Number.class.isInstance(maxStringSize)) {
 87  0
             return Number.class.cast(maxStringSize).intValue();
 88  
         }
 89  0
         return Integer.parseInt(maxStringSize.toString());
 90  
     }
 91  
 
 92  
     @Override
 93  
     public JsonGenerator createGenerator(final Writer writer) {
 94  1
         if (pretty) {
 95  0
             return new JsonPrettyGeneratorImpl(writer, bufferProvider, cache);
 96  
         }
 97  1
         return new JsonGeneratorImpl(writer, bufferProvider, cache);
 98  
     }
 99  
 
 100  
    
 101  
 
 102  
     @Override
 103  
     public JsonGenerator createGenerator(final OutputStream out) {
 104  22
         if (pretty) {
 105  1
             return new JsonPrettyGeneratorImpl(out, bufferProvider, cache);
 106  
         }
 107  21
         return new JsonGeneratorImpl(out, bufferProvider, cache);
 108  
     }
 109  
 
 110  
     @Override
 111  
     public JsonGenerator createGenerator(final OutputStream out, final Charset charset) {
 112  0
         if (pretty) {
 113  0
             return new JsonPrettyGeneratorImpl(out,charset, bufferProvider, cache);
 114  
         }
 115  0
         return new JsonGeneratorImpl(out,charset, bufferProvider, cache);
 116  
     }
 117  
 
 118  
     @Override
 119  
     public Map<String, ?> getConfigInUse() {
 120  0
         return Collections.unmodifiableMap(internalConfig);
 121  
     }
 122  
 }