trace/tracebuilder/com.nokia.tracebuilder/src/com/nokia/tracebuilder/engine/plugin/PluginTracePropertyVerifier.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2 * Copyright (c) 2008-2010 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 * Property verifier for trace objects
       
    17 *
       
    18 */
       
    19 package com.nokia.tracebuilder.engine.plugin;
       
    20 
       
    21 import java.io.File;
       
    22 import java.util.Iterator;
       
    23 
       
    24 import com.nokia.tracebuilder.engine.TraceBuilderGlobals;
       
    25 import com.nokia.tracebuilder.engine.TraceBuilderErrorCodes.FileErrorParameters;
       
    26 import com.nokia.tracebuilder.engine.TraceBuilderErrorCodes.RangeErrorParameters;
       
    27 import com.nokia.tracebuilder.engine.TraceBuilderErrorCodes.TraceBuilderErrorCode;
       
    28 import com.nokia.tracebuilder.model.Trace;
       
    29 import com.nokia.tracebuilder.model.TraceBuilderException;
       
    30 import com.nokia.tracebuilder.model.TraceConstantTable;
       
    31 import com.nokia.tracebuilder.model.TraceConstantTableEntry;
       
    32 import com.nokia.tracebuilder.model.TraceGroup;
       
    33 import com.nokia.tracebuilder.model.TraceModel;
       
    34 import com.nokia.tracebuilder.model.TraceObjectPropertyVerifier;
       
    35 import com.nokia.tracebuilder.model.TraceParameter;
       
    36 import com.nokia.tracebuilder.plugin.TraceBuilderPlugin;
       
    37 import com.nokia.tracebuilder.project.GroupNameHandlerBase;
       
    38 import com.nokia.tracebuilder.source.SourceConstants;
       
    39 import com.nokia.tracebuilder.source.SourceUtils;
       
    40 
       
    41 /**
       
    42  * Property verifier for trace objects
       
    43  * 
       
    44  */
       
    45 final class PluginTracePropertyVerifier implements TraceObjectPropertyVerifier {
       
    46 
       
    47 	/**
       
    48 	 * Valid data character range start
       
    49 	 */
       
    50 	private static final int DATA_CHAR_START = 0x20; // CodForChk_Dis_Magic
       
    51 
       
    52 	/**
       
    53 	 * Valid data character range end
       
    54 	 */
       
    55 	private static final int DATA_CHAR_END = 0x7E; // CodForChk_Dis_Magic
       
    56 
       
    57 	/**
       
    58 	 * Plugin engine
       
    59 	 */
       
    60 	private PluginEngine pluginEngine;
       
    61 
       
    62 	/**
       
    63 	 * Constructor
       
    64 	 * 
       
    65 	 * @param engine
       
    66 	 *            plug-in engine
       
    67 	 */
       
    68 	PluginTracePropertyVerifier(PluginEngine engine) {
       
    69 		this.pluginEngine = engine;
       
    70 	}
       
    71 
       
    72 	/*
       
    73 	 * (non-Javadoc)
       
    74 	 * 
       
    75 	 * @see com.nokia.tracebuilder.model.TraceObjectPropertyVerifier#
       
    76 	 *      checkConstantProperties(com.nokia.tracebuilder.model.TraceConstantTable,
       
    77 	 *      com.nokia.tracebuilder.model.TraceConstantTableEntry, int,
       
    78 	 *      java.lang.String)
       
    79 	 */
       
    80 	public void checkConstantProperties(TraceConstantTable table,
       
    81 			TraceConstantTableEntry entry, int id, String value)
       
    82 			throws TraceBuilderException {
       
    83 		if (!SourceUtils.isValidName(value)) {
       
    84 			throw new TraceBuilderException(
       
    85 					TraceBuilderErrorCode.INVALID_CONSTANT_VALUE);
       
    86 		}
       
    87 		if (table != null) {
       
    88 			// If table exists, the value and ID must be unique
       
    89 			TraceConstantTableEntry old = table.findEntryByID(id);
       
    90 			if (old != null && old != entry) {
       
    91 				throw new TraceBuilderException(
       
    92 						TraceBuilderErrorCode.DUPLICATE_CONSTANT_ID);
       
    93 			}
       
    94 			old = table.findEntryByName(value);
       
    95 			if (old != null && old != entry) {
       
    96 				throw new TraceBuilderException(
       
    97 						TraceBuilderErrorCode.DUPLICATE_CONSTANT_VALUE);
       
    98 			}
       
    99 		}
       
   100 		Iterator<TraceBuilderPlugin> itr = pluginEngine.getPlugins();
       
   101 		while (itr.hasNext()) {
       
   102 			TraceBuilderPlugin provider = itr.next();
       
   103 			if (provider instanceof TraceObjectPropertyVerifier) {
       
   104 				((TraceObjectPropertyVerifier) provider)
       
   105 						.checkConstantProperties(table, entry, id, value);
       
   106 			}
       
   107 		}
       
   108 	}
       
   109 
       
   110 	/*
       
   111 	 * (non-Javadoc)
       
   112 	 * 
       
   113 	 * @see com.nokia.tracebuilder.model.TraceObjectPropertyVerifier#
       
   114 	 *      checkConstantTableProperties(com.nokia.tracebuilder.model.TraceModel,
       
   115 	 *      com.nokia.tracebuilder.model.TraceConstantTable, int,
       
   116 	 *      java.lang.String)
       
   117 	 */
       
   118 	public void checkConstantTableProperties(TraceModel model,
       
   119 			TraceConstantTable table, int id, String tableName)
       
   120 			throws TraceBuilderException {
       
   121 		if (!SourceUtils.isValidName(tableName)) {
       
   122 			 throw new TraceBuilderException(
       
   123 			 TraceBuilderErrorCode.INVALID_CONSTANT_TABLE_NAME, false);
       
   124 		}
       
   125 		TraceConstantTable old = model.findConstantTableByID(id);
       
   126 		if (old != null && old != table) {
       
   127 			throw new TraceBuilderException(
       
   128 					TraceBuilderErrorCode.DUPLICATE_CONSTANT_TABLE_ID);
       
   129 		}
       
   130 		old = model.findConstantTableByName(tableName);
       
   131 		if (old != null && old != table) {
       
   132 			throw new TraceBuilderException(
       
   133 					TraceBuilderErrorCode.DUPLICATE_CONSTANT_TABLE_NAME);
       
   134 		}
       
   135 		Iterator<TraceBuilderPlugin> itr = pluginEngine.getPlugins();
       
   136 		while (itr.hasNext()) {
       
   137 			TraceBuilderPlugin provider = itr.next();
       
   138 			if (provider instanceof TraceObjectPropertyVerifier) {
       
   139 				((TraceObjectPropertyVerifier) provider)
       
   140 						.checkConstantTableProperties(model, table, id,
       
   141 								tableName);
       
   142 			}
       
   143 		}
       
   144 	}
       
   145 
       
   146 	/*
       
   147 	 * (non-Javadoc)
       
   148 	 * 
       
   149 	 * @see com.nokia.tracebuilder.model.TraceObjectPropertyVerifier#
       
   150 	 *      checkTraceGroupProperties(com.nokia.tracebuilder.model.TraceModel,
       
   151 	 *      com.nokia.tracebuilder.model.TraceGroup, int, java.lang.String)
       
   152 	 */
       
   153 	public void checkTraceGroupProperties(TraceModel model, TraceGroup group,
       
   154 			int id, String name) throws TraceBuilderException {
       
   155 		GroupNameHandlerBase groupNameHandler = TraceBuilderGlobals.getGroupNameHandler();
       
   156 		if (!SourceUtils.isValidName(name)) {
       
   157 			throw new TraceBuilderException(
       
   158 					TraceBuilderErrorCode.INVALID_GROUP_NAME);
       
   159 		} else if ((id < 0) || (id > groupNameHandler.getMaxGroupId())) {
       
   160 			RangeErrorParameters params = new RangeErrorParameters();
       
   161 			params.start = 0;
       
   162 			params.end = groupNameHandler.getMaxGroupId();
       
   163 			params.isHex = true;
       
   164 			throw new TraceBuilderException(
       
   165 					TraceBuilderErrorCode.INVALID_GROUP_ID, params);
       
   166 		} else {
       
   167 			TraceGroup old = model.findGroupByID(id);
       
   168 			if (old != null && old != group) {
       
   169 				throw new TraceBuilderException(
       
   170 						TraceBuilderErrorCode.DUPLICATE_GROUP_ID);
       
   171 			}
       
   172 			old = model.findGroupByName(name);
       
   173 			if (old != null && old != group) {
       
   174 				throw new TraceBuilderException(
       
   175 						TraceBuilderErrorCode.DUPLICATE_GROUP_NAME);
       
   176 			}
       
   177 		}
       
   178 		Iterator<TraceBuilderPlugin> itr = pluginEngine.getPlugins();
       
   179 		while (itr.hasNext()) {
       
   180 			TraceBuilderPlugin provider = itr.next();
       
   181 			if (provider instanceof TraceObjectPropertyVerifier) {
       
   182 				((TraceObjectPropertyVerifier) provider)
       
   183 						.checkTraceGroupProperties(model, group, id, name);
       
   184 			}
       
   185 		}
       
   186 	}
       
   187 
       
   188 	/*
       
   189 	 * (non-Javadoc)
       
   190 	 * 
       
   191 	 * @see com.nokia.tracebuilder.model.TraceObjectPropertyVerifier#
       
   192 	 *      checkTraceModelProperties(com.nokia.tracebuilder.model.TraceModel,
       
   193 	 *      int, java.lang.String, java.lang.String)
       
   194 	 */
       
   195 	public void checkTraceModelProperties(TraceModel model, int id,
       
   196 			String name, String path) throws TraceBuilderException {
       
   197 		if (!SourceUtils.isValidName(name)) {
       
   198 			throw new TraceBuilderException(
       
   199 					TraceBuilderErrorCode.INVALID_MODEL_NAME);
       
   200 		}
       
   201 		// Path is null when updating model
       
   202 		if (path != null) {
       
   203 			if (path.length() == 0) {
       
   204 				throw new TraceBuilderException(
       
   205 						TraceBuilderErrorCode.INVALID_PATH);
       
   206 			}
       
   207 			File f = new File(path);
       
   208 			if (f.exists() && !f.isDirectory()) {
       
   209 				FileErrorParameters fp = new FileErrorParameters();
       
   210 				fp.file = f.getAbsolutePath();
       
   211 				throw new TraceBuilderException(
       
   212 						TraceBuilderErrorCode.INVALID_PATH, fp);
       
   213 			}
       
   214 		}
       
   215 		Iterator<TraceBuilderPlugin> itr = pluginEngine.getPlugins();
       
   216 		while (itr.hasNext()) {
       
   217 			TraceBuilderPlugin provider = itr.next();
       
   218 			if (provider instanceof TraceObjectPropertyVerifier) {
       
   219 				((TraceObjectPropertyVerifier) provider)
       
   220 						.checkTraceModelProperties(model, id, name, path);
       
   221 			}
       
   222 		}
       
   223 	}
       
   224 
       
   225 	/*
       
   226 	 * (non-Javadoc)
       
   227 	 * 
       
   228 	 * @see com.nokia.tracebuilder.model.TraceObjectPropertyVerifier#
       
   229 	 *      checkTraceParameterProperties(com.nokia.tracebuilder.model.Trace,
       
   230 	 *      com.nokia.tracebuilder.model.TraceParameter, int, java.lang.String,
       
   231 	 *      java.lang.String)
       
   232 	 */
       
   233 	public void checkTraceParameterProperties(Trace owner,
       
   234 			TraceParameter parameter, int id, String name, String type)
       
   235 			throws TraceBuilderException {
       
   236 		if (!SourceUtils.isValidParameterName(name)) {
       
   237 			throw new TraceBuilderException(
       
   238 					TraceBuilderErrorCode.INVALID_PARAMETER_NAME);
       
   239 		}
       
   240 		TraceParameter old = owner.findParameterByID(id);
       
   241 		if (old != null && old != parameter) {
       
   242 			throw new TraceBuilderException(
       
   243 					TraceBuilderErrorCode.DUPLICATE_PARAMETER_ID);
       
   244 		}
       
   245 		old = owner.findParameterByName(name);
       
   246 		if (old != null && old != parameter) {
       
   247 			throw new TraceBuilderException(
       
   248 					TraceBuilderErrorCode.DUPLICATE_PARAMETER_NAME);
       
   249 		}
       
   250 		Iterator<TraceBuilderPlugin> itr = pluginEngine.getPlugins();
       
   251 		while (itr.hasNext()) {
       
   252 			TraceBuilderPlugin provider = itr.next();
       
   253 			if (provider instanceof TraceObjectPropertyVerifier) {
       
   254 				((TraceObjectPropertyVerifier) provider)
       
   255 						.checkTraceParameterProperties(owner, parameter, id,
       
   256 								name, type);
       
   257 			}
       
   258 		}
       
   259 	}
       
   260 
       
   261 	/*
       
   262 	 * (non-Javadoc)
       
   263 	 * 
       
   264 	 * @see com.nokia.tracebuilder.model.TraceObjectPropertyVerifier#
       
   265 	 *      checkTraceProperties(com.nokia.tracebuilder.model.TraceGroup,
       
   266 	 *      com.nokia.tracebuilder.model.Trace, int, java.lang.String,
       
   267 	 *      java.lang.String)
       
   268 	 */
       
   269 	public void checkTraceProperties(TraceGroup group, Trace trace, int id,
       
   270 			String name, String data) throws TraceBuilderException {
       
   271 		if (!isValidData(data)) {
       
   272 			throw new TraceBuilderException(
       
   273 					TraceBuilderErrorCode.INVALID_TRACE_DATA);
       
   274 		} else if (!SourceUtils.isValidName(name)) {
       
   275 			throw new TraceBuilderException(
       
   276 					TraceBuilderErrorCode.INVALID_TRACE_NAME);
       
   277 		} else if ((id < 0) || (id > TraceBuilderGlobals.MAX_TRACE_ID)) {
       
   278 			RangeErrorParameters params = new RangeErrorParameters();
       
   279 			params.start = 0;
       
   280 			params.end = TraceBuilderGlobals.MAX_TRACE_ID;
       
   281 			params.isHex = true;
       
   282 			throw new TraceBuilderException(
       
   283 					TraceBuilderErrorCode.INVALID_TRACE_ID, params);
       
   284 		} else {
       
   285 			// Verifies the trace name is globally unique
       
   286 			Trace old = TraceBuilderGlobals.getTraceModel().findTraceByName(
       
   287 					name);
       
   288 			if (old != trace && old != null) {
       
   289 				throw new TraceBuilderException(
       
   290 						TraceBuilderErrorCode.DUPLICATE_TRACE_NAME);
       
   291 			}
       
   292 			if (group != null) {
       
   293 				// If group exists, the trace ID and text must be unique within
       
   294 				// the group
       
   295 				old = group.findTraceByID(id);
       
   296 				if (old != trace && old != null) {
       
   297 					// Trace ID's must be unique within group
       
   298 					throw new TraceBuilderException(
       
   299 							TraceBuilderErrorCode.DUPLICATE_TRACE_ID);
       
   300 				}
       
   301 			}
       
   302 		}
       
   303 		Iterator<TraceBuilderPlugin> itr = pluginEngine.getPlugins();
       
   304 		while (itr.hasNext()) {
       
   305 			TraceBuilderPlugin provider = itr.next();
       
   306 			if (provider instanceof TraceObjectPropertyVerifier) {
       
   307 				((TraceObjectPropertyVerifier) provider).checkTraceProperties(
       
   308 						group, trace, id, name, data);
       
   309 			}
       
   310 		}
       
   311 	}
       
   312 
       
   313 	/**
       
   314 	 * Checks the validity of data
       
   315 	 * 
       
   316 	 * @param data
       
   317 	 *            the data
       
   318 	 * @return true if valid
       
   319 	 */
       
   320 	private boolean isValidData(String data) {
       
   321 		boolean retval;
       
   322 		if (data != null) {
       
   323 			retval = true;
       
   324 			for (int i = 0; i < data.length() && retval; i++) {
       
   325 				char c = data.charAt(i);
       
   326 				// Unescaped quotes are not allowed
       
   327 				if (c == SourceConstants.QUOTE_CHAR
       
   328 						&& (i == 0 || data.charAt(i - 1) != SourceConstants.BACKSLASH_CHAR)) {
       
   329 					retval = false;
       
   330 				} else {
       
   331 					retval = isValidDataChar(c);
       
   332 				}
       
   333 			}
       
   334 		} else {
       
   335 			retval = false;
       
   336 		}
       
   337 		return retval;
       
   338 	}
       
   339 
       
   340 	/**
       
   341 	 * Checks data character validity
       
   342 	 * 
       
   343 	 * @param c
       
   344 	 *            character
       
   345 	 * @return true if valid
       
   346 	 */
       
   347 	private boolean isValidDataChar(char c) {
       
   348 		// Special and extended characters are not allowed
       
   349 		return c >= DATA_CHAR_START && c <= DATA_CHAR_END;
       
   350 	}
       
   351 
       
   352 }