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
.core
.resources
.IResource
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
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 DEFAULT_CACHE_SIZE
= 100;
40 private final CustomTxtTraceDefinition fDefinition
;
41 private final CustomTxtEventType fEventType
;
43 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
44 fDefinition
= definition
;
45 fEventType
= new CustomTxtEventType(fDefinition
);
48 public CustomTxtTrace(final IResource resource
, final CustomTxtTraceDefinition definition
, final String path
, final int pageSize
) throws FileNotFoundException
{
49 super(resource
, CustomTxtEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
, true);
50 fDefinition
= definition
;
51 fEventType
= new CustomTxtEventType(fDefinition
);
55 public void initTrace(final IResource resource
, final String path
, final Class
<CustomTxtEvent
> eventType
) throws FileNotFoundException
{
56 super.initTrace(resource
, path
, eventType
);
60 public TmfContext
seekLocation(final ITmfLocation
<?
> location
) {
61 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
62 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
64 BufferedRandomAccessFile raFile
= null;
66 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
67 if (location
!= null && location
.getLocation() instanceof Long
)
68 raFile
.seek((Long
)location
.getLocation());
70 long rawPos
= raFile
.getFilePointer();
71 while ((line
= raFile
.getNextLine()) != null) {
72 for (final InputLine input
: getFirstLines()) {
73 final 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 (final FileNotFoundException e
) {
90 } catch (final IOException e
) {
97 } catch (final IOException e
) {
104 public TmfContext
seekLocation(final double ratio
) {
105 BufferedRandomAccessFile raFile
= null;
107 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
108 long pos
= (long) (ratio
* raFile
.length());
110 raFile
.seek(pos
- 1);
111 if (raFile
.read() == '\n') break;
114 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
115 final TmfContext context
= seekLocation(location
);
116 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
118 } catch (final FileNotFoundException e
) {
120 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
121 } catch (final IOException e
) {
123 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.INITIAL_RANK
);
128 } catch (final IOException e
) {
134 public double getLocationRatio(final ITmfLocation
<?
> location
) {
135 BufferedRandomAccessFile raFile
= null;
137 if (location
.getLocation() instanceof Long
) {
138 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
139 return (double) ((Long
) location
.getLocation()) / raFile
.length();
141 } catch (final FileNotFoundException e
) {
143 } catch (final IOException e
) {
149 } catch (final IOException e
) {
156 public ITmfLocation
<?
> getCurrentLocation() {
157 // TODO Auto-generated method stub
162 public synchronized TmfEvent
getNextEvent(final ITmfContext context
) {
163 final ITmfContext savedContext
= context
.clone();
164 final TmfEvent event
= parseEvent(context
);
166 updateIndex(savedContext
, savedContext
.getRank(), event
.getTimestamp());
167 context
.increaseRank();
173 public TmfEvent
parseEvent(final ITmfContext tmfContext
) {
174 if (!(tmfContext
instanceof CustomTxtTraceContext
))
177 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
178 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
181 CustomTxtEvent event
= parseFirstLine(context
);
183 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
184 InputLine currentInput
= null;
185 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
186 currentInput
= context
.inputLine
.childrenInputs
.get(0);
187 countMap
.put(currentInput
, 0);
190 synchronized (context
.raFile
) {
192 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
)
193 context
.raFile
.seek(context
.nextLineLocation
);
195 long rawPos
= context
.raFile
.getFilePointer();
196 while ((line
= context
.raFile
.getNextLine()) != null) {
197 boolean processed
= false;
198 if (currentInput
== null)
199 for (final InputLine input
: getFirstLines()) {
200 final 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 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
212 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
213 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0)
214 for (final InputLine input
: getFirstLines()) {
215 final Matcher matcher
= input
.getPattern().matcher(line
);
216 if (matcher
.find()) {
217 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
218 context
.firstLineMatcher
= matcher
;
219 context
.firstLine
= line
;
220 context
.nextLineLocation
= context
.raFile
.getFilePointer();
221 context
.inputLine
= input
;
225 for (final InputLine input
: nextInputs
) {
226 final Matcher matcher
= input
.getPattern().matcher(line
);
227 if (matcher
.find()) {
228 event
.processGroups(input
, matcher
);
229 currentInput
= input
;
230 if (countMap
.get(currentInput
) == null)
231 countMap
.put(currentInput
, 1);
233 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
234 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
235 while (iter
.hasNext()) {
236 final InputLine inputLine
= iter
.next();
237 if (inputLine
.level
> currentInput
.level
)
240 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
241 currentInput
= currentInput
.childrenInputs
.get(0);
242 countMap
.put(currentInput
, 0);
243 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
244 if (currentInput
.getNextInputs(countMap
).size() > 0) {
245 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
246 if (countMap
.get(currentInput
) == null)
247 countMap
.put(currentInput
, 0);
248 iter
= countMap
.keySet().iterator();
249 while (iter
.hasNext()) {
250 final InputLine inputLine
= iter
.next();
251 if (inputLine
.level
> currentInput
.level
)
262 final 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);
269 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
270 if (currentInput
.getNextInputs(countMap
).size() > 0) {
271 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
272 if (countMap
.get(currentInput
) == null)
273 countMap
.put(currentInput
, 0);
274 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
275 while (iter
.hasNext()) {
276 final InputLine inputLine
= iter
.next();
277 if (inputLine
.level
> currentInput
.level
)
283 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
286 rawPos
= context
.raFile
.getFilePointer();
288 } catch (final IOException e
) {
292 for(final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet())
293 if (entry
.getValue() < entry
.getKey().getMinCount())
295 context
.setLocation(NULL_LOCATION
);
299 public List
<InputLine
> getFirstLines() {
300 return fDefinition
.inputs
;
303 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
304 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
305 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
306 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
310 public CustomTraceDefinition
getDefinition() {