Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.util; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.BufferedReader; |
michael@0 | 8 | import java.io.BufferedWriter; |
michael@0 | 9 | import java.io.File; |
michael@0 | 10 | import java.io.FileNotFoundException; |
michael@0 | 11 | import java.io.FileReader; |
michael@0 | 12 | import java.io.FileWriter; |
michael@0 | 13 | import java.io.IOException; |
michael@0 | 14 | import java.util.Enumeration; |
michael@0 | 15 | import java.util.Hashtable; |
michael@0 | 16 | |
michael@0 | 17 | public final class INIParser extends INISection { |
michael@0 | 18 | // default file to read and write to |
michael@0 | 19 | private File mFile = null; |
michael@0 | 20 | |
michael@0 | 21 | // List of sections in the current iniFile. null if the file has not been parsed yet |
michael@0 | 22 | private Hashtable<String, INISection> mSections = null; |
michael@0 | 23 | |
michael@0 | 24 | // create a parser. The file will not be read until you attempt to |
michael@0 | 25 | // access sections or properties inside it. At that point its read synchronously |
michael@0 | 26 | public INIParser(File iniFile) { |
michael@0 | 27 | super(""); |
michael@0 | 28 | mFile = iniFile; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | // write ini data to the default file. Will overwrite anything current inside |
michael@0 | 32 | public void write() { |
michael@0 | 33 | writeTo(mFile); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | // write to the specified file. Will overwrite anything current inside |
michael@0 | 37 | public void writeTo(File f) { |
michael@0 | 38 | if (f == null) |
michael@0 | 39 | return; |
michael@0 | 40 | |
michael@0 | 41 | FileWriter outputStream = null; |
michael@0 | 42 | try { |
michael@0 | 43 | outputStream = new FileWriter(f); |
michael@0 | 44 | } catch (IOException e1) { |
michael@0 | 45 | e1.printStackTrace(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | BufferedWriter writer = new BufferedWriter(outputStream); |
michael@0 | 49 | try { |
michael@0 | 50 | write(writer); |
michael@0 | 51 | writer.close(); |
michael@0 | 52 | } catch (IOException e) { |
michael@0 | 53 | e.printStackTrace(); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | @Override |
michael@0 | 58 | public void write(BufferedWriter writer) throws IOException { |
michael@0 | 59 | super.write(writer); |
michael@0 | 60 | |
michael@0 | 61 | if (mSections != null) { |
michael@0 | 62 | for (Enumeration<INISection> e = mSections.elements(); e.hasMoreElements();) { |
michael@0 | 63 | INISection section = e.nextElement(); |
michael@0 | 64 | section.write(writer); |
michael@0 | 65 | writer.newLine(); |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | // return all of the sections inside this file |
michael@0 | 71 | public Hashtable<String, INISection> getSections() { |
michael@0 | 72 | if (mSections == null) { |
michael@0 | 73 | try { |
michael@0 | 74 | parse(); |
michael@0 | 75 | } catch (IOException e) { |
michael@0 | 76 | debug("Error parsing: " + e); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | return mSections; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // parse the default file |
michael@0 | 83 | @Override |
michael@0 | 84 | protected void parse() throws IOException { |
michael@0 | 85 | super.parse(); |
michael@0 | 86 | parse(mFile); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // parse a passed in file |
michael@0 | 90 | private void parse(File f) throws IOException { |
michael@0 | 91 | // Set up internal data members |
michael@0 | 92 | mSections = new Hashtable<String, INISection>(); |
michael@0 | 93 | |
michael@0 | 94 | if (f == null || !f.exists()) |
michael@0 | 95 | return; |
michael@0 | 96 | |
michael@0 | 97 | FileReader inputStream = null; |
michael@0 | 98 | try { |
michael@0 | 99 | inputStream = new FileReader(f); |
michael@0 | 100 | } catch (FileNotFoundException e1) { |
michael@0 | 101 | // If the file doesn't exist. Just return; |
michael@0 | 102 | return; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | BufferedReader buf = new BufferedReader(inputStream); |
michael@0 | 106 | String line = null; // current line of text we are parsing |
michael@0 | 107 | INISection currentSection = null; // section we are currently parsing |
michael@0 | 108 | |
michael@0 | 109 | while ((line = buf.readLine()) != null) { |
michael@0 | 110 | |
michael@0 | 111 | if (line != null) |
michael@0 | 112 | line = line.trim(); |
michael@0 | 113 | |
michael@0 | 114 | // blank line or a comment. ignore it |
michael@0 | 115 | if (line == null || line.length() == 0 || line.charAt(0) == ';') { |
michael@0 | 116 | debug("Ignore line: " + line); |
michael@0 | 117 | } else if (line.charAt(0) == '[') { |
michael@0 | 118 | debug("Parse as section: " + line); |
michael@0 | 119 | currentSection = new INISection(line.substring(1, line.length()-1)); |
michael@0 | 120 | mSections.put(currentSection.getName(), currentSection); |
michael@0 | 121 | } else { |
michael@0 | 122 | debug("Parse as property: " + line); |
michael@0 | 123 | |
michael@0 | 124 | String[] pieces = line.split("="); |
michael@0 | 125 | if (pieces.length != 2) |
michael@0 | 126 | continue; |
michael@0 | 127 | |
michael@0 | 128 | String key = pieces[0].trim(); |
michael@0 | 129 | String value = pieces[1].trim(); |
michael@0 | 130 | if (currentSection != null) { |
michael@0 | 131 | currentSection.setProperty(key, value); |
michael@0 | 132 | } else { |
michael@0 | 133 | mProperties.put(key, value); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | buf.close(); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | // add a section to the file |
michael@0 | 141 | public void addSection(INISection sect) { |
michael@0 | 142 | // ensure that we have parsed the file |
michael@0 | 143 | getSections(); |
michael@0 | 144 | mSections.put(sect.getName(), sect); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | // get a section from the file. will return null if the section doesn't exist |
michael@0 | 148 | public INISection getSection(String key) { |
michael@0 | 149 | // ensure that we have parsed the file |
michael@0 | 150 | getSections(); |
michael@0 | 151 | return mSections.get(key); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | // remove an entire section from the file |
michael@0 | 155 | public void removeSection(String name) { |
michael@0 | 156 | // ensure that we have parsed the file |
michael@0 | 157 | getSections(); |
michael@0 | 158 | mSections.remove(name); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | // rename a section; nuking any previous section with the new |
michael@0 | 162 | // name in the process |
michael@0 | 163 | public void renameSection(String oldName, String newName) { |
michael@0 | 164 | // ensure that we have parsed the file |
michael@0 | 165 | getSections(); |
michael@0 | 166 | |
michael@0 | 167 | mSections.remove(newName); |
michael@0 | 168 | INISection section = mSections.get(oldName); |
michael@0 | 169 | if (section == null) |
michael@0 | 170 | return; |
michael@0 | 171 | |
michael@0 | 172 | section.setName(newName); |
michael@0 | 173 | mSections.remove(oldName); |
michael@0 | 174 | mSections.put(newName, section); |
michael@0 | 175 | } |
michael@0 | 176 | } |