1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Optional;
26 import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConversionException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class Section {
32 private static final Logger LOG = LoggerFactory.getLogger(Section.class);
33 private static final String DELIMITER = "=";
34 private static final String COMMENTCHARS[] = {"#", ";"};
36 private final String name;
37 private final List<String> rawLines;
38 private final LinkedHashMap<String, SectionValue> values;
40 public Section(String name) {
41 LOG.debug("new section created: '{}'", name);
43 this.rawLines = new ArrayList<>();
44 this.values = new LinkedHashMap<>();
47 public void addLine(String line) {
48 LOG.trace("adding raw line:" + line);
49 this.rawLines.add(line);
52 public String getProperty(String key) {
53 return this.getProperty(key, null);
56 public String getProperty(String key, String defValue) {
57 if (values.containsKey(key)) {
58 return values.get(key).getValue();
63 public String getName() {
67 public void setProperty(String key, String value) {
68 boolean isuncommented = this.isCommentLine(key);
70 key = key.substring(1);
72 if (this.values.containsKey(key)) {
73 this.values.get(key).setValue(value).setIsUncommented(isuncommented);
75 this.values.put(key, new SectionValue(value,isuncommented));
79 public void parseLines() {
81 List<String> commentsForValue = new ArrayList<>();
82 boolean uncommented = false;
83 for (String line : rawLines) {
85 if (this.isCommentLine(line)) {
86 if (!line.contains(DELIMITER)) {
87 commentsForValue.add(line);
91 line = line.substring(1);
94 if (!line.contains(DELIMITER)) {
97 String hlp[] = line.split(DELIMITER);
101 line.length() > (key + DELIMITER).length() ? line.substring((key + DELIMITER).length())
103 if (this.values.containsKey(key)) {
104 this.values.get(key).setValue(value);
106 this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
107 commentsForValue = new ArrayList<>();
110 LOG.warn("ignoring unknown formatted line:" + line);
116 private boolean isCommentLine(String line) {
117 for (String c : COMMENTCHARS) {
118 if (line.startsWith(c)) {
125 public String[] toLines() {
126 List<String> lines = new ArrayList<>();
127 if (!this.name.isEmpty()) {
128 lines.add("[" + this.name + "]");
130 for (Entry<String, SectionValue> entry : this.values.entrySet()) {
131 SectionValue sectionValue = entry.getValue();
132 if (sectionValue.getComments().size() > 0) {
133 for (String comment : sectionValue.getComments()) {
137 lines.add((sectionValue.isUncommented() ? COMMENTCHARS[0] : "") + entry.getKey() + DELIMITER
138 + sectionValue.getValue());
140 String[] alines = new String[lines.size()];
141 return lines.toArray(alines);
144 public String getString(String key, String def) {
145 return this.getProperty(key, def);
148 public boolean getBoolean(String key, boolean def) throws ConversionException {
149 String v = this.getProperty(key);
150 if (v == null || v.isEmpty()) {
153 if (v.equals("true")) {
156 if (v.equals("false")) {
159 throw new ConversionException("invalid value for key " + key);
162 public int getInt(String key, int def) throws ConversionException {
163 String v = this.getProperty(key);
164 if (v == null || v.isEmpty()) {
168 return Integer.parseInt(v);
169 } catch (NumberFormatException e) {
170 throw new ConversionException(e.getMessage());
174 public Optional<Long> getLong(String key) {
175 String v = this.getProperty(key);
177 return Optional.of(Long.parseLong(v));
178 } catch (NumberFormatException e) {
180 return Optional.empty();
183 public boolean hasValues() {
184 return this.values.size() > 0;
187 public boolean hasKey(String key) {
188 return this.values.containsKey(key);
192 public String toString() {
193 return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]";