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 |
|
---|
19 | #ifndef _LOG4CXX_HELPER_PATTERN_CONVERTER_H
|
---|
20 | #define _LOG4CXX_HELPER_PATTERN_CONVERTER_H
|
---|
21 |
|
---|
22 | #if defined(_MSC_VER)
|
---|
23 | #pragma warning (push)
|
---|
24 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
---|
25 | #endif
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | #include <map>
|
---|
30 | #include <vector>
|
---|
31 | #include <log4cxx/helpers/class.h>
|
---|
32 | #include <log4cxx/pattern/patternconverter.h>
|
---|
33 | #include <log4cxx/pattern/formattinginfo.h>
|
---|
34 |
|
---|
35 | namespace log4cxx {
|
---|
36 | namespace pattern {
|
---|
37 |
|
---|
38 | typedef PatternConverterPtr (*PatternConstructor)(const std::vector<LogString>& options);
|
---|
39 | typedef std::map<LogString, PatternConstructor> PatternMap;
|
---|
40 |
|
---|
41 |
|
---|
42 | // Contributors: Nelson Minar <(nelson@monkey.org>
|
---|
43 | // Igor E. Poteryaev <jah@mail.ru>
|
---|
44 | // Reinhard Deschler <reinhard.deschler@web.de>
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Most of the work of the {@link log4cxx::PatternLayout PatternLayout} class
|
---|
48 | * is delegated to the PatternParser class.
|
---|
49 | * <p>It is this class that parses conversion patterns and creates
|
---|
50 | * a chained list of {@link PatternConverter PatternConverters}.
|
---|
51 | *
|
---|
52 | *
|
---|
53 | */
|
---|
54 | class LOG4CXX_EXPORT PatternParser {
|
---|
55 | /**
|
---|
56 | * Escape character for format specifier.
|
---|
57 | */
|
---|
58 | static const logchar ESCAPE_CHAR;
|
---|
59 |
|
---|
60 | enum {
|
---|
61 | LITERAL_STATE = 0,
|
---|
62 | CONVERTER_STATE = 1,
|
---|
63 | DOT_STATE = 3,
|
---|
64 | MIN_STATE = 4,
|
---|
65 | MAX_STATE = 5
|
---|
66 | };
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Private constructor.
|
---|
70 | */
|
---|
71 | PatternParser();
|
---|
72 |
|
---|
73 | private:
|
---|
74 | /** Extract the converter identifier found at position i.
|
---|
75 | *
|
---|
76 | * After this function returns, the variable i will point to the
|
---|
77 | * first char after the end of the converter identifier.
|
---|
78 | *
|
---|
79 | * If i points to a char which is not a character acceptable at the
|
---|
80 | * start of a unicode identifier, the value null is returned.
|
---|
81 | *
|
---|
82 | * @param lastChar last processed character.
|
---|
83 | * @param pattern format string.
|
---|
84 | * @param i current index into pattern format.
|
---|
85 | * @param convBuf buffer to receive conversion specifier.
|
---|
86 | * @param currentLiteral literal to be output in case format specifier in unrecognized.
|
---|
87 | * @return position in pattern after converter.
|
---|
88 | */
|
---|
89 | static int extractConverter(
|
---|
90 | logchar lastChar, const LogString& pattern,
|
---|
91 | LogString::size_type i, LogString& convBuf,
|
---|
92 | LogString& currentLiteral);
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Extract options.
|
---|
96 | * @param pattern conversion pattern.
|
---|
97 | * @param i start of options.
|
---|
98 | * @param options array to receive extracted options
|
---|
99 | * @return position in pattern after options.
|
---|
100 | */
|
---|
101 | static int extractOptions(const LogString& pattern, LogString::size_type i,
|
---|
102 | std::vector<LogString>& options);
|
---|
103 |
|
---|
104 | public:
|
---|
105 | /**
|
---|
106 | * Parse a format specifier.
|
---|
107 | * @param pattern pattern to parse.
|
---|
108 | * @param patternConverters list to receive pattern converters.
|
---|
109 | * @param formattingInfos list to receive field specifiers corresponding to pattern converters.
|
---|
110 | * @param rules map of stock pattern converters keyed by format specifier.
|
---|
111 | */
|
---|
112 | static void parse(
|
---|
113 | const LogString& pattern,
|
---|
114 | std::vector<PatternConverterPtr>& patternConverters,
|
---|
115 | std::vector<FormattingInfoPtr>& formattingInfos,
|
---|
116 | const PatternMap& rules);
|
---|
117 |
|
---|
118 | private:
|
---|
119 | /**
|
---|
120 | * Creates a new PatternConverter.
|
---|
121 | *
|
---|
122 | *
|
---|
123 | * @param converterId converterId.
|
---|
124 | * @param currentLiteral literal to be used if converter is unrecognized or following converter
|
---|
125 | * if converterId contains extra characters.
|
---|
126 | * @param rules map of stock pattern converters keyed by format specifier.
|
---|
127 | * @param options converter options.
|
---|
128 | * @return converter or null.
|
---|
129 | */
|
---|
130 | static PatternConverterPtr createConverter(
|
---|
131 | const LogString& converterId,
|
---|
132 | LogString& currentLiteral,
|
---|
133 | const PatternMap& rules,
|
---|
134 | std::vector<LogString>& options);
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Processes a format specifier sequence.
|
---|
138 | *
|
---|
139 | * @param c initial character of format specifier.
|
---|
140 | * @param pattern conversion pattern
|
---|
141 | * @param i current position in conversion pattern.
|
---|
142 | * @param currentLiteral current literal.
|
---|
143 | * @param formattingInfo current field specifier.
|
---|
144 | * @param rules map of stock pattern converters keyed by format specifier.
|
---|
145 | * @param patternConverters list to receive parsed pattern converter.
|
---|
146 | * @param formattingInfos list to receive corresponding field specifier.
|
---|
147 | * @return position after format specifier sequence.
|
---|
148 | */
|
---|
149 | static int finalizeConverter(
|
---|
150 | logchar c, const LogString& pattern, int i,
|
---|
151 | LogString& currentLiteral, const FormattingInfoPtr& formattingInfo,
|
---|
152 | const PatternMap& rules,
|
---|
153 | std::vector<PatternConverterPtr>& patternConverters,
|
---|
154 | std::vector<FormattingInfoPtr>& formattingInfos);
|
---|
155 |
|
---|
156 | static bool isUnicodeIdentifierStart(logchar c);
|
---|
157 | static bool isUnicodeIdentifierPart(logchar c);
|
---|
158 |
|
---|
159 |
|
---|
160 | };
|
---|
161 |
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | #if defined(_MSC_VER)
|
---|
167 | #pragma warning (pop)
|
---|
168 | #endif
|
---|
169 |
|
---|
170 |
|
---|
171 | #endif
|
---|