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