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