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