1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
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); |
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 | |
|
95 | 278 | return new JsonStreamParserImpl(in, maxSize, bufferProvider, valueBufferProvider); |
96 | |
} |
97 | |
|
98 | |
private JsonParser getDefaultJsonParserImpl(final InputStream in, final Charset charset) { |
99 | |
|
100 | 55 | return new JsonStreamParserImpl(in, charset, maxSize, bufferProvider, valueBufferProvider); |
101 | |
} |
102 | |
|
103 | |
private JsonParser getDefaultJsonParserImpl(final Reader in) { |
104 | |
|
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 | |
} |