Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/ConfigFile.java @ 69:5a69397f0f99
Discovery utility for Hg network protocol finally in the repo, with quick-n-dirty ConfigFile impl that helps to hide auth info
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Sat, 22 Jan 2011 22:53:57 +0100 |
| parents | |
| children | 0d279bcc4442 |
comparison
equal
deleted
inserted
replaced
| 68:0e499fed9b3d | 69:5a69397f0f99 |
|---|---|
| 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@svnkit.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 public ConfigFile() { | |
| 40 } | |
| 41 | |
| 42 public void addLocation(File path) { | |
| 43 read(path); | |
| 44 } | |
| 45 | |
| 46 public List<String> getSectionNames() { | |
| 47 return Collections.unmodifiableList(sections); | |
| 48 } | |
| 49 | |
| 50 public Map<String,String> getSection(String sectionName) { | |
| 51 int x = sections.indexOf(sectionName); | |
| 52 if (x == -1) { | |
| 53 return Collections.emptyMap(); | |
| 54 } | |
| 55 return Collections.unmodifiableMap(content.get(x)); | |
| 56 } | |
| 57 | |
| 58 private void read(File f) { | |
| 59 if (sections == null) { | |
| 60 sections = new ArrayList<String>(); | |
| 61 content = new ArrayList<Map<String,String>>(); | |
| 62 } | |
| 63 try { | |
| 64 BufferedReader br = new BufferedReader(new FileReader(f)); | |
| 65 String line; | |
| 66 String sectionName = ""; | |
| 67 Map<String,String> section = new LinkedHashMap<String, String>(); | |
| 68 while ((line = br.readLine()) != null) { | |
| 69 line = line.trim(); | |
| 70 if (line.length() <= 2) { // a=b or [a] are at least of length 3 | |
| 71 continue; | |
| 72 } | |
| 73 int x; | |
| 74 if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') { | |
| 75 sectionName = line.substring(1, line.length() - 1); | |
| 76 if (sections.indexOf(sectionName) == -1) { | |
| 77 sections.add(sectionName); | |
| 78 content.add(section = new LinkedHashMap<String, String>()); | |
| 79 } else { | |
| 80 section = null; // drop cached value | |
| 81 } | |
| 82 } else if ((x = line.indexOf('=')) != -1) { | |
| 83 String key = line.substring(0, x).trim(); | |
| 84 String value = line.substring(x+1).trim(); | |
| 85 if (section == null) { | |
| 86 int i = sections.indexOf(sectionName); | |
| 87 assert i >= 0; | |
| 88 section = content.get(i); | |
| 89 } | |
| 90 if (sectionName.length() == 0) { | |
| 91 // add fake section only if there are any values | |
| 92 sections.add(sectionName); | |
| 93 content.add(section); | |
| 94 } | |
| 95 section.put(key, value); | |
| 96 } | |
| 97 } | |
| 98 } catch (IOException ex) { | |
| 99 ex.printStackTrace(); // XXX shall outer world care? | |
| 100 } | |
| 101 ((ArrayList<?>) sections).trimToSize(); | |
| 102 ((ArrayList<?>) content).trimToSize(); | |
| 103 assert sections.size() == content.size(); | |
| 104 } | |
| 105 } |
