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
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
34 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> {
36 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
37 private static final int DEFAULT_CACHE_SIZE
= 100;
39 private CustomTxtTraceDefinition fDefinition
;
40 private CustomTxtEventType fEventType
;
42 public CustomTxtTrace(CustomTxtTraceDefinition definition
) {
43 fDefinition
= definition
;
44 fEventType
= new CustomTxtEventType(fDefinition
);
47 public CustomTxtTrace(String name
, CustomTxtTraceDefinition definition
, String path
, int pageSize
) throws FileNotFoundException
{
48 super(name
, CustomTxtEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
, true);
49 fDefinition
= definition
;
50 fEventType
= new CustomTxtEventType(fDefinition
);
54 public void initTrace(String name
, String path
, Class
<CustomTxtEvent
> eventType
) throws FileNotFoundException
{
55 super.initTrace(name
, path
, eventType
);
59 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
60 CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
61 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 (InputLine input
: getFirstLines()) {
74 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 (FileNotFoundException e
) {
91 } catch (IOException e
) {
98 } catch (IOException e
) {
106 public TmfContext
seekLocation(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') break;
116 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
117 TmfContext context
= seekLocation(location
);
118 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
120 } catch (FileNotFoundException e
) {
122 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
123 } catch (IOException e
) {
125 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
127 if (raFile
!= null) {
130 } catch (IOException e
) {
137 public double getLocationRatio(ITmfLocation
<?
> location
) {
138 BufferedRandomAccessFile raFile
= null;
140 if (location
.getLocation() instanceof Long
) {
141 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
142 return (double) ((Long
) location
.getLocation()) / raFile
.length();
144 } catch (FileNotFoundException e
) {
146 } catch (IOException e
) {
149 if (raFile
!= null) {
152 } catch (IOException e
) {
160 public ITmfLocation
<?
> getCurrentLocation() {
161 // TODO Auto-generated method stub
166 public synchronized TmfEvent
getNextEvent(ITmfContext context
) {
167 ITmfContext savedContext
= context
.clone();
168 TmfEvent event
= parseEvent(context
);
170 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
171 context
.increaseRank();
177 public TmfEvent
parseEvent(ITmfContext tmfContext
) {
178 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
182 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
183 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
187 CustomTxtEvent event
= parseFirstLine(context
);
189 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 (InputLine input
: getFirstLines()) {
207 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 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
220 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
221 for (InputLine input
: getFirstLines()) {
222 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 (InputLine input
: nextInputs
) {
234 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 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);
254 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 InputLine inputLine
= iter
.next();
263 if (inputLine
.level
> currentInput
.level
) {
278 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);
286 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
287 if (currentInput
.getNextInputs(countMap
).size() > 0) {
288 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
289 if (countMap
.get(currentInput
) == null) {
290 countMap
.put(currentInput
, 0);
292 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
293 while (iter
.hasNext()) {
294 InputLine inputLine
= iter
.next();
295 if (inputLine
.level
> currentInput
.level
) {
305 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
308 rawPos
= context
.raFile
.getFilePointer();
310 } catch (IOException e
) {
314 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
315 if (entry
.getValue() < entry
.getKey().getMinCount()) {
319 context
.setLocation(NULL_LOCATION
);
323 public List
<InputLine
> getFirstLines() {
324 return fDefinition
.inputs
;
327 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
328 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, (TmfTimestamp
) TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
329 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
330 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
334 public CustomTraceDefinition
getDefinition() {