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.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); |
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 | |
|
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 | |
|
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 | |
|
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 | |
|
152 | 1 | return new JsonInMemoryParser(obj); |
153 | |
} |
154 | |
|
155 | |
@Override |
156 | |
public JsonParser createParser(final JsonArray array) { |
157 | |
|
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 | |
} |