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
.ITmfLocation
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
35 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> {
37 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
38 private static final int DEFAULT_CACHE_SIZE
= 100;
40 private final CustomTxtTraceDefinition fDefinition
;
41 private final CustomTxtEventType fEventType
;
43 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
44 fDefinition
= definition
;
45 fEventType
= new CustomTxtEventType(fDefinition
);
48 public CustomTxtTrace(final IResource resource
, final CustomTxtTraceDefinition definition
, final String path
, final int pageSize
) throws FileNotFoundException
{
49 super(resource
, CustomTxtEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
50 fDefinition
= definition
;
51 fEventType
= new CustomTxtEventType(fDefinition
);
55 public void initTrace(final IResource resource
, final String path
, final Class
<CustomTxtEvent
> eventType
) throws FileNotFoundException
{
56 super.initTrace(resource
, path
, eventType
);
60 public TmfContext
seekLocation(final ITmfLocation
<?
> location
) {
61 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
62 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
64 BufferedRandomAccessFile raFile
= null;
66 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
67 if (location
!= null && location
.getLocation() instanceof Long
) {
68 raFile
.seek((Long
)location
.getLocation());
71 long rawPos
= raFile
.getFilePointer();
72 while ((line
= raFile
.getNextLine()) != null) {
73 for (final InputLine input
: getFirstLines()) {
74 final Matcher matcher
= input
.getPattern().matcher(line
);
76 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
77 context
.raFile
= raFile
;
78 context
.firstLineMatcher
= matcher
;
79 context
.firstLine
= line
;
80 context
.nextLineLocation
= raFile
.getFilePointer();
81 context
.inputLine
= input
;
85 rawPos
= raFile
.getFilePointer();
88 } catch (final FileNotFoundException e
) {
91 } catch (final IOException e
) {
98 } catch (final IOException e
) {
106 public TmfContext
seekLocation(final double ratio
) {
107 BufferedRandomAccessFile raFile
= null;
109 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
110 long pos
= (long) (ratio
* raFile
.length());
112 raFile
.seek(pos
- 1);
113 if (raFile
.read() == '\n') {
118 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
119 final TmfContext context
= seekLocation(location
);
120 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
122 } catch (final FileNotFoundException e
) {
124 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
125 } catch (final IOException e
) {
127 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
129 if (raFile
!= null) {
132 } catch (final IOException e
) {
139 public double getLocationRatio(final ITmfLocation
<?
> location
) {
140 BufferedRandomAccessFile raFile
= null;
142 if (location
.getLocation() instanceof Long
) {
143 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
144 return (double) ((Long
) location
.getLocation()) / raFile
.length();
146 } catch (final FileNotFoundException e
) {
148 } catch (final IOException e
) {
151 if (raFile
!= null) {
154 } catch (final IOException e
) {
162 public ITmfLocation
<?
> getCurrentLocation() {
163 // TODO Auto-generated method stub
168 public synchronized TmfEvent
getNextEvent(final ITmfContext context
) {
169 final ITmfContext savedContext
= context
.clone();
170 final TmfEvent event
= parseEvent(context
);
172 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
173 context
.increaseRank();
179 public TmfEvent
parseEvent(final ITmfContext tmfContext
) {
180 if (!(tmfContext
instanceof CustomTxtTraceContext
))
183 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
184 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
187 CustomTxtEvent event
= parseFirstLine(context
);
189 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
190 InputLine currentInput
= null;
191 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
192 currentInput
= context
.inputLine
.childrenInputs
.get(0);
193 countMap
.put(currentInput
, 0);
196 synchronized (context
.raFile
) {
198 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
199 context
.raFile
.seek(context
.nextLineLocation
);
202 long rawPos
= context
.raFile
.getFilePointer();
203 while ((line
= context
.raFile
.getNextLine()) != null) {
204 boolean processed
= false;
205 if (currentInput
== null) {
206 for (final InputLine input
: getFirstLines()) {
207 final Matcher matcher
= input
.getPattern().matcher(line
);
208 if (matcher
.find()) {
209 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
210 context
.firstLineMatcher
= matcher
;
211 context
.firstLine
= line
;
212 context
.nextLineLocation
= context
.raFile
.getFilePointer();
213 context
.inputLine
= input
;
218 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
219 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
220 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
221 for (final InputLine input
: getFirstLines()) {
222 final Matcher matcher
= input
.getPattern().matcher(line
);
223 if (matcher
.find()) {
224 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
225 context
.firstLineMatcher
= matcher
;
226 context
.firstLine
= line
;
227 context
.nextLineLocation
= context
.raFile
.getFilePointer();
228 context
.inputLine
= input
;
233 for (final InputLine input
: nextInputs
) {
234 final Matcher matcher
= input
.getPattern().matcher(line
);
235 if (matcher
.find()) {
236 event
.processGroups(input
, matcher
);
237 currentInput
= input
;
238 if (countMap
.get(currentInput
) == null) {
239 countMap
.put(currentInput
, 1);
241 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
243 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
244 while (iter
.hasNext()) {
245 final InputLine inputLine
= iter
.next();
246 if (inputLine
.level
> currentInput
.level
) {
250 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
251 currentInput
= currentInput
.childrenInputs
.get(0);
252 countMap
.put(currentInput
, 0);
253 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
254 if (currentInput
.getNextInputs(countMap
).size() > 0) {
255 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
256 if (countMap
.get(currentInput
) == null) {
257 countMap
.put(currentInput
, 0);
259 iter
= countMap
.keySet().iterator();
260 while (iter
.hasNext()) {
261 final InputLine inputLine
= iter
.next();
262 if (inputLine
.level
> currentInput
.level
) {
275 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
276 if (matcher
.find()) {
277 event
.processGroups(currentInput
, matcher
);
278 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
279 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
280 currentInput
= currentInput
.childrenInputs
.get(0);
281 countMap
.put(currentInput
, 0);
282 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
283 if (currentInput
.getNextInputs(countMap
).size() > 0) {
284 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
285 if (countMap
.get(currentInput
) == null) {
286 countMap
.put(currentInput
, 0);
288 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
289 while (iter
.hasNext()) {
290 final InputLine inputLine
= iter
.next();
291 if (inputLine
.level
> currentInput
.level
) {
299 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
302 rawPos
= context
.raFile
.getFilePointer();
304 } catch (final IOException e
) {
308 for(final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet())
309 if (entry
.getValue() < entry
.getKey().getMinCount()) {
312 context
.setLocation(NULL_LOCATION
);
316 public List
<InputLine
> getFirstLines() {
317 return fDefinition
.inputs
;
320 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
321 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
322 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
323 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
327 public CustomTraceDefinition
getDefinition() {