/*******************************************************************************
- * Copyright (c) 2010, 2015 Ericsson
+ * Copyright (c) 2010, 2016 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
package org.eclipse.tracecompass.tmf.core.parsers.custom;
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
+import java.util.Arrays;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.eclipse.tracecompass.internal.tmf.core.Activator;
import org.eclipse.tracecompass.internal.tmf.core.parsers.custom.CustomEventAspects;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
+import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
+import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag;
import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
+import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
private static final int CONFIDENCE = 100;
private final CustomXmlTraceDefinition fDefinition;
- private final CustomXmlEventType fEventType;
+ private final ITmfEventField fRootField;
private final CustomXmlInputElement fRecordInputElement;
private BufferedRandomAccessFile fFile;
- private final String fTraceTypeId;
+ private final @NonNull String fTraceTypeId;
private static final char SEPARATOR = ':';
private static final String CUSTOM_XML_TRACE_TYPE_PREFIX = "custom.xml.trace" + SEPARATOR; //$NON-NLS-1$
*/
public CustomXmlTrace(final CustomXmlTraceDefinition definition) {
fDefinition = definition;
- fEventType = new CustomXmlEventType(fDefinition);
+ fRootField = CustomEventType.getRootField(definition);
fRecordInputElement = getRecordInputElement(fDefinition.rootInputElement);
fTraceTypeId = buildTraceTypeId(definition.categoryName, definition.definitionName);
setCacheSize(DEFAULT_CACHE_SIZE);
}
@Override
- public Iterable<ITmfEventAspect> getEventAspects() {
+ public Iterable<ITmfEventAspect<?>> getEventAspects() {
return CustomEventAspects.generateAspects(fDefinition);
}
}
final CustomXmlTraceContext context = (CustomXmlTraceContext) tmfContext;
- if (context.getLocation() == null || !(context.getLocation().getLocationInfo() instanceof Long) || NULL_LOCATION.equals(context.getLocation())) {
+ ITmfLocation location = context.getLocation();
+ if (location == null || !(location.getLocationInfo() instanceof Long) || NULL_LOCATION.equals(location)) {
return null;
}
CustomXmlEvent event = null;
try {
// Below +1 for the <
- if (fFile.getFilePointer() != (Long) context.getLocation().getLocationInfo() + 1) {
- fFile.seek((Long) context.getLocation().getLocationInfo() + 1);
+ if (fFile.getFilePointer() != (Long) location.getLocationInfo() + 1) {
+ fFile.seek((Long) location.getLocationInfo() + 1);
}
final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
readElement(elementBuffer, fFile);
* @return The extracted event
*/
public CustomXmlEvent extractEvent(final Element element, final CustomXmlInputElement inputElement) {
- final CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.ZERO, fEventType);
+ CustomXmlEventType eventType = new CustomXmlEventType(checkNotNull(fDefinition.definitionName), fRootField);
+ final CustomXmlEvent event = new CustomXmlEvent(fDefinition, this, TmfTimestamp.ZERO, eventType);
event.setContent(new CustomEventContent(event, new StringBuffer()));
parseElement(element, event, inputElement);
return event;
}
private void parseElement(final Element element, final CustomXmlEvent event, final CustomXmlInputElement inputElement) {
- if (inputElement.getInputName() != null && !inputElement.getInputName().equals(CustomXmlTraceDefinition.TAG_IGNORE)) {
- event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.getInputName(), inputElement.getInputAction(), inputElement.getInputFormat());
+ String eventType = inputElement.getEventType();
+ if (eventType != null && event.getType() instanceof CustomEventType) {
+ ((CustomEventType) event.getType()).setName(eventType);
+ }
+ if (!inputElement.getInputTag().equals(Tag.IGNORE)) {
+ event.parseInput(parseElement(element, new StringBuffer()).toString(), inputElement.getInputTag(), inputElement.getInputName(), inputElement.getInputAction(), inputElement.getInputFormat());
}
if (inputElement.getAttributes() != null) {
for (final CustomXmlInputAttribute attribute : inputElement.getAttributes()) {
- event.parseInput(element.getAttribute(attribute.getAttributeName()), attribute.getInputName(), attribute.getInputAction(), attribute.getInputFormat());
+ event.parseInput(element.getAttribute(attribute.getAttributeName()), attribute.getInputTag(), attribute.getInputName(), attribute.getInputAction(), attribute.getInputFormat());
}
}
final NodeList childNodes = element.getChildNodes();
}
super.traceRangeUpdated(signal);
}
+
+ /**
+ * @since 3.0
+ */
+ @Override
+ public synchronized ITmfTimestamp readEnd() {
+ byte[] inputNameBytes = ("<" + fRecordInputElement.getElementName()).getBytes(); //$NON-NLS-1$
+ byte[] testBytes = new byte[inputNameBytes.length];
+ try {
+ Long pos = fFile.length() - inputNameBytes.length;
+ /* Outer loop to find the position of a matcher group. */
+ while (pos >= 0) {
+ /* Inner loop to find matching tag */
+ while (pos >= 0) {
+ fFile.seek(pos);
+ /* Make sure we have the right tag. */
+ fFile.read(testBytes, 0, testBytes.length);
+ if (Arrays.equals(inputNameBytes, testBytes)) {
+ break;
+ }
+ pos--;
+ }
+ ITmfLocation location = new TmfLongLocation(pos);
+ ITmfContext context = seekEvent(location);
+ ITmfEvent event = parseEvent(context);
+ context.dispose();
+ if (event != null) {
+ /* The last event in the trace was successfully parsed. */
+ return event.getTimestamp();
+ }
+ /*
+ * pos was after the beginning of the tag of the last event.
+ */
+ pos -= inputNameBytes.length;
+ }
+ } catch (IOException e) {
+ Activator.logError("Error seeking last event. File: " + getPath(), e); //$NON-NLS-1$
+ }
+
+ /* Empty trace */
+ return null;
+ }
}