View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.config.source.yaml;
35  
36  import info.magnolia.config.source.yaml.MgnlYamlConstructor.YamlConfigurationDependencyAggregator;
37  import info.magnolia.config.source.yaml.dependency.YamlConfigurationDependency;
38  import info.magnolia.resourceloader.Resource;
39  
40  import java.io.IOException;
41  import java.io.Reader;
42  import java.util.Collections;
43  import java.util.HashMap;
44  import java.util.Map;
45  import java.util.Set;
46  
47  import javax.inject.Singleton;
48  
49  import org.yaml.snakeyaml.Yaml;
50  import org.yaml.snakeyaml.constructor.Construct;
51  import org.yaml.snakeyaml.error.YAMLException;
52  
53  /**
54   * The {@link YamlReader} reads configuration from a yaml file and returns it as a map representing bean properties.
55   */
56  @Singleton
57  public class YamlReader {
58  
59      private final Map<String, Construct> customConstructs = new HashMap<>();
60      private final Map<String, Construct> customMultiConstructs = new HashMap<>();
61  
62      public void registerCustomConstruct(String tag, Construct construct) {
63          this.customConstructs.put(tag, construct);
64      }
65  
66      public void registerCustomMultiConstruct(String tagPrefix, Construct construct) {
67          this.customMultiConstructs.put(tagPrefix, construct);
68      }
69  
70      public Object read(Resource res) throws IOException {
71          return readNoCast(res);
72      }
73  
74      public YamlConversionResult readWithDependencies(final Resource resource) throws IOException {
75          final YamlConfigurationDependencyAggregator dependencyAggregator = new YamlConfigurationDependencyAggregator();
76          
77          return readWithDependencies(resource, dependencyAggregator);
78      }
79  
80      public YamlConversionResult readWithDependencies(final Resource resource, YamlConfigurationDependencyAggregator dependencyAggregator) throws IOException {
81          final Object result = doReadWithDependencies(resource, dependencyAggregator);
82  
83          return new YamlConversionResult(result, dependencyAggregator.getDependencies());
84      }
85  
86      protected Object doReadWithDependencies(final Resource res, YamlConfigurationDependencyAggregator dependencyAggregator) throws IOException {
87          final Yaml yaml = newSnakeYaml(res, dependencyAggregator);
88          try (final Reader reader = res.openReader()) {
89              return yaml.load(new WhiteSpaceNormalisingReader(reader));
90          } catch (YAMLException ye) {
91              throw new YamlReaderException(ye, res);
92          }
93      }
94  
95      public Map<String, Object> readToMap(final Resource resource) throws IOException {
96          //noinspection unchecked
97          return (Map<String, Object>) read(resource);
98      }
99  
100     protected Object readNoCast(final Resource res) throws IOException {
101         return doReadWithDependencies(res, new YamlConfigurationDependencyAggregator());
102     }
103 
104     protected Yaml newSnakeYaml(Resource yamlResource, YamlConfigurationDependencyAggregator dependencyAggregator) {
105         final MgnlYamlConstructornstructor.html#MgnlYamlConstructor">MgnlYamlConstructor constructor = new MgnlYamlConstructor(this, yamlResource, dependencyAggregator);
106         customConstructs.forEach(constructor::registerConstruct);
107         customMultiConstructs.forEach(constructor::registerMultiConstruct);
108 
109         return new Yaml(constructor);
110     }
111 
112     /**
113      * Result of YAML file conversion. Consists of the product object itself and a set
114      * of the so called dependencies, i.e. the other YAML files injected into the one passed to SnakeYAML
115      * via {@code !include <file>} tag.
116      */
117     public static class YamlConversionResult {
118 
119         public static YamlConversionResult empty() {
120             return new YamlConversionResult(new HashMap<>(), Collections.emptySet());
121         }
122 
123         private final Object result;
124         private final Set<YamlConfigurationDependency> dependencies;
125 
126         public YamlConversionResult(Object result, Set<YamlConfigurationDependency> dependencies) {
127             this.result = result;
128             this.dependencies = dependencies;
129         }
130 
131         public Set<YamlConfigurationDependency> getDependencies() {
132             return dependencies;
133         }
134 
135         public Object getResult() {
136             return result;
137         }
138     }
139 
140 }