ss: Expose method to get an attribute's path as a string array
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomXmlTrace.java
CommitLineData
6151d86c 1/*******************************************************************************
c9b31f60 2 * Copyright (c) 2010, 2015 Ericsson
6151d86c
PT
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
c9b31f60 11 * Bernd Hufmann - Add trace type id handling
6151d86c
PT
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.core.parsers.custom;
6151d86c
PT
15
16import java.io.ByteArrayInputStream;
eb8ea213 17import java.io.File;
6151d86c
PT
18import java.io.IOException;
19import java.io.RandomAccessFile;
032ecd45 20import java.nio.ByteBuffer;
6151d86c
PT
21
22import javax.xml.parsers.DocumentBuilder;
23import javax.xml.parsers.DocumentBuilderFactory;
24import javax.xml.parsers.ParserConfigurationException;
eb8ea213 25
6151d86c
PT
26import org.eclipse.core.resources.IProject;
27import org.eclipse.core.resources.IResource;
a3db8436
AM
28import org.eclipse.core.runtime.IStatus;
29import org.eclipse.core.runtime.Status;
c9b31f60 30import org.eclipse.jdt.annotation.NonNull;
2bdf0193 31import org.eclipse.tracecompass.internal.tmf.core.Activator;
b04903a2 32import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects;
2bdf0193 33import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
b04903a2 34import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
2bdf0193
AM
35import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
36import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
2bdf0193
AM
37import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
38import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
2bdf0193 39import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
c9b31f60 40import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
2bdf0193
AM
41import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
42import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
43import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
44import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
45import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
46import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
47import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
48import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
6151d86c
PT
49import org.w3c.dom.Document;
50import org.w3c.dom.Element;
51import org.w3c.dom.Node;
52import org.w3c.dom.NodeList;
53import org.xml.sax.EntityResolver;
54import org.xml.sax.ErrorHandler;
55import org.xml.sax.InputSource;
56import org.xml.sax.SAXException;
57import org.xml.sax.SAXParseException;
58
a0a88f65
AM
59/**
60 * Trace object for custom XML trace parsers.
61 *
62 * @author Patrick Tassé
63 */
c9b31f60 64public class CustomXmlTrace extends TmfTrace implements ITmfPersistentlyIndexable {
6151d86c 65
661becf8 66 private static final TmfLongLocation NULL_LOCATION = new TmfLongLocation(-1L);
6151d86c 67 private static final int DEFAULT_CACHE_SIZE = 100;
cd43d683
PT
68 private static final int MAX_LINES = 100;
69 private static final int CONFIDENCE = 100;
6151d86c
PT
70
71 private final CustomXmlTraceDefinition fDefinition;
72 private final CustomXmlEventType fEventType;
a7418109 73 private final CustomXmlInputElement fRecordInputElement;
6151d86c 74 private BufferedRandomAccessFile fFile;
c9b31f60
BH
75 private final String fTraceTypeId;
76
77 private static final char SEPARATOR = ':';
78 private static final String CUSTOM_XML_TRACE_TYPE_PREFIX = "custom.xml.trace" + SEPARATOR; //$NON-NLS-1$
79 private static final String LINUX_TOOLS_CUSTOM_XML_TRACE_TYPE_PREFIX = "org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace" + SEPARATOR; //$NON-NLS-1$
80 private static final String EARLY_TRACE_COMPASS_CUSTOM_XML_TRACE_TYPE_PREFIX = "org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace" + SEPARATOR; //$NON-NLS-1$
6151d86c 81
a0a88f65
AM
82 /**
83 * Basic constructor
84 *
eb8ea213
MK
85 * @param definition
86 * Trace definition
a0a88f65 87 */
6151d86c
PT
88 public CustomXmlTrace(final CustomXmlTraceDefinition definition) {
89 fDefinition = definition;
90 fEventType = new CustomXmlEventType(fDefinition);
91 fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
c9b31f60 92 fTraceTypeId = buildTraceTypeId(definition.categoryName, definition.definitionName);
6151d86c
PT
93 setCacheSize(DEFAULT_CACHE_SIZE);
94 }
95
a0a88f65
AM
96 /**
97 * Full constructor
98 *
99 * @param resource
100 * Trace resource
101 * @param definition
102 * Trace definition
103 * @param path
104 * Path to the trace/log file
105 * @param pageSize
106 * Page size to use
107 * @throws TmfTraceException
108 * If the trace/log couldn't be opened
109 */
110 public CustomXmlTrace(final IResource resource,
111 final CustomXmlTraceDefinition definition, final String path,
112 final int pageSize) throws TmfTraceException {
6151d86c
PT
113 this(definition);
114 setCacheSize((pageSize > 0) ? pageSize : DEFAULT_CACHE_SIZE);
115 initTrace(resource, path, CustomXmlEvent.class);
116 }
117
118 @Override
119 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) throws TmfTraceException {
120 super.initTrace(resource, path, eventType);
121 try {
122 fFile = new BufferedRandomAccessFile(getPath(), "r"); //$NON-NLS-1$
123 } catch (IOException e) {
124 throw new TmfTraceException(e.getMessage(), e);
125 }
6151d86c
PT
126 }
127
128 @Override
129 public synchronized void dispose() {
130 super.dispose();
131 if (fFile != null) {
132 try {
133 fFile.close();
134 } catch (IOException e) {
135 } finally {
136 fFile = null;
137 }
138 }
139 }
140
b0422293
PT
141 @Override
142 public ITmfTraceIndexer getIndexer() {
143 return super.getIndexer();
144 }
145
b04903a2
AM
146 @Override
147 public Iterable<ITmfEventAspect> getEventAspects() {
148 return CustomEventAspects.generateAspects(fDefinition);
149 }
150
6151d86c
PT
151 @Override
152 public synchronized TmfContext seekEvent(final ITmfLocation location) {
153 final CustomXmlTraceContext context = new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
154 if (NULL_LOCATION.equals(location) || fFile == null) {
155 return context;
156 }
157 try {
158 if (location == null) {
159 fFile.seek(0);
160 } else if (location.getLocationInfo() instanceof Long) {
161 fFile.seek((Long) location.getLocationInfo());
162 }
6151d86c 163 long rawPos = fFile.getFilePointer();
cd43d683
PT
164 String line = fFile.getNextLine();
165 while (line != null) {
a7418109 166 final int idx = indexOfElement(fRecordInputElement.getElementName(), line, 0);
6151d86c
PT
167 if (idx != -1) {
168 context.setLocation(new TmfLongLocation(rawPos + idx));
169 return context;
170 }
171 rawPos = fFile.getFilePointer();
cd43d683 172 line = fFile.getNextLine();
6151d86c
PT
173 }
174 return context;
175 } catch (final IOException e) {
47aafe74 176 Activator.logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
6151d86c
PT
177 return context;
178 }
179
180 }
181
182 @Override
183 public synchronized TmfContext seekEvent(final double ratio) {
184 if (fFile == null) {
185 return new CustomTxtTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
186 }
187 try {
91f6e587 188 long pos = Math.round(ratio * fFile.length());
6151d86c
PT
189 while (pos > 0) {
190 fFile.seek(pos - 1);
191 if (fFile.read() == '\n') {
192 break;
193 }
194 pos--;
195 }
196 final ITmfLocation location = new TmfLongLocation(pos);
197 final TmfContext context = seekEvent(location);
198 context.setRank(ITmfContext.UNKNOWN_RANK);
199 return context;
200 } catch (final IOException e) {
47aafe74 201 Activator.logError("Error seeking event. File: " + getPath(), e); //$NON-NLS-1$
6151d86c
PT
202 return new CustomXmlTraceContext(NULL_LOCATION, ITmfContext.UNKNOWN_RANK);
203 }
204 }
205
206 @Override
207 public synchronized double getLocationRatio(final ITmfLocation location) {
208 if (fFile == null) {
209 return 0;
210 }
211 try {
212 if (location.getLocationInfo() instanceof Long) {
0126a8ca 213 return ((Long) location.getLocationInfo()).doubleValue() / fFile.length();
6151d86c
PT
214 }
215 } catch (final IOException e) {
47aafe74 216 Activator.logError("Error getting location ration. File: " + getPath(), e); //$NON-NLS-1$
6151d86c
PT
217 }
218 return 0;
219 }
220
221 @Override
222 public ITmfLocation getCurrentLocation() {
223 // TODO Auto-generated method stub
224 return null;
225 }
226
227 @Override
228 public synchronized CustomXmlEvent parseEvent(final ITmfContext tmfContext) {
229 ITmfContext context = seekEvent(tmfContext.getLocation());
230 return parse(context);
231 }
232
233 @Override
234 public synchronized CustomXmlEvent getNext(final ITmfContext context) {
4c9f2944 235 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
6151d86c
PT
236 final CustomXmlEvent event = parse(context);
237 if (event != null) {
238 updateAttributes(savedContext, event.getTimestamp());
239 context.increaseRank();
240 }
241 return event;
242 }
243
244 private synchronized CustomXmlEvent parse(final ITmfContext tmfContext) {
245 if (fFile == null) {
246 return null;
247 }
248 if (!(tmfContext instanceof CustomXmlTraceContext)) {
249 return null;
250 }
251
252 final CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
9cbe7899 253 if (context.getLocation() == null || !(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
6151d86c
PT
254 return null;
255 }
256
257 CustomXmlEvent event = null;
258 try {
a7418109
MK
259 // Below +1 for the <
260 if (fFile.getFilePointer() != (Long) context.getLocation().getLocationInfo() + 1) {
261 fFile.seek((Long) context.getLocation().getLocationInfo() + 1);
6151d86c
PT
262 }
263 final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
264 readElement(elementBuffer, fFile);
265 final Element element = parseElementBuffer(elementBuffer);
266
267 event = extractEvent(element, fRecordInputElement);
268 ((StringBuffer) event.getContent().getValue()).append(elementBuffer);
269
6151d86c 270 long rawPos = fFile.getFilePointer();
cd43d683
PT
271 String line = fFile.getNextLine();
272 while (line != null) {
a7418109 273 final int idx = indexOfElement(fRecordInputElement.getElementName(), line, 0);
6151d86c
PT
274 if (idx != -1) {
275 context.setLocation(new TmfLongLocation(rawPos + idx));
276 return event;
277 }
278 rawPos = fFile.getFilePointer();
cd43d683 279 line = fFile.getNextLine();
6151d86c
PT
280 }
281 } catch (final IOException e) {
47aafe74 282 Activator.logError("Error parsing event. File: " + getPath(), e); //$NON-NLS-1$
6151d86c
PT
283
284 }
285 context.setLocation(NULL_LOCATION);
286 return event;
287 }
288
289 private Element parseElementBuffer(final StringBuffer elementBuffer) {
290 try {
291 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
292 final DocumentBuilder db = dbf.newDocumentBuilder();
293
294 // The following allows xml parsing without access to the dtd
eb8ea213 295 final EntityResolver resolver = new EntityResolver() {
6151d86c 296 @Override
eb8ea213 297 public InputSource resolveEntity(final String publicId, final String systemId) {
6151d86c
PT
298 final String empty = ""; //$NON-NLS-1$
299 final ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
300 return new InputSource(bais);
301 }
302 };
303 db.setEntityResolver(resolver);
304
305 // The following catches xml parsing exceptions
eb8ea213 306 db.setErrorHandler(new ErrorHandler() {
6151d86c 307 @Override
a7418109
MK
308 public void error(final SAXParseException saxparseexception) throws SAXException {
309 }
eb8ea213 310
6151d86c 311 @Override
a7418109
MK
312 public void warning(final SAXParseException saxparseexception) throws SAXException {
313 }
eb8ea213 314
6151d86c
PT
315 @Override
316 public void fatalError(final SAXParseException saxparseexception) throws SAXException {
317 throw saxparseexception;
eb8ea213
MK
318 }
319 });
6151d86c
PT
320
321 final Document doc = db.parse(new ByteArrayInputStream(elementBuffer.toString().getBytes()));
322 return doc.getDocumentElement();
323 } catch (final ParserConfigurationException e) {
47aafe74 324 Activator.logError("Error parsing element buffer. File:" + getPath(), e); //$NON-NLS-1$
6151d86c 325 } catch (final SAXException e) {
47aafe74 326 Activator.logError("Error parsing element buffer. File:" + getPath(), e); //$NON-NLS-1$
6151d86c 327 } catch (final IOException e) {
47aafe74 328 Activator.logError("Error parsing element buffer. File: " + getPath(), e); //$NON-NLS-1$
6151d86c
PT
329 }
330 return null;
331 }
332
9fa9acbb
PT
333 private static int indexOfElement(String elementName, String line, int fromIndex) {
334 final String recordElementStart = '<' + elementName;
335 int index = line.indexOf(recordElementStart, fromIndex);
336 if (index == -1) {
337 return index;
338 }
339 int nextCharIndex = index + recordElementStart.length();
340 if (nextCharIndex < line.length()) {
341 char c = line.charAt(nextCharIndex);
342 // Check that the match is not just a substring of another element
343 if (Character.isLetterOrDigit(c)) {
344 return indexOfElement(elementName, line, nextCharIndex);
345 }
346 }
347 return index;
348 }
349
6151d86c
PT
350 private void readElement(final StringBuffer buffer, final RandomAccessFile raFile) {
351 try {
352 int numRead = 0;
353 boolean startTagClosed = false;
354 int i;
355 while ((i = raFile.read()) != -1) {
356 numRead++;
eb8ea213 357 final char c = (char) i;
6151d86c
PT
358 buffer.append(c);
359 if (c == '"') {
360 readQuote(buffer, raFile, '"');
361 } else if (c == '\'') {
362 readQuote(buffer, raFile, '\'');
363 } else if (c == '<') {
364 readElement(buffer, raFile);
365 } else if (c == '/' && numRead == 1) {
366 break; // found "</"
367 } else if (c == '-' && numRead == 3 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("!-")) { //$NON-NLS-1$
368 readComment(buffer, raFile); // found "<!--"
369 } else if (i == '>') {
370 if (buffer.charAt(buffer.length() - 2) == '/') {
371 break; // found "/>"
372 } else if (startTagClosed) {
373 break; // found "<...>...</...>"
374 }
375 else {
376 startTagClosed = true; // found "<...>"
377 }
378 }
379 }
380 return;
381 } catch (final IOException e) {
382 return;
383 }
384 }
385
386 private static void readQuote(final StringBuffer buffer,
387 final RandomAccessFile raFile, final char eq) {
388 try {
389 int i;
390 while ((i = raFile.read()) != -1) {
eb8ea213 391 final char c = (char) i;
6151d86c
PT
392 buffer.append(c);
393 if (c == eq)
394 {
395 break; // found matching end-quote
396 }
397 }
398 return;
399 } catch (final IOException e) {
400 return;
401 }
402 }
403
404 private static void readComment(final StringBuffer buffer,
405 final RandomAccessFile raFile) {
406 try {
407 int numRead = 0;
408 int i;
409 while ((i = raFile.read()) != -1) {
410 numRead++;
eb8ea213 411 final char c = (char) i;
6151d86c
PT
412 buffer.append(c);
413 if (c == '>' && numRead >= 2 && buffer.substring(buffer.length() - 3, buffer.length() - 1).equals("--")) //$NON-NLS-1$
414 {
415 break; // found "-->"
416 }
417 }
418 return;
419 } catch (final IOException e) {
420 return;
421 }
422 }
423
a0a88f65
AM
424 /**
425 * Parse an XML element.
426 *
427 * @param parentElement
428 * The parent element
429 * @param buffer
430 * The contents to parse
431 * @return The parsed content
432 */
6151d86c
PT
433 public static StringBuffer parseElement(final Element parentElement, final StringBuffer buffer) {
434 final NodeList nodeList = parentElement.getChildNodes();
435 String separator = null;
436 for (int i = 0; i < nodeList.getLength(); i++) {
437 final Node node = nodeList.item(i);
438 if (node.getNodeType() == Node.ELEMENT_NODE) {
439 if (separator == null) {
440 separator = " | "; //$NON-NLS-1$
441 } else {
442 buffer.append(separator);
443 }
444 final Element element = (Element) node;
445 if (!element.hasChildNodes()) {
446 buffer.append(element.getNodeName());
447 } else if (element.getChildNodes().getLength() == 1 && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {
448 buffer.append(element.getNodeName() + ":" + element.getFirstChild().getNodeValue().trim()); //$NON-NLS-1$
449 } else {
450 buffer.append(element.getNodeName());
451 buffer.append(" [ "); //$NON-NLS-1$
452 parseElement(element, buffer);
453 buffer.append(" ]"); //$NON-NLS-1$
454 }
455 } else if (node.getNodeType() == Node.TEXT_NODE) {
456 if (node.getNodeValue().trim().length() != 0) {
457 buffer.append(node.getNodeValue().trim());
458 }
459 }
460 }
461 return buffer;
462 }
463
a0a88f65
AM
464 /**
465 * Get an input element if it is a valid record input. If not, we will look
466 * into its children for valid inputs.
467 *
468 * @param inputElement
469 * The main element to check for.
470 * @return The record element
471 */
a7418109
MK
472 public CustomXmlInputElement getRecordInputElement(final CustomXmlInputElement inputElement) {
473 if (inputElement.isLogEntry()) {
6151d86c 474 return inputElement;
a7418109
MK
475 } else if (inputElement.getChildElements() != null) {
476 for (final CustomXmlInputElement childInputElement : inputElement.getChildElements()) {
477 final CustomXmlInputElement recordInputElement = getRecordInputElement(childInputElement);
6151d86c
PT
478 if (recordInputElement != null) {
479 return recordInputElement;
480 }
481 }
482 }
483 return null;
484 }
485
a0a88f65
AM
486 /**
487 * Extract a trace event from an XML element.
488 *
489 * @param element
490 * The element
491 * @param inputElement
492 * The input element
493 * @return The extracted event
494 */
a7418109 495 public CustomXmlEvent extractEvent(final Element element, final CustomXmlInputElement inputElement) {
e1de2fd4 496 final CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.ZERO, fEventType);
6151d86c
PT
497 event.setContent(new CustomEventContent(event, new StringBuffer()));
498 parseElement(element, event, inputElement);
499 return event;
500 }
501
a7418109
MK
502 private void parseElement(final Element element, final CustomXmlEvent event, final CustomXmlInputElement inputElement) {
503 if (inputElement.getInputName() != null && !inputElement.getInputName().equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
504 event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.getInputName(), inputElement.getInputAction(), inputElement.getInputFormat());
6151d86c 505 }
a7418109
MK
506 if (inputElement.getAttributes() != null) {
507 for (final CustomXmlInputAttribute attribute : inputElement.getAttributes()) {
508 event.parseInput(element.getAttribute(attribute.getAttributeName()), attribute.getInputName(), attribute.getInputAction(), attribute.getInputFormat());
6151d86c
PT
509 }
510 }
511 final NodeList childNodes = element.getChildNodes();
a7418109 512 if (inputElement.getChildElements() != null) {
6151d86c
PT
513 for (int i = 0; i < childNodes.getLength(); i++) {
514 final Node node = childNodes.item(i);
515 if (node instanceof Element) {
a7418109
MK
516 for (final CustomXmlInputElement child : inputElement.getChildElements()) {
517 if (node.getNodeName().equals(child.getElementName())) {
6151d86c
PT
518 parseElement((Element) node, event, child);
519 break;
520 }
521 }
522 }
523 }
524 }
525 return;
526 }
527
a0a88f65
AM
528 /**
529 * Retrieve the trace definition.
530 *
531 * @return The trace definition
532 */
6151d86c
PT
533 public CustomTraceDefinition getDefinition() {
534 return fDefinition;
535 }
536
cd43d683
PT
537 /**
538 * {@inheritDoc}
539 * <p>
540 * The default implementation sets the confidence to 100 if any of the first
541 * 100 lines of the file contains a valid record input element, and 0
542 * otherwise.
543 */
6151d86c 544 @Override
a94410d9 545 public IStatus validate(IProject project, String path) {
cd43d683
PT
546 File file = new File(path);
547 if (!file.exists() || !file.isFile() || !file.canRead()) {
548 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
549 }
550 try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
551 int lineCount = 0;
cd43d683
PT
552 long rawPos = 0;
553 String line = rafile.getNextLine();
554 while ((line != null) && (lineCount++ < MAX_LINES)) {
a7418109 555 final int idx = indexOfElement(fRecordInputElement.getElementName(), line, 0);
cd43d683
PT
556 if (idx != -1) {
557 rafile.seek(rawPos + idx + 1); // +1 is for the <
558 final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
559 readElement(elementBuffer, rafile);
560 final Element element = parseElementBuffer(elementBuffer);
561 if (element != null) {
562 rafile.close();
563 return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
eb8ea213 564 }
cd43d683
PT
565 }
566 rawPos = rafile.getFilePointer();
567 line = rafile.getNextLine();
eb8ea213 568 }
cd43d683
PT
569 } catch (IOException e) {
570 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
a94410d9 571 }
cd43d683 572 return new TraceValidationStatus(0, Activator.PLUGIN_ID);
6151d86c 573 }
032ecd45
MAL
574
575 private static int fCheckpointSize = -1;
576
577 @Override
578 public synchronized int getCheckpointSize() {
579 if (fCheckpointSize == -1) {
580 TmfCheckpoint c = new TmfCheckpoint(TmfTimestamp.ZERO, new TmfLongLocation(0L), 0);
581 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
582 b.clear();
583 c.serialize(b);
584 fCheckpointSize = b.position();
585 }
586
587 return fCheckpointSize;
588 }
589
590 @Override
591 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
592 return new TmfLongLocation(bufferIn);
593 }
594
595 @Override
596 protected ITmfTraceIndexer createIndexer(int interval) {
597 return new TmfBTreeTraceIndexer(this, interval);
598 }
c9b31f60
BH
599
600 @Override
601 public String getTraceTypeId() {
602 return fTraceTypeId;
603 }
604
605 /**
606 * Build the trace type id for a custom XML trace
607 *
608 * @param category
609 * the category
610 * @param definitionName
611 * the definition name
612 * @return the trace type id
613 */
614 public static @NonNull String buildTraceTypeId(String category, String definitionName) {
615 return CUSTOM_XML_TRACE_TYPE_PREFIX + category + SEPARATOR + definitionName;
616 }
617
618 /**
619 * Checks whether the given trace type ID is a custom XML trace type ID
620 *
621 * @param traceTypeId
622 * the trace type ID to check
623 * @return <code>true</code> if it's a custom text trace type ID else <code>false</code>
624 */
625 public static boolean isCustomTraceTypeId(@NonNull String traceTypeId) {
626 return traceTypeId.startsWith(CUSTOM_XML_TRACE_TYPE_PREFIX);
627 }
628
629 /**
630 * This methods builds a trace type ID from a given ID taking into
631 * consideration any format changes that were done for the IDs of custom
632 * XML traces. For example, such format change took place when moving to
633 * Trace Compass. Trace type IDs that are part of the plug-in extension for
634 * trace types won't be changed.
635 *
636 * This method is useful for IDs that were persisted in the workspace before
637 * the format changes (e.g. in the persistent properties of a trace
638 * resource).
639 *
640 * It ensures backwards compatibility of the workspace for custom XML
641 * traces.
642 *
643 * @param traceTypeId
644 * the legacy trace type ID
645 * @return the trace type id in Trace Compass format
646 */
647 public static @NonNull String buildCompatibilityTraceTypeId(@NonNull String traceTypeId) {
648 // Handle early Trace Compass custom XML trace type IDs
649 if (traceTypeId.startsWith(EARLY_TRACE_COMPASS_CUSTOM_XML_TRACE_TYPE_PREFIX)) {
650 return CUSTOM_XML_TRACE_TYPE_PREFIX + traceTypeId.substring(EARLY_TRACE_COMPASS_CUSTOM_XML_TRACE_TYPE_PREFIX.length());
651 }
652
653 // Handle Linux Tools custom XML trace type IDs (with and without category)
654 int index = traceTypeId.lastIndexOf(SEPARATOR);
655 if ((index != -1) && (traceTypeId.startsWith(LINUX_TOOLS_CUSTOM_XML_TRACE_TYPE_PREFIX))) {
656 String definitionName = index < traceTypeId.length() ? traceTypeId.substring(index + 1) : ""; //$NON-NLS-1$
657 if (traceTypeId.contains(CustomXmlTrace.class.getSimpleName() + SEPARATOR) && traceTypeId.indexOf(SEPARATOR) == index) {
658 return buildTraceTypeId(CustomXmlTraceDefinition.CUSTOM_XML_CATEGORY, definitionName);
659 }
660 return CUSTOM_XML_TRACE_TYPE_PREFIX + traceTypeId.substring(LINUX_TOOLS_CUSTOM_XML_TRACE_TYPE_PREFIX.length());
661 }
662 return traceTypeId;
663 }
6151d86c 664}
This page took 0.128649 seconds and 5 git commands to generate.