1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Formatting rules are provided by Trace objects |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.engine.source; |
|
20 |
|
21 import java.util.Iterator; |
|
22 |
|
23 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals; |
|
24 import com.nokia.tracecompiler.engine.rules.StateTraceRule; |
|
25 import com.nokia.tracecompiler.model.Trace; |
|
26 import com.nokia.tracecompiler.model.TraceParameter; |
|
27 import com.nokia.tracecompiler.plugin.TraceFormatConstants; |
|
28 import com.nokia.tracecompiler.plugin.TraceAPIFormatter.TraceFormatType; |
|
29 import com.nokia.tracecompiler.source.SourceConstants; |
|
30 import com.nokia.tracecompiler.source.SourceUtils; |
|
31 import com.nokia.tracecompiler.source.SymbianConstants; |
|
32 |
|
33 /** |
|
34 * Static functions for source formatting |
|
35 * |
|
36 */ |
|
37 public class SourceFormatter { |
|
38 |
|
39 /** |
|
40 * Value parameter name |
|
41 */ |
|
42 public static final String VALUE_PARAMETER_NAME = "value"; //$NON-NLS-1$ |
|
43 |
|
44 /** |
|
45 * Event start trace id parameter name |
|
46 */ |
|
47 public static final String EVENT_START_TRACE_ID_PARAMETER_NAME = "linkToStart"; //$NON-NLS-1$ |
|
48 |
|
49 /** |
|
50 * Constructor is hidden |
|
51 */ |
|
52 private SourceFormatter() { |
|
53 } |
|
54 |
|
55 /** |
|
56 * Formats a trace to string format using the formatting rule from the trace |
|
57 * |
|
58 * @param trace |
|
59 * the trace |
|
60 * @param formatType |
|
61 * the type of format |
|
62 * @return the trace string |
|
63 */ |
|
64 public static String formatTrace(Trace trace, TraceFormatType formatType) { |
|
65 return formatTrace(trace, null, formatType, null, false); |
|
66 } |
|
67 |
|
68 /** |
|
69 * Formats a trace to string format |
|
70 * |
|
71 * @param trace |
|
72 * the trace |
|
73 * @param traceRule |
|
74 * the formatting rule to be used |
|
75 * @param formatType |
|
76 * the type of format |
|
77 * @param tags |
|
78 * the tags for parameters or null if parameter names are used |
|
79 * @param fixedTags |
|
80 * true if the <i>tags</i> iterator is fixed, false if the |
|
81 * contents of <i>tags</i> should go through |
|
82 * <code>SourceRule.mapNameToSource</code> |
|
83 * @return the trace string |
|
84 */ |
|
85 static String formatTrace(Trace trace, TraceFormattingRule traceRule, |
|
86 TraceFormatType formatType, Iterator<String> tags, boolean fixedTags) { |
|
87 StringBuffer data = new StringBuffer(); |
|
88 String format = null; |
|
89 if (traceRule == null) { |
|
90 |
|
91 // If rule is not explicitly provided, it is fetched from the trace |
|
92 traceRule = trace.getExtension(TraceFormattingRule.class); |
|
93 if (traceRule == null) { |
|
94 |
|
95 // If trace does not have a formatting rule, the project API's |
|
96 // should implement default rule |
|
97 traceRule = trace.getModel().getExtension( |
|
98 TraceFormattingRule.class); |
|
99 } |
|
100 } |
|
101 if (traceRule != null) { |
|
102 format = traceRule.getFormat(trace, formatType); |
|
103 } |
|
104 if (format != null && traceRule != null) { |
|
105 data.append(format); |
|
106 data.append(SourceConstants.LINE_FEED); |
|
107 buildParameterList(trace, traceRule, data, formatType, tags, |
|
108 fixedTags); |
|
109 |
|
110 String traceName = traceRule.mapNameToSource(trace); |
|
111 String traceGroupName = trace.getGroup().getName(); |
|
112 |
|
113 // %NAME% is replaced with rule-mapped trace name |
|
114 replaceData(data, traceName, TraceFormatConstants.NAME_FORMAT); |
|
115 |
|
116 // %GROUP% is replaced with group name |
|
117 replaceData(data, traceGroupName, TraceFormatConstants.GROUP_FORMAT); |
|
118 |
|
119 // %TEXT% is replaced with trace text |
|
120 replaceData(data, "\"" + trace.getTrace() //$NON-NLS-1$ |
|
121 + "\"", TraceFormatConstants.TEXT_FORMAT); //$NON-NLS-1$ |
|
122 |
|
123 // %FORMATTED_TRACE% is replaced with trace data |
|
124 replaceData(data, trace.getTrace(), |
|
125 TraceFormatConstants.FORMATTED_TRACE); |
|
126 |
|
127 // Comment is inserted before the trace |
|
128 int index = data.indexOf(TraceFormatConstants.COMMENT_FORMAT); |
|
129 if (index >= 0) { |
|
130 String comment = data.substring(index + 1); |
|
131 data.delete(index, data.length()); |
|
132 data.insert(0, comment); |
|
133 data.append(SourceConstants.LINE_FEED); |
|
134 } |
|
135 } |
|
136 |
|
137 // If trace does not have formatting, it is not shown in source |
|
138 return data.toString(); |
|
139 } |
|
140 |
|
141 /** |
|
142 * Adds the parameters to the data buffer |
|
143 * |
|
144 * @param trace |
|
145 * the trace |
|
146 * @param format |
|
147 * the formatter from trace |
|
148 * @param data |
|
149 * the data buffer where the formatted data is stored |
|
150 * @param formatType |
|
151 * the format type to be applied |
|
152 * @param tags |
|
153 * the tags for parameters or null if parameter names are used |
|
154 * @param fixedTags |
|
155 * true if the <i>tags</i> iterator is fixed, false if the |
|
156 * contents of <i>tags</i> should go through |
|
157 * <code>SourceRule.mapNameToSource</code> |
|
158 */ |
|
159 private static void buildParameterList(Trace trace, |
|
160 TraceFormattingRule format, StringBuffer data, |
|
161 TraceFormatType formatType, Iterator<String> tags, boolean fixedTags) { |
|
162 |
|
163 int count = trace.getParameterCount(); |
|
164 |
|
165 Iterator<TraceParameter> itr = trace.getParameters(); |
|
166 StringBuffer paramList = new StringBuffer(); |
|
167 // Index is incremented by one for each parameter that has been added to |
|
168 // source |
|
169 int parameterIndex = 0; |
|
170 while (itr.hasNext()) { |
|
171 TraceParameter param = itr.next(); |
|
172 TraceParameterFormattingRule rule = param |
|
173 .getExtension(TraceParameterFormattingRule.class); |
|
174 String name; |
|
175 // Count and name may be adjusted by rules provided by parameters |
|
176 if (rule != null) { |
|
177 boolean isInSource = rule.isShownInSource(); |
|
178 // If the parameter iterator is explicitly provided, the |
|
179 // parameter name is fetched from it. If the parameter list does |
|
180 // not have enough tags (for example when a new parameter is |
|
181 // added to trace) the name of the parameter is used. The source |
|
182 // rule is used to map the parameter name to correct format |
|
183 if (isInSource) { |
|
184 name = getTagWithoutMapping(tags, param); |
|
185 addParameter(paramList, param, name, ++parameterIndex, |
|
186 formatType); |
|
187 } else { |
|
188 // If the parameter is not shown in source, it is skipped |
|
189 count--; |
|
190 } |
|
191 } else { |
|
192 // If the parameter is not associated with a source rule, it is |
|
193 // added without mapping |
|
194 name = getTagWithoutMapping(tags, param); |
|
195 addParameter(paramList, param, name, ++parameterIndex, |
|
196 formatType); |
|
197 } |
|
198 } |
|
199 // %PC% is replaced with adjusted parameter count |
|
200 // In case of packed trace, the header engine does the count mapping |
|
201 if (formatType != TraceFormatType.TRACE_PACKED) { |
|
202 String val = format.mapParameterCountToSource(trace, count); |
|
203 |
|
204 if (trace.getExtension(StateTraceRule.class) != null |
|
205 && data.toString().startsWith("OstTraceState")) { //$NON-NLS-1$ |
|
206 |
|
207 // In case of State Trace macro value in trace macro is |
|
208 // parameter count - 2 |
|
209 if (count > 1) { |
|
210 val = String.valueOf(count - 2); // CodForChk_Dis_Magic |
|
211 } else { |
|
212 val = String.valueOf(count); |
|
213 } |
|
214 } |
|
215 replaceData(data, val, TraceFormatConstants.PARAM_COUNT_FORMAT); |
|
216 } |
|
217 // %PARAMETERS% is replaced with parameter names |
|
218 replaceData(data, paramList.toString(), |
|
219 TraceFormatConstants.PARAMETERS_FORMAT); |
|
220 } |
|
221 |
|
222 /** |
|
223 * Gets the name for a parameter without source rule mapping. If the tags |
|
224 * iterator contains a valid entry, the name is fetched from it. If not, the |
|
225 * parameter name is used instead. |
|
226 * |
|
227 * @param tags |
|
228 * the list of tags |
|
229 * @param param |
|
230 * the parameter |
|
231 * @return the parameter name |
|
232 */ |
|
233 private static String getTagWithoutMapping(Iterator<String> tags, |
|
234 TraceParameter param) { |
|
235 String name; |
|
236 // If the parameter iterator is explicitly provided, the |
|
237 // parameter name is fetched from it |
|
238 if (tags != null && tags.hasNext()) { |
|
239 name = tags.next(); |
|
240 // The list may contain 0-length items to represent |
|
241 // that that parameter name should be used instead |
|
242 if (name == null || name.length() == 0) { |
|
243 name = param.getName(); |
|
244 } |
|
245 } else { |
|
246 name = param.getName(); |
|
247 } |
|
248 return name; |
|
249 } |
|
250 |
|
251 /** |
|
252 * Adds a parameter to the parameter list |
|
253 * |
|
254 * @param paramList |
|
255 * the parameter list |
|
256 * @param param |
|
257 * the parameter to be added |
|
258 * @param name |
|
259 * a name replacement for the parameter |
|
260 * @param parameterIndex |
|
261 * the index of the parameter |
|
262 * @param formatType |
|
263 * the type of the format |
|
264 */ |
|
265 private static void addParameter(StringBuffer paramList, |
|
266 TraceParameter param, String name, int parameterIndex, |
|
267 TraceFormatType formatType) { |
|
268 paramList.append(SourceConstants.PARAMETER_SEPARATOR); |
|
269 if (formatType == TraceFormatType.HEADER) { |
|
270 paramList.append(SourceUtils.mapParameterTypeToSymbianType(param)); |
|
271 paramList.append(SourceConstants.SPACE_CHAR); |
|
272 paramList.append(SymbianConstants.PARAMETER_DECLARATION_PREFIX); |
|
273 paramList.append(parameterIndex); |
|
274 } else if (formatType == TraceFormatType.EMPTY_MACRO) { |
|
275 paramList.append(SymbianConstants.PARAMETER_DECLARATION_PREFIX); |
|
276 paramList.append(parameterIndex); |
|
277 } |
|
278 } |
|
279 |
|
280 /** |
|
281 * Replaces data from the stringbuffer |
|
282 * |
|
283 * @param data |
|
284 * the data |
|
285 * @param replaceData |
|
286 * the data to be used |
|
287 * @param replaceFormat |
|
288 * the format to be replaced |
|
289 */ |
|
290 private static void replaceData(StringBuffer data, String replaceData, |
|
291 String replaceFormat) { |
|
292 TraceCompilerEngineGlobals.getEvents().postInfoMessage(Messages.getString("SourceFormatter.replaceAllBeginText") + replaceFormat + Messages.getString("SourceFormatter.replaceAllMiddleText") + replaceData , null); //$NON-NLS-1$ //$NON-NLS-2$ |
|
293 |
|
294 int replaceOffset = 0; |
|
295 do { |
|
296 replaceOffset = data.indexOf(replaceFormat, replaceOffset); |
|
297 if (replaceOffset >= 0) { |
|
298 data.replace(replaceOffset, replaceOffset |
|
299 + replaceFormat.length(), replaceData); |
|
300 replaceOffset += replaceData.length(); |
|
301 } |
|
302 } while (replaceOffset != -1); |
|
303 } |
|
304 |
|
305 } |
|