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