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