| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 Ecole Polytechnique de Montreal |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made 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: |
| 10 | * Florian Wininger - Initial API and implementation |
| 11 | ******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; |
| 14 | |
| 15 | import java.util.LinkedList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.jdt.annotation.Nullable; |
| 19 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; |
| 20 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; |
| 21 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; |
| 22 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 23 | import org.w3c.dom.Element; |
| 24 | |
| 25 | /** |
| 26 | * This Class implements a Location in the XML-defined state system, ie a named |
| 27 | * shortcut to reach a given attribute, used in state attributes |
| 28 | * |
| 29 | * <pre> |
| 30 | * example: |
| 31 | * <location id="CurrentCPU"> |
| 32 | * <stateAttribute type="constant" value="CPUs" /> |
| 33 | * <stateAttribute type="eventField" name="cpu" /> |
| 34 | * ... |
| 35 | * </location> |
| 36 | * </pre> |
| 37 | * |
| 38 | * @author Florian Wininger |
| 39 | */ |
| 40 | public class TmfXmlLocation { |
| 41 | |
| 42 | /** Path in the State System */ |
| 43 | private final List<ITmfXmlStateAttribute> fPath = new LinkedList<>(); |
| 44 | |
| 45 | /** ID : name of the location */ |
| 46 | private final String fId; |
| 47 | private final IXmlStateSystemContainer fContainer; |
| 48 | |
| 49 | /** |
| 50 | * Constructor |
| 51 | * |
| 52 | * @param modelFactory |
| 53 | * The factory used to create XML model elements |
| 54 | * @param location |
| 55 | * XML node element |
| 56 | * @param container |
| 57 | * The state system container this location belongs to |
| 58 | */ |
| 59 | public TmfXmlLocation(ITmfXmlModelFactory modelFactory, Element location, IXmlStateSystemContainer container) { |
| 60 | String id = location.getAttribute(TmfXmlStrings.ID); |
| 61 | if (id == null) { |
| 62 | throw new IllegalArgumentException(); |
| 63 | } |
| 64 | fId = id; |
| 65 | fContainer = container; |
| 66 | |
| 67 | List<Element> childElements = XmlUtils.getChildElements(location); |
| 68 | for (Element attribute : childElements) { |
| 69 | if (attribute == null) { |
| 70 | continue; |
| 71 | } |
| 72 | ITmfXmlStateAttribute xAttribute = modelFactory.createStateAttribute(attribute, fContainer); |
| 73 | fPath.add(xAttribute); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the id of the location |
| 79 | * |
| 80 | * @return The id of a location |
| 81 | */ |
| 82 | public String getId() { |
| 83 | return fId; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the quark for the path represented by this location |
| 88 | * |
| 89 | * @param event |
| 90 | * The event being handled |
| 91 | * @param startQuark |
| 92 | * The starting quark for relative search, use |
| 93 | * {@link IXmlStateSystemContainer#ROOT_QUARK} for the root of |
| 94 | * the attribute tree |
| 95 | * @return The quark at the leaf of the path |
| 96 | */ |
| 97 | public int getLocationQuark(@Nullable ITmfEvent event, int startQuark) { |
| 98 | int quark = startQuark; |
| 99 | for (ITmfXmlStateAttribute attrib : fPath) { |
| 100 | quark = attrib.getAttributeQuark(event, quark); |
| 101 | if (quark == IXmlStateSystemContainer.ERROR_QUARK) { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | return quark; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the quark for the path represented by this location |
| 110 | * |
| 111 | * @param startQuark |
| 112 | * The starting quark for relative search, use |
| 113 | * {@link IXmlStateSystemContainer#ROOT_QUARK} for the root of |
| 114 | * the attribute tree |
| 115 | * @return The quark at the leaf of the path |
| 116 | */ |
| 117 | public int getLocationQuark(int startQuark) { |
| 118 | int quark = startQuark; |
| 119 | for (ITmfXmlStateAttribute attrib : fPath) { |
| 120 | quark = attrib.getAttributeQuark(quark); |
| 121 | if (quark == IXmlStateSystemContainer.ERROR_QUARK) { |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | return quark; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String toString() { |
| 130 | return "TmfXmlLocation " + fId + ": " + fPath; //$NON-NLS-1$ //$NON-NLS-2$ |
| 131 | } |
| 132 | |
| 133 | } |