/src/logging-log4cxx/src/main/cpp/patternparser.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/pattern/patternparser.h> |
20 | | #include <log4cxx/pattern/literalpatternconverter.h> |
21 | | #include <log4cxx/helpers/loglog.h> |
22 | | |
23 | | using namespace LOG4CXX_NS; |
24 | | using namespace LOG4CXX_NS::pattern; |
25 | | using namespace LOG4CXX_NS::helpers; |
26 | | |
27 | | const logchar PatternParser::ESCAPE_CHAR = 0x25; // '%' |
28 | | |
29 | | |
30 | | /** |
31 | | * Private constructor. |
32 | | */ |
33 | | PatternParser::PatternParser() |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | | bool PatternParser::isUnicodeIdentifierStart(logchar ch) |
38 | 0 | { |
39 | | // |
40 | | // greatly simplified version checks if |
41 | | // character is USACII alpha or number |
42 | | // |
43 | 0 | return (ch >= 0x41 /* 'A' */ && ch <= 0x5A /* 'Z' */) || |
44 | 0 | (ch >= 0x61 /* 'a' */ && ch <= 0x7A /* 'z' */) || |
45 | 0 | (ch >= 0x30 /* '0' */ && ch <= 0x39 /* '9' */); |
46 | 0 | } |
47 | | |
48 | | bool PatternParser::isUnicodeIdentifierPart(logchar ch) |
49 | 0 | { |
50 | | // |
51 | | // greatly simplified version checks if |
52 | | // character is USACII alpha or number |
53 | | // |
54 | 0 | return isUnicodeIdentifierStart(ch) |
55 | 0 | || (ch == 0x5F /* '_' */); |
56 | 0 | } |
57 | | |
58 | | size_t PatternParser::extractConverter( |
59 | | logchar lastChar, const LogString& pattern, |
60 | | LogString::size_type i, LogString& convBuf, |
61 | | LogString& currentLiteral) |
62 | 0 | { |
63 | 0 | if (!convBuf.empty()) |
64 | 0 | { |
65 | 0 | convBuf.erase(convBuf.begin(), convBuf.end()); |
66 | 0 | } |
67 | | |
68 | | // When this method is called, lastChar points to the first character of the |
69 | | // conversion word. For example: |
70 | | // For "%hello" lastChar = 'h' |
71 | | // For "%-5hello" lastChar = 'h' |
72 | | //System.out.println("lastchar is "+lastChar); |
73 | 0 | if (!isUnicodeIdentifierStart(lastChar)) |
74 | 0 | { |
75 | 0 | return i; |
76 | 0 | } |
77 | | |
78 | 0 | convBuf.append(1, lastChar); |
79 | |
|
80 | 0 | while ( |
81 | 0 | (i < pattern.length()) |
82 | 0 | && isUnicodeIdentifierPart(pattern[i])) |
83 | 0 | { |
84 | 0 | convBuf.append(1, pattern[i]); |
85 | 0 | currentLiteral.append(1, pattern[i]); |
86 | | |
87 | | //System.out.println("conv buffer is now ["+convBuf+"]."); |
88 | 0 | i++; |
89 | 0 | } |
90 | |
|
91 | 0 | return i; |
92 | 0 | } |
93 | | |
94 | | |
95 | | size_t PatternParser::extractOptions(const LogString& pattern, LogString::size_type i, |
96 | | std::vector<LogString>& options) |
97 | 0 | { |
98 | 0 | while ((i < pattern.length()) && (pattern[i] == 0x7B /* '{' */)) |
99 | 0 | { |
100 | 0 | size_t end = pattern.find(0x7D /* '}' */, i); |
101 | |
|
102 | 0 | if (end == pattern.npos) |
103 | 0 | { |
104 | 0 | break; |
105 | 0 | } |
106 | | |
107 | 0 | LogString r(pattern.substr(i + 1, end - i - 1)); |
108 | 0 | options.push_back(r); |
109 | 0 | i = end + 1; |
110 | 0 | } |
111 | |
|
112 | 0 | return i; |
113 | 0 | } |
114 | | |
115 | | void PatternParser::parse( |
116 | | const LogString& pattern, |
117 | | std::vector<PatternConverterPtr>& patternConverters, |
118 | | std::vector<FormattingInfoPtr>& formattingInfos, |
119 | | const PatternMap& rules) |
120 | 0 | { |
121 | |
|
122 | 0 | LogString currentLiteral; |
123 | |
|
124 | 0 | size_t patternLength = pattern.length(); |
125 | 0 | int state = LITERAL_STATE; |
126 | 0 | logchar c; |
127 | 0 | size_t i = 0; |
128 | 0 | int minDigitCount{ 0 }, maxDigitCount{ 0 }; |
129 | 0 | FormattingInfoPtr formattingInfo(FormattingInfo::getDefault()); |
130 | |
|
131 | 0 | while (i < patternLength) |
132 | 0 | { |
133 | 0 | c = pattern[i++]; |
134 | |
|
135 | 0 | switch (state) |
136 | 0 | { |
137 | 0 | case LITERAL_STATE: |
138 | | |
139 | | // In literal state, the last char is always a literal. |
140 | 0 | if (i == patternLength) |
141 | 0 | { |
142 | 0 | currentLiteral.append(1, c); |
143 | |
|
144 | 0 | continue; |
145 | 0 | } |
146 | | |
147 | 0 | if (c == ESCAPE_CHAR) |
148 | 0 | { |
149 | | // peek at the next char. |
150 | 0 | if (pattern[i] == ESCAPE_CHAR) |
151 | 0 | { |
152 | 0 | currentLiteral.append(1, c); |
153 | 0 | i++; // move pointer |
154 | 0 | } |
155 | 0 | else |
156 | 0 | { |
157 | 0 | if (!currentLiteral.empty()) |
158 | 0 | { |
159 | 0 | patternConverters.push_back( |
160 | 0 | LiteralPatternConverter::newInstance(currentLiteral)); |
161 | 0 | formattingInfos.push_back(FormattingInfo::getDefault()); |
162 | 0 | currentLiteral.erase(currentLiteral.begin(), currentLiteral.end()); |
163 | 0 | } |
164 | |
|
165 | 0 | currentLiteral.append(1, c); // append % |
166 | 0 | state = CONVERTER_STATE; |
167 | 0 | formattingInfo = FormattingInfo::getDefault(); |
168 | 0 | } |
169 | 0 | } |
170 | 0 | else |
171 | 0 | { |
172 | 0 | currentLiteral.append(1, c); |
173 | 0 | } |
174 | |
|
175 | 0 | break; |
176 | | |
177 | 0 | case CONVERTER_STATE: |
178 | 0 | currentLiteral.append(1, c); |
179 | |
|
180 | 0 | switch (c) |
181 | 0 | { |
182 | 0 | case 0x2D: // '-' |
183 | 0 | formattingInfo = std::make_shared<FormattingInfo>( |
184 | 0 | true, formattingInfo->getMinLength(), |
185 | 0 | formattingInfo->getMaxLength()); |
186 | |
|
187 | 0 | break; |
188 | | |
189 | 0 | case 0x2E: // '.' |
190 | 0 | state = DOT_STATE; |
191 | |
|
192 | 0 | break; |
193 | | |
194 | 0 | default: |
195 | |
|
196 | 0 | if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */)) |
197 | 0 | { |
198 | 0 | formattingInfo = std::make_shared<FormattingInfo>( |
199 | 0 | formattingInfo->isLeftAligned(), c - 0x30 /* '0' */, |
200 | 0 | formattingInfo->getMaxLength()); |
201 | 0 | state = MIN_STATE; |
202 | 0 | minDigitCount = 1; |
203 | 0 | } |
204 | 0 | else |
205 | 0 | { |
206 | 0 | i = finalizeConverter( |
207 | 0 | c, pattern, i, currentLiteral, formattingInfo, |
208 | 0 | rules, patternConverters, formattingInfos); |
209 | | |
210 | | // Next pattern is assumed to be a literal. |
211 | 0 | state = LITERAL_STATE; |
212 | 0 | formattingInfo = FormattingInfo::getDefault(); |
213 | |
|
214 | 0 | if (!currentLiteral.empty()) |
215 | 0 | { |
216 | 0 | currentLiteral.erase(currentLiteral.begin(), currentLiteral.end()); |
217 | 0 | } |
218 | 0 | } |
219 | 0 | } // switch |
220 | | |
221 | 0 | break; |
222 | | |
223 | 0 | case MIN_STATE: |
224 | 0 | currentLiteral.append(1, c); |
225 | |
|
226 | 0 | if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */) && minDigitCount < 3) |
227 | 0 | { |
228 | 0 | formattingInfo = std::make_shared<FormattingInfo>( |
229 | 0 | formattingInfo->isLeftAligned(), |
230 | 0 | (formattingInfo->getMinLength() * 10) + (c - 0x30 /* '0' */), |
231 | 0 | formattingInfo->getMaxLength()); |
232 | 0 | ++minDigitCount; |
233 | 0 | } |
234 | 0 | else if (c == 0x2E /* '.' */) |
235 | 0 | { |
236 | 0 | state = DOT_STATE; |
237 | 0 | } |
238 | 0 | else |
239 | 0 | { |
240 | 0 | i = finalizeConverter( |
241 | 0 | c, pattern, i, currentLiteral, formattingInfo, |
242 | 0 | rules, patternConverters, formattingInfos); |
243 | 0 | state = LITERAL_STATE; |
244 | 0 | formattingInfo = FormattingInfo::getDefault(); |
245 | |
|
246 | 0 | if (!currentLiteral.empty()) |
247 | 0 | { |
248 | 0 | currentLiteral.erase(currentLiteral.begin(), currentLiteral.end()); |
249 | 0 | } |
250 | 0 | } |
251 | |
|
252 | 0 | break; |
253 | | |
254 | 0 | case DOT_STATE: |
255 | 0 | currentLiteral.append(1, c); |
256 | |
|
257 | 0 | if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */)) |
258 | 0 | { |
259 | 0 | formattingInfo = std::make_shared<FormattingInfo>( |
260 | 0 | formattingInfo->isLeftAligned(), formattingInfo->getMinLength(), |
261 | 0 | c - 0x30 /* '0' */); |
262 | 0 | state = MAX_STATE; |
263 | 0 | maxDigitCount = 1; |
264 | 0 | } |
265 | 0 | else |
266 | 0 | { |
267 | 0 | LogLog::error(LOG4CXX_STR("Error in pattern, was expecting digit.")); |
268 | |
|
269 | 0 | state = LITERAL_STATE; |
270 | 0 | } |
271 | |
|
272 | 0 | break; |
273 | | |
274 | 0 | case MAX_STATE: |
275 | 0 | currentLiteral.append(1, c); |
276 | |
|
277 | 0 | if ((c >= 0x30 /* '0' */) && (c <= 0x39 /* '9' */) && maxDigitCount < 3) |
278 | 0 | { |
279 | 0 | formattingInfo = std::make_shared<FormattingInfo>( |
280 | 0 | formattingInfo->isLeftAligned(), formattingInfo->getMinLength(), |
281 | 0 | (formattingInfo->getMaxLength() * 10) + (c - 0x30 /* '0' */)); |
282 | 0 | ++maxDigitCount; |
283 | 0 | } |
284 | 0 | else |
285 | 0 | { |
286 | 0 | i = finalizeConverter( |
287 | 0 | c, pattern, i, currentLiteral, formattingInfo, |
288 | 0 | rules, patternConverters, formattingInfos); |
289 | 0 | state = LITERAL_STATE; |
290 | 0 | formattingInfo = FormattingInfo::getDefault(); |
291 | |
|
292 | 0 | if (!currentLiteral.empty()) |
293 | 0 | { |
294 | 0 | currentLiteral.erase(currentLiteral.begin(), currentLiteral.end()); |
295 | 0 | } |
296 | 0 | } |
297 | |
|
298 | 0 | break; |
299 | 0 | } // switch |
300 | 0 | } |
301 | | |
302 | | // while |
303 | 0 | if (currentLiteral.length() != 0) |
304 | 0 | { |
305 | 0 | patternConverters.push_back( |
306 | 0 | LiteralPatternConverter::newInstance(currentLiteral)); |
307 | 0 | formattingInfos.push_back(FormattingInfo::getDefault()); |
308 | 0 | } |
309 | 0 | } |
310 | | |
311 | | |
312 | | PatternConverterPtr PatternParser::createConverter( |
313 | | const LogString& converterId, |
314 | | LogString& currentLiteral, |
315 | | const PatternMap& rules, |
316 | | std::vector<LogString>& options) |
317 | 0 | { |
318 | |
|
319 | 0 | LogString converterName(converterId); |
320 | |
|
321 | 0 | for (size_t i = converterId.length(); i > 0; i--) |
322 | 0 | { |
323 | 0 | converterName = converterName.substr(0, i); |
324 | 0 | PatternMap::const_iterator iter = rules.find(converterName); |
325 | |
|
326 | 0 | if (iter != rules.end()) |
327 | 0 | { |
328 | 0 | currentLiteral.erase(currentLiteral.begin(), |
329 | 0 | currentLiteral.end() - (converterId.length() - i)); |
330 | 0 | return (iter->second)(options); |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | 0 | LogLog::error(LogString(LOG4CXX_STR("Unrecognized format specifier ")) + converterId); |
335 | |
|
336 | 0 | return PatternConverterPtr(); |
337 | 0 | } |
338 | | |
339 | | size_t PatternParser::finalizeConverter( |
340 | | logchar c, const LogString& pattern, size_t i, |
341 | | LogString& currentLiteral, const FormattingInfoPtr& formattingInfo, |
342 | | const PatternMap& rules, |
343 | | std::vector<PatternConverterPtr>& patternConverters, |
344 | | std::vector<FormattingInfoPtr>& formattingInfos) |
345 | 0 | { |
346 | 0 | LogString convBuf; |
347 | 0 | i = extractConverter(c, pattern, i, convBuf, currentLiteral); |
348 | |
|
349 | 0 | if (convBuf.empty()) |
350 | 0 | { |
351 | 0 | LogLog::error(LOG4CXX_STR("Empty conversion specifier")); |
352 | 0 | patternConverters.push_back( |
353 | 0 | LiteralPatternConverter::newInstance(currentLiteral)); |
354 | 0 | formattingInfos.push_back(FormattingInfo::getDefault()); |
355 | 0 | } |
356 | 0 | else |
357 | 0 | { |
358 | 0 | LogString converterId(convBuf); |
359 | |
|
360 | 0 | std::vector<LogString> options; |
361 | 0 | i = extractOptions(pattern, i, options); |
362 | |
|
363 | 0 | PatternConverterPtr pc( |
364 | 0 | createConverter( |
365 | 0 | converterId, currentLiteral, rules, options)); |
366 | |
|
367 | 0 | if (pc == NULL) |
368 | 0 | { |
369 | 0 | LogString msg(LOG4CXX_STR("Unrecognized conversion specifier [")); |
370 | 0 | msg.append(converterId); |
371 | 0 | msg.append(LOG4CXX_STR("] in conversion pattern.")); |
372 | 0 | LogLog::error(msg); |
373 | 0 | patternConverters.push_back( |
374 | 0 | LiteralPatternConverter::newInstance(currentLiteral)); |
375 | 0 | formattingInfos.push_back(FormattingInfo::getDefault()); |
376 | 0 | } |
377 | 0 | else |
378 | 0 | { |
379 | 0 | patternConverters.push_back(pc); |
380 | 0 | formattingInfos.push_back(formattingInfo); |
381 | |
|
382 | 0 | if (currentLiteral.length() > 0) |
383 | 0 | { |
384 | 0 | patternConverters.push_back( |
385 | 0 | LiteralPatternConverter::newInstance(currentLiteral)); |
386 | 0 | formattingInfos.push_back(FormattingInfo::getDefault()); |
387 | 0 | } |
388 | 0 | } |
389 | 0 | } |
390 | |
|
391 | 0 | if (!currentLiteral.empty()) |
392 | 0 | { |
393 | 0 | currentLiteral.erase(currentLiteral.begin(), currentLiteral.end()); |
394 | 0 | } |
395 | |
|
396 | 0 | return i; |
397 | 0 | } |