Refine search
private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) { Serializer serializer = new Serializer(new Emitter(output, dumperOptions), resolver, dumperOptions, rootTag); try { serializer.open(); while (data.hasNext()) { Node node = representer.represent(data.next()); serializer.serialize(node); } serializer.close(); } catch (IOException e) { throw new YAMLException(e); } }
@Override public void close() throws IOException { if (!isClosed()) { _emitter.emit(new DocumentEndEvent(null, null, false)); _emitter.emit(new StreamEndEvent(null, null)); super.close(); /* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close() * on the underlying Reader, unless we "own" it, or auto-closing * feature is enabled. * One downside: when using UTF8Writer, underlying buffer(s) * may not be properly recycled if we don't close the writer. */ if (_writer != null) { if (_ioContext.isResourceManaged() || isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) { _writer.close(); } else if (isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM)) { // If we can't close it, we should at least flush _writer.flush(); } } } }
private boolean checkSimpleKey() { int length = 0; if (event instanceof NodeEvent && ((NodeEvent) event).getAnchor() != null) { if (preparedAnchor == null) { preparedAnchor = prepareAnchor(((NodeEvent) event).getAnchor()); } length += preparedAnchor.length(); } String tag = null; if (event instanceof ScalarEvent) { tag = ((ScalarEvent) event).getTag(); } else if (event instanceof CollectionStartEvent) { tag = ((CollectionStartEvent) event).getTag(); } if (tag != null) { if (preparedTag == null) { preparedTag = prepareTag(tag); } length += preparedTag.length(); } if (event instanceof ScalarEvent) { if (analysis == null) { analysis = analyzeScalar(((ScalarEvent) event).getValue()); } length += analysis.scalar.length(); } return length < 128 && (event instanceof AliasEvent || (event instanceof ScalarEvent && !analysis.empty && !analysis.multiline) || checkEmptySequence() || checkEmptyMapping()); }
private void processScalar() throws IOException { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); style = chooseScalarStyle(); writePlain(analysis.scalar, split); } else { switch (style) { case DOUBLE_QUOTED: writeDoubleQuoted(analysis.scalar, split); break; case SINGLE_QUOTED: writeSingleQuoted(analysis.scalar, split); break; case FOLDED: writeFolded(analysis.scalar, split); break; case LITERAL: writeLiteral(analysis.scalar); break; default:
private void expectNode(boolean root, boolean mapping, boolean simpleKey) throws IOException { rootContext = root; mappingContext = mapping; simpleKeyContext = simpleKey; if (event instanceof AliasEvent) { expectAlias(); } else if (event instanceof ScalarEvent || event instanceof CollectionStartEvent) { processAnchor("&"); processTag(); if (event instanceof ScalarEvent) { expectScalar(); } else if (event instanceof SequenceStartEvent) { if (flowLevel != 0 || canonical || ((SequenceStartEvent) event).isFlow() || checkEmptySequence()) { expectFlowSequence(); } else { expectBlockSequence(); } } else {// MappingStartEvent if (flowLevel != 0 || canonical || ((MappingStartEvent) event).isFlow() || checkEmptyMapping()) { expectFlowMapping(); } else { expectBlockMapping(); } } } else { throw new EmitterException("expected NodeEvent, but got " + event); } }
void writeLiteral(String text) throws IOException { String hints = determineBlockHints(text); writeIndicator("|" + hints, true, false, false); if (hints.length() > 0 && (hints.charAt(hints.length() - 1)) == '+') { openEnded = true; writeLineBreak(null); boolean breaks = true; int start = 0, end = 0; for (char br : data.toCharArray()) { if (br == '\n') { writeLineBreak(null); } else { writeLineBreak(String.valueOf(br)); writeIndent(); stream.write(text, start, end - start); if (ch == 0) { writeLineBreak(null);
private Character chooseScalarStyle() { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); } if (ev.getStyle() != null && ev.getStyle() == '"' || this.canonical) { return '"'; } if (ev.getStyle() == null && ev.getImplicit().canOmitTagInPlainScalar()) { if (!(simpleKeyContext && (analysis.empty || analysis.multiline)) && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) { return null; } } if (ev.getStyle() != null && (ev.getStyle() == '|' || ev.getStyle() == '>')) { if (flowLevel == 0 && !simpleKeyContext && analysis.allowBlock) { return ev.getStyle(); } } if (ev.getStyle() == null || ev.getStyle() == '\'') { if (analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline)) { return '\''; } } return '"'; }
if (event instanceof ScalarEvent) { ScalarEvent ev = (ScalarEvent) event; tag = ev.getTag(); if (style == null) { style = chooseScalarStyle(); if ((!canonical || tag == null) && ((style == null && ev.getImplicit() .canOmitTagInPlainScalar()) || (style != null && ev.getImplicit() .canOmitTagInNonPlainScalar()))) { preparedTag = null; preparedTag = prepareTag(tag); writeIndicator(preparedTag, true, false, false); preparedTag = null;
private DumperOptions.ScalarStyle chooseScalarStyle() { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); } if (!ev.isPlain() && ev.getScalarStyle() == DumperOptions.ScalarStyle.DOUBLE_QUOTED || this.canonical) { return DumperOptions.ScalarStyle.DOUBLE_QUOTED; } if (ev.isPlain() && ev.getImplicit().canOmitTagInPlainScalar()) { if (!(simpleKeyContext && (analysis.empty || analysis.multiline)) && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) { return null; } } if (!ev.isPlain() && (ev.getScalarStyle() == DumperOptions.ScalarStyle.LITERAL || ev.getScalarStyle() == DumperOptions.ScalarStyle.FOLDED)) { if (flowLevel == 0 && !simpleKeyContext && analysis.allowBlock) { return ev.getScalarStyle(); } } if (ev.isPlain() || ev.getScalarStyle() == DumperOptions.ScalarStyle.SINGLE_QUOTED) { if (analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline)) { return DumperOptions.ScalarStyle.SINGLE_QUOTED; } } return DumperOptions.ScalarStyle.DOUBLE_QUOTED; }
public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures, ObjectCodec codec, Writer out, org.yaml.snakeyaml.DumperOptions.Version version) throws IOException { super(jsonFeatures, codec); _ioContext = ctxt; _formatFeatures = 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)); }
Writer writer = new OutputStreamWriter(os)) { Yaml yaml = new Yaml(); Emitter emitter = new Emitter(writer, new DumperOptions()); boolean rewrite = false; for (Event event : yaml.parse(reader)) { event = maybeSetAbsoluteUrlAsRelative(scalarEvent); else if (URLS.equals(scalarEvent.getValue())) { rewrite = true; rewrite = false; emitter.emit(event);
protected void _writeScalar(String value, String type, Character style) throws IOException { _emitter.emit(_scalarEvent(value, style)); }
private void _writeScalarBinary(Base64Variant b64variant, byte[] data) throws IOException { // 15-Dec-2017, tatu: as per [dataformats-text#62], can not use SnakeYAML's internal // codec. Also: force use of linefeed variant if using default if (b64variant == Base64Variants.getDefaultVariant()) { b64variant = Base64Variants.MIME; } String encoded = b64variant.encode(data); _emitter.emit(new ScalarEvent(null, TAG_BINARY, EXPLICIT_TAGS, encoded, null, null, STYLE_BASE64)); }
void writeFolded(String text, boolean split) throws IOException { String hints = determineBlockHints(text); writeIndicator(">" + hints, true, false, false); if (hints.length() > 0 && (hints.charAt(hints.length() - 1) == '+')) { openEnded = true; writeLineBreak(null); boolean leadingSpace = true; boolean spaces = false; if (ch == 0 || Constant.LINEBR.hasNo(ch)) { if (!leadingSpace && ch != 0 && ch != ' ' && text.charAt(start) == '\n') { writeLineBreak(null); for (char br : data.toCharArray()) { if (br == '\n') { writeLineBreak(null); } else { writeLineBreak(String.valueOf(br)); writeIndent(); if (ch != ' ') { if (start + 1 == end && this.column > this.bestWidth && split) { writeIndent(); } else { int len = end - start; this.column += len; stream.write(text, start, len);
private void processScalar() throws IOException { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); style = chooseScalarStyle(); writePlain(analysis.scalar, split); } else { switch (style) { case '"': writeDoubleQuoted(analysis.scalar, split); break; case '\'': writeSingleQuoted(analysis.scalar, split); break; case '>': writeFolded(analysis.scalar, split); break; case '|': writeLiteral(analysis.scalar); break; default:
private void expectNode(boolean root, boolean mapping, boolean simpleKey) throws IOException { rootContext = root; mappingContext = mapping; simpleKeyContext = simpleKey; if (event instanceof AliasEvent) { expectAlias(); } else if (event instanceof ScalarEvent || event instanceof CollectionStartEvent) { processAnchor("&"); processTag(); if (event instanceof ScalarEvent) { expectScalar(); } else if (event instanceof SequenceStartEvent) { if (flowLevel != 0 || canonical || ((SequenceStartEvent) event).isFlow() || checkEmptySequence()) { expectFlowSequence(); } else { expectBlockSequence(); } } else {// MappingStartEvent if (flowLevel != 0 || canonical || ((MappingStartEvent) event).isFlow() || checkEmptyMapping()) { expectFlowMapping(); } else { expectBlockMapping(); } } } else { throw new EmitterException("expected NodeEvent, but got " + event); } }
private Character chooseScalarStyle() { ScalarEvent ev = (ScalarEvent) event; if (analysis == null) { analysis = analyzeScalar(ev.getValue()); } if (ev.getStyle() != null && ev.getStyle() == '"' || this.canonical) { return '"'; } if (ev.getStyle() == null && ev.getImplicit().canOmitTagInPlainScalar()) { if (!(simpleKeyContext && (analysis.empty || analysis.multiline)) && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) { return null; } } if (ev.getStyle() != null && (ev.getStyle() == '|' || ev.getStyle() == '>')) { if (flowLevel == 0 && !simpleKeyContext && analysis.allowBlock) { return ev.getStyle(); } } if (ev.getStyle() == null || ev.getStyle() == '\'') { if (analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline)) { return '\''; } } return '"'; }
if (event instanceof ScalarEvent) { ScalarEvent ev = (ScalarEvent) event; tag = ev.getTag(); if (style == null) { style = chooseScalarStyle(); if ((!canonical || tag == null) && ((style == null && ev.getImplicit() .canOmitTagInPlainScalar()) || (style != null && ev.getImplicit() .canOmitTagInNonPlainScalar()))) { preparedTag = null; preparedTag = prepareTag(tag); writeIndicator(preparedTag, true, false, false); preparedTag = null;
public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures, ObjectCodec codec, Writer out, org.yaml.snakeyaml.DumperOptions.Version version) throws IOException { super(jsonFeatures, codec); _ioContext = ctxt; _formatFeatures = 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)); }