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 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long((long) (ratio
* raFile
.length())));
102 TmfContext context
= seekLocation(location
);
103 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
105 } catch (FileNotFoundException e
) {
107 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
108 } catch (IOException e
) {
110 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
115 public double getLocationRatio(ITmfLocation
<?
> location
) {
117 if (location
.getLocation() instanceof Long
) {
118 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
119 return (double) ((Long
) location
.getLocation()) / raFile
.length();
121 } catch (FileNotFoundException e
) {
123 } catch (IOException e
) {
130 public ITmfLocation
<?
> getCurrentLocation() {
131 // TODO Auto-generated method stub
136 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
137 ITmfContext savedContext
= context
.clone();
138 TmfEvent event
= parseEvent(context
);
140 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
141 context
.updateRank(1);
147 public TmfEvent
parseEvent(TmfContext tmfContext
) {
148 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
149 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
153 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
154 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
158 CustomTxtEvent event
= parseFirstLine(context
);
160 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
161 InputLine currentInput
= null;
162 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
163 currentInput
= context
.inputLine
.childrenInputs
.get(0);
164 countMap
.put(currentInput
, 0);
167 synchronized (context
.raFile
) {
169 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
170 context
.raFile
.seek(context
.nextLineLocation
);
173 long rawPos
= context
.raFile
.getFilePointer();
174 while ((line
= context
.raFile
.getNextLine()) != null) {
175 boolean processed
= false;
176 if (currentInput
== null) {
177 for (InputLine input
: getFirstLines()) {
178 Matcher matcher
= input
.getPattern().matcher(line
);
179 if (matcher
.find()) {
180 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
181 context
.firstLineMatcher
= matcher
;
182 context
.firstLine
= line
;
183 context
.nextLineLocation
= context
.raFile
.getFilePointer();
184 context
.inputLine
= input
;
189 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
190 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
191 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
192 for (InputLine input
: getFirstLines()) {
193 Matcher matcher
= input
.getPattern().matcher(line
);
194 if (matcher
.find()) {
195 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
196 context
.firstLineMatcher
= matcher
;
197 context
.firstLine
= line
;
198 context
.nextLineLocation
= context
.raFile
.getFilePointer();
199 context
.inputLine
= input
;
204 for (InputLine input
: nextInputs
) {
205 Matcher matcher
= input
.getPattern().matcher(line
);
206 if (matcher
.find()) {
207 event
.processGroups(input
, matcher
);
208 currentInput
= input
;
209 if (countMap
.get(currentInput
) == null) {
210 countMap
.put(currentInput
, 1);
212 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
214 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
215 while (iter
.hasNext()) {
216 InputLine inputLine
= iter
.next();
217 if (inputLine
.level
> currentInput
.level
) {
221 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
222 currentInput
= currentInput
.childrenInputs
.get(0);
223 countMap
.put(currentInput
, 0);
225 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
226 if (currentInput
.getNextInputs(countMap
).size() > 0) {
227 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
228 if (countMap
.get(currentInput
) == null) {
229 countMap
.put(currentInput
, 0);
231 iter
= countMap
.keySet().iterator();
232 while (iter
.hasNext()) {
233 InputLine inputLine
= iter
.next();
234 if (inputLine
.level
> currentInput
.level
) {
249 Matcher matcher
= currentInput
.getPattern().matcher(line
);
250 if (matcher
.find()) {
251 event
.processGroups(currentInput
, matcher
);
252 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
253 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
254 currentInput
= currentInput
.childrenInputs
.get(0);
255 countMap
.put(currentInput
, 0);
257 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
258 if (currentInput
.getNextInputs(countMap
).size() > 0) {
259 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
260 if (countMap
.get(currentInput
) == null) {
261 countMap
.put(currentInput
, 0);
263 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
264 while (iter
.hasNext()) {
265 InputLine inputLine
= iter
.next();
266 if (inputLine
.level
> currentInput
.level
) {
276 ((StringBuffer
) event
.getContent().getContent()).append("\n").append(line
); //$NON-NLS-1$
279 rawPos
= context
.raFile
.getFilePointer();
281 } catch (IOException e
) {
285 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
286 if (entry
.getValue() < entry
.getKey().getMinCount()) {
290 context
.setLocation(NULL_LOCATION
);
294 public List
<InputLine
> getFirstLines() {
295 return fDefinition
.inputs
;
298 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
299 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, TmfTimestamp
.Zero
, new TmfEventSource(""), fEventType
, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
300 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
301 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
305 public CustomTraceDefinition
getDefinition() {