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(CustomTxtTraceDefinition definition
) {
45 fDefinition
= definition
;
46 fEventType
= new CustomTxtEventType(fDefinition
);
49 public CustomTxtTrace(String name
, CustomTxtTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
50 super(name
, CustomTxtEvent
.class, path
, cacheSize
);
51 fDefinition
= definition
;
52 fEventType
= new CustomTxtEventType(fDefinition
);
56 @SuppressWarnings({ "unchecked", "rawtypes" })
57 public ITmfTrace
copy() {
58 // TODO Auto-generated method stub
63 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
64 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
65 //new Throwable().printStackTrace();
66 CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
67 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile()) {
71 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
72 if (location
!= null && location
.getLocation() instanceof Long
) {
73 raFile
.seek((Long
)location
.getLocation());
76 long rawPos
= raFile
.getFilePointer();
77 while ((line
= raFile
.getNextLine()) != null) {
78 for (InputLine input
: getFirstLines()) {
79 Matcher matcher
= input
.getPattern().matcher(line
);
81 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
82 context
.raFile
= raFile
;
83 context
.firstLineMatcher
= matcher
;
84 context
.firstLine
= line
;
85 context
.nextLineLocation
= raFile
.getFilePointer();
86 context
.inputLine
= input
;
90 rawPos
= raFile
.getFilePointer();
93 } catch (FileNotFoundException e
) {
96 } catch (IOException e
) {
104 public TmfContext
seekLocation(double ratio
) {
106 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
107 long pos
= (long) (ratio
* raFile
.length());
109 raFile
.seek(pos
- 1);
110 if (raFile
.read() == '\n') break;
113 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long(pos
));
114 TmfContext context
= seekLocation(location
);
115 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
117 } catch (FileNotFoundException e
) {
119 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
120 } catch (IOException e
) {
122 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
127 public double getLocationRatio(ITmfLocation
<?
> location
) {
129 if (location
.getLocation() instanceof Long
) {
130 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
131 return (double) ((Long
) location
.getLocation()) / raFile
.length();
133 } catch (FileNotFoundException e
) {
135 } catch (IOException e
) {
142 public ITmfLocation
<?
> getCurrentLocation() {
143 // TODO Auto-generated method stub
148 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
149 ITmfContext savedContext
= context
.clone();
150 TmfEvent event
= parseEvent(context
);
152 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
153 context
.updateRank(1);
159 public TmfEvent
parseEvent(TmfContext tmfContext
) {
160 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
161 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
165 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
166 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
170 CustomTxtEvent event
= parseFirstLine(context
);
172 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
173 InputLine currentInput
= null;
174 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
175 currentInput
= context
.inputLine
.childrenInputs
.get(0);
176 countMap
.put(currentInput
, 0);
179 synchronized (context
.raFile
) {
181 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
182 context
.raFile
.seek(context
.nextLineLocation
);
185 long rawPos
= context
.raFile
.getFilePointer();
186 while ((line
= context
.raFile
.getNextLine()) != null) {
187 boolean processed
= false;
188 if (currentInput
== null) {
189 for (InputLine input
: getFirstLines()) {
190 Matcher matcher
= input
.getPattern().matcher(line
);
191 if (matcher
.find()) {
192 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
193 context
.firstLineMatcher
= matcher
;
194 context
.firstLine
= line
;
195 context
.nextLineLocation
= context
.raFile
.getFilePointer();
196 context
.inputLine
= input
;
201 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
202 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
203 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
204 for (InputLine input
: getFirstLines()) {
205 Matcher matcher
= input
.getPattern().matcher(line
);
206 if (matcher
.find()) {
207 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
208 context
.firstLineMatcher
= matcher
;
209 context
.firstLine
= line
;
210 context
.nextLineLocation
= context
.raFile
.getFilePointer();
211 context
.inputLine
= input
;
216 for (InputLine input
: nextInputs
) {
217 Matcher matcher
= input
.getPattern().matcher(line
);
218 if (matcher
.find()) {
219 event
.processGroups(input
, matcher
);
220 currentInput
= input
;
221 if (countMap
.get(currentInput
) == null) {
222 countMap
.put(currentInput
, 1);
224 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
226 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
227 while (iter
.hasNext()) {
228 InputLine inputLine
= iter
.next();
229 if (inputLine
.level
> currentInput
.level
) {
233 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
234 currentInput
= currentInput
.childrenInputs
.get(0);
235 countMap
.put(currentInput
, 0);
237 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
238 if (currentInput
.getNextInputs(countMap
).size() > 0) {
239 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
240 if (countMap
.get(currentInput
) == null) {
241 countMap
.put(currentInput
, 0);
243 iter
= countMap
.keySet().iterator();
244 while (iter
.hasNext()) {
245 InputLine inputLine
= iter
.next();
246 if (inputLine
.level
> currentInput
.level
) {
261 Matcher matcher
= currentInput
.getPattern().matcher(line
);
262 if (matcher
.find()) {
263 event
.processGroups(currentInput
, matcher
);
264 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
265 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
266 currentInput
= currentInput
.childrenInputs
.get(0);
267 countMap
.put(currentInput
, 0);
269 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
270 if (currentInput
.getNextInputs(countMap
).size() > 0) {
271 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
272 if (countMap
.get(currentInput
) == null) {
273 countMap
.put(currentInput
, 0);
275 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
276 while (iter
.hasNext()) {
277 InputLine inputLine
= iter
.next();
278 if (inputLine
.level
> currentInput
.level
) {
288 ((StringBuffer
) event
.getContent().getContent()).append("\n").append(line
); //$NON-NLS-1$
291 rawPos
= context
.raFile
.getFilePointer();
293 } catch (IOException e
) {
297 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
298 if (entry
.getValue() < entry
.getKey().getMinCount()) {
302 context
.setLocation(NULL_LOCATION
);
306 public List
<InputLine
> getFirstLines() {
307 return fDefinition
.inputs
;
310 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
311 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.Zero
, new TmfEventSource(""), fEventType
, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
312 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
313 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
317 public CustomTraceDefinition
getDefinition() {