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