org.yaml.snakeyaml.emitter.Emitter Java Examples

The following examples show how to use org.yaml.snakeyaml.emitter.Emitter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PyErrorsTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testEmitterErrors() {
    File[] files = getStreamsByExtension(".emitter-error");
    assertTrue("No test files found.", files.length > 0);
    for (int i = 0; i < files.length; i++) {
        String content = getResource(files[i].getName());
        List<Event> document = (List<Event>) load(new EventConstructor(), content.trim());
        Writer writer = new StringWriter();
        Emitter emitter = new Emitter(writer, new DumperOptions());
        try {
            for (Event event : document) {
                emitter.emit(event);
            }
            fail("Loading must fail for " + files[i].getAbsolutePath());
            // System.err.println("Loading must fail for " +
            // files[i].getAbsolutePath());
        } catch (Exception e) {
            assertTrue(true);
        }
    }
}
 
Example #2
Source File: VersionedYamlDoc.java    From onedev with MIT License 5 votes vote down vote up
public String toYaml() {
	StringWriter writer = new StringWriter();
	DumperOptions dumperOptions = new DumperOptions();
	Serializer serializer = new Serializer(new Emitter(writer, dumperOptions), 
			new Resolver(), dumperOptions, Tag.MAP);
	try {
		serializer.open();
		serializer.serialize(this);
		serializer.close();
		return writer.toString();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #3
Source File: IndexYamlAbsoluteUrlRewriterSupport.java    From nexus-repository-helm with Eclipse Public License 1.0 5 votes vote down vote up
protected void updateUrls(final InputStream is,
                          final OutputStream os)
{
  try (Reader reader = new InputStreamReader(is);
       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)) {
      if (event instanceof ScalarEvent) {
        ScalarEvent scalarEvent = (ScalarEvent) event;
        if (rewrite) {
          event = maybeSetAbsoluteUrlAsRelative(scalarEvent);
        }
        else if (URLS.equals(scalarEvent.getValue())) {
          rewrite = true;
        }
      }
      else if (event instanceof CollectionStartEvent) {
        // NOOP
      }
      else if (event instanceof CollectionEndEvent) {
        rewrite = false;
      }
      emitter.emit(event);
    }
  }
  catch (IOException ex) {
    log.error("Error rewriting urls in index.yaml", ex);
  }
}
 
Example #4
Source File: SoyDirectives.java    From deploymentmanager-autogen with Apache License 2.0 5 votes vote down vote up
@Override
public SoyValue applyForJava(SoyValue value, List<SoyValue> args) {
  Preconditions.checkArgument(
      value instanceof PrimitiveData || value instanceof SanitizedContent,
      "|yamlprimitive directive only supports primitive types");
  Node node;
  if (value instanceof BooleanData) {
    node = representer.represent(value.booleanValue());
  } else if (value instanceof FloatData) {
    node = representer.represent(value.floatValue());
  } else if (value instanceof IntegerData) {
    node = representer.represent(value.integerValue());
  } else {
    node = representer.represent(value.coerceToString());
  }
  int indent = args.get(0).integerValue();
  StringWriter writer = new StringWriter();
  Serializer serializer = new Serializer(
      new Emitter(writer, dumperOptions), resolver, dumperOptions, null);
  try {
    serializer.open();
    serializer.serialize(node);
    serializer.close();
    return StringData.forValue(indentLines(writer.toString().trim(), indent));
  } catch (IOException e) {
    // Should not happen.
    throw new RuntimeException(e);
  }
}
 
Example #5
Source File: ConfigurationAsCode.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@VisibleForTesting
@Restricted(NoExternalUse.class)
public static void serializeYamlNode(Node root, Writer writer) throws IOException {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(BLOCK);
    options.setDefaultScalarStyle(PLAIN);
    options.setSplitLines(true);
    options.setPrettyFlow(true);
    Serializer serializer = new Serializer(new Emitter(writer, options), new Resolver(),
            options, null);
    serializer.open();
    serializer.serialize(root);
    serializer.close();
}
 
Example #6
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
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 (java.io.IOException e) {
        throw new YAMLException(e);
    }
}
 
Example #7
Source File: DumperOptions.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public void setIndent(int indent) {
    if (indent < Emitter.MIN_INDENT) {
        throw new YAMLException("Indent must be at least " + Emitter.MIN_INDENT);
    }
    if (indent > Emitter.MAX_INDENT) {
        throw new YAMLException("Indent must be at most " + Emitter.MAX_INDENT);
    }
    this.indent = indent;
}
 
Example #8
Source File: Yaml.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #9
Source File: DumperOptions.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void setIndent(int indent) {
    if (indent < Emitter.MIN_INDENT) {
        throw new YAMLException("Indent must be at least " + Emitter.MIN_INDENT);
    }
    if (indent > Emitter.MAX_INDENT) {
        throw new YAMLException("Indent must be at most " + Emitter.MAX_INDENT);
    }
    this.indent = indent;
}
 
Example #10
Source File: DumperOptions.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void setIndicatorIndent(int indicatorIndent) {
    if (indicatorIndent < 0) {
        throw new YAMLException("Indicator indent must be non-negative.");
    }
    if (indicatorIndent > Emitter.MAX_INDENT - 1) {
        throw new YAMLException("Indicator indent must be at most Emitter.MAX_INDENT-1: " + (Emitter.MAX_INDENT - 1));
    }
    this.indicatorIndent = indicatorIndent;
}
 
Example #11
Source File: PyEmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private String emit(List<Event> events) throws IOException {
    StringWriter writer = new StringWriter();
    Emitter emitter = new Emitter(writer, new DumperOptions());
    for (Event event : events) {
        emitter.emit(event);
    }
    return writer.toString();
}
 
Example #12
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #13
Source File: SerializerTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
@Override
protected void setUp() {
    DumperOptions config = new DumperOptions();
    StringWriter writer = new StringWriter();
    serializer = new Serializer(new Emitter(writer, config), new Resolver(), config, null);
}