Java Code Examples for org.yaml.snakeyaml.DumperOptions#setIndent()

The following examples show how to use org.yaml.snakeyaml.DumperOptions#setIndent() . 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: ClusterDefinitionService.java    From karamel with Apache License 2.0 6 votes vote down vote up
public static String jsonToYaml(JsonCluster jsonCluster) throws KaramelException {
  YamlCluster yamlCluster = new YamlCluster(jsonCluster);
  DumperOptions options = new DumperOptions();
  options.setIndent(2);
  options.setWidth(120);
  options.setExplicitEnd(false);
  options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  options.setPrettyFlow(true);
  options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
  YamlPropertyRepresenter yamlPropertyRepresenter = new YamlPropertyRepresenter();
  yamlPropertyRepresenter.addClassTag(YamlCluster.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Ec2.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Baremetal.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Gce.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Nova.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Occi.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(Cookbook.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(YamlGroup.class, Tag.MAP);
  yamlPropertyRepresenter.addClassTag(HashSet.class, Tag.MAP);
  Yaml yaml = new Yaml(yamlPropertyRepresenter, options);
  String content = yaml.dump(yamlCluster);
  return content;
}
 
Example 2
Source File: YAMLProcessor.java    From skript-yaml with MIT License 6 votes vote down vote up
public YAMLProcessor(File file, boolean writeDefaults, YAMLFormat format) {
	super(new LinkedHashMap<String, Object>(), writeDefaults);
	this.format = format;

	DumperOptions options = new FancyDumperOptions();
	options.setIndent(4);
	options.setDefaultFlowStyle(format.getStyle());
	options.setTimeZone(TimeZone.getDefault());

	//Representer representer = new SkriptYamlRepresenter();
	Representer representer = SkriptYaml.getInstance().getRepresenter();
	representer.setDefaultFlowStyle(format.getStyle());

	//yaml = new Yaml(new SkriptYamlConstructor(), representer, options);
	yaml = new Yaml(SkriptYaml.getInstance().getConstructor(), representer, options);
	
	this.file = file;
}
 
Example 3
Source File: YamlFactory.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
public static Yaml newYaml() {
  PropertyUtils propertyUtils = new AdvancedPropertyUtils();
  propertyUtils.setSkipMissingProperties(true);

  Constructor constructor = new Constructor(Federations.class);
  TypeDescription federationDescription = new TypeDescription(Federations.class);
  federationDescription.putListPropertyType("federatedMetaStores", FederatedMetaStore.class);
  constructor.addTypeDescription(federationDescription);
  constructor.setPropertyUtils(propertyUtils);

  Representer representer = new AdvancedRepresenter();
  representer.setPropertyUtils(new FieldOrderPropertyUtils());
  representer.addClassTag(Federations.class, Tag.MAP);
  representer.addClassTag(AbstractMetaStore.class, Tag.MAP);
  representer.addClassTag(WaggleDanceConfiguration.class, Tag.MAP);
  representer.addClassTag(YamlStorageConfiguration.class, Tag.MAP);
  representer.addClassTag(GraphiteConfiguration.class, Tag.MAP);

  DumperOptions dumperOptions = new DumperOptions();
  dumperOptions.setIndent(2);
  dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK);

  return new Yaml(constructor, representer, dumperOptions);
}
 
Example 4
Source File: SingleQuoteTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
private void checkQuotes(boolean isBlock, String expectation) {
    DumperOptions options = new DumperOptions();
    options.setIndent(4);
    if (isBlock) {
        options.setDefaultFlowStyle(FlowStyle.BLOCK);
    }
    Representer representer = new Representer();

    Yaml yaml = new Yaml(new SafeConstructor(), representer, options);

    LinkedHashMap<String, Object> lvl1 = new LinkedHashMap<String, Object>();
    lvl1.put("steak:cow", "11");
    LinkedHashMap<String, Object> root = new LinkedHashMap<String, Object>();
    root.put("cows", lvl1);
    String output = yaml.dump(root);
    assertEquals(expectation + "\n", output);

    // parse the value back
    @SuppressWarnings("unchecked")
    Map<String, Object> cows = (Map<String, Object>) yaml.load(output);
    @SuppressWarnings("unchecked")
    Map<String, String> cow = (Map<String, String>) cows.get("cows");
    assertEquals("11", cow.get("steak:cow"));
}
 
Example 5
Source File: EmitterTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testWriteIndicatorIndent() {
    DumperOptions options = new DumperOptions();
    options.setIndent(5);
    options.setIndicatorIndent(2);
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    List<?> topLevel = Arrays.asList(Collections.singletonMap("k1", "v1"), Collections.singletonMap("k2", "v2"));
    Map<String, ?> map = Collections.singletonMap("aaa", topLevel);
    Yaml yaml = new Yaml(options);
    String output = yaml.dump(map);
    String etalon = "aaa:\n  -  k1: v1\n  -  k2: v2\n";
    assertEquals(etalon, output);
}
 
Example 6
Source File: Saves.java    From Text-Fighter with MIT License 5 votes vote down vote up
private static void setupDumper() {
	options = new DumperOptions();
	representer = new Representer();

	options.setIndent(2);
	options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
	options.setAllowUnicode(Charset.defaultCharset().name().contains("UTF"));
	representer.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
}
 
Example 7
Source File: TypesRegistryServiceImpl.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
private String toYaml(Object obj) {
    DumperOptions options = new DumperOptions();
    options.setAllowReadOnlyProperties(true);
    options.setDefaultFlowStyle(FlowStyle.BLOCK);
    options.setIndent(4);
    return new Yaml(options).dump(obj);
}
 
Example 8
Source File: YamlTranslator.java    From mdw with Apache License 2.0 5 votes vote down vote up
protected DumperOptions getDumperOptions() {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setPrettyFlow(true);
    options.setIndent(2);
    return options;
}
 
Example 9
Source File: YamlBuilder.java    From mdw with Apache License 2.0 5 votes vote down vote up
protected DumperOptions getDumperOptions() {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setPrettyFlow(true);
    options.setIndent(indent);
    options.setSplitLines(false);
    return options;
}
 
Example 10
Source File: Yamlable.java    From mdw with Apache License 2.0 5 votes vote down vote up
static String toString(Yamlable yamlable, int indent) {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    options.setPrettyFlow(true);
    options.setIndent(indent);
    options.setSplitLines(false);
    return new Yaml(options).dump(yamlable.getYaml());
}
 
Example 11
Source File: YamlLoader.java    From mdw with Apache License 2.0 5 votes vote down vote up
protected DumperOptions getDumperOptions() {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
    options.setPrettyFlow(true);
    options.setIndent(2);
    return options;
}
 
Example 12
Source File: Saves.java    From Text-Fighter with MIT License 5 votes vote down vote up
private static void setupDumper() {
	options = new DumperOptions();
	representer = new Representer();

	options.setIndent(2);
	options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
	options.setAllowUnicode(Charset.defaultCharset().name().contains("UTF"));
	representer.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
}
 
Example 13
Source File: SaltStateGenerator.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate the YAML.
 *
 * @param states the states to output
 *
 */
public void generate(SaltState... states) {
    DumperOptions setup = new DumperOptions();
    setup.setIndent(4);
    setup.setAllowUnicode(true);
    setup.setPrettyFlow(true);
    setup.setLineBreak(DumperOptions.LineBreak.UNIX);
    setup.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    setup.setCanonical(false);

    Yaml yaml = new Yaml(setup);
    for (SaltState state : states) {
        yaml.dump(state.getData(), destination);
    }
}
 
Example 14
Source File: LineBreakDooubleQuotedTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDoubleQuotedStyleNoLineSplit() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    options.setWidth(20);
    options.setSplitLines(false);
    options.setIndent(4);
    Yaml yaml = new Yaml(options);
    String etalon = "12345678901234567890\n\n123  456";
    String output = yaml.dump(etalon);
    // System.out.println(output);
    assertEquals("\"12345678901234567890\\n\\n123  456\"\n", output);
    String parsed = (String) yaml.load(output);
    assertEquals(etalon, parsed);
}
 
