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 @SuppressWarnings({ "unchecked", "rawtypes" })
52 public ITmfTrace
copy() {
53 // TODO Auto-generated method stub
58 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
59 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
60 //new Throwable().printStackTrace();
61 CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
62 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile()) {
66 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
67 if (location
!= null && location
.getLocation() instanceof Long
) {
68 raFile
.seek((Long
)location
.getLocation());
71 long rawPos
= raFile
.getFilePointer();
72 while ((line
= raFile
.getNextLine()) != null) {
73 for (InputLine input
: getFirstLines()) {
74 Matcher matcher
= input
.getPattern().matcher(line
);
76 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
77 context
.raFile
= raFile
;
78 context
.firstLineMatcher
= matcher
;
79 context
.firstLine
= line
;
80 context
.nextLineLocation
= raFile
.getFilePointer();
81 context
.inputLine
= input
;
85 rawPos
= raFile
.getFilePointer();
88 } catch (FileNotFoundException e
) {
91 } catch (IOException e
) {
99 public TmfContext
seekLocation(double ratio
) {
101 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
102 long pos
= (long) (ratio
* raFile
.length());
104 raFile
.seek(pos
- 1);
105 if (raFile
.read() == '\n') break;
108 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(new Long(pos
));
109 TmfContext context
= seekLocation(location
);
110 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
112 } catch (FileNotFoundException e
) {
114 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
115 } catch (IOException e
) {
117 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
122 public double getLocationRatio(ITmfLocation
<?
> location
) {
124 if (location
.getLocation() instanceof Long
) {
125 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
126 return (double) ((Long
) location
.getLocation()) / raFile
.length();
128 } catch (FileNotFoundException e
) {
130 } catch (IOException e
) {
137 public ITmfLocation
<?
> getCurrentLocation() {
138 // TODO Auto-generated method stub
143 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
144 ITmfContext savedContext
= context
.clone();
145 TmfEvent event
= parseEvent(context
);
147 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
148 context
.updateRank(1);
154 public TmfEvent
parseEvent(TmfContext tmfContext
) {
155 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
156 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
160 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
161 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
165 CustomTxtEvent event
= parseFirstLine(context
);
167 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
168 InputLine currentInput
= null;
169 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
170 currentInput
= context
.inputLine
.childrenInputs
.get(0);
171 countMap
.put(currentInput
, 0);
174 synchronized (context
.raFile
) {
176 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
177 context
.raFile
.seek(context
.nextLineLocation
);
180 long rawPos
= context
.raFile
.getFilePointer();
181 while ((line
= context
.raFile
.getNextLine()) != null) {
182 boolean processed
= false;
183 if (currentInput
== null) {
184 for (InputLine input
: getFirstLines()) {
185 Matcher matcher
= input
.getPattern().matcher(line
);
186 if (matcher
.find()) {
187 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
188 context
.firstLineMatcher
= matcher
;
189 context
.firstLine
= line
;
190 context
.nextLineLocation
= context
.raFile
.getFilePointer();
191 context
.inputLine
= input
;
196 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
197 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
198 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
199 for (InputLine input
: getFirstLines()) {
200 Matcher matcher
= input
.getPattern().matcher(line
);
201 if (matcher
.find()) {
202 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
203 context
.firstLineMatcher
= matcher
;
204 context
.firstLine
= line
;
205 context
.nextLineLocation
= context
.raFile
.getFilePointer();
206 context
.inputLine
= input
;
211 for (InputLine input
: nextInputs
) {
212 Matcher matcher
= input
.getPattern().matcher(line
);
213 if (matcher
.find()) {
214 event
.processGroups(input
, matcher
);
215 currentInput
= input
;
216 if (countMap
.get(currentInput
) == null) {
217 countMap
.put(currentInput
, 1);
219 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
221 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
222 while (iter
.hasNext()) {
223 InputLine inputLine
= iter
.next();
224 if (inputLine
.level
> currentInput
.level
) {
228 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
229 currentInput
= currentInput
.childrenInputs
.get(0);
230 countMap
.put(currentInput
, 0);
232 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
233 if (currentInput
.getNextInputs(countMap
).size() > 0) {
234 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
235 if (countMap
.get(currentInput
) == null) {
236 countMap
.put(currentInput
, 0);
238 iter
= countMap
.keySet().iterator();
239 while (iter
.hasNext()) {
240 InputLine inputLine
= iter
.next();
241 if (inputLine
.level
> currentInput
.level
) {
256 Matcher matcher
= currentInput
.getPattern().matcher(line
);
257 if (matcher
.find()) {
258 event
.processGroups(currentInput
, matcher
);
259 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
260 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
261 currentInput
= currentInput
.childrenInputs
.get(0);
262 countMap
.put(currentInput
, 0);
264 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
265 if (currentInput
.getNextInputs(countMap
).size() > 0) {
266 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
267 if (countMap
.get(currentInput
) == null) {
268 countMap
.put(currentInput
, 0);
270 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
271 while (iter
.hasNext()) {
272 InputLine inputLine
= iter
.next();
273 if (inputLine
.level
> currentInput
.level
) {
283 ((StringBuffer
) event
.getContent().getContent()).append("\n").append(line
); //$NON-NLS-1$
286 rawPos
= context
.raFile
.getFilePointer();
288 } catch (IOException e
) {
292 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
293 if (entry
.getValue() < entry
.getKey().getMinCount()) {
297 context
.setLocation(NULL_LOCATION
);
301 public List
<InputLine
> getFirstLines() {
302 return fDefinition
.inputs
;
305 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
306 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, TmfTimestamp
.Zero
, new TmfEventSource(""), fEventType
, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
307 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
308 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
312 public CustomTraceDefinition
getDefinition() {