diff 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
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/ConfigFile.java	Wed Feb 02 21:19:02 2011 +0100
+++ b/src/org/tmatesoft/hg/internal/ConfigFile.java	Thu Feb 03 22:13:55 2011 +0100
@@ -36,18 +36,35 @@
 	private List<String> sections;
 	private List<Map<String,String>> content;
 
-	public ConfigFile() {
+	ConfigFile() {
 	}
 
 	public void addLocation(File path) {
 		read(path);
 	}
 	
+	public boolean hasSection(String sectionName) {
+		return sections == null ? false : sections.indexOf(sectionName) == -1;
+	}
+	
+	// XXX perhaps, should be moved to subclass HgRepoConfig, as it is not common operation for any config file
+	public boolean hasEnabledExtension(String extensionName) {
+		int x = sections != null ? sections.indexOf("extensions") : -1;
+		if (x == -1) {
+			return false;
+		}
+		String value = content.get(x).get(extensionName);
+		return value != null && !"!".equals(value);
+	}
+	
 	public List<String> getSectionNames() {
-		return Collections.unmodifiableList(sections);
+		return sections == null ? Collections.<String>emptyList() : Collections.unmodifiableList(sections);
 	}
 
 	public Map<String,String> getSection(String sectionName) {
+		if (sections ==  null) {
+			return Collections.emptyMap();
+		}
 		int x = sections.indexOf(sectionName);
 		if (x == -1) {
 			return Collections.emptyMap();
@@ -55,7 +72,25 @@
 		return Collections.unmodifiableMap(content.get(x));
 	}
 
+	public boolean getBoolean(String sectionName, String key, boolean defaultValue) {
+		String value = getSection(sectionName).get(key);
+		if (value == null) {
+			return defaultValue;
+		}
+		for (String s : new String[] { "true", "yes", "on", "1" }) {
+			if (s.equalsIgnoreCase(value)) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	// TODO handle %include and %unset directives
+	// TODO "" and lists
 	private void read(File f) {
+		if (f == null || !f.canRead()) {
+			return;
+		}
 		if (sections == null) {
 			sections = new ArrayList<String>();
 			content = new ArrayList<Map<String,String>>();