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