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
.IProject
;
25 import org
.eclipse
.core
.resources
.IResource
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
37 public class CustomTxtTrace
extends TmfTrace
<CustomTxtEvent
> implements ITmfEventParser
<CustomTxtEvent
> {
39 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
40 private static final int DEFAULT_CACHE_SIZE
= 100;
42 private final CustomTxtTraceDefinition fDefinition
;
43 private final CustomTxtEventType fEventType
;
45 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
46 fDefinition
= definition
;
47 fEventType
= new CustomTxtEventType(fDefinition
);
50 public CustomTxtTrace(final IResource resource
, final CustomTxtTraceDefinition definition
, final String path
, final int pageSize
) throws TmfTraceException
{
51 super(resource
, CustomTxtEvent
.class, path
, (pageSize
> 0) ? pageSize
: DEFAULT_CACHE_SIZE
);
52 fDefinition
= definition
;
53 fEventType
= new CustomTxtEventType(fDefinition
);
57 public void initTrace(final IResource resource
, final String path
, final Class
<CustomTxtEvent
> eventType
) throws TmfTraceException
{
58 super.initTrace(resource
, path
, eventType
);
62 public TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
63 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
64 if (NULL_LOCATION
.equals(location
) || !new File(getPath()).isFile())
66 BufferedRandomAccessFile raFile
= null;
68 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
69 if (location
!= null && location
.getLocation() instanceof Long
) {
70 raFile
.seek((Long
)location
.getLocation());
73 long rawPos
= raFile
.getFilePointer();
74 while ((line
= raFile
.getNextLine()) != null) {
75 for (final InputLine input
: getFirstLines()) {
76 final Matcher matcher
= input
.getPattern().matcher(line
);
78 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
79 context
.raFile
= raFile
;
80 context
.firstLineMatcher
= matcher
;
81 context
.firstLine
= line
;
82 context
.nextLineLocation
= raFile
.getFilePointer();
83 context
.inputLine
= input
;
87 rawPos
= raFile
.getFilePointer();
90 } catch (final FileNotFoundException e
) {
93 } catch (final IOException e
) {
100 } catch (final IOException e
) {
108 public TmfContext
seekEvent(final double ratio
) {
109 BufferedRandomAccessFile raFile
= null;
111 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
112 long pos
= (long) (ratio
* raFile
.length());
114 raFile
.seek(pos
- 1);
115 if (raFile
.read() == '\n') {
120 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
121 final TmfContext context
= seekEvent(location
);
122 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
124 } catch (final FileNotFoundException e
) {
126 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
127 } catch (final IOException e
) {
129 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
131 if (raFile
!= null) {
134 } catch (final IOException e
) {
141 public double getLocationRatio(final ITmfLocation
<?
> location
) {
142 BufferedRandomAccessFile raFile
= null;
144 if (location
.getLocation() instanceof Long
) {
145 raFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
146 return (double) ((Long
) location
.getLocation()) / raFile
.length();
148 } catch (final FileNotFoundException e
) {
150 } catch (final IOException e
) {
153 if (raFile
!= null) {
156 } catch (final IOException e
) {
164 public ITmfLocation
<?
> getCurrentLocation() {
165 // TODO Auto-generated method stub
170 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
171 final ITmfContext savedContext
= context
.clone();
172 final CustomTxtEvent event
= parseEvent(context
);
174 updateAttributes(savedContext
, event
.getTimestamp());
175 context
.increaseRank();
181 public CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
182 if (!(tmfContext
instanceof CustomTxtTraceContext
))
185 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
186 if (!(context
.getLocation().getLocation() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation()))
189 CustomTxtEvent event
= parseFirstLine(context
);
191 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
192 InputLine currentInput
= null;
193 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
194 currentInput
= context
.inputLine
.childrenInputs
.get(0);
195 countMap
.put(currentInput
, 0);
198 synchronized (context
.raFile
) {
200 if (context
.raFile
.getFilePointer() != context
.nextLineLocation
) {
201 context
.raFile
.seek(context
.nextLineLocation
);
204 long rawPos
= context
.raFile
.getFilePointer();
205 while ((line
= context
.raFile
.getNextLine()) != null) {
206 boolean processed
= false;
207 if (currentInput
== null) {
208 for (final InputLine input
: getFirstLines()) {
209 final Matcher matcher
= input
.getPattern().matcher(line
);
210 if (matcher
.find()) {
211 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
212 context
.firstLineMatcher
= matcher
;
213 context
.firstLine
= line
;
214 context
.nextLineLocation
= context
.raFile
.getFilePointer();
215 context
.inputLine
= input
;
220 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
221 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
222 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
223 for (final InputLine input
: getFirstLines()) {
224 final Matcher matcher
= input
.getPattern().matcher(line
);
225 if (matcher
.find()) {
226 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
227 context
.firstLineMatcher
= matcher
;
228 context
.firstLine
= line
;
229 context
.nextLineLocation
= context
.raFile
.getFilePointer();
230 context
.inputLine
= input
;
235 for (final InputLine input
: nextInputs
) {
236 final Matcher matcher
= input
.getPattern().matcher(line
);
237 if (matcher
.find()) {
238 event
.processGroups(input
, matcher
);
239 currentInput
= input
;
240 if (countMap
.get(currentInput
) == null) {
241 countMap
.put(currentInput
, 1);
243 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
245 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
246 while (iter
.hasNext()) {
247 final InputLine inputLine
= iter
.next();
248 if (inputLine
.level
> currentInput
.level
) {
252 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
253 currentInput
= currentInput
.childrenInputs
.get(0);
254 countMap
.put(currentInput
, 0);
255 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
256 if (currentInput
.getNextInputs(countMap
).size() > 0) {
257 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
258 if (countMap
.get(currentInput
) == null) {
259 countMap
.put(currentInput
, 0);
261 iter
= countMap
.keySet().iterator();
262 while (iter
.hasNext()) {
263 final InputLine inputLine
= iter
.next();
264 if (inputLine
.level
> currentInput
.level
) {
277 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
278 if (matcher
.find()) {
279 event
.processGroups(currentInput
, matcher
);
280 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
281 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
282 currentInput
= currentInput
.childrenInputs
.get(0);
283 countMap
.put(currentInput
, 0);
284 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount())
285 if (currentInput
.getNextInputs(countMap
).size() > 0) {
286 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
287 if (countMap
.get(currentInput
) == null) {
288 countMap
.put(currentInput
, 0);
290 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
291 while (iter
.hasNext()) {
292 final InputLine inputLine
= iter
.next();
293 if (inputLine
.level
> currentInput
.level
) {
301 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
304 rawPos
= context
.raFile
.getFilePointer();
306 } catch (final IOException e
) {
310 for(final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet())
311 if (entry
.getValue() < entry
.getKey().getMinCount()) {
314 context
.setLocation(NULL_LOCATION
);
318 public List
<InputLine
> getFirstLines() {
319 return fDefinition
.inputs
;
322 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
323 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
324 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
325 event
.setContent(new CustomEventContent(event
, context
.firstLine
));
329 public CustomTraceDefinition
getDefinition() {
334 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
337 public boolean validate(IProject project
, String path
) {
338 return fileExists(path
);