Refactor TmfTrace and dependencies - fix parent class, remove clone()
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomTxtTrace.java
CommitLineData
c3c5c786
FC
1/*******************************************************************************\r
2 * Copyright (c) 2010 Ericsson\r
3 * \r
4 * All rights reserved. This program and the accompanying materials are\r
5 * made available under the terms of the Eclipse Public License v1.0 which\r
6 * accompanies this distribution, and is available at\r
7 * http://www.eclipse.org/legal/epl-v10.html\r
8 * \r
9 * Contributors:\r
10 * Patrick Tasse - Initial API and implementation\r
11 *******************************************************************************/\r
12\r
d34665f9 13package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;\r
c3c5c786
FC
14\r
15import java.io.File;\r
16import java.io.FileNotFoundException;\r
17import java.io.IOException;\r
c3c5c786
FC
18import java.util.HashMap;\r
19import java.util.Iterator;\r
20import java.util.List;\r
21import java.util.Map.Entry;\r
22import java.util.regex.Matcher;\r
c3c5c786 23\r
d34665f9 24import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputLine;\r
6c13869b 25import org.eclipse.linuxtools.tmf.core.event.TmfEvent;\r
6c13869b
FC
26import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;\r
27import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;\r
28import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;\r
29import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;\r
6c13869b
FC
30import org.eclipse.linuxtools.tmf.core.trace.TmfContext;\r
31import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;\r
32import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;\r
c3c5c786
FC
33\r
34public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> {\r
35\r
d7fcacc9 36 private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);\r
3118edf1 37 private static final int DEFAULT_CACHE_SIZE = 100;\r
1b70b6dc 38\r
c3c5c786 39 private CustomTxtTraceDefinition fDefinition;\r
d7fcacc9 40 private CustomTxtEventType fEventType;\r
4bf17f4a 41\r
42 public CustomTxtTrace(CustomTxtTraceDefinition definition) {\r
43 fDefinition = definition;\r
44 fEventType = new CustomTxtEventType(fDefinition);\r
45 }\r
46\r
3118edf1 47 public CustomTxtTrace(String name, CustomTxtTraceDefinition definition, String path, int pageSize) throws FileNotFoundException {\r
3791b5df 48 super(name, CustomTxtEvent.class, path, (pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE, true);\r
c3c5c786 49 fDefinition = definition;\r
d7fcacc9 50 fEventType = new CustomTxtEventType(fDefinition);\r
c3c5c786
FC
51 }\r
52\r
1b70b6dc 53 @Override\r
3118edf1
FC
54 public void initTrace(String name, String path, Class<CustomTxtEvent> eventType) throws FileNotFoundException {\r
55 super.initTrace(name, path, eventType);\r
c3c5c786
FC
56 }\r
57\r
58 @Override\r
59 public TmfContext seekLocation(ITmfLocation<?> location) {\r
d7fcacc9
FC
60 CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);\r
61 if (NULL_LOCATION.equals(location) || !new File(getPath()).isFile()) {\r
c3c5c786
FC
62 return context;\r
63 }\r
157906bb 64 BufferedRandomAccessFile raFile = null; \r
c3c5c786 65 try {\r
157906bb 66 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
c3c5c786
FC
67 if (location != null && location.getLocation() instanceof Long) {\r
68 raFile.seek((Long)location.getLocation());\r
69 }\r
70 String line;\r
71 long rawPos = raFile.getFilePointer();\r
d7fcacc9 72 while ((line = raFile.getNextLine()) != null) {\r
c3c5c786
FC
73 for (InputLine input : getFirstLines()) {\r
74 Matcher matcher = input.getPattern().matcher(line);\r
75 if (matcher.find()) {\r
76 context.setLocation(new TmfLocation<Long>(rawPos));\r
77 context.raFile = raFile;\r
78 context.firstLineMatcher = matcher;\r
d7fcacc9 79 context.firstLine = line;\r
c3c5c786
FC
80 context.nextLineLocation = raFile.getFilePointer();\r
81 context.inputLine = input;\r
82 return context;\r
83 }\r
84 }\r
85 rawPos = raFile.getFilePointer();\r
86 }\r
87 return context;\r
88 } catch (FileNotFoundException e) {\r
89 e.printStackTrace();\r
90 return context;\r
91 } catch (IOException e) {\r
92 e.printStackTrace();\r
93 return context;\r
157906bb
AK
94 } finally {\r
95 if (raFile != null) {\r
96 try {\r
97 raFile.close();\r
98 } catch (IOException e) {\r
99 }\r
100 }\r
c3c5c786
FC
101 }\r
102 \r
103 }\r
104\r
c76c54bb
FC
105 @Override\r
106 public TmfContext seekLocation(double ratio) {\r
157906bb 107 BufferedRandomAccessFile raFile = null; \r
c76c54bb 108 try {\r
157906bb 109 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
a79913eb
FC
110 long pos = (long) (ratio * raFile.length());\r
111 while (pos > 0) {\r
112 raFile.seek(pos - 1);\r
113 if (raFile.read() == '\n') break;\r
114 pos--;\r
115 }\r
5a5c2fc7 116 ITmfLocation<?> location = new TmfLocation<Long>(pos);\r
c76c54bb
FC
117 TmfContext context = seekLocation(location);\r
118 context.setRank(ITmfContext.UNKNOWN_RANK);\r
119 return context;\r
120 } catch (FileNotFoundException e) {\r
121 e.printStackTrace();\r
122 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);\r
123 } catch (IOException e) {\r
124 e.printStackTrace();\r
125 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);\r
157906bb
AK
126 } finally {\r
127 if (raFile != null) {\r
128 try {\r
129 raFile.close();\r
130 } catch (IOException e) {\r
131 }\r
132 }\r
c76c54bb
FC
133 }\r
134 }\r
135\r
136 @Override\r
137 public double getLocationRatio(ITmfLocation<?> location) {\r
157906bb 138 BufferedRandomAccessFile raFile = null; \r
c76c54bb
FC
139 try {\r
140 if (location.getLocation() instanceof Long) {\r
157906bb 141 raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$\r
c76c54bb
FC
142 return (double) ((Long) location.getLocation()) / raFile.length();\r
143 }\r
144 } catch (FileNotFoundException e) {\r
145 e.printStackTrace();\r
146 } catch (IOException e) {\r
147 e.printStackTrace();\r
157906bb
AK
148 } finally {\r
149 if (raFile != null) {\r
150 try {\r
151 raFile.close();\r
152 } catch (IOException e) {\r
153 }\r
154 }\r
c76c54bb
FC
155 }\r
156 return 0;\r
157 }\r
158\r
c3c5c786
FC
159 @Override\r
160 public ITmfLocation<?> getCurrentLocation() {\r
161 // TODO Auto-generated method stub\r
162 return null;\r
163 }\r
164\r
165 @Override\r
a1440d1f 166 public synchronized TmfEvent getNextEvent(ITmfContext context) {\r
c3c5c786
FC
167 ITmfContext savedContext = context.clone();\r
168 TmfEvent event = parseEvent(context);\r
169 if (event != null) {\r
170 updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());\r
cbdacf03 171 context.increaseRank();\r
c3c5c786
FC
172 }\r
173 return event;\r
174 }\r
175\r
176 @Override\r
a1440d1f 177 public TmfEvent parseEvent(ITmfContext tmfContext) {\r
c3c5c786
FC
178 if (!(tmfContext instanceof CustomTxtTraceContext)) {\r
179 return null;\r
180 }\r
181 \r
182 CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;\r
d7fcacc9 183 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {\r
c3c5c786
FC
184 return null;\r
185 }\r
186\r
187 CustomTxtEvent event = parseFirstLine(context);\r
188\r
189 HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();\r
190 InputLine currentInput = null;\r
191 if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {\r
192 currentInput = context.inputLine.childrenInputs.get(0);\r
193 countMap.put(currentInput, 0);\r
194 }\r
195 \r
196 synchronized (context.raFile) {\r
197 try {\r
198 if (context.raFile.getFilePointer() != context.nextLineLocation) {\r
199 context.raFile.seek(context.nextLineLocation);\r
200 }\r
201 String line;\r
202 long rawPos = context.raFile.getFilePointer();\r
d7fcacc9 203 while ((line = context.raFile.getNextLine()) != null) {\r
c3c5c786
FC
204 boolean processed = false;\r
205 if (currentInput == null) {\r
206 for (InputLine input : getFirstLines()) {\r
207 Matcher matcher = input.getPattern().matcher(line);\r
208 if (matcher.find()) {\r
209 context.setLocation(new TmfLocation<Long>(rawPos));\r
210 context.firstLineMatcher = matcher;\r
d7fcacc9 211 context.firstLine = line;\r
c3c5c786
FC
212 context.nextLineLocation = context.raFile.getFilePointer();\r
213 context.inputLine = input;\r
214 return event;\r
215 }\r
216 }\r
217 } else {\r
218 if (countMap.get(currentInput) >= currentInput.getMinCount()) {\r
219 List<InputLine> nextInputs = currentInput.getNextInputs(countMap);\r
220 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {\r
221 for (InputLine input : getFirstLines()) {\r
222 Matcher matcher = input.getPattern().matcher(line);\r
223 if (matcher.find()) {\r
224 context.setLocation(new TmfLocation<Long>(rawPos));\r
225 context.firstLineMatcher = matcher;\r
d7fcacc9 226 context.firstLine = line;\r
c3c5c786
FC
227 context.nextLineLocation = context.raFile.getFilePointer();\r
228 context.inputLine = input;\r
229 return event;\r
230 }\r
231 }\r
232 }\r
233 for (InputLine input : nextInputs) {\r
234 Matcher matcher = input.getPattern().matcher(line);\r
235 if (matcher.find()) {\r
236 event.processGroups(input, matcher);\r
237 currentInput = input;\r
238 if (countMap.get(currentInput) == null) {\r
239 countMap.put(currentInput, 1);\r
240 } else {\r
241 countMap.put(currentInput, countMap.get(currentInput) + 1);\r
242 }\r
243 Iterator<InputLine> iter = countMap.keySet().iterator();\r
244 while (iter.hasNext()) {\r
245 InputLine inputLine = iter.next();\r
246 if (inputLine.level > currentInput.level) {\r
247 iter.remove();\r
248 }\r
249 }\r
250 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {\r
251 currentInput = currentInput.childrenInputs.get(0);\r
252 countMap.put(currentInput, 0);\r
253 } else {\r
254 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {\r
255 if (currentInput.getNextInputs(countMap).size() > 0) {\r
256 currentInput = currentInput.getNextInputs(countMap).get(0);\r
257 if (countMap.get(currentInput) == null) {\r
258 countMap.put(currentInput, 0);\r
259 }\r
260 iter = countMap.keySet().iterator();\r
261 while (iter.hasNext()) {\r
262 InputLine inputLine = iter.next();\r
263 if (inputLine.level > currentInput.level) {\r
264 iter.remove();\r
265 }\r
266 }\r
267 } else {\r
268 currentInput = null;\r
269 }\r
270 }\r
271 }\r
272 processed = true;\r
273 break;\r
274 }\r
275 }\r
276 }\r
277 if (! processed) {\r
278 Matcher matcher = currentInput.getPattern().matcher(line);\r
279 if (matcher.find()) {\r
280 event.processGroups(currentInput, matcher);\r
281 countMap.put(currentInput, countMap.get(currentInput) + 1);\r
282 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {\r
283 currentInput = currentInput.childrenInputs.get(0);\r
284 countMap.put(currentInput, 0);\r
285 } else {\r
286 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {\r
287 if (currentInput.getNextInputs(countMap).size() > 0) {\r
288 currentInput = currentInput.getNextInputs(countMap).get(0);\r
289 if (countMap.get(currentInput) == null) {\r
290 countMap.put(currentInput, 0);\r
291 }\r
292 Iterator<InputLine> iter = countMap.keySet().iterator();\r
293 while (iter.hasNext()) {\r
294 InputLine inputLine = iter.next();\r
295 if (inputLine.level > currentInput.level) {\r
296 iter.remove();\r
297 }\r
298 }\r
299 } else {\r
300 currentInput = null;\r
301 }\r
302 }\r
303 }\r
304 }\r
4c564a2d 305 ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$\r
c3c5c786
FC
306 }\r
307 }\r
308 rawPos = context.raFile.getFilePointer();\r
309 }\r
310 } catch (IOException e) {\r
311 e.printStackTrace();\r
312 }\r
313 }\r
314 for(Entry<InputLine, Integer> entry : countMap.entrySet()) {\r
315 if (entry.getValue() < entry.getKey().getMinCount()) {\r
316 event = null;\r
317 }\r
318 }\r
d7fcacc9 319 context.setLocation(NULL_LOCATION);\r
c3c5c786
FC
320 return event;\r
321 }\r
322\r
323 public List<InputLine> getFirstLines() {\r
324 return fDefinition.inputs;\r
325 }\r
326 \r
327 public CustomTxtEvent parseFirstLine(CustomTxtTraceContext context) {\r
a4115405 328 CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, (TmfTimestamp) TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$\r
c3c5c786 329 event.processGroups(context.inputLine, context.firstLineMatcher);\r
5d3e8747 330 event.setContent(new CustomEventContent(event, context.firstLine));\r
c3c5c786
FC
331 return event;\r
332 }\r
333 \r
c3c5c786
FC
334 public CustomTraceDefinition getDefinition() {\r
335 return fDefinition;\r
336 }\r
337}\r
This page took 0.045269 seconds and 5 git commands to generate.