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
;
15 import java
.io
.FileNotFoundException
;
16 import java
.io
.IOException
;
17 import java
.util
.HashMap
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.Map
.Entry
;
21 import java
.util
.regex
.Matcher
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.parsers
.custom
.CustomTxtTraceDefinition
.InputLine
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.io
.BufferedRandomAccessFile
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
38 public class CustomTxtTrace
extends TmfTrace
implements ITmfEventParser
{
40 private static final TmfLocation
<Long
> NULL_LOCATION
= new TmfLocation
<Long
>((Long
) null);
41 private static final int DEFAULT_CACHE_SIZE
= 100;
43 private final CustomTxtTraceDefinition fDefinition
;
44 private final CustomTxtEventType fEventType
;
45 private BufferedRandomAccessFile fFile
;
47 public CustomTxtTrace(final CustomTxtTraceDefinition definition
) {
48 fDefinition
= definition
;
49 fEventType
= new CustomTxtEventType(fDefinition
);
50 setCacheSize(DEFAULT_CACHE_SIZE
);
53 public CustomTxtTrace(final IResource resource
, final CustomTxtTraceDefinition definition
, final String path
, final int cacheSize
) throws TmfTraceException
{
55 setCacheSize((cacheSize
> 0) ? cacheSize
: DEFAULT_CACHE_SIZE
);
56 initTrace(resource
, path
, CustomTxtEvent
.class);
60 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
) throws TmfTraceException
{
61 super.initTrace(resource
, path
, eventType
);
63 fFile
= new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
64 } catch (IOException e
) {
65 throw new TmfTraceException(e
.getMessage(), e
);
71 public synchronized void dispose() {
76 } catch (IOException e
) {
84 public synchronized TmfContext
seekEvent(final ITmfLocation
<?
> location
) {
85 final CustomTxtTraceContext context
= new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
86 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
90 if (location
== null) {
92 } else if (location
.getLocationData() instanceof Long
) {
93 fFile
.seek((Long
) location
.getLocationData());
96 long rawPos
= fFile
.getFilePointer();
97 while ((line
= fFile
.getNextLine()) != null) {
98 for (final InputLine input
: getFirstLines()) {
99 final Matcher matcher
= input
.getPattern().matcher(line
);
100 if (matcher
.find()) {
101 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
102 context
.firstLineMatcher
= matcher
;
103 context
.firstLine
= line
;
104 context
.nextLineLocation
= fFile
.getFilePointer();
105 context
.inputLine
= input
;
109 rawPos
= fFile
.getFilePointer();
112 } catch (final FileNotFoundException e
) {
113 Activator
.getDefault().logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
115 } catch (final IOException e
) {
116 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
123 public synchronized TmfContext
seekEvent(final double ratio
) {
125 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
128 long pos
= (long) (ratio
* fFile
.length());
131 if (fFile
.read() == '\n') {
136 final ITmfLocation
<?
> location
= new TmfLocation
<Long
>(pos
);
137 final TmfContext context
= seekEvent(location
);
138 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
140 } catch (final IOException e
) {
141 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
142 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
147 public synchronized double getLocationRatio(final ITmfLocation
<?
> location
) {
152 if (location
.getLocationData() instanceof Long
) {
153 return (double) ((Long
) location
.getLocationData()) / fFile
.length();
155 } catch (final IOException e
) {
156 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
162 public ITmfLocation
<?
> getCurrentLocation() {
163 // TODO Auto-generated method stub
168 public synchronized CustomTxtEvent
parseEvent(final ITmfContext tmfContext
) {
169 ITmfContext context
= seekEvent(tmfContext
.getLocation());
170 return parse(context
);
174 public synchronized CustomTxtEvent
getNext(final ITmfContext context
) {
175 final ITmfContext savedContext
= context
.clone();
176 final CustomTxtEvent event
= parse(context
);
178 updateAttributes(savedContext
, event
.getTimestamp());
179 context
.increaseRank();
184 private synchronized CustomTxtEvent
parse(final ITmfContext tmfContext
) {
188 if (!(tmfContext
instanceof CustomTxtTraceContext
)) {
192 final CustomTxtTraceContext context
= (CustomTxtTraceContext
) tmfContext
;
193 if (!(context
.getLocation().getLocationData() instanceof Long
) || NULL_LOCATION
.equals(context
.getLocation())) {
197 CustomTxtEvent event
= parseFirstLine(context
);
199 final HashMap
<InputLine
, Integer
> countMap
= new HashMap
<InputLine
, Integer
>();
200 InputLine currentInput
= null;
201 if (context
.inputLine
.childrenInputs
!= null && context
.inputLine
.childrenInputs
.size() > 0) {
202 currentInput
= context
.inputLine
.childrenInputs
.get(0);
203 countMap
.put(currentInput
, 0);
207 if (fFile
.getFilePointer() != context
.nextLineLocation
) {
208 fFile
.seek(context
.nextLineLocation
);
211 long rawPos
= fFile
.getFilePointer();
212 while ((line
= fFile
.getNextLine()) != null) {
213 boolean processed
= false;
214 if (currentInput
== null) {
215 for (final InputLine input
: getFirstLines()) {
216 final Matcher matcher
= input
.getPattern().matcher(line
);
217 if (matcher
.find()) {
218 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
219 context
.firstLineMatcher
= matcher
;
220 context
.firstLine
= line
;
221 context
.nextLineLocation
= fFile
.getFilePointer();
222 context
.inputLine
= input
;
227 if (countMap
.get(currentInput
) >= currentInput
.getMinCount()) {
228 final List
<InputLine
> nextInputs
= currentInput
.getNextInputs(countMap
);
229 if (nextInputs
.size() == 0 || nextInputs
.get(nextInputs
.size() - 1).getMinCount() == 0) {
230 for (final InputLine input
: getFirstLines()) {
231 final Matcher matcher
= input
.getPattern().matcher(line
);
232 if (matcher
.find()) {
233 context
.setLocation(new TmfLocation
<Long
>(rawPos
));
234 context
.firstLineMatcher
= matcher
;
235 context
.firstLine
= line
;
236 context
.nextLineLocation
= fFile
.getFilePointer();
237 context
.inputLine
= input
;
242 for (final InputLine input
: nextInputs
) {
243 final Matcher matcher
= input
.getPattern().matcher(line
);
244 if (matcher
.find()) {
245 event
.processGroups(input
, matcher
);
246 currentInput
= input
;
247 if (countMap
.get(currentInput
) == null) {
248 countMap
.put(currentInput
, 1);
250 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
252 Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
253 while (iter
.hasNext()) {
254 final InputLine inputLine
= iter
.next();
255 if (inputLine
.level
> currentInput
.level
) {
259 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
260 currentInput
= currentInput
.childrenInputs
.get(0);
261 countMap
.put(currentInput
, 0);
262 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
263 if (currentInput
.getNextInputs(countMap
).size() > 0) {
264 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
265 if (countMap
.get(currentInput
) == null) {
266 countMap
.put(currentInput
, 0);
268 iter
= countMap
.keySet().iterator();
269 while (iter
.hasNext()) {
270 final InputLine inputLine
= iter
.next();
271 if (inputLine
.level
> currentInput
.level
) {
285 final Matcher matcher
= currentInput
.getPattern().matcher(line
);
286 if (matcher
.find()) {
287 event
.processGroups(currentInput
, matcher
);
288 countMap
.put(currentInput
, countMap
.get(currentInput
) + 1);
289 if (currentInput
.childrenInputs
!= null && currentInput
.childrenInputs
.size() > 0) {
290 currentInput
= currentInput
.childrenInputs
.get(0);
291 countMap
.put(currentInput
, 0);
292 } else if (countMap
.get(currentInput
) >= currentInput
.getMaxCount()) {
293 if (currentInput
.getNextInputs(countMap
).size() > 0) {
294 currentInput
= currentInput
.getNextInputs(countMap
).get(0);
295 if (countMap
.get(currentInput
) == null) {
296 countMap
.put(currentInput
, 0);
298 final Iterator
<InputLine
> iter
= countMap
.keySet().iterator();
299 while (iter
.hasNext()) {
300 final InputLine inputLine
= iter
.next();
301 if (inputLine
.level
> currentInput
.level
) {
310 ((StringBuffer
) event
.getContent().getValue()).append("\n").append(line
); //$NON-NLS-1$
313 rawPos
= fFile
.getFilePointer();
315 } catch (final IOException e
) {
316 Activator
.getDefault().logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
318 for (final Entry
<InputLine
, Integer
> entry
: countMap
.entrySet()) {
319 if (entry
.getValue() < entry
.getKey().getMinCount()) {
323 context
.setLocation(NULL_LOCATION
);
327 public List
<InputLine
> getFirstLines() {
328 return fDefinition
.inputs
;
331 public CustomTxtEvent
parseFirstLine(final CustomTxtTraceContext context
) {
332 final CustomTxtEvent event
= new CustomTxtEvent(fDefinition
, this, TmfTimestamp
.ZERO
, "", fEventType
, ""); //$NON-NLS-1$ //$NON-NLS-2$
333 event
.processGroups(context
.inputLine
, context
.firstLineMatcher
);
334 event
.setContent(new CustomEventContent(event
, new StringBuffer(context
.firstLine
)));
338 public CustomTraceDefinition
getDefinition() {
343 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
346 public boolean validate(IProject project
, String path
) {
347 return fileExists(path
);