|
| 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.dynamicarray; |
| 12 | + |
| 13 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 14 | +import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope; |
| 15 | +import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; |
| 16 | +import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; |
| 17 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser; |
| 18 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode; |
| 19 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; |
| 20 | +import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser; |
| 21 | +import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode; |
| 22 | +import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration; |
| 23 | +import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings; |
| 24 | + |
| 25 | +import com.google.gson.JsonElement; |
| 26 | +import com.google.gson.JsonObject; |
| 27 | + |
| 28 | +/** |
| 29 | + * A dynamic-length array field class describes dynamic-length array fields. |
| 30 | + * |
| 31 | + * @author Matthew Khouzam |
| 32 | + */ |
| 33 | +public final class DynamicLengthArrayParser extends AbstractScopedCommonTreeParser { |
| 34 | + |
| 35 | + /** |
| 36 | + * Instance |
| 37 | + */ |
| 38 | + public static final DynamicLengthArrayParser INSTANCE = new DynamicLengthArrayParser(); |
| 39 | + |
| 40 | + private DynamicLengthArrayParser() { |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public IDeclaration parse(ICTFMetadataNode node, ICommonTreeParserParameter param) throws ParseException { |
| 45 | + if (!(node instanceof JsonStructureFieldMemberMetadataNode)) { |
| 46 | + throw new ParseException("Dynamic-length array only supported in JSON metadata"); //$NON-NLS-1$ |
| 47 | + } |
| 48 | + if (!(param instanceof Param)) { |
| 49 | + throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$ |
| 50 | + } |
| 51 | + |
| 52 | + JsonStructureFieldMemberMetadataNode member = (JsonStructureFieldMemberMetadataNode) node; |
| 53 | + JsonObject fieldClass = member.getFieldClass().getAsJsonObject(); |
| 54 | + |
| 55 | + JsonElement lengthFieldLocation = fieldClass.get(JsonMetadataStrings.LENGTH_FIELD_LOCATION); |
| 56 | + if (lengthFieldLocation == null) { |
| 57 | + throw new ParseException("Dynamic-length array requires length-field-location property"); //$NON-NLS-1$ |
| 58 | + } |
| 59 | + |
| 60 | + JsonElement elementFieldClass = fieldClass.get(JsonMetadataStrings.ELEMENT_FIELD_CLASS); |
| 61 | + if (elementFieldClass == null) { |
| 62 | + throw new ParseException("Dynamic-length array requires element-field-class property"); //$NON-NLS-1$ |
| 63 | + } |
| 64 | + |
| 65 | + JsonElement pathElement = lengthFieldLocation.getAsJsonObject().get(JsonMetadataStrings.PATH); |
| 66 | + String lengthName = pathElement.isJsonArray() ? |
| 67 | + pathElement.getAsJsonArray().get(pathElement.getAsJsonArray().size() - 1).getAsString() : |
| 68 | + pathElement.getAsString(); |
| 69 | + CTFTrace trace = ((Param) param).getTrace(); |
| 70 | + DeclarationScope scope = ((Param) param).getScope(); |
| 71 | + |
| 72 | + JsonStructureFieldMemberMetadataNode elementNode = new JsonStructureFieldMemberMetadataNode(node, "", "", "", elementFieldClass.getAsJsonObject()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 73 | + IDeclaration elementDeclaration = TypeAliasParser.INSTANCE.parse(elementNode, new TypeAliasParser.Param(trace, scope)); |
| 74 | + |
| 75 | + return new SequenceDeclaration(lengthName, elementDeclaration); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Parameters for the dynamic-length array parser |
| 80 | + */ |
| 81 | + @NonNullByDefault |
| 82 | + public static final class Param implements ICommonTreeParserParameter { |
| 83 | + private final CTFTrace fTrace; |
| 84 | + private final DeclarationScope fScope; |
| 85 | + |
| 86 | + /** |
| 87 | + * Parameter constructor |
| 88 | + * |
| 89 | + * @param trace the trace |
| 90 | + * @param scope the declaration scope |
| 91 | + */ |
| 92 | + public Param(CTFTrace trace, DeclarationScope scope) { |
| 93 | + fTrace = trace; |
| 94 | + fScope = scope; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the trace |
| 99 | + * |
| 100 | + * @return the trace |
| 101 | + */ |
| 102 | + public CTFTrace getTrace() { |
| 103 | + return fTrace; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the scope |
| 108 | + * |
| 109 | + * @return the scope |
| 110 | + */ |
| 111 | + public DeclarationScope getScope() { |
| 112 | + return fScope; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments