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
);
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()) {
65 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
66 if (location
!= null && location
.getLocation() instanceof Long
) {
67 raFile
.seek((Long
)location
.getLocation());
70 long rawPos
= raFile
.getFilePointer();
71 while ((line
= raFile
.getNextLine()) != null) {
72 for (InputLine input
: getFirstLines()) {
73 Matcher matcher
= input
.getPattern().matcher(line
);
75 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
76 context
.raFile
= raFile
;
77 context
.firstLineMatcher
= matcher
;
78 context
.firstLine
= line
;
79 context
.nextLineLocation
= raFile
.getFilePointer();
80 context
.inputLine
= input
;
84 rawPos
= raFile
.getFilePointer();
87 } catch (FileNotFoundException e
) {
90 } catch (IOException e
) {
98 public TmfContext
seekLocation(double ratio
) {
100 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
101 long pos
= (long) (ratio
* raFile
.length());
103 raFile
.seek(pos
- 1);
104 if (raFile
.read() == '\n') break;
107 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
108 TmfContext context
= seekLocation(location
);
109 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
111 } catch (FileNotFoundException e
) {
113 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
114 } catch (IOException e
) {
116 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
121 public double getLocationRatio(ITmfLocation
<?
> location
) {
123 if (location
.getLocation() instanceof Long
) {
124 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
125 return (double) ((Long
) location
.getLocation()) / raFile
.length();
127 } catch (FileNotFoundException e
) {
129 } catch (IOException e
) {
136 public ITmfLocation
<?
> getCurrentLocation() {
137 // TODO Auto-generated method stub
142 public synchronized TmfEvent
getNextEvent(ITmfContext context
) {
143 ITmfContext savedContext
= context
.clone();
144 TmfEvent event
= parseEvent(context
);
146 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
147 context
.updateRank(1);
153 public TmfEvent
parseEvent(ITmfContext tmfContext
) {
154 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
158 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
159 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
163 CustomTxtEvent event
= parseFirstLine(context
);
165 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
166 InputLine currentInput
= null;
167 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
168 currentInput
= context
.inputLine
.childrenInputs
.get(0);
169 countMap
.put(currentInput
, 0);
172 synchronized (context
.raFile
) {
174 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
175 context
.raFile
.seek(context
.nextLineLocation
);
178 long rawPos
= context
.raFile
.getFilePointer();
179 while ((line
= context
.raFile
.getNextLine()) != null) {
180 boolean processed
= false;
181 if (currentInput
== null) {
182 for (InputLine input
: getFirstLines()) {
183 Matcher matcher
= input
.getPattern().matcher(line
);
184 if (matcher
.find()) {
185 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
186 context
.firstLineMatcher
= matcher
;
187 context
.firstLine
= line
;
188 context
.nextLineLocation
= context
.raFile
.getFilePointer();
189 context
.inputLine
= input
;
194 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
195 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
196 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
197 for (InputLine input
: getFirstLines()) {
198 Matcher matcher
= input
.getPattern().matcher(line
);
199 if (matcher
.find()) {
200 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
201 context
.firstLineMatcher
= matcher
;
202 context
.firstLine
= line
;
203 context
.nextLineLocation
= context
.raFile
.getFilePointer();
204 context
.inputLine
= input
;
209 for (InputLine input
: nextInputs
) {
210 Matcher matcher
= input
.getPattern().matcher(line
);
211 if (matcher
.find()) {
212 event
.processGroups(input
, matcher
);
213 currentInput
= input
;
214 if (countMap
.get(currentInput
) == null) {
215 countMap
.put(currentInput
, 1);
217 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
219 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
220 while (iter
.hasNext()) {
221 InputLine inputLine
= iter
.next();
222 if (inputLine
.level
> currentInput
.level
) {
226 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
227 currentInput
= currentInput
.childrenInputs
.get(0);
228 countMap
.put(currentInput
, 0);
230 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
231 if (currentInput
.getNextInputs(countMap
).size() > 0) {
232 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
233 if (countMap
.get(currentInput
) == null) {
234 countMap
.put(currentInput
, 0);
236 iter
= countMap
.keySet().iterator();
237 while (iter
.hasNext()) {
238 InputLine inputLine
= iter
.next();
239 if (inputLine
.level
> currentInput
.level
) {
254 Matcher matcher
= currentInput
.getPattern().matcher(line
);
255 if (matcher
.find()) {
256 event
.processGroups(currentInput
, matcher
);
257 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
258 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
259 currentInput
= currentInput
.childrenInputs
.get(0);
260 countMap
.put(currentInput
, 0);
262 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
263 if (currentInput
.getNextInputs(countMap
).size() > 0) {
264 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
265 if (countMap
.get(currentInput
) == null) {
266 countMap
.put(currentInput
, 0);
268 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
269 while (iter
.hasNext()) {
270 InputLine inputLine
= iter
.next();
271 if (inputLine
.level
> currentInput
.level
) {
281 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
284 rawPos
= context
.raFile
.getFilePointer();
286 } catch (IOException e
) {
290 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
291 if (entry
.getValue() < entry
.getKey().getMinCount()) {
295 context
.setLocation(NULL_LOCATION
);
299 public List
<InputLine
> getFirstLines() {
300 return fDefinition
.inputs
;
303 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
304 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, (TmfTimestamp
) TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
305 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
306 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
310 public CustomTraceDefinition
getDefinition() {