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