Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Add new feature LITERAL_BLOCK_STYLE #76

Merged
merged 2 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum Feature // implements FormatFeature // for 2.7
/**
* Whether we are to write an explicit document start marker ("---")
* or not.
*
*
* @since 2.3
*/
WRITE_DOC_START_MARKER(true),
Expand All @@ -38,17 +38,17 @@ public enum Feature // implements FormatFeature // for 2.7
* or "generic" Object Id mechanism (false). Former works better for systems that
* are YAML-centric; latter may be better choice for interoperability, when
* converting between formats or accepting other formats.
*
*
* @since 2.5
*/
USE_NATIVE_OBJECT_ID(true),

/**
* Whether to use YAML native Type Id construct for indicating type (true);
* or "generic" type property (false). Former works better for systems that
* are YAML-centric; latter may be better choice for interoperability, when
* converting between formats or accepting other formats.
*
*
* @since 2.5
*/
USE_NATIVE_TYPE_ID(true),
Expand Down Expand Up @@ -80,7 +80,7 @@ public enum Feature // implements FormatFeature // for 2.7
* @since 2.7
*/
MINIMIZE_QUOTES(false),

/**
* Whether numbers stored as strings will be rendered with quotes (true) or
* without quotes (false, default) when MINIMIZE_QUOTES is enabled.
Expand All @@ -91,12 +91,21 @@ public enum Feature // implements FormatFeature // for 2.7
*
* @since 2.8.2
*/
ALWAYS_QUOTE_NUMBERS_AS_STRINGS(false)
ALWAYS_QUOTE_NUMBERS_AS_STRINGS(false),

/**
* Whether for string containing newlines a <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>
* should be used. This automatically enabled when {@link #MINIMIZE_QUOTES} is set.
* <p>
* The content of such strings is limited to printable characters according to the rules of
* <a href="http://www.yaml.org/spec/1.2/spec.html#style/block/literal">literal block style</a>.
*/
LITERAL_BLOCK_STYLE(false)
;

protected final boolean _defaultState;
protected final int _mask;

/**
* Method that calculates bit set (flags) of all features that
* are enabled by default.
Expand All @@ -111,14 +120,14 @@ public static int collectDefaults()
}
return flags;
}

private Feature(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}

public boolean enabledByDefault() { return _defaultState; }
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
public int getMask() { return _mask; }
}

Expand All @@ -127,7 +136,7 @@ private Feature(boolean defaultState) {
/* Internal constants
/**********************************************************
*/

