View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.devops.conga.plugins.aem.maven.model;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.yaml.snakeyaml.Yaml;
25  import org.yaml.snakeyaml.constructor.Constructor;
26  
27  import io.wcm.devops.conga.generator.spi.context.PluginContextOptions;
28  import io.wcm.devops.conga.generator.spi.yaml.YamlConstructorPlugin;
29  import io.wcm.devops.conga.generator.spi.yaml.context.YamlConstructorContext;
30  import io.wcm.devops.conga.generator.util.PluginManager;
31  import io.wcm.devops.conga.generator.util.PluginManagerImpl;
32  
33  final class YamlUtil {
34  
35    private YamlUtil() {
36      // static methods only
37    }
38  
39    private static final Logger log = LoggerFactory.getLogger(YamlUtil.class);
40  
41    public static Yaml createYaml() {
42      // initialize CONGA plugin manager
43      PluginManager pluginManager = new PluginManagerImpl();
44      PluginContextOptions options = new PluginContextOptions()
45          .pluginManager(pluginManager)
46          .logger(log);
47  
48      // apply YAML plugins for modifying YAML constructor
49      Constructor constructor = new Constructor(io.wcm.devops.conga.model.util.YamlUtil.createLoaderOptions());
50      YamlConstructorContext context = new YamlConstructorContext()
51          .pluginContextOptions(options)
52          .yamlConstructor(constructor);
53      for (YamlConstructorPlugin plugin : pluginManager.getAll(YamlConstructorPlugin.class)) {
54        plugin.register(context);
55      }
56  
57      return new Yaml(constructor);
58    }
59  
60  }