001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.automation.io.yaml;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.OutputStreamWriter;
024import java.io.Writer;
025
026import org.nuxeo.ecm.automation.OperationDocumentation;
027import org.nuxeo.ecm.automation.core.OperationChainContribution.Operation;
028import org.yaml.snakeyaml.DumperOptions;
029import org.yaml.snakeyaml.DumperOptions.FlowStyle;
030import org.yaml.snakeyaml.Yaml;
031
032/**
033 * Operation to YAML converter
034 *
035 * @since 5.9.4
036 */
037public class YamlWriter {
038
039    public static void toYaml(OutputStream out, OperationDocumentation info) throws IOException {
040        toYaml(out, (Object) info);
041    }
042
043    public static void toYaml(OutputStream out, Operation info) throws IOException {
044        toYaml(out, (Object) info);
045    }
046
047    protected static void toYaml(OutputStream out, Object info) throws IOException {
048        if (info == null) {
049            return;
050        }
051        DumperOptions options = new DumperOptions();
052        options.setAllowReadOnlyProperties(true);
053        options.setDefaultFlowStyle(FlowStyle.BLOCK);
054        Yaml yaml = new Yaml(new YamlAutomationRepresenter(), options);
055        Writer writer = new OutputStreamWriter(out);
056        yaml.dump(info, writer);
057    }
058
059}