You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigFile.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.GregorianCalendar;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * Reads and writes a standard DMDirc config file.
  32. *
  33. * @author chris
  34. */
  35. public class ConfigFile {
  36. private final List<String> domains = new ArrayList<String>();
  37. private final MapList<String, String> flatdomains = new MapList<String, String>();
  38. private final Map<String, Map<String, String>> keydomains
  39. = new HashMap<String, Map<String, String>>();
  40. private final TextFile file;
  41. public ConfigFile(final String file) throws FileNotFoundException, IOException {
  42. this.file = new TextFile(file);
  43. read();
  44. }
  45. public void read() throws FileNotFoundException, IOException {
  46. String domain = null;
  47. boolean keydomain = false;
  48. int offset;
  49. for (String line : file.getLines()) {
  50. final String tline = line.trim();
  51. if (tline.indexOf('#') == 0) {
  52. continue;
  53. } else if (tline.endsWith(":") && tline.indexOf('=') == -1) {
  54. domain = tline.substring(1, tline.length() - 1);
  55. domains.add(domain);
  56. keydomain = keydomains.containsKey(domain)
  57. || flatdomains.containsValue("keysections", domain);
  58. } else if (domain != null && keydomain && (offset = tline.indexOf('=')) != -1) {
  59. final String key = tline.substring(0, offset);
  60. final String value = tline.substring(offset + 1);
  61. keydomains.get(domain).put(key, value);
  62. } else if (domain != null && !keydomain) {
  63. flatdomains.add(domain, tline);
  64. }
  65. }
  66. }
  67. public void write() throws IOException {
  68. final List<String> lines = new ArrayList<String>();
  69. lines.add("# This is a DMDirc configuration file.");
  70. lines.add("# Written on: " + new GregorianCalendar().getTime().toString());
  71. writeMeta(lines);
  72. for (String domain : domains) {
  73. if ("keysections".equals(domain)) {
  74. continue;
  75. }
  76. lines.add("");
  77. lines.add(domain + ':');
  78. if (flatdomains.containsKey(domain)) {
  79. for (String entry : flatdomains.get(domain)) {
  80. lines.add(" " + entry);
  81. }
  82. } else if (keydomains.containsKey(domain)) {
  83. for (Map.Entry<String, String> entry : keydomains.get(domain).entrySet()) {
  84. lines.add(" " + entry.getKey() + "=" + entry.getValue());
  85. }
  86. }
  87. }
  88. file.writeLines(lines);
  89. }
  90. private void writeMeta(final List<String> lines) {
  91. lines.add("");
  92. lines.add("# This section indicates which sections below take key/value");
  93. lines.add("# pairs, rather than a simple list. It should be placed above");
  94. lines.add("# any sections that take key/values.");
  95. lines.add("keysections:");
  96. for (String domain : domains) {
  97. if ("keysections".equals(domain)) {
  98. continue;
  99. } else if (keydomains.containsKey(domain)) {
  100. lines.add(" " + domain);
  101. }
  102. }
  103. }
  104. public Map<String, String> getKeyDomain(final String domain) {
  105. return keydomains.get(domain);
  106. }
  107. public List<String> getListDomain(final String domain) {
  108. return flatdomains.get(domain);
  109. }
  110. public boolean hasDomain(final String domain) {
  111. return keydomains.containsKey(domain) || flatdomains.containsKey(domain);
  112. }
  113. }