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