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