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
.internal
.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
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
35 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> {
37 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
38 private static final int CACHE_SIZE
= 100;
40 private CustomTxtTraceDefinition fDefinition
;
41 private CustomTxtEventType fEventType
;
43 public CustomTxtTrace(CustomTxtTraceDefinition definition
) {
44 fDefinition
= definition
;
45 fEventType
= new CustomTxtEventType(fDefinition
);
48 public CustomTxtTrace(String name
, CustomTxtTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
49 super(name
, CustomTxtEvent
.class, path
, cacheSize
);
50 fDefinition
= definition
;
51 fEventType
= new CustomTxtEventType(fDefinition
);
55 public void initTrace(String name
, String path
, Class
<CustomTxtEvent
> eventType
, boolean indexTrace
) throws FileNotFoundException
{
56 super.initTrace(name
, path
, eventType
, CACHE_SIZE
, indexTrace
);
60 @SuppressWarnings({ "unchecked", "rawtypes" })
61 public ITmfTrace
copy() {
62 // TODO Auto-generated method stub
67 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
68 CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
69 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile()) {
73 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
74 if (location
!= null && location
.getLocation() instanceof Long
) {
75 raFile
.seek((Long
)location
.getLocation());
78 long rawPos
= raFile
.getFilePointer();
79 while ((line
= raFile
.getNextLine()) != null) {
80 for (InputLine input
: getFirstLines()) {
81 Matcher matcher
= input
.getPattern().matcher(line
);
83 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
84 context
.raFile
= raFile
;
85 context
.firstLineMatcher
= matcher
;
86 context
.firstLine
= line
;
87 context
.nextLineLocation
= raFile
.getFilePointer();
88 context
.inputLine
= input
;
92 rawPos
= raFile
.getFilePointer();
95 } catch (FileNotFoundException e
) {
98 } catch (IOException e
) {
106 public TmfContext
seekLocation(double ratio
) {
108 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
109 long pos
= (long) (ratio
* raFile
.length());
111 raFile
.seek(pos
- 1);
112 if (raFile
.read() == '\n') break;
115 ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
116 TmfContext context
= seekLocation(location
);
117 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
119 } catch (FileNotFoundException e
) {
121 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
122 } catch (IOException e
) {
124 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
129 public double getLocationRatio(ITmfLocation
<?
> location
) {
131 if (location
.getLocation() instanceof Long
) {
132 BufferedRandomAccessFile raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
133 return (double) ((Long
) location
.getLocation()) / raFile
.length();
135 } catch (FileNotFoundException e
) {
137 } catch (IOException e
) {
144 public ITmfLocation
<?
> getCurrentLocation() {
145 // TODO Auto-generated method stub
150 public synchronized TmfEvent
getNextEvent(ITmfContext context
) {
151 ITmfContext savedContext
= context
.clone();
152 TmfEvent event
= parseEvent(context
);
154 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
155 context
.updateRank(1);
161 public TmfEvent
parseEvent(ITmfContext tmfContext
) {
162 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
166 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
167 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
171 CustomTxtEvent event
= parseFirstLine(context
);
173 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
174 InputLine currentInput
= null;
175 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
176 currentInput
= context
.inputLine
.childrenInputs
.get(0);
177 countMap
.put(currentInput
, 0);
180 synchronized (context
.raFile
) {
182 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
183 context
.raFile
.seek(context
.nextLineLocation
);
186 long rawPos
= context
.raFile
.getFilePointer();
187 while ((line
= context
.raFile
.getNextLine()) != null) {
188 boolean processed
= false;
189 if (currentInput
== null) {
190 for (InputLine input
: getFirstLines()) {
191 Matcher matcher
= input
.getPattern().matcher(line
);
192 if (matcher
.find()) {
193 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
194 context
.firstLineMatcher
= matcher
;
195 context
.firstLine
= line
;
196 context
.nextLineLocation
= context
.raFile
.getFilePointer();
197 context
.inputLine
= input
;
202 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
203 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
204 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
205 for (InputLine input
: getFirstLines()) {
206 Matcher matcher
= input
.getPattern().matcher(line
);
207 if (matcher
.find()) {
208 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
209 context
.firstLineMatcher
= matcher
;
210 context
.firstLine
= line
;
211 context
.nextLineLocation
= context
.raFile
.getFilePointer();
212 context
.inputLine
= input
;
217 for (InputLine input
: nextInputs
) {
218 Matcher matcher
= input
.getPattern().matcher(line
);
219 if (matcher
.find()) {
220 event
.processGroups(input
, matcher
);
221 currentInput
= input
;
222 if (countMap
.get(currentInput
) == null) {
223 countMap
.put(currentInput
, 1);
225 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
227 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
228 while (iter
.hasNext()) {
229 InputLine inputLine
= iter
.next();
230 if (inputLine
.level
> currentInput
.level
) {
234 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
235 currentInput
= currentInput
.childrenInputs
.get(0);
236 countMap
.put(currentInput
, 0);
238 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
239 if (currentInput
.getNextInputs(countMap
).size() > 0) {
240 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
241 if (countMap
.get(currentInput
) == null) {
242 countMap
.put(currentInput
, 0);
244 iter
= countMap
.keySet().iterator();
245 while (iter
.hasNext()) {
246 InputLine inputLine
= iter
.next();
247 if (inputLine
.level
> currentInput
.level
) {
262 Matcher matcher
= currentInput
.getPattern().matcher(line
);
263 if (matcher
.find()) {
264 event
.processGroups(currentInput
, matcher
);
265 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
266 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
267 currentInput
= currentInput
.childrenInputs
.get(0);
268 countMap
.put(currentInput
, 0);
270 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
271 if (currentInput
.getNextInputs(countMap
).size() > 0) {
272 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
273 if (countMap
.get(currentInput
) == null) {
274 countMap
.put(currentInput
, 0);
276 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
277 while (iter
.hasNext()) {
278 InputLine inputLine
= iter
.next();
279 if (inputLine
.level
> currentInput
.level
) {
289 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
292 rawPos
= context
.raFile
.getFilePointer();
294 } catch (IOException e
) {
298 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
299 if (entry
.getValue() < entry
.getKey().getMinCount()) {
303 context
.setLocation(NULL_LOCATION
);
307 public List
<InputLine
> getFirstLines() {
308 return fDefinition
.inputs
;
311 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
312 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, (TmfTimestamp
) TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
313 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
314 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
318 public CustomTraceDefinition
getDefinition() {