001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.automation.io.yaml;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import java.io.OutputStreamWriter;
022import java.io.Writer;
023
024import org.nuxeo.ecm.automation.OperationDocumentation;
025import org.nuxeo.ecm.automation.core.OperationChainContribution.Operation;
026import org.yaml.snakeyaml.DumperOptions;
027import org.yaml.snakeyaml.DumperOptions.FlowStyle;
028import org.yaml.snakeyaml.Yaml;
029
030/**
031 * Operation to YAML converter
032 *
033 * @since 5.9.4
034 */
035public class YamlWriter {
036
037    public static void toYaml(OutputStream out, OperationDocumentation info) throws IOException {
038        toYaml(out, (Object) info);
039    }
040
041    public static void toYaml(OutputStream out, Operation info) throws IOException {
042        toYaml(out, (Object) info);
043    }
044
045    protected static void toYaml(OutputStream out, Object info) throws IOException {
046        if (info == null) {
047            return;
048        }
049        DumperOptions options = new DumperOptions();
050        options.setAllowReadOnlyProperties(true);
051        options.setDefaultFlowStyle(FlowStyle.BLOCK);
052        Yaml yaml = new Yaml(new YamlAutomationRepresenter(), options);
053        Writer writer = new OutputStreamWriter(out);
054        yaml.dump(info, writer);
055    }
056
057}