1 /* |
|
2 * Copyright (c) 2007 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 * Creates trace objects and provides configurable rules for trace object creation |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.model; |
|
20 |
|
21 import java.util.Iterator; |
|
22 |
|
23 /** |
|
24 * Creates trace objects and provides configurable rules for trace object |
|
25 * creation |
|
26 * |
|
27 */ |
|
28 public class TraceObjectFactory { |
|
29 |
|
30 /** |
|
31 * The trace model |
|
32 */ |
|
33 private TraceModel model; |
|
34 |
|
35 /** |
|
36 * Rule factory |
|
37 */ |
|
38 private TraceObjectRuleFactory ruleFactory; |
|
39 |
|
40 /** |
|
41 * Creates a new factory |
|
42 * |
|
43 * @param model |
|
44 * the model |
|
45 * @param factory |
|
46 * the rule factory |
|
47 * @throws TraceCompilerException |
|
48 */ |
|
49 TraceObjectFactory(TraceModel model, TraceObjectRuleFactory factory) throws TraceCompilerException { |
|
50 this.model = model; |
|
51 this.ruleFactory = factory; |
|
52 processNewObjectRules(model); |
|
53 model.setComplete(); |
|
54 } |
|
55 |
|
56 /** |
|
57 * Gets the rule factory |
|
58 * |
|
59 * @return the rule factory |
|
60 */ |
|
61 public TraceObjectRuleFactory getRuleFactory() { |
|
62 return ruleFactory; |
|
63 } |
|
64 |
|
65 /** |
|
66 * Creates a new trace group |
|
67 * |
|
68 * @param id |
|
69 * the group ID |
|
70 * @param name |
|
71 * the name for the group |
|
72 * @param extensions |
|
73 * list of extensions to be added to the group |
|
74 * @return the new group |
|
75 * @throws TraceCompilerException |
|
76 */ |
|
77 public TraceGroup createTraceGroup(int id, String name, |
|
78 TraceModelExtension[] extensions) throws TraceCompilerException { |
|
79 TraceGroup group = new TraceGroup(model); |
|
80 group.setID(id); |
|
81 group.setName(name); |
|
82 setExtensions(group, extensions); |
|
83 processNewObjectRules(group); |
|
84 group.setComplete(); |
|
85 return group; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Creates a new trace |
|
90 * |
|
91 * @param group |
|
92 * the trace group |
|
93 * @param id |
|
94 * the id for the trace |
|
95 * @param name |
|
96 * the trace name |
|
97 * @param traceText |
|
98 * the trace text |
|
99 * @param extensions |
|
100 * list of extensions to be added to the trace |
|
101 * @return the new trace |
|
102 * @throws TraceCompilerException |
|
103 */ |
|
104 public Trace createTrace(TraceGroup group, int id, String name, |
|
105 String traceText, TraceModelExtension[] extensions) throws TraceCompilerException { |
|
106 Trace trace = new Trace(group); |
|
107 trace.setID(id); |
|
108 trace.setName(name); |
|
109 trace.setTrace(traceText); |
|
110 setExtensions(trace, extensions); |
|
111 processNewObjectRules(trace); |
|
112 trace.setComplete(); |
|
113 return trace; |
|
114 } |
|
115 |
|
116 /** |
|
117 * Creates a new trace parameter |
|
118 * |
|
119 * @param trace |
|
120 * the trace the parameter is associated to |
|
121 * @param id |
|
122 * the parameter ID |
|
123 * @param name |
|
124 * the parameter name |
|
125 * @param type |
|
126 * parameter type |
|
127 * @param extensions |
|
128 * list of extensions to be added to the parameter |
|
129 * @return the new parameter |
|
130 * @throws TraceCompilerException |
|
131 */ |
|
132 public TraceParameter createTraceParameter(Trace trace, int id, |
|
133 String name, String type, TraceModelExtension[] extensions) throws TraceCompilerException { |
|
134 TraceParameter parameter = new TraceParameter(trace); |
|
135 initializeParameter(parameter, id, name, type, extensions); |
|
136 return parameter; |
|
137 } |
|
138 |
|
139 /** |
|
140 * Creates a new trace parameter inserting it into the specified index |
|
141 * |
|
142 * @param objectIndex |
|
143 * the index for the object |
|
144 * @param trace |
|
145 * the trace the parameter is associated to |
|
146 * @param id |
|
147 * the parameter ID |
|
148 * @param name |
|
149 * the parameter name |
|
150 * @param type |
|
151 * parameter type |
|
152 * @param extensions |
|
153 * list of extensions to be added to the parameter |
|
154 * @return the new parameter |
|
155 * @throws TraceCompilerException |
|
156 */ |
|
157 public TraceParameter createTraceParameter(int objectIndex, Trace trace, |
|
158 int id, String name, String type, TraceModelExtension[] extensions) throws TraceCompilerException { |
|
159 TraceParameter parameter = new TraceParameter(trace, objectIndex); |
|
160 initializeParameter(parameter, id, name, type, extensions); |
|
161 return parameter; |
|
162 } |
|
163 |
|
164 /** |
|
165 * Initializes a parameter |
|
166 * |
|
167 * @param parameter |
|
168 * the parameter |
|
169 * @param id |
|
170 * the parameter ID |
|
171 * @param name |
|
172 * the parameter name |
|
173 * @param type |
|
174 * parameter type |
|
175 * @param extensions |
|
176 * list of extensions to be added to the parameter |
|
177 * @throws TraceCompilerException |
|
178 */ |
|
179 private void initializeParameter(TraceParameter parameter, int id, |
|
180 String name, String type, TraceModelExtension[] extensions) throws TraceCompilerException { |
|
181 parameter.setID(id); |
|
182 parameter.setName(name); |
|
183 parameter.setType(type); |
|
184 setExtensions(parameter, extensions); |
|
185 processNewObjectRules(parameter); |
|
186 parameter.setComplete(); |
|
187 } |
|
188 |
|
189 /** |
|
190 * Creates a new constant table |
|
191 * |
|
192 * @param id |
|
193 * id for the table |
|
194 * @param typeName |
|
195 * the name for the table |
|
196 * @param extensions |
|
197 * list of extensions to be added to the table |
|
198 * @return the constant table |
|
199 * @throws TraceCompilerException |
|
200 */ |
|
201 public TraceConstantTable createConstantTable(int id, String typeName, |
|
202 TraceModelExtension[] extensions) throws TraceCompilerException { |
|
203 TraceConstantTable table = new TraceConstantTable(model); |
|
204 table.setID(id); |
|
205 table.setName(typeName); |
|
206 setExtensions(table, extensions); |
|
207 processNewObjectRules(table); |
|
208 table.setComplete(); |
|
209 for (TraceGroup group : model) { |
|
210 for (Trace trace : group) { |
|
211 for (TraceParameter param : trace) { |
|
212 if (param.getType().equals(typeName)) { |
|
213 table.addParameterReference(param); |
|
214 } |
|
215 } |
|
216 } |
|
217 } |
|
218 return table; |
|
219 } |
|
220 |
|
221 /** |
|
222 * Creates a new constant table entry |
|
223 * |
|
224 * @param table |
|
225 * constant table |
|
226 * @param id |
|
227 * id for the entry |
|
228 * @param value |
|
229 * value for the entry |
|
230 * @param extensions |
|
231 * list of extensions to be added to the constant |
|
232 * @return the constant table entry |
|
233 * @throws TraceCompilerException |
|
234 */ |
|
235 public TraceConstantTableEntry createConstantTableEntry( |
|
236 TraceConstantTable table, int id, String value, |
|
237 TraceModelExtension[] extensions) throws TraceCompilerException { |
|
238 TraceConstantTableEntry entry = new TraceConstantTableEntry(table); |
|
239 entry.setID(id); |
|
240 entry.setName(value); |
|
241 setExtensions(entry, extensions); |
|
242 processNewObjectRules(entry); |
|
243 entry.setComplete(); |
|
244 return entry; |
|
245 } |
|
246 |
|
247 /** |
|
248 * Creates an extension based on its name. This does not add the extension |
|
249 * to the object, since the setData method should be called first |
|
250 * |
|
251 * @param object |
|
252 * the target object for the extension |
|
253 * @param name |
|
254 * the extension name |
|
255 * @return the created extension |
|
256 */ |
|
257 public TraceModelPersistentExtension createExtension(TraceObject object, |
|
258 String name) { |
|
259 return ruleFactory.createExtension(object, name); |
|
260 } |
|
261 |
|
262 /** |
|
263 * Adds extensions to given object |
|
264 * |
|
265 * @param object |
|
266 * the object |
|
267 * @param extensions |
|
268 * extensions to be added |
|
269 */ |
|
270 private void setExtensions(TraceObject object, |
|
271 TraceModelExtension[] extensions) { |
|
272 if (extensions != null) { |
|
273 for (TraceModelExtension element : extensions) { |
|
274 object.addExtension(element); |
|
275 } |
|
276 } |
|
277 } |
|
278 |
|
279 /** |
|
280 * Processes the rules of a new object |
|
281 * |
|
282 * @param object |
|
283 * the object to be processed |
|
284 * @throws TraceCompilerException |
|
285 */ |
|
286 private void processNewObjectRules(TraceObject object) throws TraceCompilerException { |
|
287 // Calls the factory to preprocess the extensions passed to |
|
288 // create-function |
|
289 ruleFactory.preProcessNewRules(object); |
|
290 // The rules may contain object creation rules |
|
291 Iterator<TraceObjectRule> rules = object |
|
292 .getExtensions(TraceObjectRule.class); |
|
293 while (rules.hasNext()) { |
|
294 TraceObjectRule rule = rules.next(); |
|
295 if (rule instanceof TraceObjectRuleCreateObject) { |
|
296 TraceObjectRuleCreateObject createRule = (TraceObjectRuleCreateObject) rule; |
|
297 createRule.createObject(); |
|
298 } |
|
299 } |
|
300 // Some rules are removed after the objects have been created |
|
301 Iterator<TraceObjectRuleRemoveOnCreate> itr; |
|
302 boolean changed; |
|
303 do { |
|
304 changed = false; |
|
305 itr = object.getExtensions(TraceObjectRuleRemoveOnCreate.class); |
|
306 while (itr.hasNext() && !changed) { |
|
307 TraceObjectRuleRemoveOnCreate ext = itr.next(); |
|
308 if (ext.canBeRemoved()) { |
|
309 object.removeExtension(ext); |
|
310 changed = true; |
|
311 } |
|
312 } |
|
313 } while (changed); |
|
314 // After processing is complete, the rule factory is used to do |
|
315 // post-processing |
|
316 ruleFactory.postProcessNewRules(object); |
|
317 } |
|
318 |
|
319 } |
|