View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.construct.IncludeYamlFile;
37  import info.magnolia.config.source.yaml.construct.MgnlYamlConstruct;
38  import info.magnolia.config.source.yaml.dependency.YamlConfigurationDependency;
39  import info.magnolia.resourceloader.Resource;
40  
41  import java.util.HashSet;
42  import java.util.Set;
43  
44  import org.yaml.snakeyaml.constructor.Construct;
45  import org.yaml.snakeyaml.constructor.Constructor;
46  import org.yaml.snakeyaml.nodes.MappingNode;
47  import org.yaml.snakeyaml.nodes.Node;
48  import org.yaml.snakeyaml.nodes.ScalarNode;
49  import org.yaml.snakeyaml.nodes.SequenceNode;
50  import org.yaml.snakeyaml.nodes.Tag;
51  
52  /**
53   * Magnolia-specific extension of {@link Constructor SnakeYAML constructor}.
54   * Provides simple API for registering custom constructs bound to tags and tag prefixes. Exposes the factory method for producing the
55   * {@link Construct constructs} by the node types ({@link MgnlYamlConstructor#getConstructByNodeType(Node)}). Finally, {@link MgnlYamlConstructor}
56   * allows to aggregate the dependencies encountered during YAML parsing (other YAML files, definitions etc).
57   */
58  public class MgnlYamlConstructor extends Constructor {
59  
60      private final YamlConfigurationDependencyAggregator dependencyAggregator;
61  
62      MgnlYamlConstructor(YamlReader yamlReader, Resource baseResource, YamlConfigurationDependencyAggregator dependencyAggregator) {
63          this.dependencyAggregator = dependencyAggregator;
64          registerConstruct(IncludeYamlFile.TAG, new IncludeYamlFile(yamlReader, baseResource.getOrigin(), problem -> {}));
65      }
66  
67      void registerConstruct(String tag, Construct construct) {
68          if (construct instanceof MgnlYamlConstruct) {
69              ((MgnlYamlConstruct) construct).setConstructor(this);
70          }
71          yamlConstructors.put(new Tag(tag), construct);
72      }
73  
74      void registerMultiConstruct(String tagPrefix, Construct construct) {
75          if (construct instanceof MgnlYamlConstruct) {
76              ((MgnlYamlConstruct) construct).setConstructor(this);
77          }
78          yamlMultiConstructors.put(tagPrefix, construct);
79      }
80  
81      public Construct getConstructByNodeType(Node node) {
82          // (re-)initialise base construct
83          // if there's an extension tag present, YAML will get confused about the actual expected type
84          // of the node (it'll assume some abstract 'object')
85          if (node instanceof MappingNode) {
86              return new ConstructYamlMap();
87          } else if (node instanceof SequenceNode) {
88              return new ConstructYamlSeq();
89          }
90  
91          return new ConstructScalar() {
92              @Override
93              public Object construct(Node n) {
94                  return ((ScalarNode) n).getValue();
95              }
96          };
97      }
98  
99      public YamlConfigurationDependencyAggregator getDependencyAggregator() {
100         return dependencyAggregator;
101     }
102 
103     /**
104      * Simple aggregator class which allows to collect a set of {@link YamlConfigurationDependency} instances.
105      */
106     public static class YamlConfigurationDependencyAggregator {
107 
108         private Set<YamlConfigurationDependency> dependencies = new HashSet<>();
109 
110         YamlConfigurationDependencyAggregator() {}
111 
112         public void addDependency(YamlConfigurationDependency dependency) {
113             this.dependencies.add(dependency);
114         }
115 
116         public Set<YamlConfigurationDependency> getDependencies() {
117             return this.dependencies;
118         }
119 
120     }
121 }