Mercurial > jhg
comparison hg4j/src/main/java/org/tmatesoft/hg/internal/ConfigFile.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
author | Alexander Kitaev <kitaev@gmail.com> |
---|---|
date | Tue, 10 May 2011 10:52:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
212:edb2e2829352 | 213:6ec4af642ba8 |
---|---|
1 /* | |
2 * Copyright (c) 2011 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import java.io.BufferedReader; | |
20 import java.io.File; | |
21 import java.io.FileReader; | |
22 import java.io.IOException; | |
23 import java.util.ArrayList; | |
24 import java.util.Collections; | |
25 import java.util.LinkedHashMap; | |
26 import java.util.List; | |
27 import java.util.Map; | |
28 | |
29 /** | |
30 * | |
31 * @author Artem Tikhomirov | |
32 * @author TMate Software Ltd. | |
33 */ | |
34 public class ConfigFile { | |
35 | |
36 private List<String> sections; | |
37 private List<Map<String,String>> content; | |
38 | |
39 ConfigFile() { | |
40 } | |
41 | |
42 public void addLocation(File path) { | |
43 read(path); | |
44 } | |
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 | |
60 public List<String> getSectionNames() { | |
61 return sections == null ? Collections.<String>emptyList() : Collections.unmodifiableList(sections); | |
62 } | |
63 | |
64 public Map<String,String> getSection(String sectionName) { | |
65 if (sections == null) { | |
66 return Collections.emptyMap(); | |
67 } | |
68 int x = sections.indexOf(sectionName); | |
69 if (x == -1) { | |
70 return Collections.emptyMap(); | |
71 } | |
72 return Collections.unmodifiableMap(content.get(x)); | |
73 } | |
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 public String getString(String sectionName, String key, String defaultValue) { | |
89 String value = getSection(sectionName).get(key); | |
90 return value == null ? defaultValue : value; | |
91 } | |
92 | |
93 // TODO handle %include and %unset directives | |
94 // TODO "" and lists | |
95 private void read(File f) { | |
96 if (f == null || !f.canRead()) { | |
97 return; | |
98 } | |
99 if (sections == null) { | |
100 sections = new ArrayList<String>(); | |
101 content = new ArrayList<Map<String,String>>(); | |
102 } | |
103 try { | |
104 BufferedReader br = new BufferedReader(new FileReader(f)); | |
105 String line; | |
106 String sectionName = ""; | |
107 Map<String,String> section = new LinkedHashMap<String, String>(); | |
108 while ((line = br.readLine()) != null) { | |
109 line = line.trim(); | |
110 int x; | |
111 if ((x = line.indexOf('#')) != -1) { | |
112 // do not keep comments in memory, get new, shorter string | |
113 line = new String(line.substring(0, x).trim()); | |
114 } | |
115 if (line.length() <= 2) { // a=b or [a] are at least of length 3 | |
116 continue; | |
117 } | |
118 if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') { | |
119 sectionName = line.substring(1, line.length() - 1); | |
120 if (sections.indexOf(sectionName) == -1) { | |
121 sections.add(sectionName); | |
122 content.add(section = new LinkedHashMap<String, String>()); | |
123 } else { | |
124 section = null; // drop cached value | |
125 } | |
126 } else if ((x = line.indexOf('=')) != -1) { | |
127 // share char[] of the original string | |
128 String key = line.substring(0, x).trim(); | |
129 String value = line.substring(x+1).trim(); | |
130 if (section == null) { | |
131 int i = sections.indexOf(sectionName); | |
132 assert i >= 0; | |
133 section = content.get(i); | |
134 } | |
135 if (sectionName.length() == 0) { | |
136 // add fake section only if there are any values | |
137 sections.add(sectionName); | |
138 content.add(section); | |
139 } | |
140 section.put(key, value); | |
141 } | |
142 } | |
143 br.close(); | |
144 } catch (IOException ex) { | |
145 ex.printStackTrace(); // XXX shall outer world care? | |
146 } | |
147 ((ArrayList<?>) sections).trimToSize(); | |
148 ((ArrayList<?>) content).trimToSize(); | |
149 assert sections.size() == content.size(); | |
150 } | |
151 } |