Merge branch 'master' into lttng-kepler
[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.TmfContext;
35 import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
36 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
37
38 public class CustomTxtTrace extends TmfTrace implements ITmfEventParser {
39
40 private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation((Long) null);
41 private static final int DEFAULT_CACHE_SIZE = 100;
42
43 private final CustomTxtTraceDefinition fDefinition;
44 private final CustomTxtEventType fEventType;
45 private BufferedRandomAccessFile fFile;
46
47 public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
48 fDefinition = definition;
49 fEventType = new CustomTxtEventType(fDefinition);
50 setCacheSize(DEFAULT_CACHE_SIZE);
51 }
52
53 public CustomTxtTrace(final IResource resource, final CustomTxtTraceDefinition definition, final String path, final int cacheSize) throws TmfTraceException {
54 this(definition);
55 setCacheSize((cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE);
56 initTrace(resource, path, CustomTxtEvent.class);
57 }
58
59 @Override
60 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
61 super.initTrace(resource, path, eventType);
62 try {
63 fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
64 } catch (IOException e) {
65 throw new TmfTraceException(e.getMessage(), e);
66 }
67 indexTrace(false);
68 }
69
70 @Override
71 public synchronized void dispose() {
72 super.dispose();
73 if (fFile != null) {
74 try {
75 fFile.close();
76 } catch (IOException e) {
77 } finally {
78 fFile = null;
79 }
80 }
81 }
82
83 @Override
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) {
87 return context;
88 }
89 try {
90 if (location == null) {
91 fFile.seek(0);
92 } else if (location.getLocationInfo() instanceof Long) {
93 fFile.seek((Long) location.getLocationInfo());
94 }
95 String line;
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 TmfLongLocation(rawPos));
102 context.firstLineMatcher = matcher;
103 context.firstLine = line;
104 context.nextLineLocation = fFile.getFilePointer();
105 context.inputLine = input;
106 return context;
107 }
108 }
109 rawPos = fFile.getFilePointer();
110 }
111 return context;
112 } catch (final FileNotFoundException e) {
113 Activator.getDefault().logError("Error seeking event. File not found: " + getPath(), e); //$NON-NLS-1$
114 return context;
115 } catch (final IOException e) {
116 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
117 return context;
118 }
119
120 }
121
122 @Override
123 public synchronized TmfContext seekEvent(final double ratio) {
124 if (fFile == null) {
125 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
126 }
127 try {
128 long pos = (long) (ratio * fFile.length());
129 while (pos > 0) {
130 fFile.seek(pos - 1);
131 if (fFile.read() == '\n') {
132 break;
133 }
134 pos--;
135 }
136 final ITmfLocation location = new TmfLongLocation(pos);
137 final TmfContext context = seekEvent(location);
138 context.setRank(ITmfContext.UNKNOWN_RANK);
139 return context;
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);
143 }
144 }
145
146 @Override
147 public synchronized double getLocationRatio(final ITmfLocation location) {
148 if (fFile == null) {
149 return 0;
150 }
151 try {
152 if (location.getLocationInfo() instanceof Long) {
153 return (double) ((Long) location.getLocationInfo()) / fFile.length();
154 }
155 } catch (final IOException e) {
156 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
157 }
158 return 0;
159 }
160
161 @Override
162 public ITmfLocation getCurrentLocation() {
163 // TODO Auto-generated method stub
164 return null;
165 }
166
167 @Override
168 public synchronized CustomTxtEvent parseEvent(final ITmfContext tmfContext) {
169 ITmfContext context = seekEvent(tmfContext.getLocation());
170 return parse(context);
171 }
172
173 @Override
174 public synchronized CustomTxtEvent getNext(final ITmfContext context) {
175 final ITmfContext savedContext = context.clone();
176 final CustomTxtEvent event = parse(context);
177 if (event != null) {
178 updateAttributes(savedContext, event.getTimestamp());
179 context.increaseRank();
180 }
181 return event;
182 }
183
184 private synchronized CustomTxtEvent parse(final ITmfContext tmfContext) {
185 if (fFile == null) {
186 return null;
187 }
188 if (!(tmfContext instanceof CustomTxtTraceContext)) {
189 return null;
190 }
191
192 final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
193 if (!(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
194 return null;
195 }
196
197 CustomTxtEvent event = parseFirstLine(context);
198
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);
204 }
205
206 try {
207 if (fFile.getFilePointer() != context.nextLineLocation) {
208 fFile.seek(context.nextLineLocation);
209 }
210 String line;
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 TmfLongLocation(rawPos));
219 context.firstLineMatcher = matcher;
220 context.firstLine = line;
221 context.nextLineLocation = fFile.getFilePointer();
222 context.inputLine = input;
223 return event;
224 }
225 }
226 } else {
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 TmfLongLocation(rawPos));
234 context.firstLineMatcher = matcher;
235 context.firstLine = line;
236 context.nextLineLocation = fFile.getFilePointer();
237 context.inputLine = input;
238 return event;
239 }
240 }
241 }
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);
249 } else {
250 countMap.put(currentInput, countMap.get(currentInput) + 1);
251 }
252 Iterator<InputLine> iter = countMap.keySet().iterator();
253 while (iter.hasNext()) {
254 final InputLine inputLine = iter.next();
255 if (inputLine.level > currentInput.level) {
256 iter.remove();
257 }
258 }
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);
267 }
268 iter = countMap.keySet().iterator();
269 while (iter.hasNext()) {
270 final InputLine inputLine = iter.next();
271 if (inputLine.level > currentInput.level) {
272 iter.remove();
273 }
274 }
275 } else {
276 currentInput = null;
277 }
278 }
279 processed = true;
280 break;
281 }
282 }
283 }
284 if (! processed) {
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);
297 }
298 final Iterator<InputLine> iter = countMap.keySet().iterator();
299 while (iter.hasNext()) {
300 final InputLine inputLine = iter.next();
301 if (inputLine.level > currentInput.level) {
302 iter.remove();
303 }
304 }
305 } else {
306 currentInput = null;
307 }
308 }
309 }
310 ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
311 }
312 }
313 rawPos = fFile.getFilePointer();
314 }
315 } catch (final IOException e) {
316 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
317 }
318 for (final Entry<InputLine, Integer> entry : countMap.entrySet()) {
319 if (entry.getValue() < entry.getKey().getMinCount()) {
320 event = null;
321 }
322 }
323 context.setLocation(NULL_LOCATION);
324 return event;
325 }
326
327 public List<InputLine> getFirstLines() {
328 return fDefinition.inputs;
329 }
330
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)));
335 return event;
336 }
337
338 public CustomTraceDefinition getDefinition() {
339 return fDefinition;
340 }
341
342 /* (non-Javadoc)
343 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
344 */
345 @Override
346 public boolean validate(IProject project, String path) {
347 return fileExists(path);
348 }
349 }
This page took 0.0407 seconds and 5 git commands to generate.