ctf: split up IOStructGen into 44 files
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / ByteOrderParser.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl;
11
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;
14
15 import java.nio.ByteOrder;
16
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;
22
23 /**
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
28 * conversion.
29 *
30 * TSDL metadata representation:
31 *
32 * <pre>
33 * /* network and be are aliases * /
34 * byte_order= /* native OR network OR be OR le * /;
35 * </pre>
36 *
37 * The native keyword selects the byte order described in the trace description.
38 * The network byte order is an alias for big endian.
39 *
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.
44 *
45 * @author Matthew Khouzam
46 * @author Efficios - Javadoc preamble
47 *
48 */
49 public final class ByteOrderParser implements ICommonTreeParser {
50
51 /**
52 * Parameter object with a trace
53 *
54 * @author Matthew Khouzam
55 *
56 */
57 public static final class Param implements ICommonTreeParserParameter {
58 private final CTFTrace fTrace;
59
60 /**
61 * Constructor
62 *
63 * @param trace
64 * the trace
65 */
66 public Param(CTFTrace trace) {
67 fTrace = trace;
68 }
69 }
70
71 /**
72 * Instance
73 */
74 public static final ByteOrderParser INSTANCE = new ByteOrderParser();
75
76 private static final String INVALID_VALUE_FOR_BYTE_ORDER = "Invalid value for byte order"; //$NON-NLS-1$
77
78 private ByteOrderParser() {
79 }
80
81 /**
82 * Gets the value of a "byte_order" integer attribute.
83 *
84 * @param byteOrderTree
85 * A CTF_RIGHT node.
86 *
87 * @return The "byte_order" value.
88 * @throws ParseException
89 * if the value is invalid
90 */
91 @Override
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$
95 }
96 CTFTrace trace = ((Param) param).fTrace;
97 CommonTree firstChild = (CommonTree) byteOrderTree.getChild(0);
98
99 if (isUnaryString(firstChild)) {
100 String strval = concatenateUnaryStrings(byteOrderTree.getChildren());
101
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;
110 } else {
111 throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER);
112 }
113 }
114 throw new ParseException(INVALID_VALUE_FOR_BYTE_ORDER);
115 }
116 }
This page took 0.0340859999999999 seconds and 5 git commands to generate.