summaryrefslogtreecommitdiff
path: root/java/src/Ice/PropertiesI.java
blob: 8a506ef4171427d98091bf6446d8a63d5c8a4e4e (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
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

package Ice;

class PropertiesI implements Properties
{
    public String
    getProperty(String key)
    {
        String result = (String)_properties.get(key);
        if (result == null)
        {
            result = System.getProperty(key);
        }
        return result;
    }

    public void
    setProperty(String key, String value)
    {
        _properties.put(key, value);
    }

    public Properties
    _clone()
    {
        PropertiesI p = new PropertiesI(new String[0]);
        p._properties.putAll(_properties);
        return p;
    }

    PropertiesI(String[] args)
    {
        for (int i = 0; i < args.length; i++)
        {
            if (args[i].startsWith("--Ice.Config"))
            {
                String line = args[i];
                if (line.indexOf('=') == -1)
                {
                    line += "=1";
                }
                parseLine(line.substring(2));
            }
        }

        String file = getProperty("Ice.Config");

        if (file == null || file.equals("1"))
        {
            file = "";
        }

        if (file.length() > 0)
        {
            load(file);
        }

        parseArgs(args);
        setProperty("Ice.Config", file);
    }

    PropertiesI(String[] args, String file)
    {
        if (file == null)
        {
            file = "";
        }

        if (file.length() > 0)
        {
            load(file);
        }

        parseArgs(args);
        setProperty("Ice.Config", file);
    }

    private void
    parseArgs(String[] args)
    {
        int idx = 0;
        while (idx < args.length)
        {
            if (args[idx].startsWith("--Ice."))
            {
                String line = args[idx];
                if (line.indexOf('=') == -1)
                {
                    line += "=1";
                }

                parseLine(line.substring(2));
            }
            else
            {
                idx++;
            }
        }
    }

    private void
    load(String file)
    {
        try
        {
            java.io.FileReader fr = new java.io.FileReader(file);
            java.io.BufferedReader br = new java.io.BufferedReader(br);
            parse(br);
        }
        catch (java.io.IOException ex)
        {
            SystemException ex = new SystemException();
            ex.initCause(ex); // Exception chaining
            throw ex;
        }
    }

    private void
    parse(java.io.BufferedReader in)
    {
        String line;
        while ((line = in.readLine()) != null)
        {
            parseLine(line);
        }
    }

    private void
    parseLine(String line)
    {
        String s = line;

        int hash = s.indexOf('#');
        if (hash == 0)
        {
            return; // ignore comment lines
        }
        else if (hash != -1)
        {
            s = s.substring(0, hash);
        }

        s = s.trim();

        // TODO: Could also use a regex
        int pos = s.indexOf(' ');
        if (pos == -1)
        {
            pos = s.indexOf('\t');
        }
        if (pos == -1)
        {
            pos = s.indexOf('=');
        }
        if (pos == -1 || pos == s.length() - 1)
        {
            return;
        }

        String key = s.substring(0, pos);
        String value = s.substring(pos + 1).trim();
        setProperty(key, value);
    }

    private java.util.HashMap _properties = new java.util.HashMap();
}