2 * Copyright (C) 2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 package org
.strongswan
.android
.utils
;
18 import java
.util
.Arrays
;
19 import java
.util
.LinkedHashMap
;
20 import java
.util
.Map
.Entry
;
21 import java
.util
.regex
.Pattern
;
25 * Simple generator for data/files that may be parsed by libstrongswan's
28 public class SettingsWriter
33 private final SettingsSection mTop
= new SettingsSection();
41 public SettingsWriter
setValue(String key
, String value
)
43 Pattern pattern
= Pattern
.compile("[^#{}=\"\\n\\t ]+");
44 if (key
== null
|| !pattern
.matcher(key
).matches())
48 String
[] keys
= key
.split("\\.");
49 SettingsSection section
= mTop
;
50 section
= findOrCreateSection(Arrays
.copyOfRange(keys
, 0, keys
.length
-1));
51 section
.Settings
.put(keys
[keys
.length
-1], value
);
56 * Set an integer value
61 public SettingsWriter
setValue(String key
, Integer value
)
63 return setValue(key
, value
== null ? null
: value
.toString());
72 public SettingsWriter
setValue(String key
, Boolean value
)
74 return setValue(key
, value
== null ? null
: value ?
"1" : "0");
78 * Serializes the settings to a string in the format understood by
79 * libstrongswan's settings_t parser.
80 * @return serialized settings
82 public String
serialize()
84 StringBuilder builder
= new StringBuilder();
85 serializeSection(mTop
, builder
);
86 return builder
.toString();
90 * Serialize the settings in a section and recursively serialize sub-sections
94 private void serializeSection(SettingsSection section
, StringBuilder builder
)
96 for (Entry
<String
, String
> setting
: section
.Settings
.entrySet())
98 builder
.append(setting
.getKey()).append('=');
99 if (setting
.getValue() != null
)
101 builder
.append("\"").append(escapeValue(setting
.getValue())).append("\"");
103 builder
.append('\n');
106 for (Entry
<String
, SettingsSection
> subsection
: section
.Sections
.entrySet())
108 builder
.append(subsection
.getKey()).append(" {\n");
109 serializeSection(subsection
.getValue(), builder
);
110 builder
.append("}\n");
115 * Escape value so it may be wrapped in "
119 private String
escapeValue(String value
)
121 return value
.replace("\"", "\\\"");
125 * Find or create the nested sections with the given names
126 * @param sections list of section names
127 * @return final section
129 private SettingsSection
findOrCreateSection(String
[] sections
)
131 SettingsSection section
= mTop
;
132 for (String name
: sections
)
134 SettingsSection subsection
= section
.Sections
.get(name
);
135 if (subsection
== null
)
137 subsection
= new SettingsSection();
138 section
.Sections
.put(name
, subsection
);
140 section
= subsection
;
146 * A section containing sub-sections and settings.
148 private class SettingsSection
151 * Assigned key/value pairs
153 LinkedHashMap
<String
,String
> Settings
= new LinkedHashMap
<String
, String
>();
156 * Assigned sub-sections
158 LinkedHashMap
<String
,SettingsSection
> Sections
= new LinkedHashMap
<String
, SettingsWriter
.SettingsSection
>();