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