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
.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
.tmf
.event
.TmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventReference
;
26 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventSource
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.io
.BufferedRandomAccessFile
;
29 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfContext
;
30 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfLocation
;
31 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
37 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> {
39 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
41 private CustomTxtTraceDefinition fDefinition
;
42 private CustomTxtEventType fEventType
;
44 public CustomTxtTrace(String name
, CustomTxtTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
45 super(name
, CustomTxtEvent
.class, path
, cacheSize
);
46 fDefinition
= definition
;
47 fEventType
= new CustomTxtEventType(fDefinition
);
51 public ITmfTrace
createTraceCopy() {
52 // TODO Auto-generated method stub
57 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
58 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
59 //new Throwable().printStackTrace();
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
>(new 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(TmfContext 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(TmfContext tmfContext
) {
154 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
155 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
159 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
160 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
164 CustomTxtEvent event
= parseFirstLine(context
);
166 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
167 InputLine currentInput
= null;
168 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
169 currentInput
= context
.inputLine
.childrenInputs
.get(0);
170 countMap
.put(currentInput
, 0);
173 synchronized (context
.raFile
) {
175 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
176 context
.raFile
.seek(context
.nextLineLocation
);
179 long rawPos
= context
.raFile
.getFilePointer();
180 while ((line
= context
.raFile
.getNextLine()) != null) {
181 boolean processed
= false;
182 if (currentInput
== null) {
183 for (InputLine input
: getFirstLines()) {
184 Matcher matcher
= input
.getPattern().matcher(line
);
185 if (matcher
.find()) {
186 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
187 context
.firstLineMatcher
= matcher
;
188 context
.firstLine
= line
;
189 context
.nextLineLocation
= context
.raFile
.getFilePointer();
190 context
.inputLine
= input
;
195 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
196 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
197 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
198 for (InputLine input
: getFirstLines()) {
199 Matcher matcher
= input
.getPattern().matcher(line
);
200 if (matcher
.find()) {
201 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
202 context
.firstLineMatcher
= matcher
;
203 context
.firstLine
= line
;
204 context
.nextLineLocation
= context
.raFile
.getFilePointer();
205 context
.inputLine
= input
;
210 for (InputLine input
: nextInputs
) {
211 Matcher matcher
= input
.getPattern().matcher(line
);
212 if (matcher
.find()) {
213 event
.processGroups(input
, matcher
);
214 currentInput
= input
;
215 if (countMap
.get(currentInput
) == null) {
216 countMap
.put(currentInput
, 1);
218 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
220 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
221 while (iter
.hasNext()) {
222 InputLine inputLine
= iter
.next();
223 if (inputLine
.level
> currentInput
.level
) {
227 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
228 currentInput
= currentInput
.childrenInputs
.get(0);
229 countMap
.put(currentInput
, 0);
231 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
232 if (currentInput
.getNextInputs(countMap
).size() > 0) {
233 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
234 if (countMap
.get(currentInput
) == null) {
235 countMap
.put(currentInput
, 0);
237 iter
= countMap
.keySet().iterator();
238 while (iter
.hasNext()) {
239 InputLine inputLine
= iter
.next();
240 if (inputLine
.level
> currentInput
.level
) {
255 Matcher matcher
= currentInput
.getPattern().matcher(line
);
256 if (matcher
.find()) {
257 event
.processGroups(currentInput
, matcher
);
258 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
259 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
260 currentInput
= currentInput
.childrenInputs
.get(0);
261 countMap
.put(currentInput
, 0);
263 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
264 if (currentInput
.getNextInputs(countMap
).size() > 0) {
265 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
266 if (countMap
.get(currentInput
) == null) {
267 countMap
.put(currentInput
, 0);
269 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
270 while (iter
.hasNext()) {
271 InputLine inputLine
= iter
.next();
272 if (inputLine
.level
> currentInput
.level
) {
282 ((StringBuffer
) event
.getContent().getContent()).append("\n").append(line
); //$NON-NLS-1$
285 rawPos
= context
.raFile
.getFilePointer();
287 } catch (IOException e
) {
291 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
292 if (entry
.getValue() < entry
.getKey().getMinCount()) {
296 context
.setLocation(NULL_LOCATION
);
300 public List
<InputLine
> getFirstLines() {
301 return fDefinition
.inputs
;
304 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
305 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, TmfTimestamp
.Zero
, new TmfEventSource(""), fEventType
, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
306 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
307 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
311 public CustomTraceDefinition
getDefinition() {