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