comparison src/org/tmatesoft/hg/repo/HgRepoConfig.java @ 331:a37ce7145c3f

Access to repository configuration
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sat, 05 Nov 2011 04:21:18 +0100
parents
children 467fd379b653
comparison
equal deleted inserted replaced
330:9747a786a34d 331:a37ce7145c3f
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.repo;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.tmatesoft.hg.internal.ConfigFile;
26 import org.tmatesoft.hg.internal.Experimental;
27 import org.tmatesoft.hg.util.Pair;
28
29 /**
30 * WORK IN PROGRESS
31 *
32 * Repository-specific configuration.
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 @Experimental(reason="WORK IN PROGRESS")
38 public final class HgRepoConfig /*implements RepoChangeListener, perhaps, also RepoChangeNotifier? */{
39 /*ease access for inner classes*/ final ConfigFile config;
40
41 /*package-local*/HgRepoConfig(ConfigFile configFile) {
42 config = configFile;
43 }
44
45 public Section getSection(String name) {
46 if (name == null) {
47 throw new IllegalArgumentException();
48 }
49 if ("paths".equals(name)) {
50 return new PathsSection();
51 }
52 if ("extensions".equals(name)) {
53 return new ExtensionsSection();
54 }
55 return new Section(name);
56 }
57
58 public boolean hasSection(String name) {
59 return config.hasSection(name);
60 }
61
62 public boolean getBooleanValue(String section, String key, boolean defaultValue) {
63 return config.getBoolean(section, key, defaultValue);
64 }
65
66 public String getStringValue(String section, String key, String defaultValue) {
67 return config.getString(section, key, defaultValue);
68 }
69
70 //
71 //
72
73 public PathsSection getPaths() {
74 Section s = getSection("paths");
75 if (s.exists()) {
76 return (PathsSection) s;
77 }
78 return new PathsSection();
79 }
80
81 public ExtensionsSection getExtensions() {
82 Section s = getSection("extensions");
83 if (s.exists()) {
84 return (ExtensionsSection) s;
85 }
86 return new ExtensionsSection();
87 }
88
89 /*
90 * IMPLEMENTATION NOTE: Section is merely a view to configuration file, without any state.
91 * In case I need to sync access to config (i.e. write) or refresh it later - can be easily done
92 */
93
94 public class Section implements Iterable<Pair<String,String>> {
95
96 protected final String section;
97
98 /*package-local*/Section(String sectionName) {
99 section = sectionName;
100 }
101
102 public String getName() {
103 return section;
104 }
105
106 /**
107 * Whether this is real section or a bare non-null instance
108 */
109 public boolean exists() {
110 return hasSection(section);
111 }
112
113 /**
114 * @return defined keys, in the order they appear in the section
115 */
116 public List<String> getKeys() {
117 return new ArrayList<String>(config.getSection(section).keySet());
118 }
119
120 /**
121 * Find out whether key is present and got any value
122 * @param key identifies an entry to look up
123 * @return true if key is present in the section and has non-empty value
124 */
125 public boolean isKeySet(String key) {
126 String value = getStringValue(section, key, null);
127 return value != null && value.length() > 0;
128 }
129
130 /**
131 * Value of a key as boolean
132 * @param key identifies an entry to look up
133 * @param defaultValue optional value to return if no entry for the key found in this section
134 * @return value corresponding to the key, or defaultValue if key not found
135 */
136 public boolean getBoolean(String key, boolean defaultValue) {
137 return getBooleanValue(section, key, defaultValue);
138 }
139
140 /**
141 * Value of a key as regular string
142 * @param key identifies entry to look up
143 * @param defaultValue optional value to return if no entry for the key found in this section
144 * @return value corresponding to the key, or defaultValue if key not found
145 */
146 public String getString(String key, String defaultValue) {
147 return getStringValue(section, key, defaultValue);
148 }
149
150 public Iterator<Pair<String, String>> iterator() {
151 final Map<String, String> m = config.getSection(section);
152 if (m.isEmpty()) {
153 return Collections.<Pair<String,String>>emptyList().iterator();
154 }
155 ArrayList<Pair<String, String>> rv = new ArrayList<Pair<String,String>>(m.size());
156 for (Map.Entry<String, String> e : m.entrySet()) {
157 rv.add(new Pair<String,String>(e.getKey(), e.getValue()));
158 }
159 return rv.iterator();
160 }
161 }
162
163 /*
164 * Few well-known sections may get their specific subclasses
165 */
166
167 /**
168 * Section [paths]
169 */
170 public class PathsSection extends Section {
171 PathsSection() {
172 super("paths");
173 }
174
175 public boolean hasDefault() {
176 return isKeySet("default");
177 }
178 public String getDefault() {
179 return super.getString("default", null);
180 }
181 public boolean hasDefaultPush() {
182 return isKeySet("default-push");
183 }
184 public String getDefaultPush() {
185 return super.getString("default-push", null);
186 }
187 }
188
189 /**
190 * Section [extensions]
191 *
192 * @author Artem Tikhomirov
193 * @author TMate Software Ltd.
194 */
195 public class ExtensionsSection extends Section {
196 ExtensionsSection() {
197 super("extensions");
198 }
199
200 public boolean isEnabled(String extensionName) {
201 String value = config.getSection(section).get(extensionName);
202 return value != null && value.length() > 0 && '!' != value.charAt(0) ;
203 }
204 }
205 }