|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Ericsson |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.staticstring; |
| 12 | + |
| 13 | +import java.nio.charset.Charset; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 17 | +import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; |
| 18 | +import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; |
| 19 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser; |
| 20 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode; |
| 21 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; |
| 22 | +import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode; |
| 23 | +import org.eclipse.tracecompass.internal.ctf.core.event.types.StaticLengthStringDeclaration; |
| 24 | +import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings; |
| 25 | + |
| 26 | +import com.google.gson.JsonElement; |
| 27 | +import com.google.gson.JsonObject; |
| 28 | + |
| 29 | +/** |
| 30 | + * A dynamic-length string field class is an abstract string field class which |
| 31 | + * describes dynamic-length string fields. |
| 32 | + * |
| 33 | + * A dynamic-length string field is a sequence of zero or more contiguous |
| 34 | + * encoded Unicode codepoints. All the encoded codepoints of a dynamic-length |
| 35 | + * string field before the first "NULL" (U+0000) codepoint, if any, form the |
| 36 | + * resulting string value. The first U+0000 codepoint, if any, and all the |
| 37 | + * following bytes are considered padding (garbage data). |
| 38 | + * |
| 39 | + * The length, or number of bytes, of a dynamic-length string field is the |
| 40 | + * value of another, anterior (already encoded/decoded) length field. A |
| 41 | + * consumer can locate this length field thanks to the length-field-location |
| 42 | + * property of the dynamic-length string field class. |
| 43 | + * |
| 44 | + * @author Matthew Khouzam |
| 45 | + */ |
| 46 | +public final class StaticLengthStringParser extends AbstractScopedCommonTreeParser { |
| 47 | + |
| 48 | + /** |
| 49 | + * Instance |
| 50 | + */ |
| 51 | + public static final StaticLengthStringParser INSTANCE = new StaticLengthStringParser(); |
| 52 | + |
| 53 | + private StaticLengthStringParser() { |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public IDeclaration parse(ICTFMetadataNode node, ICommonTreeParserParameter param) throws ParseException { |
| 58 | + if (!(node instanceof JsonStructureFieldMemberMetadataNode)) { |
| 59 | + throw new ParseException("Dynamic-length string only supported in JSON metadata"); //$NON-NLS-1$ |
| 60 | + } |
| 61 | + |
| 62 | + JsonStructureFieldMemberMetadataNode member = (JsonStructureFieldMemberMetadataNode) node; |
| 63 | + JsonObject fieldClass = member.getFieldClass().getAsJsonObject(); |
| 64 | + |
| 65 | + JsonElement lengthField = fieldClass.get(JsonMetadataStrings.LENGTH); |
| 66 | + if (lengthField == null) { |
| 67 | + throw new ParseException("Dynamic-length string requires length-field-location property"); //$NON-NLS-1$ |
| 68 | + } |
| 69 | + JsonElement encodingField = fieldClass.get(JsonMetadataStrings.ENCODING); |
| 70 | + int length = lengthField.getAsInt(); |
| 71 | + Charset encoding = encodingField != null ? |
| 72 | + JsonMetadataStrings.ENCODINGS.getOrDefault(encodingField.getAsString(), StandardCharsets.UTF_8) : |
| 73 | + StandardCharsets.UTF_8; |
| 74 | + return new StaticLengthStringDeclaration(length, encoding); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Parameters for the dynamic-length string parser |
| 79 | + */ |
| 80 | + @NonNullByDefault |
| 81 | + public static final class Param implements ICommonTreeParserParameter { |
| 82 | + private final CTFTrace fTrace; |
| 83 | + |
| 84 | + /** |
| 85 | + * Parameter constructor |
| 86 | + * |
| 87 | + * @param trace the trace |
| 88 | + */ |
| 89 | + public Param(CTFTrace trace) { |
| 90 | + fTrace = trace; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the trace |
| 95 | + * |
| 96 | + * @return the trace |
| 97 | + */ |
| 98 | + public CTFTrace getTrace() { |
| 99 | + return fTrace; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments