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
.Activator
;
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())
68 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
69 if (location
!= null && location
.getLocation() instanceof Long
) {
70 raFile
.seek((Long
)location
.getLocation());
73 long rawPos
= raFile
.getFilePointer();
74 while ((line
= raFile
.getNextLine()) != null) {
75 for (final InputLine input
: getFirstLines()) {
76 final Matcher matcher
= input
.getPattern().matcher(line
);
78 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
79 context
.raFile
= raFile
;
80 context
.firstLineMatcher
= matcher
;
81 context
.firstLine
= line
;
82 context
.nextLineLocation
= raFile
.getFilePointer();
83 context
.inputLine
= input
;
87 rawPos
= raFile
.getFilePointer();
90 } catch (final FileNotFoundException e
) {
91 Activator
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
93 } catch (final IOException e
) {
94 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
101 public TmfContext
seekEvent(final double ratio
) {
102 BufferedRandomAccessFile raFile
= null;
104 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
105 long pos
= (long) (ratio
* raFile
.length());
107 raFile
.seek(pos
- 1);
108 if (raFile
.read() == '\n') {
113 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
114 final TmfContext context
= seekEvent(location
);
115 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
117 } catch (final FileNotFoundException e
) {
118 Activator
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
119 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
120 } catch (final IOException e
) {
121 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
122 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
124 if (raFile
!= null) {
127 } catch (final IOException e
) {
134 public double getLocationRatio(final ITmfLocation
<?
> location
) {
135 BufferedRandomAccessFile raFile
= null;
137 if (location
.getLocation() instanceof Long
) {
138 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
139 return (double) ((Long
) location
.getLocation()) / raFile
.length();
141 } catch (final FileNotFoundException e
) {
142 Activator
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
143 } catch (final IOException e
) {
144 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
146 if (raFile
!= null) {
149 } catch (final IOException e
) {
157 public ITmfLocation
<?
> getCurrentLocation() {
158 // TODO Auto-generated method stub
163 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
164 final ITmfContext savedContext
= context
.clone();
165 final CustomTxtEvent event
= parseEvent(context
);
167 updateAttributes(savedContext
, event
.getTimestamp());
168 context
.increaseRank();
174 public CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
175 if (!(tmfContext
instanceof CustomTxtTraceContext
))
178 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
179 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
182 CustomTxtEvent event
= parseFirstLine(context
);
184 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
185 InputLine currentInput
= null;
186 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
187 currentInput
= context
.inputLine
.childrenInputs
.get(0);
188 countMap
.put(currentInput
, 0);
191 synchronized (context
.raFile
) {
193 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
194 context
.raFile
.seek(context
.nextLineLocation
);
197 long rawPos
= context
.raFile
.getFilePointer();
198 while ((line
= context
.raFile
.getNextLine()) != null) {
199 boolean processed
= false;
200 if (currentInput
== null) {
201 for (final InputLine input
: getFirstLines()) {
202 final Matcher matcher
= input
.getPattern().matcher(line
);
203 if (matcher
.find()) {
204 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
205 context
.firstLineMatcher
= matcher
;
206 context
.firstLine
= line
;
207 context
.nextLineLocation
= context
.raFile
.getFilePointer();
208 context
.inputLine
= input
;
213 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
214 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
215 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
216 for (final InputLine input
: getFirstLines()) {
217 final Matcher matcher
= input
.getPattern().matcher(line
);
218 if (matcher
.find()) {
219 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
220 context
.firstLineMatcher
= matcher
;
221 context
.firstLine
= line
;
222 context
.nextLineLocation
= context
.raFile
.getFilePointer();
223 context
.inputLine
= input
;
228 for (final InputLine input
: nextInputs
) {
229 final Matcher matcher
= input
.getPattern().matcher(line
);
230 if (matcher
.find()) {
231 event
.processGroups(input
, matcher
);
232 currentInput
= input
;
233 if (countMap
.get(currentInput
) == null) {
234 countMap
.put(currentInput
, 1);
236 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
238 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
239 while (iter
.hasNext()) {
240 final InputLine inputLine
= iter
.next();
241 if (inputLine
.level
> currentInput
.level
) {
245 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
246 currentInput
= currentInput
.childrenInputs
.get(0);
247 countMap
.put(currentInput
, 0);
248 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
249 if (currentInput
.getNextInputs(countMap
).size() > 0) {
250 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
251 if (countMap
.get(currentInput
) == null) {
252 countMap
.put(currentInput
, 0);
254 iter
= countMap
.keySet().iterator();
255 while (iter
.hasNext()) {
256 final InputLine inputLine
= iter
.next();
257 if (inputLine
.level
> currentInput
.level
) {
270 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
271 if (matcher
.find()) {
272 event
.processGroups(currentInput
, matcher
);
273 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
274 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
275 currentInput
= currentInput
.childrenInputs
.get(0);
276 countMap
.put(currentInput
, 0);
277 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
278 if (currentInput
.getNextInputs(countMap
).size() > 0) {
279 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
280 if (countMap
.get(currentInput
) == null) {
281 countMap
.put(currentInput
, 0);
283 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
284 while (iter
.hasNext()) {
285 final InputLine inputLine
= iter
.next();
286 if (inputLine
.level
> currentInput
.level
) {
294 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
297 rawPos
= context
.raFile
.getFilePointer();
299 } catch (final IOException e
) {
300 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
303 for(final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet())
304 if (entry
.getValue() < entry
.getKey().getMinCount()) {
307 context
.setLocation(NULL_LOCATION
);
311 public List
<InputLine
> getFirstLines() {
312 return fDefinition
.inputs
;
315 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
316 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
317 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
318 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
322 public CustomTraceDefinition
getDefinition() {
327 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
330 public boolean validate(IProject project
, String path
) {
331 return fileExists(path
);