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.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;
44
private BufferedRandomAccessFile fFile;
46
public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
47
fDefinition = definition;
48
fEventType = new CustomTxtEventType(fDefinition);
49
setCacheSize(DEFAULT_CACHE_SIZE);
52
public CustomTxtTrace(final IResource resource, final CustomTxtTraceDefinition definition, final String path, final int cacheSize) throws TmfTraceException {
54
setCacheSize((cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE);
55
initTrace(resource, path, CustomTxtEvent.class);
59
public void initTrace(final IResource resource, final String path, final Class<CustomTxtEvent> eventType) throws TmfTraceException {
60
super.initTrace(resource, path, eventType);
62
fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
63
} catch (IOException e) {
64
throw new TmfTraceException(e.getMessage(), e);
70
public synchronized void dispose() {
75
} catch (IOException e) {
83
public synchronized TmfContext seekEvent(final ITmfLocation<?> location) {
84
final CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
85
if (NULL_LOCATION.equals(location) || fFile == null) {
89
if (location == null) {
91
} else if (location.getLocation() instanceof Long) {
92
fFile.seek((Long) location.getLocation());
95
long rawPos = fFile.getFilePointer();
96
while ((line = fFile.getNextLine()) != null) {
97
for (final InputLine input : getFirstLines()) {
98
final Matcher matcher = input.getPattern().matcher(line);
100
context.setLocation(new TmfLocation<Long>(rawPos));
101
context.firstLineMatcher = matcher;
102
context.firstLine = line;
103
context.nextLineLocation = fFile.getFilePointer();
104
context.inputLine = input;
108
rawPos = fFile.getFilePointer();
111
} catch (final FileNotFoundException e) {
112
Activator.getDefault().logError("Error seeking event. File not found: " + getPath(), e); //$NON-NLS-1$
114
} catch (final IOException e) {
115
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
122
public synchronized TmfContext seekEvent(final double ratio) {
124
return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
127
long pos = (long) (ratio * fFile.length());
130
if (fFile.read() == '\n') {
135
final ITmfLocation<?> location = new TmfLocation<Long>(pos);
136
final TmfContext context = seekEvent(location);
137
context.setRank(ITmfContext.UNKNOWN_RANK);
139
} catch (final IOException e) {
140
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
141
return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
146
public synchronized double getLocationRatio(final ITmfLocation<?> location) {
151
if (location.getLocation() instanceof Long) {
152
return (double) ((Long) location.getLocation()) / fFile.length();
154
} catch (final IOException e) {
155
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
161
public ITmfLocation<?> getCurrentLocation() {
162
// TODO Auto-generated method stub
167
public synchronized CustomTxtEvent parseEvent(final ITmfContext tmfContext) {
168
ITmfContext context = seekEvent(tmfContext.getLocation());
169
return parse(context);
173
public synchronized CustomTxtEvent getNext(final ITmfContext context) {
174
final ITmfContext savedContext = context.clone();
175
final CustomTxtEvent event = parse(context);
177
updateAttributes(savedContext, event.getTimestamp());
178
context.increaseRank();
183
private synchronized CustomTxtEvent parse(final ITmfContext tmfContext) {
187
if (!(tmfContext instanceof CustomTxtTraceContext)) {
191
final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
192
if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
196
CustomTxtEvent event = parseFirstLine(context);
198
final HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
199
InputLine currentInput = null;
200
if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
201
currentInput = context.inputLine.childrenInputs.get(0);
202
countMap.put(currentInput, 0);
206
if (fFile.getFilePointer() != context.nextLineLocation) {
207
fFile.seek(context.nextLineLocation);
210
long rawPos = fFile.getFilePointer();
211
while ((line = fFile.getNextLine()) != null) {
212
boolean processed = false;
213
if (currentInput == null) {
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 = fFile.getFilePointer();
221
context.inputLine = input;
226
if (countMap.get(currentInput) >= currentInput.getMinCount()) {
227
final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
228
if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
229
for (final InputLine input : getFirstLines()) {
230
final Matcher matcher = input.getPattern().matcher(line);
231
if (matcher.find()) {
232
context.setLocation(new TmfLocation<Long>(rawPos));
233
context.firstLineMatcher = matcher;
234
context.firstLine = line;
235
context.nextLineLocation = fFile.getFilePointer();
236
context.inputLine = input;
241
for (final InputLine input : nextInputs) {
242
final Matcher matcher = input.getPattern().matcher(line);
243
if (matcher.find()) {
244
event.processGroups(input, matcher);
245
currentInput = input;
246
if (countMap.get(currentInput) == null) {
247
countMap.put(currentInput, 1);
249
countMap.put(currentInput, countMap.get(currentInput) + 1);
251
Iterator<InputLine> iter = countMap.keySet().iterator();
252
while (iter.hasNext()) {
253
final InputLine inputLine = iter.next();
254
if (inputLine.level > currentInput.level) {
258
if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
259
currentInput = currentInput.childrenInputs.get(0);
260
countMap.put(currentInput, 0);
261
} else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
262
if (currentInput.getNextInputs(countMap).size() > 0) {
263
currentInput = currentInput.getNextInputs(countMap).get(0);
264
if (countMap.get(currentInput) == null) {
265
countMap.put(currentInput, 0);
267
iter = countMap.keySet().iterator();
268
while (iter.hasNext()) {
269
final InputLine inputLine = iter.next();
270
if (inputLine.level > currentInput.level) {
284
final Matcher matcher = currentInput.getPattern().matcher(line);
285
if (matcher.find()) {
286
event.processGroups(currentInput, matcher);
287
countMap.put(currentInput, countMap.get(currentInput) + 1);
288
if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
289
currentInput = currentInput.childrenInputs.get(0);
290
countMap.put(currentInput, 0);
291
} else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
292
if (currentInput.getNextInputs(countMap).size() > 0) {
293
currentInput = currentInput.getNextInputs(countMap).get(0);
294
if (countMap.get(currentInput) == null) {
295
countMap.put(currentInput, 0);
297
final Iterator<InputLine> iter = countMap.keySet().iterator();
298
while (iter.hasNext()) {
299
final InputLine inputLine = iter.next();
300
if (inputLine.level > currentInput.level) {
309
((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
312
rawPos = fFile.getFilePointer();
314
} catch (final IOException e) {
315
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
317
for (final Entry<InputLine, Integer> entry : countMap.entrySet()) {
318
if (entry.getValue() < entry.getKey().getMinCount()) {
322
context.setLocation(NULL_LOCATION);
326
public List<InputLine> getFirstLines() {
327
return fDefinition.inputs;
330
public CustomTxtEvent parseFirstLine(final CustomTxtTraceContext context) {
331
final CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
332
event.processGroups(context.inputLine, context.firstLineMatcher);
333
event.setContent(new CustomEventContent(event, new StringBuffer(context.firstLine)));
337
public CustomTraceDefinition getDefinition() {
342
* @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
345
public boolean validate(IProject project, String path) {
346
return fileExists(path);
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.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;
44
private BufferedRandomAccessFile fFile;
46
public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
47
fDefinition = definition;
48
fEventType = new CustomTxtEventType(fDefinition);
49
setCacheSize(DEFAULT_CACHE_SIZE);
52
public CustomTxtTrace(final IResource resource, final CustomTxtTraceDefinition definition, final String path, final int cacheSize) throws TmfTraceException {
54
setCacheSize((cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE);
55
initTrace(resource, path, CustomTxtEvent.class);
59
public void initTrace(final IResource resource, final String path, final Class<CustomTxtEvent> eventType) throws TmfTraceException {
60
super.initTrace(resource, path, eventType);
62
fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
63
} catch (IOException e) {
64
throw new TmfTraceException(e.getMessage(), e);
70
public synchronized void dispose() {
75
} catch (IOException e) {
83
public synchronized TmfContext seekEvent(final ITmfLocation<?> location) {
84
final CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
85
if (NULL_LOCATION.equals(location) || fFile == null) {
89
if (location == null) {
91
} else if (location.getLocation() instanceof Long) {
92
fFile.seek((Long) location.getLocation());
95
long rawPos = fFile.getFilePointer();
96
while ((line = fFile.getNextLine()) != null) {
97
for (final InputLine input : getFirstLines()) {
98
final Matcher matcher = input.getPattern().matcher(line);
100
context.setLocation(new TmfLocation<Long>(rawPos));
101
context.firstLineMatcher = matcher;
102
context.firstLine = line;
103
context.nextLineLocation = fFile.getFilePointer();
104
context.inputLine = input;
108
rawPos = fFile.getFilePointer();
111
} catch (final FileNotFoundException e) {
112
Activator.getDefault().logError("Error seeking event. File not found: " + getPath(), e); //$NON-NLS-1$
114
} catch (final IOException e) {
115
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
122
public synchronized TmfContext seekEvent(final double ratio) {
124
return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
127
long pos = Math.round(ratio * fFile.length());
130
if (fFile.read() == '\n') {
135
final ITmfLocation<?> location = new TmfLocation<Long>(pos);
136
final TmfContext context = seekEvent(location);
137
context.setRank(ITmfContext.UNKNOWN_RANK);
139
} catch (final IOException e) {
140
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
141
return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
146
public synchronized double getLocationRatio(final ITmfLocation<?> location) {
151
if (location.getLocation() instanceof Long) {
152
return (double) ((Long) location.getLocation()) / fFile.length();
154
} catch (final IOException e) {
155
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
161
public ITmfLocation<?> getCurrentLocation() {
162
// TODO Auto-generated method stub
167
public synchronized CustomTxtEvent parseEvent(final ITmfContext tmfContext) {
168
ITmfContext context = seekEvent(tmfContext.getLocation());
169
return parse(context);
173
public synchronized CustomTxtEvent getNext(final ITmfContext context) {
174
final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
175
final CustomTxtEvent event = parse(context);
177
updateAttributes(savedContext, event.getTimestamp());
178
context.increaseRank();
183
private synchronized CustomTxtEvent parse(final ITmfContext tmfContext) {
187
if (!(tmfContext instanceof CustomTxtTraceContext)) {
191
final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
192
if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
196
CustomTxtEvent event = parseFirstLine(context);
198
final HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
199
InputLine currentInput = null;
200
if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
201
currentInput = context.inputLine.childrenInputs.get(0);
202
countMap.put(currentInput, 0);
206
if (fFile.getFilePointer() != context.nextLineLocation) {
207
fFile.seek(context.nextLineLocation);
210
long rawPos = fFile.getFilePointer();
211
while ((line = fFile.getNextLine()) != null) {
212
boolean processed = false;
213
if (currentInput == null) {
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 = fFile.getFilePointer();
221
context.inputLine = input;
226
if (countMap.get(currentInput) >= currentInput.getMinCount()) {
227
final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
228
if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
229
for (final InputLine input : getFirstLines()) {
230
final Matcher matcher = input.getPattern().matcher(line);
231
if (matcher.find()) {
232
context.setLocation(new TmfLocation<Long>(rawPos));
233
context.firstLineMatcher = matcher;
234
context.firstLine = line;
235
context.nextLineLocation = fFile.getFilePointer();
236
context.inputLine = input;
241
for (final InputLine input : nextInputs) {
242
final Matcher matcher = input.getPattern().matcher(line);
243
if (matcher.find()) {
244
event.processGroups(input, matcher);
245
currentInput = input;
246
if (countMap.get(currentInput) == null) {
247
countMap.put(currentInput, 1);
249
countMap.put(currentInput, countMap.get(currentInput) + 1);
251
Iterator<InputLine> iter = countMap.keySet().iterator();
252
while (iter.hasNext()) {
253
final InputLine inputLine = iter.next();
254
if (inputLine.level > currentInput.level) {
258
if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
259
currentInput = currentInput.childrenInputs.get(0);
260
countMap.put(currentInput, 0);
261
} else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
262
if (currentInput.getNextInputs(countMap).size() > 0) {
263
currentInput = currentInput.getNextInputs(countMap).get(0);
264
if (countMap.get(currentInput) == null) {
265
countMap.put(currentInput, 0);
267
iter = countMap.keySet().iterator();
268
while (iter.hasNext()) {
269
final InputLine inputLine = iter.next();
270
if (inputLine.level > currentInput.level) {
284
final Matcher matcher = currentInput.getPattern().matcher(line);
285
if (matcher.find()) {
286
event.processGroups(currentInput, matcher);
287
countMap.put(currentInput, countMap.get(currentInput) + 1);
288
if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
289
currentInput = currentInput.childrenInputs.get(0);
290
countMap.put(currentInput, 0);
291
} else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
292
if (currentInput.getNextInputs(countMap).size() > 0) {
293
currentInput = currentInput.getNextInputs(countMap).get(0);
294
if (countMap.get(currentInput) == null) {
295
countMap.put(currentInput, 0);
297
final Iterator<InputLine> iter = countMap.keySet().iterator();
298
while (iter.hasNext()) {
299
final InputLine inputLine = iter.next();
300
if (inputLine.level > currentInput.level) {
309
((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
312
rawPos = fFile.getFilePointer();
314
} catch (final IOException e) {
315
Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
317
for (final Entry<InputLine, Integer> entry : countMap.entrySet()) {
318
if (entry.getValue() < entry.getKey().getMinCount()) {
322
context.setLocation(NULL_LOCATION);
326
public List<InputLine> getFirstLines() {
327
return fDefinition.inputs;
330
public CustomTxtEvent parseFirstLine(final CustomTxtTraceContext context) {
331
final CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
332
event.processGroups(context.inputLine, context.firstLineMatcher);
333
event.setContent(new CustomEventContent(event, new StringBuffer(context.firstLine)));
337
public CustomTraceDefinition getDefinition() {
342
* @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
345
public boolean validate(IProject project, String path) {
346
return fileExists(path);