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
.core
.event
.TmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
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
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
35 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> {
37 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
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 cacheSize
) throws FileNotFoundException
{
48 super(name
, CustomTxtEvent
.class, path
, cacheSize
);
49 fDefinition
= definition
;
50 fEventType
= new CustomTxtEventType(fDefinition
);
54 @SuppressWarnings({ "unchecked", "rawtypes" })
55 public ITmfTrace
copy() {
56 // TODO Auto-generated method stub
61 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
62 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
63 //new Throwable().printStackTrace();
64 CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
65 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile()) {
69 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
70 if (location
!= null && location
.getLocation() instanceof Long
) {
71 raFile
.seek((Long
)location
.getLocation());
74 long rawPos
= raFile
.getFilePointer();
75 while ((line
= raFile
.getNextLine()) != null) {
76 for (InputLine input
: getFirstLines()) {
77 Matcher matcher
= input
.getPattern().matcher(line
);
79 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
80 context
.raFile
= raFile
;
81 context
.firstLineMatcher
= matcher
;
82 context
.firstLine
= line
;
83 context
.nextLineLocation
= raFile
.getFilePointer();
84 context
.inputLine
= input
;
88 rawPos
= raFile
.getFilePointer();
91 } catch (FileNotFoundException e
) {
94 } catch (IOException e
) {
102 public TmfContext
seekLocation(double ratio
) {
104 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
105 long pos
= (long) (ratio
* raFile
.length());
107 raFile
.seek(pos
- 1);
108 if (raFile
.read() == '\n') break;
111 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long(pos
));
112 TmfContext context
= seekLocation(location
);
113 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
115 } catch (FileNotFoundException e
) {
117 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
118 } catch (IOException e
) {
120 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
125 public double getLocationRatio(ITmfLocation
<?
> location
) {
127 if (location
.getLocation() instanceof Long
) {
128 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
129 return (double) ((Long
) location
.getLocation()) / raFile
.length();
131 } catch (FileNotFoundException e
) {
133 } catch (IOException e
) {
140 public ITmfLocation
<?
> getCurrentLocation() {
141 // TODO Auto-generated method stub
146 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
147 ITmfContext savedContext
= context
.clone();
148 TmfEvent event
= parseEvent(context
);
150 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
151 context
.updateRank(1);
157 public TmfEvent
parseEvent(TmfContext tmfContext
) {
158 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
159 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
163 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
164 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
168 CustomTxtEvent event
= parseFirstLine(context
);
170 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
171 InputLine currentInput
= null;
172 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
173 currentInput
= context
.inputLine
.childrenInputs
.get(0);
174 countMap
.put(currentInput
, 0);
177 synchronized (context
.raFile
) {
179 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
180 context
.raFile
.seek(context
.nextLineLocation
);
183 long rawPos
= context
.raFile
.getFilePointer();
184 while ((line
= context
.raFile
.getNextLine()) != null) {
185 boolean processed
= false;
186 if (currentInput
== null) {
187 for (InputLine input
: getFirstLines()) {
188 Matcher matcher
= input
.getPattern().matcher(line
);
189 if (matcher
.find()) {
190 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
191 context
.firstLineMatcher
= matcher
;
192 context
.firstLine
= line
;
193 context
.nextLineLocation
= context
.raFile
.getFilePointer();
194 context
.inputLine
= input
;
199 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
200 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
201 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
202 for (InputLine input
: getFirstLines()) {
203 Matcher matcher
= input
.getPattern().matcher(line
);
204 if (matcher
.find()) {
205 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
206 context
.firstLineMatcher
= matcher
;
207 context
.firstLine
= line
;
208 context
.nextLineLocation
= context
.raFile
.getFilePointer();
209 context
.inputLine
= input
;
214 for (InputLine input
: nextInputs
) {
215 Matcher matcher
= input
.getPattern().matcher(line
);
216 if (matcher
.find()) {
217 event
.processGroups(input
, matcher
);
218 currentInput
= input
;
219 if (countMap
.get(currentInput
) == null) {
220 countMap
.put(currentInput
, 1);
222 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
224 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
225 while (iter
.hasNext()) {
226 InputLine inputLine
= iter
.next();
227 if (inputLine
.level
> currentInput
.level
) {
231 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
232 currentInput
= currentInput
.childrenInputs
.get(0);
233 countMap
.put(currentInput
, 0);
235 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
236 if (currentInput
.getNextInputs(countMap
).size() > 0) {
237 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
238 if (countMap
.get(currentInput
) == null) {
239 countMap
.put(currentInput
, 0);
241 iter
= countMap
.keySet().iterator();
242 while (iter
.hasNext()) {
243 InputLine inputLine
= iter
.next();
244 if (inputLine
.level
> currentInput
.level
) {
259 Matcher matcher
= currentInput
.getPattern().matcher(line
);
260 if (matcher
.find()) {
261 event
.processGroups(currentInput
, matcher
);
262 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
263 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
264 currentInput
= currentInput
.childrenInputs
.get(0);
265 countMap
.put(currentInput
, 0);
267 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
268 if (currentInput
.getNextInputs(countMap
).size() > 0) {
269 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
270 if (countMap
.get(currentInput
) == null) {
271 countMap
.put(currentInput
, 0);
273 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
274 while (iter
.hasNext()) {
275 InputLine inputLine
= iter
.next();
276 if (inputLine
.level
> currentInput
.level
) {
286 ((StringBuffer
) event
.getContent().getContent()).append("\n").append(line
); //$NON-NLS-1$
289 rawPos
= context
.raFile
.getFilePointer();
291 } catch (IOException e
) {
295 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
296 if (entry
.getValue() < entry
.getKey().getMinCount()) {
300 context
.setLocation(NULL_LOCATION
);
304 public List
<InputLine
> getFirstLines() {
305 return fDefinition
.inputs
;
308 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
309 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, (TmfTimestamp
) TmfTimestamp
.Zero
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
310 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
311 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
315 public CustomTraceDefinition
getDefinition() {