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