annotate src/org/tmatesoft/hg/internal/ConfigFile.java @ 379:fa2be7a05af6

Implement discovery of mercurial installation, use it to read/write configuration files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 09 Feb 2012 16:47:17 +0100
parents 9fb990c8a724
children e31e85cf4d4c
rev   line source
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
102
a3a2e5deb320 Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 73
diff changeset
15 * contact TMate Software at support@hg4j.com
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.BufferedReader;
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
20 import java.io.ByteArrayOutputStream;
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.File;
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
22 import java.io.FileOutputStream;
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.FileReader;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.io.IOException;
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
25 import java.io.PrintStream;
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
26 import java.nio.ByteBuffer;
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
27 import java.nio.channels.FileChannel;
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
28 import java.nio.channels.FileLock;
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import java.util.ArrayList;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import java.util.Collections;
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
31 import java.util.Iterator;
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 import java.util.LinkedHashMap;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 import java.util.List;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 import java.util.Map;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 /**
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
37 * .ini / .rc file reader
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 * @author Artem Tikhomirov
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 * @author TMate Software Ltd.
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 */
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 public class ConfigFile {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 private List<String> sections;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 private List<Map<String,String>> content;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
46 public ConfigFile() {
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
49 public void addLocation(File path) throws IOException {
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 read(path);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
53 public boolean hasSection(String sectionName) {
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
54 return sections == null ? false : sections.indexOf(sectionName) != -1;
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
55 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
56
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 public List<String> getSectionNames() {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
58 return sections == null ? Collections.<String>emptyList() : Collections.unmodifiableList(sections);
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 public Map<String,String> getSection(String sectionName) {
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
62 if (sections == null) {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
63 return Collections.emptyMap();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
64 }
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 int x = sections.indexOf(sectionName);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 if (x == -1) {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 return Collections.emptyMap();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 return Collections.unmodifiableMap(content.get(x));
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
72 public boolean getBoolean(String sectionName, String key, boolean defaultValue) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
73 String value = getSection(sectionName).get(key);
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
74 if (value == null) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
75 return defaultValue;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
76 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
77 for (String s : new String[] { "true", "yes", "on", "1" }) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
78 if (s.equalsIgnoreCase(value)) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
79 return true;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
80 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
81 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
82 return false;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
83 }
128
44b97930570c Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
84
44b97930570c Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
85 public String getString(String sectionName, String key, String defaultValue) {
44b97930570c Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
86 String value = getSection(sectionName).get(key);
44b97930570c Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
87 return value == null ? defaultValue : value;
44b97930570c Introduced ChangelogHelper to look up changesets files were modified in
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
88 }
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
89
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
90 public void putString(String sectionName, String key, String newValue) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
91 Map<String, String> section = null;
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
92 if (sections == null) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
93 // init, in case we didn't read any file with prev config
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
94 sections = new ArrayList<String>();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
95 content = new ArrayList<Map<String,String>>();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
96 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
97 int x = sections.indexOf(sectionName);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
98 if (x == -1) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
99 if (newValue == null) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
100 return;
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
101 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
102 sections.add(sectionName);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
103 content.add(section = new LinkedHashMap<String, String>());
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
104 } else {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
105 section = content.get(x);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
106 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
107 if (newValue == null) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
108 section.remove(key);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
109 } else {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
110 section.put(key, newValue);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
111 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
112 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
113
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
114 // TODO handle %include and %unset directives
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
115 // TODO "" and lists
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
116 // XXX perhaps, single string to keep whole section with substrings for keys/values to minimize number of arrays (String.value)
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
117 private void read(File f) throws IOException {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
118 if (f == null || !f.canRead()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
119 return;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 102
diff changeset
120 }
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 if (sections == null) {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 sections = new ArrayList<String>();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 content = new ArrayList<Map<String,String>>();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 }
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
125 BufferedReader br = null;
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 try {
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
127 br = new BufferedReader(new FileReader(f));
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 String line;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 String sectionName = "";
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 Map<String,String> section = new LinkedHashMap<String, String>();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 while ((line = br.readLine()) != null) {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 line = line.trim();
136
947bf231acbb Strip off comments in config file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 128
diff changeset
133 int x;
947bf231acbb Strip off comments in config file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 128
diff changeset
134 if ((x = line.indexOf('#')) != -1) {
947bf231acbb Strip off comments in config file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 128
diff changeset
135 // do not keep comments in memory, get new, shorter string
947bf231acbb Strip off comments in config file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 128
diff changeset
136 line = new String(line.substring(0, x).trim());
947bf231acbb Strip off comments in config file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 128
diff changeset
137 }
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138 if (line.length() <= 2) { // a=b or [a] are at least of length 3
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 continue;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141 if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 sectionName = line.substring(1, line.length() - 1);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 if (sections.indexOf(sectionName) == -1) {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 sections.add(sectionName);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 content.add(section = new LinkedHashMap<String, String>());
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 } else {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 section = null; // drop cached value
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 } else if ((x = line.indexOf('=')) != -1) {
136
947bf231acbb Strip off comments in config file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 128
diff changeset
150 // share char[] of the original string
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 String key = line.substring(0, x).trim();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 String value = line.substring(x+1).trim();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 if (section == null) {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 int i = sections.indexOf(sectionName);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 assert i >= 0;
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 section = content.get(i);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 if (sectionName.length() == 0) {
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 // add fake section only if there are any values
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 sections.add(sectionName);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 content.add(section);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 section.put(key, value);
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 }
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
166 } finally {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
167 ((ArrayList<?>) sections).trimToSize();
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
168 ((ArrayList<?>) content).trimToSize();
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
169 if (br != null) {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
170 br.close();
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 136
diff changeset
171 }
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 }
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 assert sections.size() == content.size();
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
174 }
378
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
175
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
176 public void writeTo(File f) throws IOException {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
177 byte[] data = compose();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
178 if (!f.exists()) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
179 f.createNewFile();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
180 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
181 FileChannel fc = new FileOutputStream(f).getChannel();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
182 FileLock fl = fc.lock();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
183 try {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
184 fc.write(ByteBuffer.wrap(data));
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
185 } finally {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
186 fl.release();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
187 fc.close();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
188 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
189 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
190
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
191 private byte[] compose() {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
192 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
193 PrintStream ps = new PrintStream(bos);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
194 Iterator<String> sectionNames = sections.iterator();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
195 for (Map<String,String> s : content) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
196 final String name = sectionNames.next(); // iterate through names despite section may be empty
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
197 if (s.isEmpty()) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
198 continue; // do not write an empty section
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
199 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
200 ps.print('[');
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
201 ps.print(name);
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
202 ps.println(']');
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
203 for (Map.Entry<String, String> e : s.entrySet()) {
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
204 ps.print(e.getKey());
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
205 ps.print('=');
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
206 ps.println(e.getValue());
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
207 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
208 ps.println();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
209 }
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
210 ps.flush();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
211 return bos.toByteArray();
9fb990c8a724 Investigate approaches to alter Mercurial configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
212 }
69
5a69397f0f99 Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
213 }