1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef _LOG4CXX_HELPER_PROPERTIES_H
|
---|
19 | #define _LOG4CXX_HELPER_PROPERTIES_H
|
---|
20 |
|
---|
21 | #if defined(_MSC_VER)
|
---|
22 | #pragma warning (push)
|
---|
23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
24 | #endif
|
---|
25 |
|
---|
26 |
|
---|
27 | #include <log4cxx/logstring.h>
|
---|
28 | #include <log4cxx/helpers/objectptr.h>
|
---|
29 | #include <log4cxx/helpers/objectimpl.h>
|
---|
30 | #include <log4cxx/helpers/inputstream.h>
|
---|
31 | #include <map>
|
---|
32 | #include <vector>
|
---|
33 | #include <istream>
|
---|
34 |
|
---|
35 | namespace log4cxx
|
---|
36 | {
|
---|
37 | namespace helpers
|
---|
38 | {
|
---|
39 | class LOG4CXX_EXPORT Properties
|
---|
40 | {
|
---|
41 | private:
|
---|
42 | typedef std::map<LogString, LogString> PropertyMap;
|
---|
43 | PropertyMap* properties;
|
---|
44 | Properties(const Properties&);
|
---|
45 | Properties& operator=(const Properties&);
|
---|
46 |
|
---|
47 | public:
|
---|
48 | /**
|
---|
49 | * Create new instance.
|
---|
50 | */
|
---|
51 | Properties();
|
---|
52 | /**
|
---|
53 | * Destructor.
|
---|
54 | */
|
---|
55 | ~Properties();
|
---|
56 | /**
|
---|
57 | Reads a property list (key and element pairs) from the input stream.
|
---|
58 | The stream is assumed to be using the ISO 8859-1 character encoding.
|
---|
59 |
|
---|
60 | <p>Every property occupies one line of the input stream.
|
---|
61 | Each line is terminated by a line terminator (<code>\\n</code> or
|
---|
62 | <code>\\r</code> or <code>\\r\\n</code>).
|
---|
63 | Lines from the input stream are processed until end of file is reached
|
---|
64 | on the input stream.
|
---|
65 |
|
---|
66 | <p>A line that contains only whitespace or whose first non-whitespace
|
---|
67 | character is an ASCII <code>#</code> or <code>!</code> is ignored
|
---|
68 | (thus, <code>#</code> or <code>!</code> indicate comment lines).
|
---|
69 |
|
---|
70 | <p>Every line other than a blank line or a comment line describes one
|
---|
71 | property to be added to the table (except that if a line ends with \,
|
---|
72 | then the following line, if it exists, is treated as a continuation
|
---|
73 | line, as described below). The key consists of all the characters in
|
---|
74 | the line starting with the first non-whitespace character and up to,
|
---|
75 | but not including, the first ASCII <code>=</code>, <code>:</code>,
|
---|
76 | or whitespace character. All of the
|
---|
77 | key termination characters may be included in the key by preceding them
|
---|
78 | with a <code>\\</code>. Any whitespace after the key is skipped;
|
---|
79 | if the first
|
---|
80 | non-whitespace character after the key is <code>=</code> or
|
---|
81 | <code>:</code>, then it is ignored
|
---|
82 | and any whitespace characters after it are also skipped. All remaining
|
---|
83 | characters on the line become part of the associated element string.
|
---|
84 | Within the element string, the ASCII escape sequences <code>\\t</code>,
|
---|
85 | <code>\\n</code>, <code>\\r</code>, <code>\\</code>, <code>\\"</code>,
|
---|
86 | <code>\\'</code>, <code>\\</code> (a backslash and a space), and
|
---|
87 | <code>\\uxxxx</code> are recognized
|
---|
88 | and converted to single characters. Moreover, if the last character on
|
---|
89 | the line is <code>\\</code>, then the next line is treated as a
|
---|
90 | continuation of the
|
---|
91 | current line; the <code>\\</code> and line terminator are simply
|
---|
92 | discarded, and any
|
---|
93 | leading whitespace characters on the continuation line are also
|
---|
94 | discarded and are not part of the element string.
|
---|
95 |
|
---|
96 | <p>As an example, each of the following four lines specifies the key
|
---|
97 | "Truth" and the associated element value "Beauty":
|
---|
98 |
|
---|
99 | <pre>
|
---|
100 | Truth = Beauty
|
---|
101 | Truth:Beauty
|
---|
102 | Truth :Beauty
|
---|
103 | </pre>
|
---|
104 |
|
---|
105 | As another example, the following three lines specify a single
|
---|
106 | property:
|
---|
107 | <pre>
|
---|
108 | fruits apple, banana, pear, \
|
---|
109 | cantaloupe, watermelon, \
|
---|
110 | kiwi, mango
|
---|
111 | </pre>
|
---|
112 | The key is "<code>fruits</code>" and the associated element is:
|
---|
113 | <pre>
|
---|
114 | "apple, banana, pear, cantaloupe, watermelon, kiwi, mango"
|
---|
115 | </pre>
|
---|
116 | Note that a space appears before each \ so that a space will appear
|
---|
117 | after each comma in the final result; the \, line terminator, and
|
---|
118 | leading whitespace on the continuation line are merely discarded and are
|
---|
119 | not replaced by one or more other characters.
|
---|
120 |
|
---|
121 | <p>As a third example, the line:
|
---|
122 | <pre>
|
---|
123 | cheeses
|
---|
124 | </pre>
|
---|
125 | specifies that the key is "<code>cheeses</code>" and the associated
|
---|
126 | element is the empty string.
|
---|
127 |
|
---|
128 | @param inStream the input stream.
|
---|
129 |
|
---|
130 | @throw IOException if an error occurred when reading from the input
|
---|
131 | stream.
|
---|
132 | */
|
---|
133 | void load(InputStreamPtr inStream);
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Calls Properties::put.
|
---|
137 | * @param key the key to be placed into this property list.
|
---|
138 | * @param value the value corresponding to key.
|
---|
139 | * @return the previous value of the specified key in this
|
---|
140 | * property list, or an empty string if it did not have one.
|
---|
141 | */
|
---|
142 | LogString setProperty(const LogString& key, const LogString& value);
|
---|
143 | /**
|
---|
144 | * Puts a property value into the collection.
|
---|
145 | * @param key the key to be placed into this property list.
|
---|
146 | * @param value the value corresponding to key.
|
---|
147 | * @return the previous value of the specified key in this
|
---|
148 | * property list, or an empty string if it did not have one.
|
---|
149 | */
|
---|
150 | LogString put(const LogString& key, const LogString& value);
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Calls Properties::get.
|
---|
155 | * @param key the property key.
|
---|
156 | * @return the value in this property list with the specified
|
---|
157 | * key value or empty string.
|
---|
158 | */
|
---|
159 | LogString getProperty(const LogString& key) const;
|
---|
160 | /**
|
---|
161 | * Gets a property value.
|
---|
162 | * @param key the property key.
|
---|
163 | * @return the value in this property list with the specified
|
---|
164 | * key value or empty string.
|
---|
165 | */
|
---|
166 | LogString get(const LogString& key) const;
|
---|
167 |
|
---|
168 | /**
|
---|
169 | Returns an enumeration of all the keys in this property list,
|
---|
170 | including distinct keys in the default property list if a key
|
---|
171 | of the same name has not already been found from the main
|
---|
172 | properties list.
|
---|
173 | @return an array of all the keys in this
|
---|
174 | property list, including the keys in the default property list.
|
---|
175 | */
|
---|
176 | std::vector<LogString> propertyNames() const;
|
---|
177 | }; // class Properties
|
---|
178 | } // namespace helpers
|
---|
179 | } // namespace log4cxx
|
---|
180 |
|
---|
181 | #if defined(_MSC_VER)
|
---|
182 | #pragma warning (pop)
|
---|
183 | #endif
|
---|
184 |
|
---|
185 |
|
---|
186 | #endif //_LOG4CXX_HELPER_PROPERTIES_H
|
---|