Example 15
Source File: LineBreakDooubleQuotedTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testDoubleQuotedStyle() {
    DumperOptions options = new DumperOptions();
    options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
    options.setWidth(20);
    options.setIndent(4);
    Yaml yaml = new Yaml(options);
    String etalon = "12345678901234567890\n\n123  456";
    String output = yaml.dump(etalon);
    // System.out.println(output);
    assertEquals("\"12345678901234567890\\n\\\n    \\n123  456\"\n", output);
    String parsed = (String) yaml.load(output);
    assertEquals(etalon, parsed);
}
 
Example 16
Source File: YAMLProcessor.java    From skript-yaml with MIT License 5 votes vote down vote up
/**
 * Sets the indentation amount used when the yaml file is saved
 * 
 * @param indent
 *            an amount from 1 to 10
 */
public void setIndent(int indent) {
	try {
		Field dumperOptions = yaml.getClass().getDeclaredField("dumperOptions");
		dumperOptions.setAccessible(true);
		DumperOptions dump = (DumperOptions) dumperOptions.get(yaml);
		try {
			dump.setIndent(indent);
		} catch (YAMLException ex) {
			SkriptYaml.warn(ex.getMessage());
		}
	} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
		e.printStackTrace();
	}
}
 
Example 17
Source File: ObjectMapConfiguration.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public void save(Writer writer) {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(FlowStyle.AUTO);
    options.setIndent(4);
    Representer representer = new Representer();
    representer.getPropertyUtils().setBeanAccess(BeanAccess.DEFAULT);
    new Yaml(options).dump(this, writer);
}
 
Example 18
Source File: Configuration.java    From Modern-LWC with MIT License 5 votes vote down vote up
protected Configuration(File file) {
    super(new HashMap<String, Object>());

    DumperOptions options = new DumperOptions();
    options.setIndent(4);
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

    yaml = new Yaml(new SafeConstructor(), new Representer(), options);
    this.file = file;
}