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
.IResource
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
36 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> implements ITmfEventParser
<CustomTxtEvent
> {
38 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
39 private static final int DEFAULT_CACHE_SIZE
= 100;
41 private final CustomTxtTraceDefinition fDefinition
;
42 private final CustomTxtEventType fEventType
;
44 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
45 fDefinition
= definition
;
46 fEventType
= new CustomTxtEventType(fDefinition
);
49 public CustomTxtTrace(final IResource resource
, final CustomTxtTraceDefinition definition
, final String path
, final int pageSize
) throws FileNotFoundException
{
50 super(resource
, CustomTxtEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
51 fDefinition
= definition
;
52 fEventType
= new CustomTxtEventType(fDefinition
);
56 public void initTrace(final IResource resource
, final String path
, final Class
<CustomTxtEvent
> eventType
) throws FileNotFoundException
{
57 super.initTrace(resource
, path
, eventType
);
61 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
62 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
63 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
65 BufferedRandomAccessFile raFile
= null;
67 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
68 if (location
!= null && location
.getLocation() instanceof Long
) {
69 raFile
.seek((Long
)location
.getLocation());
72 long rawPos
= raFile
.getFilePointer();
73 while ((line
= raFile
.getNextLine()) != null) {
74 for (final InputLine input
: getFirstLines()) {
75 final Matcher matcher
= input
.getPattern().matcher(line
);
77 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
78 context
.raFile
= raFile
;
79 context
.firstLineMatcher
= matcher
;
80 context
.firstLine
= line
;
81 context
.nextLineLocation
= raFile
.getFilePointer();
82 context
.inputLine
= input
;
86 rawPos
= raFile
.getFilePointer();
89 } catch (final FileNotFoundException e
) {
92 } catch (final IOException e
) {
99 } catch (final IOException e
) {
107 public TmfContext
seekEvent(final double ratio
) {
108 BufferedRandomAccessFile raFile
= null;
110 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
111 long pos
= (long) (ratio
* raFile
.length());
113 raFile
.seek(pos
- 1);
114 if (raFile
.read() == '\n') {
119 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
120 final TmfContext context
= seekEvent(location
);
121 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
123 } catch (final FileNotFoundException e
) {
125 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
126 } catch (final IOException e
) {
128 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
130 if (raFile
!= null) {
133 } catch (final IOException e
) {
140 public double getLocationRatio(final ITmfLocation
<?
> location
) {
141 BufferedRandomAccessFile raFile
= null;
143 if (location
.getLocation() instanceof Long
) {
144 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
145 return (double) ((Long
) location
.getLocation()) / raFile
.length();
147 } catch (final FileNotFoundException e
) {
149 } catch (final IOException e
) {
152 if (raFile
!= null) {
155 } catch (final IOException e
) {
163 public ITmfLocation
<?
> getCurrentLocation() {
164 // TODO Auto-generated method stub
169 public synchronized TmfEvent
readEvent(final ITmfContext context
) {
170 final ITmfContext savedContext
= context
.clone();
171 final TmfEvent event
= parseEvent(context
);
173 updateAttributes(savedContext
, event
.getTimestamp());
174 context
.increaseRank();
180 public TmfEvent
parseEvent(final ITmfContext tmfContext
) {
181 if (!(tmfContext
instanceof CustomTxtTraceContext
))
184 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
185 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
188 CustomTxtEvent event
= parseFirstLine(context
);
190 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
191 InputLine currentInput
= null;
192 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
193 currentInput
= context
.inputLine
.childrenInputs
.get(0);
194 countMap
.put(currentInput
, 0);
197 synchronized (context
.raFile
) {
199 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
200 context
.raFile
.seek(context
.nextLineLocation
);
203 long rawPos
= context
.raFile
.getFilePointer();
204 while ((line
= context
.raFile
.getNextLine()) != null) {
205 boolean processed
= false;
206 if (currentInput
== null) {
207 for (final InputLine input
: getFirstLines()) {
208 final Matcher matcher
= input
.getPattern().matcher(line
);
209 if (matcher
.find()) {
210 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
211 context
.firstLineMatcher
= matcher
;
212 context
.firstLine
= line
;
213 context
.nextLineLocation
= context
.raFile
.getFilePointer();
214 context
.inputLine
= input
;
219 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
220 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
221 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
222 for (final InputLine input
: getFirstLines()) {
223 final Matcher matcher
= input
.getPattern().matcher(line
);
224 if (matcher
.find()) {
225 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
226 context
.firstLineMatcher
= matcher
;
227 context
.firstLine
= line
;
228 context
.nextLineLocation
= context
.raFile
.getFilePointer();
229 context
.inputLine
= input
;
234 for (final InputLine input
: nextInputs
) {
235 final Matcher matcher
= input
.getPattern().matcher(line
);
236 if (matcher
.find()) {
237 event
.processGroups(input
, matcher
);
238 currentInput
= input
;
239 if (countMap
.get(currentInput
) == null) {
240 countMap
.put(currentInput
, 1);
242 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
244 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
245 while (iter
.hasNext()) {
246 final InputLine inputLine
= iter
.next();
247 if (inputLine
.level
> currentInput
.level
) {
251 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
252 currentInput
= currentInput
.childrenInputs
.get(0);
253 countMap
.put(currentInput
, 0);
254 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
255 if (currentInput
.getNextInputs(countMap
).size() > 0) {
256 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
257 if (countMap
.get(currentInput
) == null) {
258 countMap
.put(currentInput
, 0);
260 iter
= countMap
.keySet().iterator();
261 while (iter
.hasNext()) {
262 final InputLine inputLine
= iter
.next();
263 if (inputLine
.level
> currentInput
.level
) {
276 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
277 if (matcher
.find()) {
278 event
.processGroups(currentInput
, matcher
);
279 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
280 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
281 currentInput
= currentInput
.childrenInputs
.get(0);
282 countMap
.put(currentInput
, 0);
283 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
284 if (currentInput
.getNextInputs(countMap
).size() > 0) {
285 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
286 if (countMap
.get(currentInput
) == null) {
287 countMap
.put(currentInput
, 0);
289 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
290 while (iter
.hasNext()) {
291 final InputLine inputLine
= iter
.next();
292 if (inputLine
.level
> currentInput
.level
) {
300 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
303 rawPos
= context
.raFile
.getFilePointer();
305 } catch (final IOException e
) {
309 for(final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet())
310 if (entry
.getValue() < entry
.getKey().getMinCount()) {
313 context
.setLocation(NULL_LOCATION
);
317 public List
<InputLine
> getFirstLines() {
318 return fDefinition
.inputs
;
321 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
322 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
323 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
324 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
328 public CustomTraceDefinition
getDefinition() {