1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.trace
;
12 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.isAnyUnaryString
;
14 import java
.util
.UUID
;
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
;
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
27 * @author Matthew Khouzam
30 public class UUIDParser
implements ICommonTreeParser
{
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$
35 public static final UUIDParser INSTANCE
= new UUIDParser();
37 private UUIDParser() {
41 * Parse a UUID String and get a {@link UUID} in return.
47 * @return a {@link UUID}
48 * @throws ParseException
49 * the AST was malformed
52 public UUID
parse(CommonTree tree
, ICommonTreeParserParameter unused
) throws ParseException
{
54 CommonTree firstChild
= (CommonTree
) tree
.getChild(0);
56 if (isAnyUnaryString(firstChild
)) {
57 if (tree
.getChildCount() > 1) {
58 throw new ParseException(INVALID_VALUE_FOR_UUID
);
61 String uuidstr
= UnaryStringParser
.INSTANCE
.parse(firstChild
, null);
64 return UUID
.fromString(uuidstr
);
65 } catch (IllegalArgumentException e
) {
66 throw new ParseException(INVALID_FORMAT_FOR_UUID
, e
);
69 throw new ParseException(INVALID_VALUE_FOR_UUID
);