Commit | Line | Data |
---|---|---|
b1ea73b5 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which 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.trace; | |
11 | ||
12 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.isAnyUnaryString; | |
13 | ||
14 | import java.util.UUID; | |
15 | ||
16 | import org.antlr.runtime.tree.CommonTree; | |
17 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser; | |
18 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; | |
19 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.UnaryStringParser; | |
20 | ||
21 | /** | |
22 | * <strong>Trace UUID</strong>, used to ensure the event packet match the | |
23 | * metadata used. Note: we cannot use a metadata checksum in every cases instead | |
24 | * of a UUID because metadata can be appended to while tracing is active. This | |
25 | * field is optional. | |
26 | * | |
27 | * @author Matthew Khouzam | |
28 | * | |
29 | */ | |
4055c3a1 | 30 | public final class UUIDParser implements ICommonTreeParser { |
b1ea73b5 MK |
31 | |
32 | private static final String INVALID_FORMAT_FOR_UUID = "Invalid format for UUID"; //$NON-NLS-1$ | |
33 | private static final String INVALID_VALUE_FOR_UUID = "Invalid value for UUID"; //$NON-NLS-1$ | |
34 | /** Instance */ | |
35 | public static final UUIDParser INSTANCE = new UUIDParser(); | |
36 | ||
37 | private UUIDParser() { | |
38 | } | |
39 | ||
40 | /** | |
41 | * Parse a UUID String and get a {@link UUID} in return. | |
42 | * | |
43 | * @param tree | |
44 | * the UUID AST | |
45 | * @param unused | |
46 | * unused | |
47 | * @return a {@link UUID} | |
48 | * @throws ParseException | |
49 | * the AST was malformed | |
50 | */ | |
51 | @Override | |
52 | public UUID parse(CommonTree tree, ICommonTreeParserParameter unused) throws ParseException { | |
53 | ||
54 | CommonTree firstChild = (CommonTree) tree.getChild(0); | |
55 | ||
56 | if (isAnyUnaryString(firstChild)) { | |
57 | if (tree.getChildCount() > 1) { | |
58 | throw new ParseException(INVALID_VALUE_FOR_UUID); | |
59 | } | |
60 | ||
61 | String uuidstr = UnaryStringParser.INSTANCE.parse(firstChild, null); | |
62 | ||
63 | try { | |
64 | return UUID.fromString(uuidstr); | |
65 | } catch (IllegalArgumentException e) { | |
66 | throw new ParseException(INVALID_FORMAT_FOR_UUID, e); | |
67 | } | |
68 | } | |
69 | throw new ParseException(INVALID_VALUE_FOR_UUID); | |
70 | } | |
71 | ||
72 | } |