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