protected final static long MIN_INT_AS_LONG = (long) Integer.MIN_VALUE;
protected final static long MAX_INT_AS_LONG = (long) Integer.MAX_VALUE;
protected final static Pattern PLAIN_NUMBER_P = Pattern.compile("[0-9]*(\\.[0-9]*)?");
Expand All @@ -153,7 +162,7 @@ private Feature(boolean defaultState) {

// for field names, leave out quotes
private final static Character STYLE_NAME = null;

// numbers, booleans, should use implicit
private final static Character STYLE_SCALAR = null;
// Strings quoted for fun
Expand Down Expand Up @@ -185,7 +194,7 @@ private Feature(boolean defaultState) {
* need to output one.
*/
protected String _typeId;

/*
/**********************************************************
/* Life-cycle
Expand All @@ -203,14 +212,14 @@ public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures,
_writer = out;

_outputOptions = buildDumperOptions(jsonFeatures, yamlFeatures, version);

_emitter = new Emitter(_writer, _outputOptions);
// should we start output now, or try to defer?
_emitter.emit(new StreamStartEvent(null, null));
Map<String,String> noTags = Collections.emptyMap();

boolean startMarker = Feature.WRITE_DOC_START_MARKER.enabledIn(yamlFeatures);

_emitter.emit(new DocumentStartEvent(null, null, startMarker,
version, // for 1.10 was: ((version == null) ? null : version.getArray()),
noTags));
Expand All @@ -232,10 +241,10 @@ protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures, o
return opt;
}

/*
/**********************************************************
/* Versioned
/**********************************************************
/*
/**********************************************************
/* Versioned
/**********************************************************
*/

@Override
Expand Down Expand Up @@ -291,7 +300,7 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) {
_formatFeatures = (_formatFeatures & ~mask) | (values & mask);
return this;
}

@Override
public boolean canUseSchema(FormatSchema schema) {
return false;
Expand Down Expand Up @@ -330,13 +339,13 @@ public YAMLGenerator configure(Feature f, boolean state) {
}
return this;
}

/*
/**********************************************************************
/* Overridden methods; writing field names
/**********************************************************************
*/

/* And then methods overridden to make final, streamline some
* aspects...
*/
Expand Down Expand Up @@ -389,7 +398,7 @@ public final void flush() throws IOException
{
_writer.flush();
}

@Override
public void close() throws IOException
{
Expand All @@ -406,7 +415,7 @@ public void close() throws IOException
/* Public API: structural output
/**********************************************************
*/

@Override
public final void writeStartArray() throws IOException
{
Expand All @@ -422,15 +431,15 @@ public final void writeStartArray() throws IOException
_emitter.emit(new SequenceStartEvent(anchor, yamlTag,
implicit, null, null, style));
}

@Override
public final void writeEndArray() throws IOException
{
if (!_writeContext.inArray()) {
_reportError("Current context not Array but "+_writeContext.typeDesc());
}
// just to make sure we don't "leak" type ids
_typeId = null;
_typeId = null;
_writeContext = _writeContext.getParent();
_emitter.emit(new SequenceEndEvent(null, null));
}
Expand Down Expand Up @@ -458,7 +467,7 @@ public final void writeEndObject() throws IOException
_reportError("Current context not Object but "+_writeContext.typeDesc());
}
// just to make sure we don't "leak" type ids
_typeId = null;
_typeId = null;
_writeContext = _writeContext.getParent();
_emitter.emit(new MappingEndEvent(null, null));
}
Expand Down Expand Up @@ -488,6 +497,8 @@ public void writeString(String text) throws IOException,JsonGenerationException
} else {
style = STYLE_PLAIN;
}
} else if (Feature.LITERAL_BLOCK_STYLE.enabledIn(_formatFeatures) && text.indexOf('\n') >= 0) {
style = STYLE_LITERAL;
}
_writeScalar(text, "string", style);
}
Expand Down Expand Up @@ -565,7 +576,7 @@ public void writeRawValue(char[] text, int offset, int len) throws IOException {
/* Output method implementations, base64-encoded binary
/**********************************************************
*/

@Override
public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len) throws IOException
{
Expand Down Expand Up @@ -624,13 +635,13 @@ public void writeNumber(BigInteger v) throws IOException
_verifyValueWrite("write number");
_writeScalar(String.valueOf(v.toString()), "java.math.BigInteger", STYLE_SCALAR);
}

@Override
public void writeNumber(double d) throws IOException
{
_verifyValueWrite("write number");
_writeScalar(String.valueOf(d), "double", STYLE_SCALAR);
}
}

@Override
public void writeNumber(float f) throws IOException
Expand Down Expand Up @@ -681,14 +692,14 @@ public boolean canWriteObjectId() {
// yes, YAML does support Native Type Ids!
// 10-Sep-2014, tatu: Except as per [#23] might not want to...
return Feature.USE_NATIVE_OBJECT_ID.enabledIn(_formatFeatures);
}
}

@Override
public boolean canWriteTypeId() {
// yes, YAML does support Native Type Ids!
// 10-Sep-2014, tatu: Except as per [#22] might not want to...
return Feature.USE_NATIVE_TYPE_ID.enabledIn(_formatFeatures);
}
}

@Override
public void writeTypeId(Object id)
Expand All @@ -706,7 +717,7 @@ public void writeObjectRef(Object id)
AliasEvent evt = new AliasEvent(String.valueOf(id), null, null);
_emitter.emit(evt);
}

@Override
public void writeObjectId(Object id)
throws IOException
Expand Down Expand Up @@ -749,7 +760,7 @@ protected void _writeScalar(String value, String type, Character style) throws I
{
_emitter.emit(_scalarEvent(value, style));
}

protected ScalarEvent _scalarEvent(String value, Character style)
{
String yamlTag = _typeId;
Expand Down
Loading