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