2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.config;
25 * Extends the class {@link ConfigProperties} with basic parsing capabilities.
28 public class ParsedProperties extends ConfigProperties {
30 //================= variables ======================================
31 //==================================================================
33 // ================= initialization =================================
34 // ==================================================================
37 * Calls super constructor.
38 * @see ConfigProperties#ConfigProperties(String[])
40 public ParsedProperties( String[] pars ) {
45 // ------------------------------------------------------------------
48 * Calls super constructor.
49 * @see ConfigProperties#ConfigProperties(String)
51 public ParsedProperties( String filename ) throws IOException {
57 // =========== Public methods ========================================
58 // ===================================================================
62 * Loads given file. It works exactly as <code>Properties.load</code>
63 * with a file input stream to the given file, except that the file is parsed
64 * the following way allowing to compress some property names
65 * using <code>{</code> and <code>}</code>.
67 When a bracket is present, it must
68 be the only non-space element of a line. The last property defined
69 before the opening bracket define the prefix that is added to all the
70 properties defined included between brackets.
71 In other words, a construct like this:
73 control.degree GraphObserver
79 is equivalent to the definition of these three properties:
81 control.degree GraphObserver
82 control.degree.protocol newscast
86 Nested brackets are possible. The rule of the last property before
87 the opening bracket applies also to the inside brackets, with
88 the prefix being the complete property definition (so, including
89 the prefix observed before). Example:
91 control.1 DynamicNetwork
102 defines the following properties:
104 control.1 DynamicNetwork
107 control.1.init.0 WireKOut
108 control.1.init.0.degree DEGREE
109 control.1.init.0.protocol 0
114 The parsing mechanism is very simple; no complex error messages
115 are provided. In case of missing closing brackets, the method
116 will stop reporting the number of missing brackets. Additional
117 closing brackets (i.e., missing opening brackets) produce an
118 error messages reporting the line where the closing bracket
119 is present. Misplaced brackets (included in lines that
120 contains other characters) are ignored, thus may indirectly produce
121 the previous error messages.
123 public void load( String fileName ) throws IOException {
125 /* This set is used to store prefixes that have been associated
126 * to brackets blocks. If a prefix is inserted twice, this means
127 * that there are two blocks referring to the same prefix -
128 * which may be caused by a commented prefix in the config
129 * file, something like this:
141 Set<String> prefixes = new HashSet<String>();
144 new BufferedReader(new FileReader( fileName ));
146 parseStream(f, "", 0, lines, prefixes);
151 // --------------------------------------------------------------------
153 private int parseStream(BufferedReader f, String prefix, int pars,
154 int lines, Set prefixes)
157 if (prefix.equals(".")) {
158 System.err.println("Error at line " + lines + ": bracket block not " +
159 "associated with any configuration entry");
162 if (prefixes.contains(prefix)) {
163 System.err.println("Error at line " + lines + ": multiple bracket " +
164 "blocks referring to the same configuration entry " + prefix);
167 prefixes.add(prefix);
170 boolean complete = true;
174 while ((part = f.readLine()) != null)
179 if (complete) line = "";
181 // Check if the line is a comment line
182 // If so, remove the comment
183 int index = part.indexOf('#');
186 part = part.substring(0, index);
189 // Check if the line is empty
191 if ("".equals(part)) continue;
193 complete = (part.charAt(part.length()-1) != '\\');
196 line = line + part.substring(0, part.length()-2) + " ";
202 if (line.equals("{"))
204 lines = parseStream(f, last+".", pars+1, lines, prefixes);
206 else if (line.equals("}"))
211 "Error: Additional } at line " + lines +
212 " when parsing the configuration file");
219 // Search the first token
220 String[] tokens = line.split("[\\s:=]+", 2);
221 if (tokens.length == 1)
223 setProperty(prefix+tokens[0], "");
227 setProperty(prefix+tokens[0], tokens[1]);
229 last = prefix + tokens[0];
234 System.err.println("Error: One closing bracket ('}') is missing");
239 System.err.println("Error: " + pars+" closing brackets ('}') are missing");
245 // --------------------------------------------------------------------
248 public static void main(String[] args)
250 java.util.Properties prop = new ParsedProperties(args);