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