trace/tracebuilder/com.nokia.tracebuilder/src/com/nokia/tracebuilder/engine/TraceLocationMap.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     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.tracebuilder.engine;
       
    20 
       
    21 import java.io.File;
       
    22 import java.util.ArrayList;
       
    23 import java.util.HashMap;
       
    24 import java.util.Iterator;
       
    25 
       
    26 import com.nokia.tracebuilder.engine.source.SourceProperties;
       
    27 import com.nokia.tracebuilder.model.Trace;
       
    28 import com.nokia.tracebuilder.model.TraceGroup;
       
    29 import com.nokia.tracebuilder.model.TraceModel;
       
    30 import com.nokia.tracebuilder.model.TraceObject;
       
    31 import com.nokia.tracebuilder.source.SourceContext;
       
    32 
       
    33 /**
       
    34  * Maps trace locations into traces and vice versa.
       
    35  * 
       
    36  */
       
    37 public final class TraceLocationMap {
       
    38 
       
    39 	/**
       
    40 	 * List of unrelated traces
       
    41 	 */
       
    42 	private TraceLocationList unrelated = new TraceLocationList();
       
    43 
       
    44 	/**
       
    45 	 * Parser groups
       
    46 	 */
       
    47 	private HashMap<String, TraceLocationList> parserGroups = new HashMap<String, TraceLocationList>();
       
    48 
       
    49 	/**
       
    50 	 * The trace model
       
    51 	 */
       
    52 	private TraceModel model;
       
    53 
       
    54 	/**
       
    55 	 * Global list of locations, used for verification purposes with
       
    56 	 * GLOBAL_LOCATION_ASSERTS configuration flag
       
    57 	 */
       
    58 	private ArrayList<TraceLocation> globalList;
       
    59 
       
    60 	/**
       
    61 	 * Flag which is set when "Source missing" error is shown. Prevents it to be
       
    62 	 * shown again
       
    63 	 */
       
    64 	private boolean missingSourceErrorShown;
       
    65 
       
    66 	/**
       
    67 	 * Creates a location mapper
       
    68 	 * 
       
    69 	 * @param model
       
    70 	 *            the trace model
       
    71 	 */
       
    72 	public TraceLocationMap(TraceModel model) {
       
    73 		if (TraceBuilderConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
    74 			globalList = new ArrayList<TraceLocation>();
       
    75 		}
       
    76 		this.model = model;
       
    77 		model.addModelListener(new LocationMapModelListener(this));
       
    78 		model.addExtension(unrelated);
       
    79 	}
       
    80 
       
    81 	/**
       
    82 	 * Adds the locations from the source file to the map
       
    83 	 * 
       
    84 	 * @param source
       
    85 	 *            properties of the source to be added
       
    86 	 */
       
    87 	public void addSource(SourceProperties source) {
       
    88 		for (TraceLocation location : source) {
       
    89 			if (TraceBuilderConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
    90 				if (globalList.contains(location)) {
       
    91 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
    92 							"Location already in global list", //$NON-NLS-1$
       
    93 							location.getConvertedName());
       
    94 				} else {
       
    95 					globalList.add(location);
       
    96 				}
       
    97 			}
       
    98 			// Generates locationAdded event via TraceLocationListListener
       
    99 			addNewLocationToTrace(location);
       
   100 		}
       
   101 		removeFromStorage(source);
       
   102 	}
       
   103 
       
   104 	/**
       
   105 	 * Updates the locations based on the new source properties
       
   106 	 * 
       
   107 	 * @param source
       
   108 	 *            the source properties of the updated source
       
   109 	 */
       
   110 	void updateSource(SourceProperties source) {
       
   111 		for (TraceLocation location : source) {
       
   112 			if (location.isDeleted()) {
       
   113 				// Generates locationRemoved event via TraceLocationListListener
       
   114 				locationDeleted(location);
       
   115 			} else if (location.isContentChanged()) {
       
   116 				locationContentChanged(location);
       
   117 			} else {
       
   118 				// Generates locationChanged events if offset or length has
       
   119 				// changed
       
   120 				location.notifyLocationChanged();
       
   121 			}
       
   122 		}
       
   123 		checkGlobalValidity();
       
   124 	}
       
   125 
       
   126 	/**
       
   127 	 * Removes the locations of the closed source properties
       
   128 	 * 
       
   129 	 * @param source
       
   130 	 *            the source properties of the removed source
       
   131 	 * @param path
       
   132 	 *            the file path as string
       
   133 	 * @param name
       
   134 	 *            the file name
       
   135 	 */
       
   136 	public void removeSource(SourceProperties source, String path,
       
   137 			String name) {
       
   138 		missingSourceErrorShown = false;
       
   139 		for (TraceLocation location : source) {
       
   140 			if (TraceBuilderConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
   141 				if (!globalList.remove(location)) {
       
   142 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   143 							"Location not in global list", //$NON-NLS-1$
       
   144 							location.getConvertedName());
       
   145 				}
       
   146 			}
       
   147 			TraceLocationList list = location.getLocationList();
       
   148 			if (list != null) {
       
   149 				addLastKnownLocation(source, location, list, path, name);
       
   150 				// Generates locationRemoved event via TraceLocationListListener
       
   151 				list.removeLocation(location);
       
   152 			} else {
       
   153 				if (TraceBuilderConfiguration.ASSERTIONS_ENABLED) {
       
   154 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   155 							"Unassociated location on remove", //$NON-NLS-1$
       
   156 							location.getConvertedName());
       
   157 				}
       
   158 			}
       
   159 		}
       
   160 	}	
       
   161 
       
   162 	/**
       
   163 	 * Converts a location to a last known  location
       
   164 	 * 
       
   165 	 * @param source
       
   166 	 *            the source file
       
   167 	 * @param location
       
   168 	 *            the location
       
   169 	 * @param list
       
   170 	 *            the location list
       
   171 	 * @param path
       
   172 	 *            the file path as string
       
   173 	 * @param name
       
   174 	 *            the file name
       
   175 	 */
       
   176 	public void addLastKnownLocation(SourceProperties source,
       
   177 			TraceLocation location, TraceLocationList list, String path, String name) {
       
   178 		TraceObject owner = list.getOwner();
       
   179 		if (owner instanceof Trace) {
       
   180 			LastKnownLocationList pll = owner
       
   181 					.getExtension(LastKnownLocationList.class);
       
   182 			if (pll == null) {
       
   183 				pll = new LastKnownLocationList();
       
   184 				owner.addExtension(pll);
       
   185 			}
       
   186 			if (path == null) {
       
   187 				path = source.getFilePath();
       
   188 			}
       
   189 			if (name == null) {
       
   190 				name = source.getFileName();
       
   191 			}			
       
   192 			if (path != null && name != null) {
       
   193 				if (new File(path + name).exists()) {
       
   194 					String cname = null;
       
   195 					String mname = null;
       
   196 					SourceContext context = source.getSourceEditor()
       
   197 							.getContext(location.getOffset());
       
   198 					if (context != null) {
       
   199 						cname = context.getClassName();
       
   200 						mname = context.getFunctionName();
       
   201 					}
       
   202 					LastKnownLocation loc = new LastKnownLocation(path, name,
       
   203 							location.getLineNumber(), cname, mname);
       
   204 					pll.addLocation(loc);
       
   205 				} else {
       
   206 					showSourceRemovedError(name);
       
   207 				}
       
   208 			}
       
   209 		}
       
   210 	}	
       
   211 	
       
   212 	/**
       
   213 	 * Shows source with traces removed error
       
   214 	 * 
       
   215 	 * @param name
       
   216 	 *            the source name
       
   217 	 */
       
   218 	private void showSourceRemovedError(String name) {
       
   219 		// TODO: Add an error code for this and move message there
       
   220 		if (!missingSourceErrorShown) {
       
   221 			missingSourceErrorShown = true;
       
   222 			String msg = Messages
       
   223 					.getString("TraceLocationMap.SourceWithTraceRemoved"); //$NON-NLS-1$
       
   224 			TraceBuilderGlobals.getEvents().postErrorMessage(msg, name, true);
       
   225 		}
       
   226 	}
       
   227 
       
   228 	/**
       
   229 	 * Removes locations from last known storage
       
   230 	 * 
       
   231 	 * @param source
       
   232 	 *            the source file
       
   233 	 */
       
   234 	private void removeFromStorage(SourceProperties source) {
       
   235 		File f = new File(source.getFilePath() + source.getFileName());
       
   236 		Iterator<TraceGroup> groups = model.getGroups();
       
   237 		while (groups.hasNext()) {
       
   238 			Iterator<Trace> traces = groups.next().getTraces();
       
   239 			while (traces.hasNext()) {
       
   240 				LastKnownLocationList list = traces.next().getExtension(
       
   241 						LastKnownLocationList.class);
       
   242 				if (list != null) {
       
   243 					Iterator<LocationProperties> locs = list.iterator();
       
   244 					while (locs.hasNext()) {
       
   245 						LastKnownLocation loc = (LastKnownLocation) locs
       
   246 								.next();
       
   247 						File locf = new File(loc.getFilePath()
       
   248 								+ loc.getFileName());
       
   249 						if (f.equals(locf)) {
       
   250 							locs.remove();
       
   251 							list.fireLocationRemoved(loc);
       
   252 						}
       
   253 					}
       
   254 				}
       
   255 			}
       
   256 		}
       
   257 	}
       
   258 
       
   259 	/**
       
   260 	 * Adds a location to trace or to the unrelated list if a trace cannot be
       
   261 	 * found.
       
   262 	 * 
       
   263 	 * @param location
       
   264 	 *            the location to be added
       
   265 	 */
       
   266 	private void addNewLocationToTrace(TraceLocation location) {
       
   267 		TraceLocationList list;
       
   268 		Trace trace = model.findTraceByName(location.getOriginalName());
       
   269 		if (trace != null) {
       
   270 			list = trace.getExtension(TraceLocationList.class);
       
   271 			if (list == null) {
       
   272 				list = new TraceLocationList();
       
   273 				trace.addExtension(list);
       
   274 			}
       
   275 		} else {
       
   276 			String name = location.getParserRule().getLocationParser()
       
   277 					.getLocationGroup();
       
   278 			if (name == null) {
       
   279 				list = unrelated;
       
   280 			} else {
       
   281 				list = parserGroups.get(name);
       
   282 				if (list == null) {
       
   283 					list = new TraceLocationList(name);
       
   284 					model.addExtension(list);
       
   285 					parserGroups.put(name, list);
       
   286 				}
       
   287 			}
       
   288 		}
       
   289 		list.addLocation(location);
       
   290 	}
       
   291 
       
   292 	/**
       
   293 	 * Checks that all locations are valid.
       
   294 	 * Posts assertion failed events if not valid
       
   295 	 */
       
   296 	private void checkGlobalValidity() {
       
   297 		if (TraceBuilderConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
   298 			boolean failure = false;
       
   299 			for (int i = 0; i < globalList.size(); i++) {
       
   300 				TraceLocation loc = globalList.get(i);
       
   301 				if (loc.isDeleted()) {
       
   302 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   303 							"Deleted location found", //$NON-NLS-1$
       
   304 							loc.getConvertedName());
       
   305 					failure = true;
       
   306 				} else if (loc.getLocationList() == null) {
       
   307 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   308 							"Unassociated location found", //$NON-NLS-1$
       
   309 							loc.getConvertedName());
       
   310 					failure = true;
       
   311 				} else if (loc.getTag() == null) {
       
   312 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   313 							"Untagged location found", loc.getConvertedName()); //$NON-NLS-1$
       
   314 					failure = true;
       
   315 				} else if (loc.getLength() <= loc.getTag().length()) {
       
   316 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   317 							"Unassociated location found", //$NON-NLS-1$
       
   318 							loc.getConvertedName());
       
   319 					failure = true;
       
   320 				}
       
   321 			}
       
   322 			if (failure) {
       
   323 				TraceBuilderGlobals.getEvents().postCriticalAssertionFailed(
       
   324 						"Invalid location(s) found", null); //$NON-NLS-1$
       
   325 			}
       
   326 		}
       
   327 	}
       
   328 
       
   329 	/**
       
   330 	 * Processes a location which has been deleted
       
   331 	 * 
       
   332 	 * @param location
       
   333 	 *            the location that was deleted
       
   334 	 */
       
   335 	private void locationDeleted(TraceLocation location) {
       
   336 		if (TraceBuilderConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
   337 			if (!globalList.remove(location)) {
       
   338 				TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   339 						"Location not in global list", //$NON-NLS-1$
       
   340 						location.getConvertedName());
       
   341 			}
       
   342 		}
       
   343 		TraceLocationList list = location.getLocationList();
       
   344 		if (list != null) {
       
   345 			list.removeLocation(location);
       
   346 		} else {
       
   347 			if (TraceBuilderConfiguration.ASSERTIONS_ENABLED) {
       
   348 				TraceBuilderGlobals.getEvents().postCriticalAssertionFailed(
       
   349 						"Unassociated location on delete", //$NON-NLS-1$
       
   350 						location.getConvertedName());
       
   351 			}
       
   352 		}
       
   353 	}
       
   354 
       
   355 	/**
       
   356 	 * Processes a location that has content changed flag
       
   357 	 * 
       
   358 	 * @param location
       
   359 	 *            the location to be processed
       
   360 	 */
       
   361 	private void locationContentChanged(TraceLocation location) {
       
   362 		// If location is new, it does not have a list
       
   363 		if (location.getLocationList() == null) {
       
   364 			if (TraceBuilderConfiguration.GLOBAL_LOCATION_ASSERTS) {
       
   365 				if (globalList.contains(location)) {
       
   366 					TraceBuilderGlobals.getEvents().postAssertionFailed(
       
   367 							"Location already in global list", //$NON-NLS-1$
       
   368 							location.getConvertedName());
       
   369 				} else {
       
   370 					globalList.add(location);
       
   371 				}
       
   372 			}
       
   373 			// Generates locationAdded event via TraceLocationListListener
       
   374 			addNewLocationToTrace(location);
       
   375 		} else if (location.isNameChanged()) {
       
   376 			// Generates locationRemoved to listeners of old list and
       
   377 			// locationAdded to listeners of new list
       
   378 			moveLocation(location);
       
   379 			// Generates content and validity changed events
       
   380 			location.notifyLocationChanged();
       
   381 		} else if (location.isContentChanged()) {
       
   382 			// Generates content and validity changed events
       
   383 			location.notifyLocationChanged();
       
   384 		}
       
   385 	}
       
   386 
       
   387 	/**
       
   388 	 * Moves a location from trace to another. Does nothing if the target trace
       
   389 	 * is same as source trace
       
   390 	 * 
       
   391 	 * @param location
       
   392 	 *            the location to be moved
       
   393 	 */
       
   394 	private void moveLocation(TraceLocation location) {
       
   395 		Trace trace = location.getTrace();
       
   396 		Trace newTrace = model.findTraceByName(location.getOriginalName());
       
   397 		// If the traces differ, this relocates the location
       
   398 		// to different location array
       
   399 		if (trace != newTrace) {
       
   400 			TraceLocationList list = location.getLocationList();
       
   401 			// Removes from existing list and adds the existing list to the
       
   402 			// updates list
       
   403 			list.removeLocation(location);
       
   404 			// Adds to new list. If new list does not exist, it is created
       
   405 			if (newTrace != null) {
       
   406 				list = newTrace.getExtension(TraceLocationList.class);
       
   407 				if (list == null) {
       
   408 					list = new TraceLocationList();
       
   409 					newTrace.addExtension(list);
       
   410 				}
       
   411 			} else {
       
   412 				list = unrelated;
       
   413 			}
       
   414 			list.addLocation(location);
       
   415 		}
       
   416 	}
       
   417 
       
   418 	/**
       
   419 	 * Returns the list of unrelated trace objects.
       
   420 	 * 
       
   421 	 * @return list of unrelated traces
       
   422 	 */
       
   423 	TraceLocationList getUnrelatedTraces() {
       
   424 		return unrelated;
       
   425 	}
       
   426 
       
   427 	/**
       
   428 	 * Removes all location lists from the model
       
   429 	 */
       
   430 	public void clearAll() {
       
   431 		model.removeExtension(unrelated);
       
   432 		Iterator<TraceGroup> groups = model.getGroups();
       
   433 		while (groups.hasNext()) {
       
   434 			TraceGroup group = groups.next();
       
   435 			Iterator<Trace> traces = group.getTraces();
       
   436 			while (traces.hasNext()) {
       
   437 				Trace trace = traces.next();
       
   438 				TraceLocationList list = trace
       
   439 						.getExtension(TraceLocationList.class);
       
   440 				trace.removeExtension(list);
       
   441 			}
       
   442 		}
       
   443 	}
       
   444 
       
   445 	/**
       
   446 	 * Moves the locations from trace to unrelated list
       
   447 	 * 
       
   448 	 * @param trace
       
   449 	 *            the trace
       
   450 	 */
       
   451 	void moveToUnrelated(Trace trace) {
       
   452 		TraceLocationList list = trace.getExtension(TraceLocationList.class);
       
   453 		if (list != null) {
       
   454 			trace.removeExtension(list);
       
   455 			for (LocationProperties loc : list) {
       
   456 				unrelated.addLocation((TraceLocation) loc);
       
   457 			}
       
   458 		}
       
   459 	}
       
   460 
       
   461 	/**
       
   462 	 * Moves locations from unrelated to the given trace
       
   463 	 * 
       
   464 	 * @param trace
       
   465 	 *            the trace
       
   466 	 */
       
   467 	void moveFromUnrelated(Trace trace) {
       
   468 		String name = trace.getName();
       
   469 		TraceLocationList list = null;
       
   470 		Iterator<LocationProperties> itr = unrelated.iterator();
       
   471 		while (itr.hasNext()) {
       
   472 			TraceLocation location = (TraceLocation) itr.next();
       
   473 			if (name.equals(location.getOriginalName())) {
       
   474 				list = trace.getExtension(TraceLocationList.class);
       
   475 				if (list == null) {
       
   476 					list = new TraceLocationList();
       
   477 					trace.addExtension(list);
       
   478 				}
       
   479 				// NOTE: This must replicate the behavior of
       
   480 				// TraceLocationList.removeLocation
       
   481 				itr.remove();
       
   482 				unrelated.fireLocationRemoved(location);
       
   483 				list.addLocation(location);
       
   484 			}
       
   485 		}
       
   486 	}
       
   487 }