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