tmf: Switch tmf.ui to Java 7 + fix warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / parsers / custom / CustomTxtTrace.java
CommitLineData
6151d86c 1/*******************************************************************************
c8422608 2 * Copyright (c) 2010, 2013 Ericsson
6151d86c
PT
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.internal.tmf.ui.parsers.custom;
14
eb8ea213 15import java.io.File;
6151d86c
PT
16import java.io.FileNotFoundException;
17import java.io.IOException;
032ecd45 18import java.nio.ByteBuffer;
6151d86c
PT
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Map.Entry;
23import java.util.regex.Matcher;
24
25import org.eclipse.core.resources.IProject;
26import org.eclipse.core.resources.IResource;
a94410d9
MK
27import org.eclipse.core.runtime.IStatus;
28import org.eclipse.core.runtime.Status;
6151d86c
PT
29import org.eclipse.linuxtools.internal.tmf.ui.Activator;
30import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputLine;
31import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6151d86c
PT
32import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
33import org.eclipse.linuxtools.tmf.core.io.BufferedRandomAccessFile;
3bd46eef 34import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
6151d86c
PT
35import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
36import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
6151d86c 37import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
6151d86c 38import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
032ecd45 39import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
a3db8436 40import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
032ecd45
MAL
41import org.eclipse.linuxtools.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
42import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
43import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
a3db8436
AM
44import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
45import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
6151d86c 46
a0a88f65
AM
47/**
48 * Base class for custom plain text traces.
49 *
50 * @author Patrick Tassé
51 */
032ecd45 52public class CustomTxtTrace extends TmfTrace implements ITmfEventParser, ITmfPersistentlyIndexable {
6151d86c
PT
53
54 private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation((Long) null);
55 private static final int DEFAULT_CACHE_SIZE = 100;
56
57 private final CustomTxtTraceDefinition fDefinition;
58 private final CustomTxtEventType fEventType;
59 private BufferedRandomAccessFile fFile;
60
a0a88f65
AM
61 /**
62 * Basic constructor.
63 *
64 * @param definition
65 * Text trace definition
66 */
6151d86c
PT
67 public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
68 fDefinition = definition;
69 fEventType = new CustomTxtEventType(fDefinition);
70 setCacheSize(DEFAULT_CACHE_SIZE);
71 }
72
a0a88f65
AM
73 /**
74 * Full constructor.
75 *
76 * @param resource
77 * Trace's resource.
78 * @param definition
79 * Text trace definition
80 * @param path
81 * Path to the trace file
82 * @param cacheSize
83 * Cache size to use
84 * @throws TmfTraceException
85 * If we couldn't open the trace at 'path'
86 */
87 public CustomTxtTrace(final IResource resource,
88 final CustomTxtTraceDefinition definition, final String path,
89 final int cacheSize) throws TmfTraceException {
6151d86c
PT
90 this(definition);
91 setCacheSize((cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE);
92 initTrace(resource, path, CustomTxtEvent.class);
93 }
94
95 @Override
96 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
97 super.initTrace(resource, path, eventType);
98 try {
99 fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
100 } catch (IOException e) {
101 throw new TmfTraceException(e.getMessage(), e);
102 }
6151d86c
PT
103 }
104
105 @Override
106 public synchronized void dispose() {
107 super.dispose();
108 if (fFile != null) {
109 try {
110 fFile.close();
111 } catch (IOException e) {
112 } finally {
113 fFile = null;
114 }
115 }
116 }
117
b0422293
PT
118 @Override
119 public ITmfTraceIndexer getIndexer() {
120 return super.getIndexer();
121 }
122
6151d86c
PT
123 @Override
124 public synchronized TmfContext seekEvent(final ITmfLocation location) {
125 final CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
126 if (NULL_LOCATION.equals(location) || fFile == null) {
127 return context;
128 }
129 try {
130 if (location == null) {
131 fFile.seek(0);
132 } else if (location.getLocationInfo() instanceof Long) {
133 fFile.seek((Long) location.getLocationInfo());
134 }
135 String line;
136 long rawPos = fFile.getFilePointer();
137 while ((line = fFile.getNextLine()) != null) {
138 for (final InputLine input : getFirstLines()) {
139 final Matcher matcher = input.getPattern().matcher(line);
140 if (matcher.find()) {
141 context.setLocation(new TmfLongLocation(rawPos));
142 context.firstLineMatcher = matcher;
143 context.firstLine = line;
144 context.nextLineLocation = fFile.getFilePointer();
145 context.inputLine = input;
146 return context;
147 }
148 }
149 rawPos = fFile.getFilePointer();
150 }
151 return context;
152 } catch (final FileNotFoundException e) {
153 Activator.getDefault().logError("Error seeking event. File not found: " + getPath(), e); //$NON-NLS-1$
154 return context;
155 } catch (final IOException e) {
156 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
157 return context;
158 }
159
160 }
161
162 @Override
163 public synchronized TmfContext seekEvent(final double ratio) {
164 if (fFile == null) {
165 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
166 }
167 try {
91f6e587 168 long pos = Math.round(ratio * fFile.length());
6151d86c
PT
169 while (pos > 0) {
170 fFile.seek(pos - 1);
171 if (fFile.read() == '\n') {
172 break;
173 }
174 pos--;
175 }
176 final ITmfLocation location = new TmfLongLocation(pos);
177 final TmfContext context = seekEvent(location);
178 context.setRank(ITmfContext.UNKNOWN_RANK);
179 return context;
180 } catch (final IOException e) {
181 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
182 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
183 }
184 }
185
186 @Override
187 public synchronized double getLocationRatio(final ITmfLocation location) {
188 if (fFile == null) {
189 return 0;
190 }
191 try {
192 if (location.getLocationInfo() instanceof Long) {
193 return (double) ((Long) location.getLocationInfo()) / fFile.length();
194 }
195 } catch (final IOException e) {
196 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
197 }
198 return 0;
199 }
200
201 @Override
202 public ITmfLocation getCurrentLocation() {
203 // TODO Auto-generated method stub
204 return null;
205 }
206
207 @Override
208 public synchronized CustomTxtEvent parseEvent(final ITmfContext tmfContext) {
209 ITmfContext context = seekEvent(tmfContext.getLocation());
210 return parse(context);
211 }
212
213 @Override
214 public synchronized CustomTxtEvent getNext(final ITmfContext context) {
4c9f2944 215 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
6151d86c
PT
216 final CustomTxtEvent event = parse(context);
217 if (event != null) {
218 updateAttributes(savedContext, event.getTimestamp());
219 context.increaseRank();
220 }
221 return event;
222 }
223
224 private synchronized CustomTxtEvent parse(final ITmfContext tmfContext) {
225 if (fFile == null) {
226 return null;
227 }
228 if (!(tmfContext instanceof CustomTxtTraceContext)) {
229 return null;
230 }
231
232 final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
9cbe7899 233 if (context.getLocation() == null || !(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
6151d86c
PT
234 return null;
235 }
236
237 CustomTxtEvent event = parseFirstLine(context);
238
507b1336 239 final HashMap<InputLine, Integer> countMap = new HashMap<>();
6151d86c
PT
240 InputLine currentInput = null;
241 if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
242 currentInput = context.inputLine.childrenInputs.get(0);
243 countMap.put(currentInput, 0);
244 }
245
246 try {
247 if (fFile.getFilePointer() != context.nextLineLocation) {
248 fFile.seek(context.nextLineLocation);
249 }
250 String line;
251 long rawPos = fFile.getFilePointer();
252 while ((line = fFile.getNextLine()) != null) {
253 boolean processed = false;
254 if (currentInput == null) {
255 for (final InputLine input : getFirstLines()) {
256 final Matcher matcher = input.getPattern().matcher(line);
257 if (matcher.find()) {
258 context.setLocation(new TmfLongLocation(rawPos));
259 context.firstLineMatcher = matcher;
260 context.firstLine = line;
261 context.nextLineLocation = fFile.getFilePointer();
262 context.inputLine = input;
263 return event;
264 }
265 }
266 } else {
267 if (countMap.get(currentInput) >= currentInput.getMinCount()) {
268 final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
269 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
270 for (final InputLine input : getFirstLines()) {
271 final Matcher matcher = input.getPattern().matcher(line);
272 if (matcher.find()) {
273 context.setLocation(new TmfLongLocation(rawPos));
274 context.firstLineMatcher = matcher;
275 context.firstLine = line;
276 context.nextLineLocation = fFile.getFilePointer();
277 context.inputLine = input;
278 return event;
279 }
280 }
281 }
282 for (final InputLine input : nextInputs) {
283 final Matcher matcher = input.getPattern().matcher(line);
284 if (matcher.find()) {
285 event.processGroups(input, matcher);
286 currentInput = input;
287 if (countMap.get(currentInput) == null) {
288 countMap.put(currentInput, 1);
289 } else {
290 countMap.put(currentInput, countMap.get(currentInput) + 1);
291 }
292 Iterator<InputLine> iter = countMap.keySet().iterator();
293 while (iter.hasNext()) {
294 final InputLine inputLine = iter.next();
295 if (inputLine.level > currentInput.level) {
296 iter.remove();
297 }
298 }
299 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
300 currentInput = currentInput.childrenInputs.get(0);
301 countMap.put(currentInput, 0);
302 } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
303 if (currentInput.getNextInputs(countMap).size() > 0) {
304 currentInput = currentInput.getNextInputs(countMap).get(0);
305 if (countMap.get(currentInput) == null) {
306 countMap.put(currentInput, 0);
307 }
308 iter = countMap.keySet().iterator();
309 while (iter.hasNext()) {
310 final InputLine inputLine = iter.next();
311 if (inputLine.level > currentInput.level) {
312 iter.remove();
313 }
314 }
315 } else {
316 currentInput = null;
317 }
318 }
319 processed = true;
320 break;
321 }
322 }
323 }
3dca7aa5 324 if (!processed && currentInput != null) {
6151d86c
PT
325 final Matcher matcher = currentInput.getPattern().matcher(line);
326 if (matcher.find()) {
327 event.processGroups(currentInput, matcher);
328 countMap.put(currentInput, countMap.get(currentInput) + 1);
329 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
330 currentInput = currentInput.childrenInputs.get(0);
331 countMap.put(currentInput, 0);
332 } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
333 if (currentInput.getNextInputs(countMap).size() > 0) {
334 currentInput = currentInput.getNextInputs(countMap).get(0);
335 if (countMap.get(currentInput) == null) {
336 countMap.put(currentInput, 0);
337 }
338 final Iterator<InputLine> iter = countMap.keySet().iterator();
339 while (iter.hasNext()) {
340 final InputLine inputLine = iter.next();
341 if (inputLine.level > currentInput.level) {
342 iter.remove();
343 }
344 }
345 } else {
346 currentInput = null;
347 }
348 }
349 }
350 ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
351 }
352 }
353 rawPos = fFile.getFilePointer();
354 }
355 } catch (final IOException e) {
356 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
357 }
358 for (final Entry<InputLine, Integer> entry : countMap.entrySet()) {
359 if (entry.getValue() < entry.getKey().getMinCount()) {
360 event = null;
361 }
362 }
363 context.setLocation(NULL_LOCATION);
364 return event;
365 }
366
a0a88f65
AM
367 /**
368 * @return The first few lines of the text file
369 */
6151d86c
PT
370 public List<InputLine> getFirstLines() {
371 return fDefinition.inputs;
372 }
373
a0a88f65
AM
374 /**
375 * Parse the first line of the trace (to recognize the type).
376 *
377 * @param context
378 * Trace context
379 * @return The first event
380 */
6151d86c
PT
381 public CustomTxtEvent parseFirstLine(final CustomTxtTraceContext context) {
382 final CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
383 event.processGroups(context.inputLine, context.firstLineMatcher);
384 event.setContent(new CustomEventContent(event, new StringBuffer(context.firstLine)));
385 return event;
386 }
387
a0a88f65
AM
388 /**
389 * Get the trace definition.
390 *
391 * @return The trace definition
392 */
6151d86c
PT
393 public CustomTraceDefinition getDefinition() {
394 return fDefinition;
395 }
396
6151d86c 397 @Override
a94410d9 398 public IStatus validate(IProject project, String path) {
eb8ea213
MK
399 File file = new File(path);
400 if (file.exists() && file.isFile() && file.canRead() && file.length() > 0) {
a94410d9
MK
401 return Status.OK_STATUS;
402 }
eb8ea213 403 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
6151d86c 404 }
032ecd45
MAL
405
406 private static int fCheckpointSize = -1;
407
408 @Override
409 public synchronized int getCheckpointSize() {
410 if (fCheckpointSize == -1) {
411 TmfCheckpoint c = new TmfCheckpoint(TmfTimestamp.ZERO, new TmfLongLocation(0L), 0);
412 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
413 b.clear();
414 c.serialize(b);
415 fCheckpointSize = b.position();
416 }
417
418 return fCheckpointSize;
419 }
420
421 @Override
422 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
423 return new TmfLongLocation(bufferIn);
424 }
425
426 @Override
427 protected ITmfTraceIndexer createIndexer(int interval) {
428 return new TmfBTreeTraceIndexer(this, interval);
429 }
6151d86c 430}
This page took 0.075888 seconds and 5 git commands to generate.