-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
avaje-config/src/main/java/io/avaje/config/PropertiesParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.avaje.config; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.io.UncheckedIOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
final class PropertiesParser implements ConfigParser { | ||
|
||
private static final String[] extensions = new String[]{"properties"}; | ||
|
||
@Override | ||
public String[] supportedExtensions() { | ||
return extensions; | ||
} | ||
|
||
@Override | ||
public Map<String, String> load(Reader reader) { | ||
try { | ||
Properties p = new Properties(); | ||
p.load(reader); | ||
return toMap(p); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, String> load(InputStream is) { | ||
try { | ||
Properties p = new Properties(); | ||
p.load(is); | ||
return toMap(p); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private static Map<String, String> toMap(Properties p) { | ||
Map<String, String> result = new LinkedHashMap<>(); | ||
Set<Map.Entry<Object, Object>> entries = p.entrySet(); | ||
for (Map.Entry<Object, Object> entry : entries) { | ||
Object value = entry.getValue(); | ||
if (value != null) { | ||
result.put(entry.getKey().toString(), entry.getValue().toString()); | ||
} | ||
} | ||
return result; | ||
} | ||
} |