Update TmfTrace as per ITmfTrace
[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 try {
65 BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
66 if (location != null && location.getLocation() instanceof Long) {
67 raFile.seek((Long)location.getLocation());
68 }
69 String line;
70 long rawPos = raFile.getFilePointer();
71 while ((line = raFile.getNextLine()) != null) {
72 for (InputLine input : getFirstLines()) {
73 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 (FileNotFoundException e) {
88 e.printStackTrace();
89 return context;
90 } catch (IOException e) {
91 e.printStackTrace();
92 return context;
93 }
94
95 }
96
97 @Override
98 public TmfContext seekLocation(double ratio) {
99 try {
100 BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
101 long pos = (long) (ratio * raFile.length());
102 while (pos > 0) {
103 raFile.seek(pos - 1);
104 if (raFile.read() == '\n') break;
105 pos--;
106 }
107 ITmfLocation<?> location = new TmfLocation<Long>(pos);
108 TmfContext context = seekLocation(location);
109 context.setRank(ITmfContext.UNKNOWN_RANK);
110 return context;
111 } catch (FileNotFoundException e) {
112 e.printStackTrace();
113 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
114 } catch (IOException e) {
115 e.printStackTrace();
116 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.INITIAL_RANK);
117 }
118 }
119
120 @Override
121 public double getLocationRatio(ITmfLocation<?> location) {
122 try {
123 if (location.getLocation() instanceof Long) {
124 BufferedRandomAccessFile raFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
125 return (double) ((Long) location.getLocation()) / raFile.length();
126 }
127 } catch (FileNotFoundException e) {
128 e.printStackTrace();
129 } catch (IOException e) {
130 e.printStackTrace();
131 }
132 return 0;
133 }
134
135 @Override
136 public ITmfLocation<?> getCurrentLocation() {
137 // TODO Auto-generated method stub
138 return null;
139 }
140
141 @Override
142 public synchronized TmfEvent getNextEvent(ITmfContext context) {
143 ITmfContext savedContext = context.clone();
144 TmfEvent event = parseEvent(context);
145 if (event != null) {
146 updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());
147 context.updateRank(1);
148 }
149 return event;
150 }
151
152 @Override
153 public TmfEvent parseEvent(ITmfContext tmfContext) {
154 if (!(tmfContext instanceof CustomTxtTraceContext)) {
155 return null;
156 }
157
158 CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
159 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
160 return null;
161 }
162
163 CustomTxtEvent event = parseFirstLine(context);
164
165 HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
166 InputLine currentInput = null;
167 if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
168 currentInput = context.inputLine.childrenInputs.get(0);
169 countMap.put(currentInput, 0);
170 }
171
172 synchronized (context.raFile) {
173 try {
174 if (context.raFile.getFilePointer() != context.nextLineLocation) {
175 context.raFile.seek(context.nextLineLocation);
176 }
177 String line;
178 long rawPos = context.raFile.getFilePointer();
179 while ((line = context.raFile.getNextLine()) != null) {
180 boolean processed = false;
181 if (currentInput == null) {
182 for (InputLine input : getFirstLines()) {
183 Matcher matcher = input.getPattern().matcher(line);
184 if (matcher.find()) {
185 context.setLocation(new TmfLocation<Long>(rawPos));
186 context.firstLineMatcher = matcher;
187 context.firstLine = line;
188 context.nextLineLocation = context.raFile.getFilePointer();
189 context.inputLine = input;
190 return event;
191 }
192 }
193 } else {
194 if (countMap.get(currentInput) >= currentInput.getMinCount()) {
195 List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
196 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
197 for (InputLine input : getFirstLines()) {
198 Matcher matcher = input.getPattern().matcher(line);
199 if (matcher.find()) {
200 context.setLocation(new TmfLocation<Long>(rawPos));
201 context.firstLineMatcher = matcher;
202 context.firstLine = line;
203 context.nextLineLocation = context.raFile.getFilePointer();
204 context.inputLine = input;
205 return event;
206 }
207 }
208 }
209 for (InputLine input : nextInputs) {
210 Matcher matcher = input.getPattern().matcher(line);
211 if (matcher.find()) {
212 event.processGroups(input, matcher);
213 currentInput = input;
214 if (countMap.get(currentInput) == null) {
215 countMap.put(currentInput, 1);
216 } else {
217 countMap.put(currentInput, countMap.get(currentInput) + 1);
218 }
219 Iterator<InputLine> iter = countMap.keySet().iterator();
220 while (iter.hasNext()) {
221 InputLine inputLine = iter.next();
222 if (inputLine.level > currentInput.level) {
223 iter.remove();
224 }
225 }
226 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
227 currentInput = currentInput.childrenInputs.get(0);
228 countMap.put(currentInput, 0);
229 } else {
230 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
231 if (currentInput.getNextInputs(countMap).size() > 0) {
232 currentInput = currentInput.getNextInputs(countMap).get(0);
233 if (countMap.get(currentInput) == null) {
234 countMap.put(currentInput, 0);
235 }
236 iter = countMap.keySet().iterator();
237 while (iter.hasNext()) {
238 InputLine inputLine = iter.next();
239 if (inputLine.level > currentInput.level) {
240 iter.remove();
241 }
242 }
243 } else {
244 currentInput = null;
245 }
246 }
247 }
248 processed = true;
249 break;
250 }
251 }
252 }
253 if (! processed) {
254 Matcher matcher = currentInput.getPattern().matcher(line);
255 if (matcher.find()) {
256 event.processGroups(currentInput, matcher);
257 countMap.put(currentInput, countMap.get(currentInput) + 1);
258 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
259 currentInput = currentInput.childrenInputs.get(0);
260 countMap.put(currentInput, 0);
261 } else {
262 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
263 if (currentInput.getNextInputs(countMap).size() > 0) {
264 currentInput = currentInput.getNextInputs(countMap).get(0);
265 if (countMap.get(currentInput) == null) {
266 countMap.put(currentInput, 0);
267 }
268 Iterator<InputLine> iter = countMap.keySet().iterator();
269 while (iter.hasNext()) {
270 InputLine inputLine = iter.next();
271 if (inputLine.level > currentInput.level) {
272 iter.remove();
273 }
274 }
275 } else {
276 currentInput = null;
277 }
278 }
279 }
280 }
281 ((StringBuffer) event.getContent().getValue()).append("\n").append(line); //$NON-NLS-1$
282 }
283 }
284 rawPos = context.raFile.getFilePointer();
285 }
286 } catch (IOException e) {
287 e.printStackTrace();
288 }
289 }
290 for(Entry<InputLine, Integer> entry : countMap.entrySet()) {
291 if (entry.getValue() < entry.getKey().getMinCount()) {
292 event = null;
293 }
294 }
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(CustomTxtTraceContext context) {
304 CustomTxtEvent event = new CustomTxtEvent(fDefinition, this, (TmfTimestamp) 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.039593 seconds and 6 git commands to generate.