Update bookmarks file API, link to editor and delete & rename handlers
[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 public class CustomTxtTrace extends TmfTrace implements ITmfEventParser {
40
41 private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation((Long) null);
42 private static final int DEFAULT_CACHE_SIZE = 100;
43
44 private final CustomTxtTraceDefinition fDefinition;
45 private final CustomTxtEventType fEventType;
46 private BufferedRandomAccessFile fFile;
47
48 public CustomTxtTrace(final CustomTxtTraceDefinition definition) {
49 fDefinition = definition;
50 fEventType = new CustomTxtEventType(fDefinition);
51 setCacheSize(DEFAULT_CACHE_SIZE);
52 }
53
54 public CustomTxtTrace(final IResource resource, final CustomTxtTraceDefinition definition, final String path, final int cacheSize) throws TmfTraceException {
55 this(definition);
56 setCacheSize((cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE);
57 initTrace(resource, path, CustomTxtEvent.class);
58 }
59
60 @Override
61 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
62 super.initTrace(resource, path, eventType);
63 try {
64 fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
65 } catch (IOException e) {
66 throw new TmfTraceException(e.getMessage(), e);
67 }
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 ITmfTraceIndexer getIndexer() {
85 return super.getIndexer();
86 }
87
88 @Override
89 public synchronized TmfContext seekEvent(final ITmfLocation location) {
90 final CustomTxtTraceContext context = new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
91 if (NULL_LOCATION.equals(location) || fFile == null) {
92 return context;
93 }
94 try {
95 if (location == null) {
96 fFile.seek(0);
97 } else if (location.getLocationInfo() instanceof Long) {
98 fFile.seek((Long) location.getLocationInfo());
99 }
100 String line;
101 long rawPos = fFile.getFilePointer();
102 while ((line = fFile.getNextLine()) != null) {
103 for (final InputLine input : getFirstLines()) {
104 final Matcher matcher = input.getPattern().matcher(line);
105 if (matcher.find()) {
106 context.setLocation(new TmfLongLocation(rawPos));
107 context.firstLineMatcher = matcher;
108 context.firstLine = line;
109 context.nextLineLocation = fFile.getFilePointer();
110 context.inputLine = input;
111 return context;
112 }
113 }
114 rawPos = fFile.getFilePointer();
115 }
116 return context;
117 } catch (final FileNotFoundException e) {
118 Activator.getDefault().logError("Error seeking event. File not found: " + getPath(), e); //$NON-NLS-1$
119 return context;
120 } catch (final IOException e) {
121 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
122 return context;
123 }
124
125 }
126
127 @Override
128 public synchronized TmfContext seekEvent(final double ratio) {
129 if (fFile == null) {
130 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
131 }
132 try {
133 long pos = Math.round(ratio * fFile.length());
134 while (pos > 0) {
135 fFile.seek(pos - 1);
136 if (fFile.read() == '\n') {
137 break;
138 }
139 pos--;
140 }
141 final ITmfLocation location = new TmfLongLocation(pos);
142 final TmfContext context = seekEvent(location);
143 context.setRank(ITmfContext.UNKNOWN_RANK);
144 return context;
145 } catch (final IOException e) {
146 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
147 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
148 }
149 }
150
151 @Override
152 public synchronized double getLocationRatio(final ITmfLocation location) {
153 if (fFile == null) {
154 return 0;
155 }
156 try {
157 if (location.getLocationInfo() instanceof Long) {
158 return (double) ((Long) location.getLocationInfo()) / fFile.length();
159 }
160 } catch (final IOException e) {
161 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
162 }
163 return 0;
164 }
165
166 @Override
167 public ITmfLocation getCurrentLocation() {
168 // TODO Auto-generated method stub
169 return null;
170 }
171
172 @Override
173 public synchronized CustomTxtEvent parseEvent(final ITmfContext tmfContext) {
174 ITmfContext context = seekEvent(tmfContext.getLocation());
175 return parse(context);
176 }
177
178 @Override
179 public synchronized CustomTxtEvent getNext(final ITmfContext context) {
180 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
181 final CustomTxtEvent event = parse(context);
182 if (event != null) {
183 updateAttributes(savedContext, event.getTimestamp());
184 context.increaseRank();
185 }
186 return event;
187 }
188
189 private synchronized CustomTxtEvent parse(final ITmfContext tmfContext) {
190 if (fFile == null) {
191 return null;
192 }
193 if (!(tmfContext instanceof CustomTxtTraceContext)) {
194 return null;
195 }
196
197 final CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
198 if (context.getLocation() == null || !(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
199 return null;
200 }
201
202 CustomTxtEvent event = parseFirstLine(context);
203
204 final HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
205 InputLine currentInput = null;
206 if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
207 currentInput = context.inputLine.childrenInputs.get(0);
208 countMap.put(currentInput, 0);
209 }
210
211 try {
212 if (fFile.getFilePointer() != context.nextLineLocation) {
213 fFile.seek(context.nextLineLocation);
214 }
215 String line;
216 long rawPos = fFile.getFilePointer();
217 while ((line = fFile.getNextLine()) != null) {
218 boolean processed = false;
219 if (currentInput == null) {
220 for (final InputLine input : getFirstLines()) {
221 final Matcher matcher = input.getPattern().matcher(line);
222 if (matcher.find()) {
223 context.setLocation(new TmfLongLocation(rawPos));
224 context.firstLineMatcher = matcher;
225 context.firstLine = line;
226 context.nextLineLocation = fFile.getFilePointer();
227 context.inputLine = input;
228 return event;
229 }
230 }
231 } else {
232 if (countMap.get(currentInput) >= currentInput.getMinCount()) {
233 final List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
234 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
235 for (final InputLine input : getFirstLines()) {
236 final Matcher matcher = input.getPattern().matcher(line);
237 if (matcher.find()) {
238 context.setLocation(new TmfLongLocation(rawPos));
239 context.firstLineMatcher = matcher;
240 context.firstLine = line;
241 context.nextLineLocation = fFile.getFilePointer();
242 context.inputLine = input;
243 return event;
244 }
245 }
246 }
247 for (final InputLine input : nextInputs) {
248 final Matcher matcher = input.getPattern().matcher(line);
249 if (matcher.find()) {
250 event.processGroups(input, matcher);
251 currentInput = input;
252 if (countMap.get(currentInput) == null) {
253 countMap.put(currentInput, 1);
254 } else {
255 countMap.put(currentInput, countMap.get(currentInput) + 1);
256 }
257 Iterator<InputLine> iter = countMap.keySet().iterator();
258 while (iter.hasNext()) {
259 final InputLine inputLine = iter.next();
260 if (inputLine.level > currentInput.level) {
261 iter.remove();
262 }
263 }
264 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
265 currentInput = currentInput.childrenInputs.get(0);
266 countMap.put(currentInput, 0);
267 } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
268 if (currentInput.getNextInputs(countMap).size() > 0) {
269 currentInput = currentInput.getNextInputs(countMap).get(0);
270 if (countMap.get(currentInput) == null) {
271 countMap.put(currentInput, 0);
272 }
273 iter = countMap.keySet().iterator();
274 while (iter.hasNext()) {
275 final InputLine inputLine = iter.next();
276 if (inputLine.level > currentInput.level) {
277 iter.remove();
278 }
279 }
280 } else {
281 currentInput = null;
282 }
283 }
284 processed = true;
285 break;
286 }
287 }
288 }
289 if (! processed) {
290 final Matcher matcher = currentInput.getPattern().matcher(line);
291 if (matcher.find()) {
292 event.processGroups(currentInput, matcher);
293 countMap.put(currentInput, countMap.get(currentInput) + 1);
294 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
295 currentInput = currentInput.childrenInputs.get(0);
296 countMap.put(currentInput, 0);
297 } else if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
298 if (currentInput.getNextInputs(countMap).size() > 0) {
299 currentInput = currentInput.getNextInputs(countMap).get(0);
300 if (countMap.get(currentInput) == null) {
301 countMap.put(currentInput, 0);
302 }
303 final Iterator<InputLine> iter = countMap.keySet().iterator();
304 while (iter.hasNext()) {
305 final InputLine inputLine = iter.next();
306 if (inputLine.level > currentInput.level) {
307 iter.remove();
308 }
309 }
310 } else {
311 currentInput = null;
312 }
313 }
314 }
315 ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
316 }
317 }
318 rawPos = fFile.getFilePointer();
319 }
320 } catch (final IOException e) {
321 Activator.getDefault().logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
322 }
323 for (final Entry<InputLine, Integer> entry : countMap.entrySet()) {
324 if (entry.getValue() < entry.getKey().getMinCount()) {
325 event = null;
326 }
327 }
328 context.setLocation(NULL_LOCATION);
329 return event;
330 }
331
332 public List<InputLine> getFirstLines() {
333 return fDefinition.inputs;
334 }
335
336 public CustomTxtEvent parseFirstLine(final CustomTxtTraceContext context) {
337 final CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, TmfTimestamp.ZERO, "", fEventType, ""); //$NON-NLS-1$ //$NON-NLS-2$
338 event.processGroups(context.inputLine, context.firstLineMatcher);
339 event.setContent(new CustomEventContent(event, new StringBuffer(context.firstLine)));
340 return event;
341 }
342
343 public CustomTraceDefinition getDefinition() {
344 return fDefinition;
345 }
346
347 /* (non-Javadoc)
348 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
349 */
350 @Override
351 public boolean validate(IProject project, String path) {
352 return fileExists(path);
353 }
354 }
This page took 0.0377 seconds and 5 git commands to generate.