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
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.IOException
;
18 import java
.util
.HashMap
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
21 import java
.util
.Map
.Entry
;
22 import java
.util
.regex
.Matcher
;
24 import org
.eclipse
.core
.resources
.IProject
;
25 import org
.eclipse
.core
.resources
.IResource
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.TmfUiPlugin
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
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
.TmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
38 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> implements ITmfEventParser
<CustomTxtEvent
> {
40 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
41 private static final int DEFAULT_CACHE_SIZE
= 100;
43 private final CustomTxtTraceDefinition fDefinition
;
44 private final CustomTxtEventType fEventType
;
46 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
47 fDefinition
= definition
;
48 fEventType
= new CustomTxtEventType(fDefinition
);
51 public CustomTxtTrace(final IResource resource
, final CustomTxtTraceDefinition definition
, final String path
, final int pageSize
) throws TmfTraceException
{
52 super(resource
, CustomTxtEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
53 fDefinition
= definition
;
54 fEventType
= new CustomTxtEventType(fDefinition
);
58 public void initTrace(final IResource resource
, final String path
, final Class
<CustomTxtEvent
> eventType
) throws TmfTraceException
{
59 super.initTrace(resource
, path
, eventType
);
63 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
64 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
65 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
67 BufferedRandomAccessFile raFile
= null;
69 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
70 if (location
!= null && location
.getLocation() instanceof Long
) {
71 raFile
.seek((Long
)location
.getLocation());
74 long rawPos
= raFile
.getFilePointer();
75 while ((line
= raFile
.getNextLine()) != null) {
76 for (final InputLine input
: getFirstLines()) {
77 final Matcher matcher
= input
.getPattern().matcher(line
);
79 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
80 context
.raFile
= raFile
;
81 context
.firstLineMatcher
= matcher
;
82 context
.firstLine
= line
;
83 context
.nextLineLocation
= raFile
.getFilePointer();
84 context
.inputLine
= input
;
88 rawPos
= raFile
.getFilePointer();
91 } catch (final FileNotFoundException e
) {
92 TmfUiPlugin
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
94 } catch (final IOException e
) {
95 TmfUiPlugin
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
101 } catch (final IOException e
) {
109 public TmfContext
seekEvent(final double ratio
) {
110 BufferedRandomAccessFile raFile
= null;
112 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
113 long pos
= (long) (ratio
* raFile
.length());
115 raFile
.seek(pos
- 1);
116 if (raFile
.read() == '\n') {
121 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
122 final TmfContext context
= seekEvent(location
);
123 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
125 } catch (final FileNotFoundException e
) {
126 TmfUiPlugin
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
127 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
128 } catch (final IOException e
) {
129 TmfUiPlugin
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
130 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
132 if (raFile
!= null) {
135 } catch (final IOException e
) {
142 public double getLocationRatio(final ITmfLocation
<?
> location
) {
143 BufferedRandomAccessFile raFile
= null;
145 if (location
.getLocation() instanceof Long
) {
146 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
147 return (double) ((Long
) location
.getLocation()) / raFile
.length();
149 } catch (final FileNotFoundException e
) {
150 TmfUiPlugin
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
151 } catch (final IOException e
) {
152 TmfUiPlugin
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
154 if (raFile
!= null) {
157 } catch (final IOException e
) {
165 public ITmfLocation
<?
> getCurrentLocation() {
166 // TODO Auto-generated method stub
171 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
172 final ITmfContext savedContext
= context
.clone();
173 final CustomTxtEvent event
= parseEvent(context
);
175 updateAttributes(savedContext
, event
.getTimestamp());
176 context
.increaseRank();
182 public CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
183 if (!(tmfContext
instanceof CustomTxtTraceContext
))
186 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
187 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
190 CustomTxtEvent event
= parseFirstLine(context
);
192 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
193 InputLine currentInput
= null;
194 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
195 currentInput
= context
.inputLine
.childrenInputs
.get(0);
196 countMap
.put(currentInput
, 0);
199 synchronized (context
.raFile
) {
201 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
202 context
.raFile
.seek(context
.nextLineLocation
);
205 long rawPos
= context
.raFile
.getFilePointer();
206 while ((line
= context
.raFile
.getNextLine()) != null) {
207 boolean processed
= false;
208 if (currentInput
== null) {
209 for (final InputLine input
: getFirstLines()) {
210 final Matcher matcher
= input
.getPattern().matcher(line
);
211 if (matcher
.find()) {
212 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
213 context
.firstLineMatcher
= matcher
;
214 context
.firstLine
= line
;
215 context
.nextLineLocation
= context
.raFile
.getFilePointer();
216 context
.inputLine
= input
;
221 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
222 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
223 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
224 for (final InputLine input
: getFirstLines()) {
225 final Matcher matcher
= input
.getPattern().matcher(line
);
226 if (matcher
.find()) {
227 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
228 context
.firstLineMatcher
= matcher
;
229 context
.firstLine
= line
;
230 context
.nextLineLocation
= context
.raFile
.getFilePointer();
231 context
.inputLine
= input
;
236 for (final InputLine input
: nextInputs
) {
237 final Matcher matcher
= input
.getPattern().matcher(line
);
238 if (matcher
.find()) {
239 event
.processGroups(input
, matcher
);
240 currentInput
= input
;
241 if (countMap
.get(currentInput
) == null) {
242 countMap
.put(currentInput
, 1);
244 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
246 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
247 while (iter
.hasNext()) {
248 final InputLine inputLine
= iter
.next();
249 if (inputLine
.level
> currentInput
.level
) {
253 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
254 currentInput
= currentInput
.childrenInputs
.get(0);
255 countMap
.put(currentInput
, 0);
256 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
257 if (currentInput
.getNextInputs(countMap
).size() > 0) {
258 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
259 if (countMap
.get(currentInput
) == null) {
260 countMap
.put(currentInput
, 0);
262 iter
= countMap
.keySet().iterator();
263 while (iter
.hasNext()) {
264 final InputLine inputLine
= iter
.next();
265 if (inputLine
.level
> currentInput
.level
) {
278 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
279 if (matcher
.find()) {
280 event
.processGroups(currentInput
, matcher
);
281 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
282 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
283 currentInput
= currentInput
.childrenInputs
.get(0);
284 countMap
.put(currentInput
, 0);
285 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
286 if (currentInput
.getNextInputs(countMap
).size() > 0) {
287 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
288 if (countMap
.get(currentInput
) == null) {
289 countMap
.put(currentInput
, 0);
291 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
292 while (iter
.hasNext()) {
293 final InputLine inputLine
= iter
.next();
294 if (inputLine
.level
> currentInput
.level
) {
302 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
305 rawPos
= context
.raFile
.getFilePointer();
307 } catch (final IOException e
) {
308 TmfUiPlugin
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
311 for(final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet())
312 if (entry
.getValue() < entry
.getKey().getMinCount()) {
315 context
.setLocation(NULL_LOCATION
);
319 public List
<InputLine
> getFirstLines() {
320 return fDefinition
.inputs
;
323 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
324 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
325 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
326 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
330 public CustomTraceDefinition
getDefinition() {
335 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
338 public boolean validate(IProject project
, String path
) {
339 return fileExists(path
);