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