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