Commit | Line | Data |
---|---|---|
a3fc8213 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 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 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Alexendre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
14 | ||
15 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; | |
16 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; | |
17 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
18 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
19 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
20 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
21 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
22 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; | |
23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
24 | ||
25 | /** | |
26 | * <b><u>CTFEventField</u></b> | |
27 | */ | |
28 | public abstract class CtfTmfEventField implements ITmfEventField { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | protected final String name; | |
35 | ||
36 | // ------------------------------------------------------------------------ | |
37 | // Constructors | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
40 | protected CtfTmfEventField(String name) { | |
f13dfe18 AM |
41 | /* Strip the damn underscores, screw you CTF */ |
42 | if ( name.startsWith("_") ) { //$NON-NLS-1$ | |
43 | this.name = name.substring(1); | |
44 | } else { | |
45 | this.name = name; | |
46 | } | |
a3fc8213 AM |
47 | } |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // Getters/Setters/Predicates | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
53 | @Override | |
54 | public String getName() { | |
55 | return this.name; | |
56 | } | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Operations | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | public static CtfTmfEventField parseField(Definition fieldDef, | |
63 | String fieldName) { | |
64 | CtfTmfEventField field = null; | |
65 | ||
66 | /* Determine the Definition type */ | |
67 | if (fieldDef instanceof IntegerDefinition) { | |
68 | field = new CTFIntegerField( | |
69 | ((IntegerDefinition) fieldDef).getValue(), fieldName); | |
70 | ||
71 | } else if (fieldDef instanceof StringDefinition) { | |
72 | field = new CTFStringField( | |
73 | ((StringDefinition) fieldDef).getValue(), fieldName); | |
74 | ||
75 | } else if (fieldDef instanceof ArrayDefinition) { | |
76 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; | |
77 | ArrayDeclaration arrayDecl = arrayDef.getDeclaration(); | |
78 | ||
79 | if (arrayDef.isString()) { | |
80 | /* This is an array of UTF-8 bytes, a.k.a. a String! */ | |
81 | field = new CTFStringField(fieldDef.toString(), fieldName); | |
82 | ||
83 | } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) { | |
84 | /* This is a an array of CTF Integers */ | |
85 | long[] values = new long[arrayDecl.getLength()]; | |
86 | for (int i = 0; i < arrayDecl.getLength(); i++) { | |
87 | values[i] = ((IntegerDefinition) arrayDef.getElem(i)).getValue(); | |
88 | } | |
89 | field = new CTFIntegerArrayField(values, fieldName); | |
90 | } | |
91 | /* Add other types of arrays here */ | |
92 | ||
93 | } else if (fieldDef instanceof SequenceDefinition) { | |
94 | SequenceDefinition seqDef = (SequenceDefinition) fieldDef; | |
95 | SequenceDeclaration seqDecl = seqDef.getDeclaration(); | |
96 | ||
97 | if (seqDef.getLength() == 0) { | |
98 | /* Some sequences have length = 0. Simply use an empty string */ | |
99 | field = new CTFStringField("", fieldName); //$NON-NLS-1$ | |
100 | } else if (seqDef.isString()) { | |
101 | /* Interpret this sequence as a String */ | |
102 | field = new CTFStringField(seqDef.toString(), fieldName); | |
103 | } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { | |
104 | /* Sequence of integers => CTFIntegerArrayField */ | |
105 | long[] values = new long[seqDef.getLength()]; | |
106 | for (int i = 0; i < seqDef.getLength(); i++) { | |
107 | values[i] = ((IntegerDefinition) seqDef.getElem(i)).getValue(); | |
108 | } | |
109 | field = new CTFIntegerArrayField(values, fieldName); | |
110 | } | |
111 | /* Add other Sequence types here */ | |
112 | } | |
113 | /* Add other field types here */ | |
114 | ||
115 | return field; | |
116 | } | |
117 | ||
118 | public static CtfTmfEventField copyFrom(CtfTmfEventField other) { | |
119 | switch (other.getFieldType()) { | |
120 | case 0: | |
121 | return new CTFIntegerField(((CTFIntegerField) other).getValue(), | |
122 | other.name); | |
123 | case 1: | |
124 | return new CTFStringField(((CTFStringField) other).getValue(), | |
125 | other.name); | |
126 | case 2: | |
127 | return new CTFIntegerArrayField( | |
128 | ((CTFIntegerArrayField) other).getValue(), other.name); | |
129 | default: | |
130 | return null; | |
131 | } | |
132 | } | |
133 | ||
134 | @Override | |
135 | public CtfTmfEventField clone() { | |
136 | return CtfTmfEventField.copyFrom(this); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Return the int representing this field's value type | |
ce2388e0 | 141 | * |
0d9a6d76 | 142 | * @return the field type |
a3fc8213 AM |
143 | */ |
144 | public abstract int getFieldType(); | |
145 | ||
146 | /** | |
147 | * Return this field's value. You can cast it to the correct type depending | |
148 | * on what getFieldType says. | |
ce2388e0 | 149 | * |
0d9a6d76 | 150 | * @return the field value |
a3fc8213 AM |
151 | */ |
152 | @Override | |
153 | public abstract Object getValue(); | |
154 | ||
155 | /** | |
0d9a6d76 | 156 | * Other methods defined by ITmfEventField, but not used here: the CTF |
a3fc8213 | 157 | * fields do not have sub-fields (yet!) |
ce2388e0 | 158 | * |
0d9a6d76 | 159 | * @return the field names |
a3fc8213 | 160 | */ |
a3fc8213 AM |
161 | @Override |
162 | public String[] getFieldNames() { | |
163 | return null; | |
164 | } | |
165 | ||
166 | @Override | |
167 | public String getFieldName(int index) { | |
168 | return null; | |
169 | } | |
170 | ||
171 | @Override | |
172 | public ITmfEventField[] getFields() { | |
173 | return null; | |
174 | } | |
175 | ||
176 | @Override | |
177 | public ITmfEventField getField(String fieldName) { | |
178 | return null; | |
179 | } | |
180 | ||
181 | @Override | |
182 | public ITmfEventField getField(int index) { | |
183 | return null; | |
184 | } | |
185 | } | |
186 | ||
187 | /** | |
188 | * <b><u>CTFIntegerField</u></b> | |
189 | */ | |
190 | final class CTFIntegerField extends CtfTmfEventField { | |
191 | ||
192 | private final long longValue; | |
193 | ||
194 | /** | |
195 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
196 | * Java parser this is interpreted as a long. | |
197 | */ | |
198 | CTFIntegerField(long longValue, String name) { | |
199 | super(name); | |
200 | this.longValue = longValue; | |
201 | } | |
202 | ||
203 | @Override | |
204 | public int getFieldType() { | |
205 | return 0; | |
206 | } | |
207 | ||
208 | @Override | |
209 | public Long getValue() { | |
210 | return this.longValue; | |
211 | } | |
212 | ||
213 | /* | |
214 | * (non-Javadoc) | |
ce2388e0 | 215 | * |
a3fc8213 AM |
216 | * @see java.lang.Object#toString() |
217 | */ | |
218 | @Override | |
219 | public String toString() { | |
220 | return name + '=' + longValue; | |
221 | } | |
222 | } | |
223 | ||
224 | /** | |
225 | * <b><u>CTFStringField</u></b> | |
226 | */ | |
227 | final class CTFStringField extends CtfTmfEventField { | |
228 | ||
229 | private final String strValue; | |
230 | ||
231 | CTFStringField(String strValue, String name) { | |
232 | super(name); | |
233 | this.strValue = strValue; | |
234 | } | |
235 | ||
236 | @Override | |
237 | public int getFieldType() { | |
238 | return 1; | |
239 | } | |
240 | ||
241 | @Override | |
242 | public String getValue() { | |
243 | return this.strValue; | |
244 | } | |
245 | ||
246 | /* | |
247 | * (non-Javadoc) | |
ce2388e0 | 248 | * |
a3fc8213 AM |
249 | * @see java.lang.Object#toString() |
250 | */ | |
251 | @Override | |
252 | public String toString() { | |
253 | return name + '=' + strValue; | |
254 | } | |
255 | } | |
256 | ||
257 | /** | |
258 | * <b><u>CTFIntegerArrayField</u></b> | |
259 | */ | |
260 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
261 | ||
262 | private final long[] longValues; | |
263 | ||
264 | CTFIntegerArrayField(long[] longValues, String name) { | |
265 | super(name); | |
266 | this.longValues = longValues; | |
267 | } | |
268 | ||
269 | @Override | |
270 | public int getFieldType() { | |
271 | return 2; | |
272 | } | |
273 | ||
274 | @Override | |
275 | public long[] getValue() { | |
276 | return this.longValues; | |
277 | } | |
278 | ||
279 | @Override | |
280 | public String toString() { | |
281 | StringBuffer buffer = new StringBuffer(); | |
282 | buffer.append("{ "); //$NON-NLS-1$ | |
283 | ||
284 | buffer.append(longValues[0]); | |
285 | for (int i = 1; i < longValues.length; i++) { | |
286 | buffer.append(", " + longValues[i]); //$NON-NLS-1$ | |
287 | } | |
288 | buffer.append('}'); | |
289 | return name + '=' + buffer.toString(); | |
290 | } | |
291 | } | |
292 | ||
293 | /* Implement other possible fields types here... */ |