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