1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
;
15 import java
.io
.FileNotFoundException
;
16 import java
.io
.IOException
;
17 import java
.util
.HashMap
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.Map
.Entry
;
21 import java
.util
.regex
.Matcher
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceIndexer
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
40 * Base class for custom plain text traces.
42 * @author Patrick Tassé
44 public class CustomTxtTrace
extends TmfTrace
implements ITmfEventParser
{
46 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation((Long
) null);
47 private static final int DEFAULT_CACHE_SIZE
= 100;
49 private final CustomTxtTraceDefinition fDefinition
;
50 private final CustomTxtEventType fEventType
;
51 private BufferedRandomAccessFile fFile
;
57 * Text trace definition
59 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
60 fDefinition
= definition
;
61 fEventType
= new CustomTxtEventType(fDefinition
);
62 setCacheSize(DEFAULT_CACHE_SIZE
);
71 * Text trace definition
73 * Path to the trace file
76 * @throws TmfTraceException
77 * If we couldn't open the trace at 'path'
79 public CustomTxtTrace(final IResource resource
,
80 final CustomTxtTraceDefinition definition
, final String path
,
81 final int cacheSize
) throws TmfTraceException
{
83 setCacheSize((cacheSize
> 0) ? cacheSize
: DEFAULT_CACHE_SIZE
);
84 initTrace(resource
, path
, CustomTxtEvent
.class);
88 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
89 super.initTrace(resource
, path
, eventType
);
91 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
92 } catch (IOException e
) {
93 throw new TmfTraceException(e
.getMessage(), e
);
98 public synchronized void dispose() {
103 } catch (IOException e
) {
111 public ITmfTraceIndexer
getIndexer() {
112 return super.getIndexer();
116 public synchronized TmfContext
seekEvent(final ITmfLocation location
) {
117 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
118 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
122 if (location
== null) {
124 } else if (location
.getLocationInfo() instanceof Long
) {
125 fFile
.seek((Long
) location
.getLocationInfo());
128 long rawPos
= fFile
.getFilePointer();
129 while ((line
= fFile
.getNextLine()) != null) {
130 for (final InputLine input
: getFirstLines()) {
131 final Matcher matcher
= input
.getPattern().matcher(line
);
132 if (matcher
.find()) {
133 context
.setLocation(new TmfLongLocation(rawPos
));
134 context
.firstLineMatcher
= matcher
;
135 context
.firstLine
= line
;
136 context
.nextLineLocation
= fFile
.getFilePointer();
137 context
.inputLine
= input
;
141 rawPos
= fFile
.getFilePointer();
144 } catch (final FileNotFoundException e
) {
145 Activator
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
147 } catch (final IOException e
) {
148 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
155 public synchronized TmfContext
seekEvent(final double ratio
) {
157 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
160 long pos
= Math
.round(ratio
* fFile
.length());
163 if (fFile
.read() == '\n') {
168 final ITmfLocation location
= new TmfLongLocation(pos
);
169 final TmfContext context
= seekEvent(location
);
170 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
172 } catch (final IOException e
) {
173 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
174 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
179 public synchronized double getLocationRatio(final ITmfLocation location
) {
184 if (location
.getLocationInfo() instanceof Long
) {
185 return (double) ((Long
) location
.getLocationInfo()) / fFile
.length();
187 } catch (final IOException e
) {
188 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
194 public ITmfLocation
getCurrentLocation() {
195 // TODO Auto-generated method stub
200 public synchronized CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
201 ITmfContext context
= seekEvent(tmfContext
.getLocation());
202 return parse(context
);
206 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
207 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
208 final CustomTxtEvent event
= parse(context
);
210 updateAttributes(savedContext
, event
.getTimestamp());
211 context
.increaseRank();
216 private synchronized CustomTxtEvent
parse(final ITmfContext tmfContext
) {
220 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
224 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
225 if (context
.getLocation() == null || !(context
.getLocation().getLocationInfo() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
229 CustomTxtEvent event
= parseFirstLine(context
);
231 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
232 InputLine currentInput
= null;
233 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
234 currentInput
= context
.inputLine
.childrenInputs
.get(0);
235 countMap
.put(currentInput
, 0);
239 if (fFile
.getFilePointer() != context
.nextLineLocation
) {
240 fFile
.seek(context
.nextLineLocation
);
243 long rawPos
= fFile
.getFilePointer();
244 while ((line
= fFile
.getNextLine()) != null) {
245 boolean processed
= false;
246 if (currentInput
== null) {
247 for (final InputLine input
: getFirstLines()) {
248 final Matcher matcher
= input
.getPattern().matcher(line
);
249 if (matcher
.find()) {
250 context
.setLocation(new TmfLongLocation(rawPos
));
251 context
.firstLineMatcher
= matcher
;
252 context
.firstLine
= line
;
253 context
.nextLineLocation
= fFile
.getFilePointer();
254 context
.inputLine
= input
;
259 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
260 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
261 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
262 for (final InputLine input
: getFirstLines()) {
263 final Matcher matcher
= input
.getPattern().matcher(line
);
264 if (matcher
.find()) {
265 context
.setLocation(new TmfLongLocation(rawPos
));
266 context
.firstLineMatcher
= matcher
;
267 context
.firstLine
= line
;
268 context
.nextLineLocation
= fFile
.getFilePointer();
269 context
.inputLine
= input
;
274 for (final InputLine input
: nextInputs
) {
275 final Matcher matcher
= input
.getPattern().matcher(line
);
276 if (matcher
.find()) {
277 event
.processGroups(input
, matcher
);
278 currentInput
= input
;
279 if (countMap
.get(currentInput
) == null) {
280 countMap
.put(currentInput
, 1);
282 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
284 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
285 while (iter
.hasNext()) {
286 final InputLine inputLine
= iter
.next();
287 if (inputLine
.level
> currentInput
.level
) {
291 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
292 currentInput
= currentInput
.childrenInputs
.get(0);
293 countMap
.put(currentInput
, 0);
294 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
295 if (currentInput
.getNextInputs(countMap
).size() > 0) {
296 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
297 if (countMap
.get(currentInput
) == null) {
298 countMap
.put(currentInput
, 0);
300 iter
= countMap
.keySet().iterator();
301 while (iter
.hasNext()) {
302 final InputLine inputLine
= iter
.next();
303 if (inputLine
.level
> currentInput
.level
) {
316 if (!processed
&& currentInput
!= null) {
317 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
318 if (matcher
.find()) {
319 event
.processGroups(currentInput
, matcher
);
320 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
321 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
322 currentInput
= currentInput
.childrenInputs
.get(0);
323 countMap
.put(currentInput
, 0);
324 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
325 if (currentInput
.getNextInputs(countMap
).size() > 0) {
326 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
327 if (countMap
.get(currentInput
) == null) {
328 countMap
.put(currentInput
, 0);
330 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
331 while (iter
.hasNext()) {
332 final InputLine inputLine
= iter
.next();
333 if (inputLine
.level
> currentInput
.level
) {
342 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
345 rawPos
= fFile
.getFilePointer();
347 } catch (final IOException e
) {
348 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
350 for (final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
351 if (entry
.getValue() < entry
.getKey().getMinCount()) {
355 context
.setLocation(NULL_LOCATION
);
360 * @return The first few lines of the text file
362 public List
<InputLine
> getFirstLines() {
363 return fDefinition
.inputs
;
367 * Parse the first line of the trace (to recognize the type).
371 * @return The first event
373 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
374 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
375 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
376 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
381 * Get the trace definition.
383 * @return The trace definition
385 public CustomTraceDefinition
getDefinition() {
390 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
393 public boolean validate(IProject project
, String path
) {
394 return fileExists(path
);