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