summaryrefslogtreecommitdiff
path: root/qpid/java/broker-plugins/experimental/info/src/main/java/org/apache/qpid/info/util/IniFileReader.java
blob: 60a025d3221f31eecfdb8d22a0564734d14ff4c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package org.apache.qpid.info.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * This class is simple implementation of an ini file reader. It expects a
 * file with the following structure:
 *
 * ; global values, can be overwritten in sections
 * key1=value1
 * key2=value2
 *
 * [Section1]
 * key1=value1_new  ; overwriting the global key1
 * key3=value3
 * key4=value4
 *
 * [Section2]
 * key5=value5
 * key6=value6
 * key7=value7
 *
 * Note: Commentaries are preceded by ; or # and are supported throughout
 * A commentary line at the end of section is interpreted as
 * a section end marker
 *
 * A structure <String,Properties> (section name, associated properties)
 * is generated as a result of processing the ini file.
 */
public class IniFileReader
{
    private final Map<String, Properties> _sections;

    private final String COMMENT_SEMICOLON = ";";

    private final String COMMENT_HASH = "#";

    enum State
    {
        IN_SECTION, OFF_SECTION, GLOBAL
    }

    /*
     * IniFileReader constructor
     */

    public IniFileReader()
    {
        _sections = new HashMap<String, Properties>();
    }

    /**
     * Cleans up the after comments or the empty spaces/tabs surrounding the given string
     *
     * @param str The String to be cleaned
     *
     * @return String Cleanup Version
     */
    private String cleanUp(String str)
    {
        if (str.contains(COMMENT_SEMICOLON))
        {
            str = str.substring(0, str.indexOf(COMMENT_SEMICOLON));
        }
        if (str.contains(COMMENT_HASH))
        {
            str = str.substring(0, str.indexOf(COMMENT_HASH));
        }
        return str.trim();
    }

    /**
     * Loads and parses the ini file with the full path specified in the argument
     *
     * @param fileName Full path to the ini file
     *
     * @throws IllegalArgumentException If the file cannot be processed
     */
    public void load(String fileName) throws IllegalArgumentException
    {
        if (!new File(fileName).isFile())
        {
            throw new IllegalArgumentException("File: " + fileName + " does not exist or cannot be read.");
        }
        State state = State.GLOBAL;
        String line;
        Properties sectionProps = new Properties();
        String sectionName = "";
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(fileName));
            while ((line = in.readLine()) != null)
            {
                String str = cleanUp(line);

                // Did we get a section header?
                if (str.startsWith("["))
                {
                    if (!str.endsWith("]"))
                    {
                        // Index of 1 to skip '['
                        throw new IllegalArgumentException(str.substring(1)
                                                           + " is not closed");
                    }

                    // We encountered a new section header
                    if (state != State.IN_SECTION)
                    {
                        _sections.put(sectionName, sectionProps);
                        sectionProps = new Properties();
                        sectionName = str.replace("[", "").replace("]", "")
                                .trim();
                        state = State.IN_SECTION;
                    }
                }

                // Any other line tested separately, ignore if out of a section
                // and add if in section
                if (str.length() == 0)
                {
                    // We encountered a commented or an empty line, both cases
                    // mean we are off the section
                    if (state == State.IN_SECTION)
                    {
                        _sections.put(sectionName, sectionProps);
                        state = State.OFF_SECTION;
                    }
                }
                else
                {
                    // proper line, add it to the props
                    if (state != State.OFF_SECTION)
                    {
                        if (str.contains("="))
                        {
                            int ix = str.indexOf("=");
                            sectionProps.put(str.substring(0, ix).trim(), str
                                    .substring(ix + 1).trim());
                        }
                    }
                }
            }
            in.close();
        }
        catch (IOException e)
        {
            _sections.clear();
            return;
        }
        if (state != State.OFF_SECTION)
        {
            _sections.put(sectionName, sectionProps);
        }
    }

    /**
     * Getter for the Sections Map
     *
     * @return Map<String,Properties> The parsed content of the ini file in this structure
     */
    public Map<String, Properties> getSections()
    {
        return _sections;
    }

}