comparison src/org/tmatesoft/hg/internal/ConfigFile.java @ 114:46291ec605a0

Filters to read and initialize according to configuration files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 03 Feb 2011 22:13:55 +0100
parents a3a2e5deb320
children 44b97930570c
comparison
equal deleted inserted replaced
113:67ae317408c9 114:46291ec605a0
34 public class ConfigFile { 34 public class ConfigFile {
35 35
36 private List<String> sections; 36 private List<String> sections;
37 private List<Map<String,String>> content; 37 private List<Map<String,String>> content;
38 38
39 public ConfigFile() { 39 ConfigFile() {
40 } 40 }
41 41
42 public void addLocation(File path) { 42 public void addLocation(File path) {
43 read(path); 43 read(path);
44 } 44 }
45 45
46 public boolean hasSection(String sectionName) {
47 return sections == null ? false : sections.indexOf(sectionName) == -1;
48 }
49
50 // XXX perhaps, should be moved to subclass HgRepoConfig, as it is not common operation for any config file
51 public boolean hasEnabledExtension(String extensionName) {
52 int x = sections != null ? sections.indexOf("extensions") : -1;
53 if (x == -1) {
54 return false;
55 }
56 String value = content.get(x).get(extensionName);
57 return value != null && !"!".equals(value);
58 }
59
46 public List<String> getSectionNames() { 60 public List<String> getSectionNames() {
47 return Collections.unmodifiableList(sections); 61 return sections == null ? Collections.<String>emptyList() : Collections.unmodifiableList(sections);
48 } 62 }
49 63
50 public Map<String,String> getSection(String sectionName) { 64 public Map<String,String> getSection(String sectionName) {
65 if (sections == null) {
66 return Collections.emptyMap();
67 }
51 int x = sections.indexOf(sectionName); 68 int x = sections.indexOf(sectionName);
52 if (x == -1) { 69 if (x == -1) {
53 return Collections.emptyMap(); 70 return Collections.emptyMap();
54 } 71 }
55 return Collections.unmodifiableMap(content.get(x)); 72 return Collections.unmodifiableMap(content.get(x));
56 } 73 }
57 74
75 public boolean getBoolean(String sectionName, String key, boolean defaultValue) {
76 String value = getSection(sectionName).get(key);
77 if (value == null) {
78 return defaultValue;
79 }
80 for (String s : new String[] { "true", "yes", "on", "1" }) {
81 if (s.equalsIgnoreCase(value)) {
82 return true;
83 }
84 }
85 return false;
86 }
87
88 // TODO handle %include and %unset directives
89 // TODO "" and lists
58 private void read(File f) { 90 private void read(File f) {
91 if (f == null || !f.canRead()) {
92 return;
93 }
59 if (sections == null) { 94 if (sections == null) {
60 sections = new ArrayList<String>(); 95 sections = new ArrayList<String>();
61 content = new ArrayList<Map<String,String>>(); 96 content = new ArrayList<Map<String,String>>();
62 } 97 }
63 try { 98 try {