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