1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
;
12 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.concatenateUnaryStrings
;
13 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.isUnaryString
;
15 import java
.nio
.ByteOrder
;
17 import org
.antlr
.runtime
.tree
.CommonTree
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
19 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ICommonTreeParser
;
20 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.MetadataStrings
;
21 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ParseException
;
24 * By default, byte order of a basic type is the byte order described in the
25 * trace description. It can be overridden by specifying a byte_order attribute
26 * for a basic type. Typical use-case is to specify the network byte order (big
27 * endian: be) to save data captured from the network into the trace without
30 * TSDL metadata representation:
33 * /* network and be are aliases * /
34 * byte_order= /* native OR network OR be OR le * /;
37 * The native keyword selects the byte order described in the trace description.
38 * The network byte order is an alias for big endian.
40 * Even though the trace description section is not per se a type, for sake of
41 * clarity, it should be noted that native and network byte orders are only
42 * allowed within type declaration. The byte_order specified in the trace
43 * description section only accepts be or le values.
45 * @author Matthew Khouzam
46 * @author Efficios - Javadoc preamble
49 public final class ByteOrderParser
implements ICommonTreeParser
{
52 * Parameter object with a trace
54 * @author Matthew Khouzam
57 public static final class Param
implements ICommonTreeParserParameter
{
58 private final CTFTrace fTrace
;
66 public Param(CTFTrace trace
) {
74 public static final ByteOrderParser INSTANCE
= new ByteOrderParser();
76 private static final String INVALID_VALUE_FOR_BYTE_ORDER
= "Invalid value for byte order"; //$NON-NLS-1$
78 private ByteOrderParser() {
82 * Gets the value of a "byte_order" integer attribute.
84 * @param byteOrderTree
87 * @return The "byte_order" value.
88 * @throws ParseException
89 * if the value is invalid
92 public final ByteOrder
parse(CommonTree byteOrderTree
, ICommonTreeParserParameter param
) throws ParseException
{
93 if (!(param
instanceof Param
)) {
94 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
96 CTFTrace trace
= ((Param
) param
).fTrace
;
97 CommonTree firstChild
= (CommonTree
) byteOrderTree
.getChild(0);
99 if (isUnaryString(firstChild
)) {
100 String strval
= concatenateUnaryStrings(byteOrderTree
.getChildren());
102 if (strval
.equals(MetadataStrings
.LE
)) {
103 return ByteOrder
.LITTLE_ENDIAN
;
104 } else if (strval
.equals(MetadataStrings
.BE
)
105 || strval
.equals(MetadataStrings
.NETWORK
)) {
106 return ByteOrder
.BIG_ENDIAN
;
107 } else if (strval
.equals(MetadataStrings
.NATIVE
)) {
108 ByteOrder byteOrder
= trace
.getByteOrder();
109 return (byteOrder
== null) ? ByteOrder
.nativeOrder() : byteOrder
;
111 throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER
);
114 throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER
);