Upgrade Vulnerable Direct Dependencies [snakeyaml]
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / services / StrictMapAppenderConstructor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.tosca.services;
22
23 import org.yaml.snakeyaml.constructor.Constructor;
24 import org.yaml.snakeyaml.nodes.MappingNode;
25 import org.yaml.snakeyaml.parser.ParserException;
26
27 import java.util.AbstractMap;
28 import java.util.Map;
29 import java.util.Set;
30
31 public class StrictMapAppenderConstructor extends Constructor {
32     /**
33      * Instantiates a new Strict map appender constructor.
34      *
35      * @param theRoot the the root
36      */
37     public StrictMapAppenderConstructor(Class<?> theRoot) {
38         super(theRoot);
39     }
40
41     @Override
42     protected Map<Object, Object> createDefaultMap(int initSize) {
43         final Map<Object, Object> delegate = super.createDefaultMap(initSize);
44         return new AbstractMap<>() {
45             @Override
46             public Object put(Object key, Object value) {
47                 if (delegate.containsKey(key)) {
48                     throw new IllegalStateException("duplicate key: " + key);
49                 }
50                 return delegate.put(key, value);
51             }
52
53             @Override
54             public Set<Entry<Object, Object>> entrySet() {
55                 return delegate.entrySet();
56             }
57         };
58     }
59
60     @Override
61     protected Map<Object, Object> constructMapping(MappingNode node) {
62         try {
63             return super.constructMapping(node);
64         } catch (IllegalStateException exception) {
65             throw new ParserException("while parsing MappingNode",
66                     node.getStartMark(), exception.getMessage(),
67                     node.getEndMark());
68         }
69     }
70 }