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 | |
18 | import java.io.RandomAccessFile;\r | |
19 | import java.util.HashMap;\r | |
20 | import java.util.Iterator;\r | |
21 | import java.util.List;\r | |
22 | import java.util.Map.Entry;\r | |
23 | import java.util.regex.Matcher;\r | |
24 | import java.util.regex.Pattern;\r | |
25 | \r | |
26 | import org.eclipse.linuxtools.tmf.event.TmfEvent;\r | |
27 | import org.eclipse.linuxtools.tmf.event.TmfEventReference;\r | |
28 | import org.eclipse.linuxtools.tmf.event.TmfEventSource;\r | |
29 | import org.eclipse.linuxtools.tmf.event.TmfEventType;\r | |
30 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp;\r | |
31 | import org.eclipse.linuxtools.tmf.trace.ITmfContext;\r | |
32 | import org.eclipse.linuxtools.tmf.trace.ITmfLocation;\r | |
33 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace;\r | |
34 | import org.eclipse.linuxtools.tmf.trace.TmfContext;\r | |
35 | import org.eclipse.linuxtools.tmf.trace.TmfLocation;\r | |
36 | import org.eclipse.linuxtools.tmf.trace.TmfTrace;\r | |
37 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition.InputLine;\r | |
38 | \r | |
39 | public class CustomTxtTrace extends TmfTrace<CustomTxtEvent> {\r | |
40 | \r | |
41 | private CustomTxtTraceDefinition fDefinition;\r | |
42 | \r | |
43 | public CustomTxtTrace(String name, CustomTxtTraceDefinition definition, String path, int cacheSize) throws FileNotFoundException {\r | |
44 | super(name, CustomTxtEvent.class, path, cacheSize);\r | |
45 | fDefinition = definition;\r | |
46 | }\r | |
47 | \r | |
d4011df2 | 48 | @Override\r |
c3c5c786 FC |
49 | public ITmfTrace createTraceCopy() {\r |
50 | // TODO Auto-generated method stub\r | |
51 | return null;\r | |
52 | }\r | |
53 | \r | |
54 | @Override\r | |
55 | public TmfContext seekLocation(ITmfLocation<?> location) {\r | |
56 | //System.out.println(Thread.currentThread().getName() + "::" + getName() + " seekLocation(" + ((location == null || location.getLocation() == null) ? "null" : location) + ")");\r | |
57 | //new Throwable().printStackTrace();\r | |
58 | CustomTxtTraceContext context = new CustomTxtTraceContext(new TmfLocation<Long>((Long)null), ITmfContext.INITIAL_RANK);\r | |
59 | if (!new File(getPath()).isFile()) {\r | |
60 | return context;\r | |
61 | }\r | |
62 | try {\r | |
63 | RandomAccessFile raFile = new RandomAccessFile(getPath(), "r");\r | |
64 | if (location != null && location.getLocation() instanceof Long) {\r | |
65 | raFile.seek((Long)location.getLocation());\r | |
66 | }\r | |
67 | String line;\r | |
68 | long rawPos = raFile.getFilePointer();\r | |
69 | while ((line = raFile.readLine()) != null) {\r | |
70 | for (InputLine input : getFirstLines()) {\r | |
71 | Matcher matcher = input.getPattern().matcher(line);\r | |
72 | if (matcher.find()) {\r | |
73 | context.setLocation(new TmfLocation<Long>(rawPos));\r | |
74 | context.raFile = raFile;\r | |
75 | context.firstLineMatcher = matcher;\r | |
76 | context.nextLineLocation = raFile.getFilePointer();\r | |
77 | context.inputLine = input;\r | |
78 | return context;\r | |
79 | }\r | |
80 | }\r | |
81 | rawPos = raFile.getFilePointer();\r | |
82 | }\r | |
83 | return context;\r | |
84 | } catch (FileNotFoundException e) {\r | |
85 | e.printStackTrace();\r | |
86 | return context;\r | |
87 | } catch (IOException e) {\r | |
88 | e.printStackTrace();\r | |
89 | return context;\r | |
90 | }\r | |
91 | \r | |
92 | }\r | |
93 | \r | |
94 | @Override\r | |
95 | public ITmfLocation<?> getCurrentLocation() {\r | |
96 | // TODO Auto-generated method stub\r | |
97 | return null;\r | |
98 | }\r | |
99 | \r | |
100 | @Override\r | |
101 | public synchronized TmfEvent getNextEvent(TmfContext context) {\r | |
102 | ITmfContext savedContext = context.clone();\r | |
103 | TmfEvent event = parseEvent(context);\r | |
104 | if (event != null) {\r | |
105 | updateIndex(savedContext, savedContext.getRank(), event.getTimestamp());\r | |
106 | context.updateRank(1);\r | |
107 | }\r | |
108 | return event;\r | |
109 | }\r | |
110 | \r | |
111 | @Override\r | |
112 | public TmfEvent parseEvent(TmfContext tmfContext) {\r | |
113 | //System.out.println(Thread.currentThread().getName() + ":: " + getName() + " parseEvent(" + tmfContext.getRank() + " @ " + (tmfContext.getLocation().getLocation() == null ? "null" : tmfContext.getLocation()));\r | |
114 | if (!(tmfContext instanceof CustomTxtTraceContext)) {\r | |
115 | return null;\r | |
116 | }\r | |
117 | \r | |
118 | CustomTxtTraceContext context = (CustomTxtTraceContext) tmfContext;\r | |
119 | if (!(context.getLocation().getLocation() instanceof Long)) {\r | |
120 | return null;\r | |
121 | }\r | |
122 | \r | |
123 | CustomTxtEvent event = parseFirstLine(context);\r | |
124 | \r | |
125 | HashMap<InputLine, Integer> countMap = new HashMap<InputLine, Integer>();\r | |
126 | InputLine currentInput = null;\r | |
127 | if (context.inputLine.childrenInputs != null && context.inputLine.childrenInputs.size() > 0) {\r | |
128 | currentInput = context.inputLine.childrenInputs.get(0);\r | |
129 | countMap.put(currentInput, 0);\r | |
130 | }\r | |
131 | \r | |
132 | synchronized (context.raFile) {\r | |
133 | try {\r | |
134 | if (context.raFile.getFilePointer() != context.nextLineLocation) {\r | |
135 | context.raFile.seek(context.nextLineLocation);\r | |
136 | }\r | |
137 | String line;\r | |
138 | long rawPos = context.raFile.getFilePointer();\r | |
139 | while ((line = context.raFile.readLine()) != null) {\r | |
140 | boolean processed = false;\r | |
141 | if (currentInput == null) {\r | |
142 | for (InputLine input : getFirstLines()) {\r | |
143 | Matcher matcher = input.getPattern().matcher(line);\r | |
144 | if (matcher.find()) {\r | |
145 | context.setLocation(new TmfLocation<Long>(rawPos));\r | |
146 | context.firstLineMatcher = matcher;\r | |
147 | context.nextLineLocation = context.raFile.getFilePointer();\r | |
148 | context.inputLine = input;\r | |
149 | return event;\r | |
150 | }\r | |
151 | }\r | |
152 | } else {\r | |
153 | if (countMap.get(currentInput) >= currentInput.getMinCount()) {\r | |
154 | List<InputLine> nextInputs = currentInput.getNextInputs(countMap);\r | |
155 | if (nextInputs.size() == 0 || nextInputs.get(nextInputs.size() - 1).getMinCount() == 0) {\r | |
156 | for (InputLine input : getFirstLines()) {\r | |
157 | Matcher matcher = input.getPattern().matcher(line);\r | |
158 | if (matcher.find()) {\r | |
159 | context.setLocation(new TmfLocation<Long>(rawPos));\r | |
160 | context.firstLineMatcher = matcher;\r | |
161 | context.nextLineLocation = context.raFile.getFilePointer();\r | |
162 | context.inputLine = input;\r | |
163 | return event;\r | |
164 | }\r | |
165 | }\r | |
166 | }\r | |
167 | for (InputLine input : nextInputs) {\r | |
168 | Matcher matcher = input.getPattern().matcher(line);\r | |
169 | if (matcher.find()) {\r | |
170 | event.processGroups(input, matcher);\r | |
171 | currentInput = input;\r | |
172 | if (countMap.get(currentInput) == null) {\r | |
173 | countMap.put(currentInput, 1);\r | |
174 | } else {\r | |
175 | countMap.put(currentInput, countMap.get(currentInput) + 1);\r | |
176 | }\r | |
177 | Iterator<InputLine> iter = countMap.keySet().iterator();\r | |
178 | while (iter.hasNext()) {\r | |
179 | InputLine inputLine = iter.next();\r | |
180 | if (inputLine.level > currentInput.level) {\r | |
181 | iter.remove();\r | |
182 | }\r | |
183 | }\r | |
184 | if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {\r | |
185 | currentInput = currentInput.childrenInputs.get(0);\r | |
186 | countMap.put(currentInput, 0);\r | |
187 | } else {\r | |
188 | if (countMap.get(currentInput) >= currentInput.getMaxCount()) {\r | |
189 | if (currentInput.getNextInputs(countMap).size() > 0) {\r | |
190 | currentInput = currentInput.getNextInputs(countMap).get(0);\r | |
191 | if (countMap.get(currentInput) == null) {\r | |
192 | countMap.put(currentInput, 0);\r | |
193 | }\r | |
194 | iter = countMap.keySet().iterator();\r | |
195 | while (iter.hasNext()) {\r | |
196 | InputLine inputLine = iter.next();\r | |
197 | if (inputLine.level > currentInput.level) {\r | |
198 | iter.remove();\r | |
199 | }\r | |
200 | }\r | |
201 | } else {\r | |
202 | currentInput = null;\r | |
203 | }\r | |
204 | }\r | |
205 | }\r | |
206 | processed = true;\r | |
207 | break;\r | |
208 | }\r | |
209 | }\r | |
210 | }\r | |
211 | if (! processed) {\r | |
212 | Matcher matcher = currentInput.getPattern().matcher(line);\r | |
213 | if (matcher.find()) {\r | |
214 | event.processGroups(currentInput, matcher);\r | |
215 | countMap.put(currentInput, countMap.get(currentInput) + 1);\r | |
216 | if (currentInput.childrenInputs != null && currentInput.childrenInputs.size() > 0) {\r | |
217 | currentInput = currentInput.childrenInputs.get(0);\r | |
218 | countMap.put(currentInput, 0);\r | |
219 | } else {\r | |
220 | if (countMap.get(currentInput) >= currentInput.getMaxCount()) {\r | |
221 | if (currentInput.getNextInputs(countMap).size() > 0) {\r | |
222 | currentInput = currentInput.getNextInputs(countMap).get(0);\r | |
223 | if (countMap.get(currentInput) == null) {\r | |
224 | countMap.put(currentInput, 0);\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 | } else {\r | |
234 | currentInput = null;\r | |
235 | }\r | |
236 | }\r | |
237 | }\r | |
238 | }\r | |
239 | }\r | |
240 | }\r | |
241 | rawPos = context.raFile.getFilePointer();\r | |
242 | }\r | |
243 | } catch (IOException e) {\r | |
244 | e.printStackTrace();\r | |
245 | }\r | |
246 | }\r | |
247 | for(Entry<InputLine, Integer> entry : countMap.entrySet()) {\r | |
248 | if (entry.getValue() < entry.getKey().getMinCount()) {\r | |
249 | event = null;\r | |
250 | }\r | |
251 | }\r | |
252 | context.setLocation(new TmfLocation<Long>((Long)null));\r | |
253 | return event;\r | |
254 | }\r | |
255 | \r | |
256 | public List<InputLine> getFirstLines() {\r | |
257 | return fDefinition.inputs;\r | |
258 | }\r | |
259 | \r | |
260 | public CustomTxtEvent parseFirstLine(CustomTxtTraceContext context) {\r | |
261 | CustomTxtEvent event = new CustomTxtEvent(fDefinition, TmfTimestamp.Zero, new TmfEventSource(""), new TmfEventType(fDefinition.definitionName, new String[0]), new TmfEventReference(""));\r | |
262 | event.processGroups(context.inputLine, context.firstLineMatcher);\r | |
263 | return event;\r | |
264 | }\r | |
265 | \r | |
266 | public void parseNextLine(CustomTxtEvent event, String line, InputLine input) {\r | |
267 | Pattern pattern = input.getPattern();\r | |
268 | Matcher matcher = pattern.matcher(line);\r | |
269 | if (matcher.find()) {\r | |
270 | event.processGroups(input, matcher);\r | |
271 | return;\r | |
272 | }\r | |
273 | }\r | |
274 | \r | |
275 | public CustomTraceDefinition getDefinition() {\r | |
276 | return fDefinition;\r | |
277 | }\r | |
278 | }\r |