comparison src/org/tmatesoft/hg/internal/ConfigFile.java @ 295:981f9f50bb6c

Issue 11: Error log facility. SessionContext to share common facilities
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 16 Sep 2011 05:35:32 +0200
parents 947bf231acbb
children a37ce7145c3f
comparison
equal deleted inserted replaced
294:32890bab7209 295:981f9f50bb6c
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 ConfigFile() { 39 public ConfigFile() {
40 } 40 }
41 41
42 public void addLocation(File path) { 42 public void addLocation(File path) throws IOException {
43 read(path); 43 read(path);
44 } 44 }
45 45
46 public boolean hasSection(String sectionName) { 46 public boolean hasSection(String sectionName) {
47 return sections == null ? false : sections.indexOf(sectionName) == -1; 47 return sections == null ? false : sections.indexOf(sectionName) == -1;
90 return value == null ? defaultValue : value; 90 return value == null ? defaultValue : value;
91 } 91 }
92 92
93 // TODO handle %include and %unset directives 93 // TODO handle %include and %unset directives
94 // TODO "" and lists 94 // TODO "" and lists
95 private void read(File f) { 95 private void read(File f) throws IOException {
96 if (f == null || !f.canRead()) { 96 if (f == null || !f.canRead()) {
97 return; 97 return;
98 } 98 }
99 if (sections == null) { 99 if (sections == null) {
100 sections = new ArrayList<String>(); 100 sections = new ArrayList<String>();
101 content = new ArrayList<Map<String,String>>(); 101 content = new ArrayList<Map<String,String>>();
102 } 102 }
103 BufferedReader br = null;
103 try { 104 try {
104 BufferedReader br = new BufferedReader(new FileReader(f)); 105 br = new BufferedReader(new FileReader(f));
105 String line; 106 String line;
106 String sectionName = ""; 107 String sectionName = "";
107 Map<String,String> section = new LinkedHashMap<String, String>(); 108 Map<String,String> section = new LinkedHashMap<String, String>();
108 while ((line = br.readLine()) != null) { 109 while ((line = br.readLine()) != null) {
109 line = line.trim(); 110 line = line.trim();
138 content.add(section); 139 content.add(section);
139 } 140 }
140 section.put(key, value); 141 section.put(key, value);
141 } 142 }
142 } 143 }
143 br.close(); 144 } finally {
144 } catch (IOException ex) { 145 ((ArrayList<?>) sections).trimToSize();
145 ex.printStackTrace(); // XXX shall outer world care? 146 ((ArrayList<?>) content).trimToSize();
147 if (br != null) {
148 br.close();
149 }
146 } 150 }
147 ((ArrayList<?>) sections).trimToSize();
148 ((ArrayList<?>) content).trimToSize();
149 assert sections.size() == content.size(); 151 assert sections.size() == content.size();
150 } 152 }
151 } 153 }