tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/TraceLocationMap.java
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * Maps trace locations into traces and vice versa
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.HashMap;
       
    23 import java.util.Iterator;
       
    24 
       
    25 import com.nokia.tracecompiler.engine.source.SourceProperties;
       
    26 import com.nokia.tracecompiler.model.Trace;
       
    27 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    28 import com.nokia.tracecompiler.model.TraceModel;
       
    29 
       
    30 /**
       
    31  * Maps trace locations into traces and vice versa.
       
    32  * 
       
    33  */
       
    34 final class TraceLocationMap {
       
    35 
       
    36 	/**
       
    37 	 * List of unrelated traces
       
    38 	 */
       
    39 	private TraceLocationList unrelated = new TraceLocationList();
       
    40 
       
    41 	/**
       
    42 	 * Parser groups
       
    43 	 */
       
    44 	private HashMap<String, TraceLocationList> parserGroups = new HashMap<String, TraceLocationList>();
       
    45 
       
    46 	/**
       
    47 	 * The trace model
       
    48 	 */
       
    49 	private TraceModel model;
       
    50 
       
    51 	/**
       
    52 	 * Global list of locations, used for verification purposes with
       
    53 	 * GLOBAL_LOCATION_ASSERTS configuration flag
       
    54 	 */
       
    55 	private ArrayList<TraceLocation> globalList;
       
    56 
       
    57 	/**
       
    58 	 * Creates a location mapper
       
    59 	 * 
       
    60 	 * @param model
       
    61 	 *            the trace model
       
    62 	 */
       
    63 	public TraceLocationMap(TraceModel model) {
       
    64 		if (TraceCompilerEngineConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
    65 			globalList = new ArrayList<TraceLocation>();
       
    66 		}
       
    67 		this.model = model;
       
    68 		model.addModelListener(new LocationMapModelListener(this));
       
    69 		model.addExtension(unrelated);
       
    70 	}
       
    71 
       
    72 	/**
       
    73 	 * Adds the locations from the source file to the map
       
    74 	 * 
       
    75 	 * @param source
       
    76 	 *            properties of the source to be added
       
    77 	 * @throws TraceCompilerException 
       
    78 	 */
       
    79 	void addSource(SourceProperties source) throws TraceCompilerException {
       
    80 		for (TraceLocation location : source) {
       
    81 			if (TraceCompilerEngineConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
    82 				if (globalList.contains(location)) {
       
    83 					TraceCompilerEngineGlobals.getEvents().postAssertionFailed(
       
    84 							"Location already in global list", //$NON-NLS-1$
       
    85 							location.getConvertedName());
       
    86 				} else {
       
    87 					globalList.add(location);
       
    88 				}
       
    89 			}
       
    90 			// Generates locationAdded event via TraceLocationListListener
       
    91 			addNewLocationToTrace(location);
       
    92 		}
       
    93 	}
       
    94 
       
    95 	/**
       
    96 	 * Adds a location to trace or to the unrelated list if a trace cannot be
       
    97 	 * found.
       
    98 	 * 
       
    99 	 * @param location
       
   100 	 *            the location to be added
       
   101 	 * @throws TraceCompilerException 
       
   102 	 */
       
   103 	private void addNewLocationToTrace(TraceLocation location) throws TraceCompilerException {
       
   104 		TraceLocationList list;
       
   105 		Trace trace = model.findTraceByName(location.getOriginalName());
       
   106 		if (trace != null) {
       
   107 			list = trace.getExtension(TraceLocationList.class);
       
   108 			if (list == null) {
       
   109 				list = new TraceLocationList();
       
   110 				trace.addExtension(list);
       
   111 			}
       
   112 		} else {
       
   113 			String name = location.getParserRule().getLocationParser()
       
   114 					.getLocationGroup();
       
   115 			if (name == null) {
       
   116 				list = unrelated;
       
   117 			} else {
       
   118 				list = parserGroups.get(name);
       
   119 				if (list == null) {
       
   120 					list = new TraceLocationList(name);
       
   121 					model.addExtension(list);
       
   122 					parserGroups.put(name, list);
       
   123 				}
       
   124 			}
       
   125 		}
       
   126 		list.addLocation(location);
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * Moves the locations from trace to unrelated list
       
   131 	 * 
       
   132 	 * @param trace
       
   133 	 *            the trace
       
   134 	 * @throws TraceCompilerException 
       
   135 	 */
       
   136 	void moveToUnrelated(Trace trace) throws TraceCompilerException {
       
   137 		TraceLocationList list = trace.getExtension(TraceLocationList.class);
       
   138 		if (list != null) {
       
   139 			trace.removeExtension(list);
       
   140 			for (LocationProperties loc : list) {
       
   141 				unrelated.addLocation((TraceLocation) loc);
       
   142 			}
       
   143 		}
       
   144 	}
       
   145 
       
   146 	/**
       
   147 	 * Moves locations from unrelated to the given trace
       
   148 	 * 
       
   149 	 * @param trace
       
   150 	 *            the trace
       
   151 	 * @throws TraceCompilerException 
       
   152 	 */
       
   153 	void moveFromUnrelated(Trace trace) throws TraceCompilerException {
       
   154 		String name = trace.getName();
       
   155 		TraceLocationList list = null;
       
   156 		Iterator<LocationProperties> itr = unrelated.iterator();
       
   157 		while (itr.hasNext()) {
       
   158 			TraceLocation location = (TraceLocation) itr.next();
       
   159 			if (name.equals(location.getOriginalName())) {
       
   160 				list = trace.getExtension(TraceLocationList.class);
       
   161 				if (list == null) {
       
   162 					list = new TraceLocationList();
       
   163 					trace.addExtension(list);
       
   164 				}
       
   165 				// NOTE: This must replicate the behavior of
       
   166 				// TraceLocationList.removeLocation
       
   167 				itr.remove();
       
   168 				unrelated.fireLocationRemoved(location);
       
   169 				list.addLocation(location);
       
   170 			}
       
   171 		}
       
   172 	}
       
   173 
       
   174 }