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
.io
.RandomAccessFile
;
19 import java
.util
.HashMap
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
22 import java
.util
.Map
.Entry
;
23 import java
.util
.regex
.Matcher
;
24 import java
.util
.regex
.Pattern
;
26 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventReference
;
28 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventSource
;
29 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventType
;
30 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
39 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> {
41 private CustomTxtTraceDefinition fDefinition
;
43 public CustomTxtTrace(String name
, CustomTxtTraceDefinition definition
, String path
, int cacheSize
) throws FileNotFoundException
{
44 super(name
, CustomTxtEvent
.class, path
, cacheSize
);
45 fDefinition
= definition
;
49 public ITmfTrace
createTraceCopy() {
50 // TODO Auto-generated method stub
55 public TmfContext
seekLocation(ITmfLocation
<?
> location
) {
56 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
57 //new Throwable().printStackTrace();
58 CustomTxtTraceContext context
= new CustomTxtTraceContext(new TmfLocation
<Long
>((Long
)null), ITmfContext
.INITIAL_RANK
);
59 if (!new File(getPath()).isFile()) {
63 RandomAccessFile raFile
= new RandomAccessFile(getPath(), "r"); //$NON-NLS-1$
64 if (location
!= null && location
.getLocation() instanceof Long
) {
65 raFile
.seek((Long
)location
.getLocation());
68 long rawPos
= raFile
.getFilePointer();
69 while ((line
= raFile
.readLine()) != null) {
70 for (InputLine input
: getFirstLines()) {
71 Matcher matcher
= input
.getPattern().matcher(line
);
73 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
74 context
.raFile
= raFile
;
75 context
.firstLineMatcher
= matcher
;
76 context
.nextLineLocation
= raFile
.getFilePointer();
77 context
.inputLine
= input
;
81 rawPos
= raFile
.getFilePointer();
84 } catch (FileNotFoundException e
) {
87 } catch (IOException e
) {
95 public ITmfLocation
<?
> getCurrentLocation() {
96 // TODO Auto-generated method stub
101 public synchronized TmfEvent
getNextEvent(TmfContext context
) {
102 ITmfContext savedContext
= context
.clone();
103 TmfEvent event
= parseEvent(context
);
105 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
106 context
.updateRank(1);
112 public TmfEvent
parseEvent(TmfContext tmfContext
) {
113 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
114 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
118 CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
119 if (!(context
.getLocation().getLocation() instanceof Long
)) {
123 CustomTxtEvent event
= parseFirstLine(context
);
125 HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
126 InputLine currentInput
= null;
127 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
128 currentInput
= context
.inputLine
.childrenInputs
.get(0);
129 countMap
.put(currentInput
, 0);
132 synchronized (context
.raFile
) {
134 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
135 context
.raFile
.seek(context
.nextLineLocation
);
138 long rawPos
= context
.raFile
.getFilePointer();
139 while ((line
= context
.raFile
.readLine()) != null) {
140 boolean processed
= false;
141 if (currentInput
== null) {
142 for (InputLine input
: getFirstLines()) {
143 Matcher matcher
= input
.getPattern().matcher(line
);
144 if (matcher
.find()) {
145 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
146 context
.firstLineMatcher
= matcher
;
147 context
.nextLineLocation
= context
.raFile
.getFilePointer();
148 context
.inputLine
= input
;
153 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
154 List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
155 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
156 for (InputLine input
: getFirstLines()) {
157 Matcher matcher
= input
.getPattern().matcher(line
);
158 if (matcher
.find()) {
159 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
160 context
.firstLineMatcher
= matcher
;
161 context
.nextLineLocation
= context
.raFile
.getFilePointer();
162 context
.inputLine
= input
;
167 for (InputLine input
: nextInputs
) {
168 Matcher matcher
= input
.getPattern().matcher(line
);
169 if (matcher
.find()) {
170 event
.processGroups(input
, matcher
);
171 currentInput
= input
;
172 if (countMap
.get(currentInput
) == null) {
173 countMap
.put(currentInput
, 1);
175 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
177 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
178 while (iter
.hasNext()) {
179 InputLine inputLine
= iter
.next();
180 if (inputLine
.level
> currentInput
.level
) {
184 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
185 currentInput
= currentInput
.childrenInputs
.get(0);
186 countMap
.put(currentInput
, 0);
188 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
189 if (currentInput
.getNextInputs(countMap
).size() > 0) {
190 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
191 if (countMap
.get(currentInput
) == null) {
192 countMap
.put(currentInput
, 0);
194 iter
= countMap
.keySet().iterator();
195 while (iter
.hasNext()) {
196 InputLine inputLine
= iter
.next();
197 if (inputLine
.level
> currentInput
.level
) {
212 Matcher matcher
= currentInput
.getPattern().matcher(line
);
213 if (matcher
.find()) {
214 event
.processGroups(currentInput
, matcher
);
215 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
216 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
217 currentInput
= currentInput
.childrenInputs
.get(0);
218 countMap
.put(currentInput
, 0);
220 if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
221 if (currentInput
.getNextInputs(countMap
).size() > 0) {
222 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
223 if (countMap
.get(currentInput
) == null) {
224 countMap
.put(currentInput
, 0);
226 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
227 while (iter
.hasNext()) {
228 InputLine inputLine
= iter
.next();
229 if (inputLine
.level
> currentInput
.level
) {
241 rawPos
= context
.raFile
.getFilePointer();
243 } catch (IOException e
) {
247 for(Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
248 if (entry
.getValue() < entry
.getKey().getMinCount()) {
252 context
.setLocation(new TmfLocation
<Long
>((Long
)null));
256 public List
<InputLine
> getFirstLines() {
257 return fDefinition
.inputs
;
260 public CustomTxtEvent
parseFirstLine(CustomTxtTraceContext context
) {
261 CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, TmfTimestamp
.Zero
, new TmfEventSource(""), new TmfEventType(fDefinition
.definitionName
, new String
[0]), new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
262 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
266 public void parseNextLine(CustomTxtEvent event
, String line
, InputLine input
) {
267 Pattern pattern
= input
.getPattern();
268 Matcher matcher
= pattern
.matcher(line
);
269 if (matcher
.find()) {
270 event
.processGroups(input
, matcher
);
275 public CustomTraceDefinition
getDefinition() {