| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.net.URL; |
| 5 |
|
| 6 |
import com.fasterxml.jackson.core.*; |
| 7 |
import com.fasterxml.jackson.core.format.InputAccessor; |
| 8 |
import com.fasterxml.jackson.core.format.MatchStrength; |
| 9 |
import com.fasterxml.jackson.core.io.IOContext; |
| 10 |
|
| 11 |
/** |
| 12 |
* Factory used for constructing {@link SmileParser} and {@link SmileGenerator} |
| 13 |
* instances; both of which handle |
| 14 |
* <a href="http://wiki.fasterxml.com/SmileFormat">Smile</a> encoded data. |
| 15 |
*<p> |
| 16 |
* Extends {@link JsonFactory} mostly so that users can actually use it in place |
| 17 |
* of regular non-Smile factory instances. |
| 18 |
*<p> |
| 19 |
* Note on using non-byte-based sources/targets (char based, like |
| 20 |
* {@link java.io.Reader} and {@link java.io.Writer}): these can not be |
| 21 |
* used for Smile-format documents, and thus will either downgrade to |
| 22 |
* textual JSON (when parsing), or throw exception (when trying to create |
| 23 |
* generator). |
| 24 |
* |
| 25 |
* @author Tatu Saloranta |
| 26 |
*/ |
| 27 |
public class SmileFactory extends JsonFactory |
| 28 |
{ |
| 29 |
private static final long serialVersionUID = 1L; // since 2.6 |
| 30 |
|
| 31 |
/* |
| 32 |
/********************************************************** |
| 33 |
/* Constants |
| 34 |
/********************************************************** |
| 35 |
*/ |
| 36 |
|
| 37 |
/** |
| 38 |
* Name used to identify Smile format. |
| 39 |
* (and returned by {@link #getFormatName()} |
| 40 |
*/ |
| 41 |
public final static String FORMAT_NAME_SMILE = "Smile"; |
| 42 |
|
| 43 |
/** |
| 44 |
* Bitfield (set of flags) of all parser features that are enabled |
| 45 |
* by default. |
| 46 |
*/ |
| 47 |
final static int DEFAULT_SMILE_PARSER_FEATURE_FLAGS = SmileParser.Feature.collectDefaults(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Bitfield (set of flags) of all generator features that are enabled |
| 51 |
* by default. |
| 52 |
*/ |
| 53 |
final static int DEFAULT_SMILE_GENERATOR_FEATURE_FLAGS = SmileGenerator.Feature.collectDefaults(); |
| 54 |
|
| 55 |
/* |
| 56 |
/********************************************************** |
| 57 |
/* Configuration |
| 58 |
/********************************************************** |
| 59 |
*/ |
| 60 |
|
| 61 |
/** |
| 62 |
* Whether non-supported methods (ones trying to output using |
| 63 |
* char-based targets like {@link java.io.Writer}, for example) |
| 64 |
* should be delegated to regular Jackson JSON processing |
| 65 |
* (if set to true); or throw {@link UnsupportedOperationException} |
| 66 |
* (if set to false) |
| 67 |
*/ |
| 68 |
protected boolean _cfgDelegateToTextual; |
| 69 |
|
| 70 |
protected int _smileParserFeatures; |
| 71 |
protected int _smileGeneratorFeatures; |
| 72 |
|
| 73 |
/* |
| 74 |
/********************************************************** |
| 75 |
/* Factory construction, configuration |
| 76 |
/********************************************************** |
| 77 |
*/ |
| 78 |
|
| 79 |
/** |
| 80 |
* Default constructor used to create factory instances. |
| 81 |
* Creation of a factory instance is a light-weight operation, |
| 82 |
* but it is still a good idea to reuse limited number of |
| 83 |
* factory instances (and quite often just a single instance): |
| 84 |
* factories are used as context for storing some reused |
| 85 |
* processing objects (such as symbol tables parsers use) |
| 86 |
* and this reuse only works within context of a single |
| 87 |
* factory instance. |
| 88 |
*/ |
| 89 |
public SmileFactory() { this(null); } |
| 90 |
|
| 91 |
public SmileFactory(ObjectCodec oc) { |
| 92 |
super(oc); |
| 93 |
_smileParserFeatures = DEFAULT_SMILE_PARSER_FEATURE_FLAGS; |
| 94 |
_smileGeneratorFeatures = DEFAULT_SMILE_GENERATOR_FEATURE_FLAGS; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Note: REQUIRES 2.2.1 -- unfortunate intra-patch dep but seems |
| 99 |
* preferable to just leaving bug be as is |
| 100 |
* |
| 101 |
* @since 2.2.1 |
| 102 |
*/ |
| 103 |
public SmileFactory(SmileFactory src, ObjectCodec oc) |
| 104 |
{ |
| 105 |
super(src, oc); |
| 106 |
_cfgDelegateToTextual = src._cfgDelegateToTextual; |
| 107 |
_smileParserFeatures = src._smileParserFeatures; |
| 108 |
_smileGeneratorFeatures = src._smileGeneratorFeatures; |
| 109 |
} |
| 110 |
|
| 111 |
// @since 2.1 |
| 112 |
@Override |
| 113 |
public SmileFactory copy() |
| 114 |
{ |
| 115 |
_checkInvalidCopy(SmileFactory.class); |
| 116 |
// note: as with base class, must NOT copy mapper reference |
| 117 |
return new SmileFactory(this, null); |
| 118 |
} |
| 119 |
|
| 120 |
public void delegateToTextual(boolean state) { |
| 121 |
_cfgDelegateToTextual = state; |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
/********************************************************** |
| 126 |
/* Serializable overrides |
| 127 |
/********************************************************** |
| 128 |
*/ |
| 129 |
|
| 130 |
/** |
| 131 |
* Method that we need to override to actually make restoration go |
| 132 |
* through constructors etc. |
| 133 |
* Also: must be overridden by sub-classes as well. |
| 134 |
*/ |
| 135 |
@Override |
| 136 |
protected Object readResolve() { |
| 137 |
return new SmileFactory(this, _objectCodec); |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
/********************************************************** |
| 142 |
/* Versioned |
| 143 |
/********************************************************** |
| 144 |
*/ |
| 145 |
|
| 146 |
@Override |
| 147 |
public Version version() { |
| 148 |
return PackageVersion.VERSION; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
/********************************************************** |
| 153 |
/* Format detection functionality |
| 154 |
/********************************************************** |
| 155 |
*/ |
| 156 |
|
| 157 |
@Override |
| 158 |
public String getFormatName() { |
| 159 |
return FORMAT_NAME_SMILE; |
| 160 |
} |
| 161 |
|
| 162 |
// Defaults work fine for this: |
| 163 |
// public boolean canUseSchema(FormatSchema schema) { } |
| 164 |
|
| 165 |
/** |
| 166 |
* Sub-classes need to override this method (as of 1.8) |
| 167 |
*/ |
| 168 |
@Override |
| 169 |
public MatchStrength hasFormat(InputAccessor acc) throws IOException { |
| 170 |
return SmileParserBootstrapper.hasSmileFormat(acc); |
| 171 |
} |
| 172 |
|
| 173 |
/* |
| 174 |
/********************************************************** |
| 175 |
/* Capability introspection |
| 176 |
/********************************************************** |
| 177 |
*/ |
| 178 |
|
| 179 |
@Override |
| 180 |
public boolean canHandleBinaryNatively() { |
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
@Override // since 2.6 |
| 185 |
public Class<SmileParser.Feature> getFormatReadFeatureType() { |
| 186 |
return SmileParser.Feature.class; |
| 187 |
} |
| 188 |
|
| 189 |
@Override // since 2.6 |
| 190 |
public Class<SmileGenerator.Feature> getFormatWriteFeatureType() { |
| 191 |
return SmileGenerator.Feature.class; |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
/********************************************************** |
| 196 |
/* Configuration, parser settings |
| 197 |
/********************************************************** |
| 198 |
*/ |
| 199 |
|
| 200 |
/** |
| 201 |
* Method for enabling or disabling specified parser feature |
| 202 |
* (check {@link SmileParser.Feature} for list of features) |
| 203 |
*/ |
| 204 |
public final SmileFactory configure(SmileParser.Feature f, boolean state) |
| 205 |
{ |
| 206 |
if (state) { |
| 207 |
enable(f); |
| 208 |
} else { |
| 209 |
disable(f); |
| 210 |
} |
| 211 |
return this; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Method for enabling specified parser feature |
| 216 |
* (check {@link SmileParser.Feature} for list of features) |
| 217 |
*/ |
| 218 |
public SmileFactory enable(SmileParser.Feature f) { |
| 219 |
_smileParserFeatures |= f.getMask(); |
| 220 |
return this; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Method for disabling specified parser features |
| 225 |
* (check {@link SmileParser.Feature} for list of features) |
| 226 |
*/ |
| 227 |
public SmileFactory disable(SmileParser.Feature f) { |
| 228 |
_smileParserFeatures &= ~f.getMask(); |
| 229 |
return this; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Checked whether specified parser feature is enabled. |
| 234 |
*/ |
| 235 |
public final boolean isEnabled(SmileParser.Feature f) { |
| 236 |
return (_smileParserFeatures & f.getMask()) != 0; |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
/********************************************************** |
| 241 |
/* Configuration, generator settings |
| 242 |
/********************************************************** |
| 243 |
*/ |
| 244 |
|
| 245 |
/** |
| 246 |
* Method for enabling or disabling specified generator feature |
| 247 |
* (check {@link SmileGenerator.Feature} for list of features) |
| 248 |
* |
| 249 |
* @since 1.2 |
| 250 |
*/ |
| 251 |
public final SmileFactory configure(SmileGenerator.Feature f, boolean state) { |
| 252 |
if (state) { |
| 253 |
enable(f); |
| 254 |
} else { |
| 255 |
disable(f); |
| 256 |
} |
| 257 |
return this; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
/** |
| 262 |
* Method for enabling specified generator features |
| 263 |
* (check {@link SmileGenerator.Feature} for list of features) |
| 264 |
*/ |
| 265 |
public SmileFactory enable(SmileGenerator.Feature f) { |
| 266 |
_smileGeneratorFeatures |= f.getMask(); |
| 267 |
return this; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Method for disabling specified generator feature |
| 272 |
* (check {@link SmileGenerator.Feature} for list of features) |
| 273 |
*/ |
| 274 |
public SmileFactory disable(SmileGenerator.Feature f) { |
| 275 |
_smileGeneratorFeatures &= ~f.getMask(); |
| 276 |
return this; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Check whether specified generator feature is enabled. |
| 281 |
*/ |
| 282 |
public final boolean isEnabled(SmileGenerator.Feature f) { |
| 283 |
return (_smileGeneratorFeatures & f.getMask()) != 0; |
| 284 |
} |
| 285 |
|
| 286 |
/* |
| 287 |
/********************************************************** |
| 288 |
/* Overridden parser factory methods: only override methods |
| 289 |
/* that can use co-variance (to return SmileParser) |
| 290 |
/********************************************************** |
| 291 |
*/ |
| 292 |
|
| 293 |
@SuppressWarnings("resource") |
| 294 |
@Override |
| 295 |
public SmileParser createParser(File f) throws IOException { |
| 296 |
IOContext ctxt = _createContext(f, true); |
| 297 |
return _createParser(_decorate(new FileInputStream(f), ctxt), ctxt); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public SmileParser createParser(URL url) throws IOException { |
| 302 |
IOContext ctxt = _createContext(url, true); |
| 303 |
return _createParser(_decorate(_optimizedStreamFromURL(url), ctxt), ctxt); |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public SmileParser createParser(InputStream in) throws IOException { |
| 308 |
IOContext ctxt = _createContext(in, false); |
| 309 |
return _createParser(_decorate(in, ctxt), ctxt); |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public SmileParser createParser(byte[] data) throws IOException { |
| 314 |
return createParser(data, 0, data.length); |
| 315 |
} |
| 316 |
|
| 317 |
@SuppressWarnings("resource") |
| 318 |
@Override |
| 319 |
public SmileParser createParser(byte[] data, int offset, int len) throws IOException { |
| 320 |
IOContext ctxt = _createContext(data, true); |
| 321 |
if (_inputDecorator != null) { |
| 322 |
InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length); |
| 323 |
if (in != null) { |
| 324 |
return _createParser(_decorate(in, ctxt), ctxt); |
| 325 |
} |
| 326 |
} |
| 327 |
return _createParser(data, offset, len, ctxt); |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
/********************************************************** |
| 332 |
/* Overridden generator factory methods: mostly |
| 333 |
/* overridden for co-variance (returns SmileGenerator) |
| 334 |
/********************************************************** |
| 335 |
*/ |
| 336 |
|
| 337 |
/** |
| 338 |
* Method for constructing {@link JsonGenerator} for generating |
| 339 |
* Smile-encoded output. |
| 340 |
*<p> |
| 341 |
* Since Smile format always uses UTF-8 internally, <code>enc</code> |
| 342 |
* argument is ignored. |
| 343 |
*/ |
| 344 |
@Override |
| 345 |
public SmileGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException { |
| 346 |
// false -> we won't manage the stream unless explicitly directed to |
| 347 |
IOContext ctxt = _createContext(out, false); |
| 348 |
return _createGenerator(_decorate(out, ctxt), ctxt); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Method for constructing {@link JsonGenerator} for generating |
| 353 |
* Smile-encoded output. |
| 354 |
*<p> |
| 355 |
* Since Smile format always uses UTF-8 internally, no encoding need |
| 356 |
* to be passed to this method. |
| 357 |
*/ |
| 358 |
@Override |
| 359 |
public SmileGenerator createGenerator(OutputStream out) throws IOException { |
| 360 |
// false -> we won't manage the stream unless explicitly directed to |
| 361 |
IOContext ctxt = _createContext(out, false); |
| 362 |
return _createGenerator(_decorate(out, ctxt), ctxt); |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
/****************************************************** |
| 367 |
/* Overridden internal factory methods |
| 368 |
/****************************************************** |
| 369 |
*/ |
| 370 |
|
| 371 |
/** |
| 372 |
* Overridable factory method that actually instantiates desired parser. |
| 373 |
*/ |
| 374 |
@Override |
| 375 |
protected SmileParser _createParser(InputStream in, IOContext ctxt) throws IOException |
| 376 |
{ |
| 377 |
SmileParserBootstrapper bs = new SmileParserBootstrapper(ctxt, in); |
| 378 |
return bs.constructParser(_factoryFeatures, _parserFeatures, |
| 379 |
_smileParserFeatures, _objectCodec, _byteSymbolCanonicalizer); |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException |
| 384 |
{ |
| 385 |
if (_cfgDelegateToTextual) { |
| 386 |
return super._createParser(r, ctxt); |
| 387 |
} |
| 388 |
return _nonByteSource(); |
| 389 |
} |
| 390 |
|
| 391 |
@Override |
| 392 |
protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt, |
| 393 |
boolean recyclable) throws IOException |
| 394 |
{ |
| 395 |
if (_cfgDelegateToTextual) { |
| 396 |
return super._createParser(data, offset, len, ctxt, recyclable); |
| 397 |
} |
| 398 |
return _nonByteSource(); |
| 399 |
} |
| 400 |
|
| 401 |
@Override |
| 402 |
protected SmileParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException |
| 403 |
{ |
| 404 |
return new SmileParserBootstrapper(ctxt, data, offset, len).constructParser( |
| 405 |
_factoryFeatures, _parserFeatures, _smileParserFeatures, |
| 406 |
_objectCodec, _byteSymbolCanonicalizer); |
| 407 |
} |
| 408 |
|
| 409 |
@Override |
| 410 |
protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOException |
| 411 |
{ |
| 412 |
if (_cfgDelegateToTextual) { |
| 413 |
return super._createGenerator(out, ctxt); |
| 414 |
} |
| 415 |
return _nonByteTarget(); |
| 416 |
} |
| 417 |
|
| 418 |
@Override |
| 419 |
protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) throws IOException { |
| 420 |
return _createGenerator(out, ctxt); |
| 421 |
} |
| 422 |
|
| 423 |
//public BufferRecycler _getBufferRecycler() |
| 424 |
|
| 425 |
@Override |
| 426 |
protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException |
| 427 |
{ |
| 428 |
if (_cfgDelegateToTextual) { |
| 429 |
return super._createWriter(out, enc, ctxt); |
| 430 |
} |
| 431 |
return _nonByteTarget(); |
| 432 |
} |
| 433 |
|
| 434 |
/* |
| 435 |
/********************************************************** |
| 436 |
/* Internal methods |
| 437 |
/********************************************************** |
| 438 |
*/ |
| 439 |
|
| 440 |
protected <T> T _nonByteSource() throws IOException { |
| 441 |
throw new UnsupportedOperationException("Can not create generator for character-based (not byte-based) source"); |
| 442 |
} |
| 443 |
|
| 444 |
protected <T> T _nonByteTarget() throws IOException { |
| 445 |
throw new UnsupportedOperationException("Can not create generator for character-based (not byte-based) target"); |
| 446 |
} |
| 447 |
|
| 448 |
protected SmileGenerator _createGenerator(OutputStream out, IOContext ctxt) throws IOException |
| 449 |
{ |
| 450 |
int feats = _smileGeneratorFeatures; |
| 451 |
/* One sanity check: MUST write header if shared string values setting is enabled, |
| 452 |
* or quoting of binary data disabled. |
| 453 |
* But should we force writing, or throw exception, if settings are in conflict? |
| 454 |
* For now, let's error out... |
| 455 |
*/ |
| 456 |
SmileGenerator gen = new SmileGenerator(ctxt, _generatorFeatures, feats, _objectCodec, out); |
| 457 |
if ((feats & SmileGenerator.Feature.WRITE_HEADER.getMask()) != 0) { |
| 458 |
gen.writeHeader(); |
| 459 |
} else { |
| 460 |
if ((feats & SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES.getMask()) != 0) { |
| 461 |
throw new JsonGenerationException( |
| 462 |
"Inconsistent settings: WRITE_HEADER disabled, but CHECK_SHARED_STRING_VALUES enabled; can not construct generator" |
| 463 |
+" due to possible data loss (either enable WRITE_HEADER, or disable CHECK_SHARED_STRING_VALUES to resolve)"); |
| 464 |
} |
| 465 |
if ((feats & SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT.getMask()) == 0) { |
| 466 |
throw new JsonGenerationException( |
| 467 |
"Inconsistent settings: WRITE_HEADER disabled, but ENCODE_BINARY_AS_7BIT disabled; can not construct generator" |
| 468 |
+" due to possible data loss (either enable WRITE_HEADER, or ENCODE_BINARY_AS_7BIT to resolve)"); |
| 469 |
} |
| 470 |
} |
| 471 |
return gen; |
| 472 |
} |
| 473 |
} |