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