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