Contribution for Bug353020
[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.event.TmfEvent;
25 import org.eclipse.linuxtools.tmf.event.TmfEventReference;
26 import org.eclipse.linuxtools.tmf.event.TmfEventSource;
27 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
28 import org.eclipse.linuxtools.tmf.io.BufferedRandomAccessFile;
29 import org.eclipse.linuxtools.tmf.trace.ITmfContext;
30 import org.eclipse.linuxtools.tmf.trace.ITmfLocation;
31 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
32 import org.eclipse.linuxtools.tmf.trace.TmfContext;
33 import org.eclipse.linuxtools.tmf.trace.TmfLocation;
34 import org.eclipse.linuxtools.tmf.trace.TmfTrace;
35 import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputLine;
36
37 public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> {
38
39 private static final TmfLocation<Long> NULL_LOCATION = new TmfLocation<Long>((Long) null);
40
41 private CustomTxtTraceDefinition fDefinition;
42 private CustomTxtEventType fEventType;
43
44 public CustomTxtTrace(String name, CustomTxtTraceDefinition definition, String path, int cacheSize) throws FileNotFoundException {
45 super(name, CustomTxtEvent.class, path, cacheSize);
46 fDefinition = definition;
47 fEventType = new CustomTxtEventType(fDefinition);
48 }
49
50 @Override
51 public ITmfTrace createTraceCopy() {
52 // TODO Auto-generated method stub
53 return null;
54 }
55
56 @Override
57 public TmfContext seekLocation(ITmfLocation<?> location) {
58 //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");
59 //new Throwable().printStackTrace();
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>(new 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(TmfContext 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(TmfContext tmfContext) {
154 //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));
155 if (!(tmfContext instanceof CustomTxtTraceContext)) {
156 return null;
157 }
158
159 CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;
160 if (!(context.getLocation().getLocation() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
161 return null;
162 }
163
164 CustomTxtEvent event = parseFirstLine(context);
165
166 HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();
167 InputLine currentInput = null;
168 if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {
169 currentInput = context.inputLine.childrenInputs.get(0);
170 countMap.put(currentInput, 0);
171 }
172
173 synchronized (context.raFile) {
174 try {
175 if (context.raFile.getFilePointer() != context.nextLineLocation) {
176 context.raFile.seek(context.nextLineLocation);
177 }
178 String line;
179 long rawPos = context.raFile.getFilePointer();
180 while ((line = context.raFile.getNextLine()) != null) {
181 boolean processed = false;
182 if (currentInput == null) {
183 for (InputLine input : getFirstLines()) {
184 Matcher matcher = input.getPattern().matcher(line);
185 if (matcher.find()) {
186 context.setLocation(new TmfLocation<Long>(rawPos));
187 context.firstLineMatcher = matcher;
188 context.firstLine = line;
189 context.nextLineLocation = context.raFile.getFilePointer();
190 context.inputLine = input;
191 return event;
192 }
193 }
194 } else {
195 if (countMap.get(currentInput) >= currentInput.getMinCount()) {
196 List<InputLine> nextInputs = currentInput.getNextInputs(countMap);
197 if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {
198 for (InputLine input : getFirstLines()) {
199 Matcher matcher = input.getPattern().matcher(line);
200 if (matcher.find()) {
201 context.setLocation(new TmfLocation<Long>(rawPos));
202 context.firstLineMatcher = matcher;
203 context.firstLine = line;
204 context.nextLineLocation = context.raFile.getFilePointer();
205 context.inputLine = input;
206 return event;
207 }
208 }
209 }
210 for (InputLine input : nextInputs) {
211 Matcher matcher = input.getPattern().matcher(line);
212 if (matcher.find()) {
213 event.processGroups(input, matcher);
214 currentInput = input;
215 if (countMap.get(currentInput) == null) {
216 countMap.put(currentInput, 1);
217 } else {
218 countMap.put(currentInput, countMap.get(currentInput) + 1);
219 }
220 Iterator<InputLine> iter = countMap.keySet().iterator();
221 while (iter.hasNext()) {
222 InputLine inputLine = iter.next();
223 if (inputLine.level > currentInput.level) {
224 iter.remove();
225 }
226 }
227 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
228 currentInput = currentInput.childrenInputs.get(0);
229 countMap.put(currentInput, 0);
230 } else {
231 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
232 if (currentInput.getNextInputs(countMap).size() > 0) {
233 currentInput = currentInput.getNextInputs(countMap).get(0);
234 if (countMap.get(currentInput) == null) {
235 countMap.put(currentInput, 0);
236 }
237 iter = countMap.keySet().iterator();
238 while (iter.hasNext()) {
239 InputLine inputLine = iter.next();
240 if (inputLine.level > currentInput.level) {
241 iter.remove();
242 }
243 }
244 } else {
245 currentInput = null;
246 }
247 }
248 }
249 processed = true;
250 break;
251 }
252 }
253 }
254 if (! processed) {
255 Matcher matcher = currentInput.getPattern().matcher(line);
256 if (matcher.find()) {
257 event.processGroups(currentInput, matcher);
258 countMap.put(currentInput, countMap.get(currentInput) + 1);
259 if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {
260 currentInput = currentInput.childrenInputs.get(0);
261 countMap.put(currentInput, 0);
262 } else {
263 if (countMap.get(currentInput) >= currentInput.getMaxCount()) {
264 if (currentInput.getNextInputs(countMap).size() > 0) {
265 currentInput = currentInput.getNextInputs(countMap).get(0);
266 if (countMap.get(currentInput) == null) {
267 countMap.put(currentInput, 0);
268 }
269 Iterator<InputLine> iter = countMap.keySet().iterator();
270 while (iter.hasNext()) {
271 InputLine inputLine = iter.next();
272 if (inputLine.level > currentInput.level) {
273 iter.remove();
274 }
275 }
276 } else {
277 currentInput = null;
278 }
279 }
280 }
281 }
282 ((StringBuffer) event.getContent().getContent()).append("\n").append(line); //$NON-NLS-1$
283 }
284 }
285 rawPos = context.raFile.getFilePointer();
286 }
287 } catch (IOException e) {
288 e.printStackTrace();
289 }
290 }
291 for(Entry<InputLine, Integer> entry : countMap.entrySet()) {
292 if (entry.getValue() < entry.getKey().getMinCount()) {
293 event = null;
294 }
295 }
296 context.setLocation(NULL_LOCATION);
297 return event;
298 }
299
300 public List<InputLine> getFirstLines() {
301 return fDefinition.inputs;
302 }
303
304 public CustomTxtEvent parseFirstLine(CustomTxtTraceContext context) {
305 CustomTxtEvent event = new CustomTxtEvent(fDefinition, TmfTimestamp.Zero, new TmfEventSource(""), fEventType, new TmfEventReference("")); //$NON-NLS-1$ //$NON-NLS-2$
306 event.processGroups(context.inputLine, context.firstLineMatcher);
307 event.setContent(new CustomEventContent(event, new StringBuffer(context.firstLine)));
308 return event;
309 }
310
311 public CustomTraceDefinition getDefinition() {
312 return fDefinition;
313 }
314 }
This page took 0.038812 seconds and 6 git commands to generate.