mobile/android/base/util/INIParser.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/INIParser.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,176 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.util;
     1.9 +
    1.10 +import java.io.BufferedReader;
    1.11 +import java.io.BufferedWriter;
    1.12 +import java.io.File;
    1.13 +import java.io.FileNotFoundException;
    1.14 +import java.io.FileReader;
    1.15 +import java.io.FileWriter;
    1.16 +import java.io.IOException;
    1.17 +import java.util.Enumeration;
    1.18 +import java.util.Hashtable;
    1.19 +
    1.20 +public final class INIParser extends INISection {
    1.21 +    // default file to read and write to
    1.22 +    private File mFile = null;
    1.23 +
    1.24 +    // List of sections in the current iniFile. null if the file has not been parsed yet
    1.25 +    private Hashtable<String, INISection> mSections = null;
    1.26 +
    1.27 +    // create a parser. The file will not be read until you attempt to
    1.28 +    // access sections or properties inside it. At that point its read synchronously
    1.29 +    public INIParser(File iniFile) {
    1.30 +        super("");
    1.31 +        mFile = iniFile;
    1.32 +    }
    1.33 +
    1.34 +    // write ini data to the default file. Will overwrite anything current inside
    1.35 +    public void write() {
    1.36 +        writeTo(mFile);
    1.37 +    }
    1.38 +
    1.39 +    // write to the specified file. Will overwrite anything current inside
    1.40 +    public void writeTo(File f) {
    1.41 +        if (f == null)
    1.42 +            return;
    1.43 +  
    1.44 +        FileWriter outputStream = null;
    1.45 +        try {
    1.46 +            outputStream = new FileWriter(f);
    1.47 +        } catch (IOException e1) {
    1.48 +            e1.printStackTrace();
    1.49 +        }
    1.50 +  
    1.51 +        BufferedWriter writer = new BufferedWriter(outputStream);
    1.52 +        try {
    1.53 +            write(writer);
    1.54 +            writer.close();
    1.55 +        } catch (IOException e) {
    1.56 +            e.printStackTrace();
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60 +    @Override
    1.61 +    public void write(BufferedWriter writer) throws IOException {
    1.62 +        super.write(writer);
    1.63 +
    1.64 +        if (mSections != null) {
    1.65 +            for (Enumeration<INISection> e = mSections.elements(); e.hasMoreElements();) {
    1.66 +                INISection section = e.nextElement();
    1.67 +                section.write(writer);
    1.68 +                writer.newLine();
    1.69 +            }
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    // return all of the sections inside this file
    1.74 +    public Hashtable<String, INISection> getSections() {
    1.75 +        if (mSections == null) {
    1.76 +            try {
    1.77 +                parse();
    1.78 +            } catch (IOException e) {
    1.79 +                debug("Error parsing: " + e);
    1.80 +            }
    1.81 +        }
    1.82 +        return mSections;
    1.83 +    }
    1.84 +
    1.85 +    // parse the default file
    1.86 +    @Override
    1.87 +    protected void parse() throws IOException {
    1.88 +        super.parse();
    1.89 +        parse(mFile);
    1.90 +    }
    1.91 +   
    1.92 +    // parse a passed in file
    1.93 +    private void parse(File f) throws IOException {
    1.94 +        // Set up internal data members
    1.95 +        mSections = new Hashtable<String, INISection>();
    1.96 +  
    1.97 +        if (f == null || !f.exists())
    1.98 +            return;
    1.99 +  
   1.100 +        FileReader inputStream = null;
   1.101 +        try {
   1.102 +            inputStream = new FileReader(f);
   1.103 +        } catch (FileNotFoundException e1) {
   1.104 +            // If the file doesn't exist. Just return;
   1.105 +            return;
   1.106 +        }
   1.107 +  
   1.108 +        BufferedReader buf = new BufferedReader(inputStream);
   1.109 +        String line = null;            // current line of text we are parsing
   1.110 +        INISection currentSection = null; // section we are currently parsing
   1.111 +  
   1.112 +        while ((line = buf.readLine()) != null) {
   1.113 +  
   1.114 +            if (line != null)
   1.115 +                line = line.trim();
   1.116 +  
   1.117 +            // blank line or a comment. ignore it
   1.118 +            if (line == null || line.length() == 0 || line.charAt(0) == ';') {
   1.119 +                debug("Ignore line: " + line);
   1.120 +            } else if (line.charAt(0) == '[') {
   1.121 +                debug("Parse as section: " + line);
   1.122 +                currentSection = new INISection(line.substring(1, line.length()-1));
   1.123 +                mSections.put(currentSection.getName(), currentSection);
   1.124 +            } else {
   1.125 +                debug("Parse as property: " + line);
   1.126 +  
   1.127 +                String[] pieces = line.split("=");
   1.128 +                if (pieces.length != 2)
   1.129 +                    continue;
   1.130 +  
   1.131 +                String key = pieces[0].trim();
   1.132 +                String value = pieces[1].trim();
   1.133 +                if (currentSection != null) {
   1.134 +                    currentSection.setProperty(key, value);
   1.135 +                } else {
   1.136 +                    mProperties.put(key, value);
   1.137 +                }
   1.138 +            }
   1.139 +        }
   1.140 +        buf.close();
   1.141 +    }
   1.142 +
   1.143 +    // add a section to the file
   1.144 +    public void addSection(INISection sect) {
   1.145 +        // ensure that we have parsed the file
   1.146 +        getSections();
   1.147 +        mSections.put(sect.getName(), sect);
   1.148 +    }
   1.149 +
   1.150 +    // get a section from the file. will return null if the section doesn't exist
   1.151 +    public INISection getSection(String key) {
   1.152 +        // ensure that we have parsed the file
   1.153 +        getSections();
   1.154 +        return mSections.get(key);
   1.155 +    }
   1.156 + 
   1.157 +    // remove an entire section from the file
   1.158 +    public void removeSection(String name) {
   1.159 +        // ensure that we have parsed the file
   1.160 +        getSections();
   1.161 +        mSections.remove(name);
   1.162 +    }
   1.163 +
   1.164 +    // rename a section; nuking any previous section with the new
   1.165 +    // name in the process
   1.166 +    public void renameSection(String oldName, String newName) {
   1.167 +        // ensure that we have parsed the file
   1.168 +        getSections();
   1.169 +
   1.170 +        mSections.remove(newName);
   1.171 +        INISection section = mSections.get(oldName);
   1.172 +        if (section == null)
   1.173 +            return;
   1.174 +
   1.175 +        section.setName(newName);
   1.176 +        mSections.remove(oldName);
   1.177 +        mSections.put(newName, section);
   1.178 +    }
   1.179 +}

mercurial