2018-05-02 17:29:58 +02:00
|
|
|
/*
|
2018-09-06 03:17:14 +02:00
|
|
|
* Copyright © 2018 WireGuard LLC. All Rights Reserved.
|
2018-07-06 04:09:48 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 17:29:58 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
package com.wireguard.config;
|
|
|
|
|
2018-04-17 05:20:17 +02:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
2017-07-30 00:30:33 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
import java9.util.Optional;
|
2017-07-30 00:30:33 +02:00
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
public final class Attribute {
|
|
|
|
private static final Pattern LINE_PATTERN = Pattern.compile("(\\w+)\\s*=\\s*([^\\s#][^#]*)");
|
|
|
|
private static final Pattern LIST_SEPARATOR = Pattern.compile("\\s*,\\s*");
|
2017-07-30 00:30:33 +02:00
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
private final String key;
|
|
|
|
private final String value;
|
2018-04-27 18:29:14 +02:00
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
private Attribute(final String key, final String value) {
|
|
|
|
this.key = key;
|
|
|
|
this.value = value;
|
2018-04-30 18:39:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
public static String join(final Iterable<?> values) {
|
|
|
|
return TextUtils.join(", ", values);
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
public static Optional<Attribute> parse(final CharSequence line) {
|
|
|
|
final Matcher matcher = LINE_PATTERN.matcher(line);
|
|
|
|
if (!matcher.matches())
|
|
|
|
return Optional.empty();
|
|
|
|
return Optional.of(new Attribute(matcher.group(1), matcher.group(2)));
|
2018-04-18 05:28:31 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
public static String[] split(final CharSequence value) {
|
|
|
|
return LIST_SEPARATOR.split(value);
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
public String getKey() {
|
|
|
|
return key;
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|
2018-04-17 05:20:17 +02:00
|
|
|
|
2018-09-06 03:17:14 +02:00
|
|
|
public String getValue() {
|
|
|
|
return value;
|
2018-04-17 05:20:17 +02:00
|
|
|
}
|
2017-07-30 00:30:33 +02:00
|
|
|
}
|