tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/utils/TraceUtils.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) 2007 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 * Utility functions related to traces
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine.utils;
       
    20 
       
    21 import com.nokia.tracecompiler.plugin.TraceFormatConstants;
       
    22 
       
    23 /**
       
    24  * Utility functions related to traces
       
    25  * 
       
    26  */
       
    27 public final class TraceUtils {
       
    28 
       
    29 	/**
       
    30 	 * Underscore character
       
    31 	 */
       
    32 	private static final String UNDERSCORE = "_"; //$NON-NLS-1$
       
    33 
       
    34 	/**
       
    35 	 * Underscore character
       
    36 	 */
       
    37 	private static final char UNDERSCORE_CHAR = '_';
       
    38 
       
    39 	/**
       
    40 	 * Cannot be constructed
       
    41 	 */
       
    42 	private TraceUtils() {
       
    43 	}
       
    44 
       
    45 	/**
       
    46 	 * Formats a trace
       
    47 	 * 
       
    48 	 * @param format
       
    49 	 *            the format specification
       
    50 	 * @param cname
       
    51 	 *            the class name
       
    52 	 * @param fname
       
    53 	 *            the function name
       
    54 	 * @return the formatted trace
       
    55 	 */
       
    56 	public static String formatTrace(String format, String cname, String fname) {
       
    57 		StringBuffer sb = new StringBuffer(format);
       
    58 		int cnindex = sb
       
    59 				.indexOf(TraceFormatConstants.FORMAT_CLASS_NAME_NORMAL_CASE);
       
    60 		if (cnindex >= 0) {
       
    61 			if (cname != null) {
       
    62 				sb.replace(cnindex, cnindex
       
    63 						+ TraceFormatConstants.FORMAT_CLASS_NAME_NORMAL_CASE
       
    64 								.length(), cname);
       
    65 			} else {
       
    66 				sb.replace(cnindex, cnindex
       
    67 						+ TraceFormatConstants.FORMAT_CLASS_NAME_NORMAL_CASE
       
    68 								.length(), ""); //$NON-NLS-1$
       
    69 			}
       
    70 		}
       
    71 		int cnup = sb
       
    72 				.indexOf(TraceFormatConstants.FORMAT_CLASS_NAME_UPPER_CASE);
       
    73 		if (cnup >= 0) {
       
    74 			if (cname != null) {
       
    75 				sb.replace(cnup, cnup
       
    76 						+ TraceFormatConstants.FORMAT_CLASS_NAME_UPPER_CASE
       
    77 								.length(), cname.toUpperCase());
       
    78 			} else {
       
    79 				sb.replace(cnup, cnup
       
    80 						+ TraceFormatConstants.FORMAT_CLASS_NAME_UPPER_CASE
       
    81 								.length(), ""); //$NON-NLS-1$
       
    82 			}
       
    83 		}
       
    84 		int fnindex = sb
       
    85 				.indexOf(TraceFormatConstants.FORMAT_FUNCTION_NAME_NORMAL_CASE);
       
    86 		if (fnindex >= 0) {
       
    87 			sb.replace(fnindex, fnindex
       
    88 					+ TraceFormatConstants.FORMAT_FUNCTION_NAME_NORMAL_CASE
       
    89 							.length(), fname);
       
    90 		}
       
    91 		int fnup = sb
       
    92 				.indexOf(TraceFormatConstants.FORMAT_FUNCTION_NAME_UPPER_CASE);
       
    93 		if (fnup >= 0) {
       
    94 			sb.replace(fnup, fnup
       
    95 					+ TraceFormatConstants.FORMAT_FUNCTION_NAME_UPPER_CASE
       
    96 							.length(), fname.toUpperCase());
       
    97 		}
       
    98 		return sb.toString();
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Replaces invalid characters with '_'
       
   103 	 * 
       
   104 	 * @param name
       
   105 	 *            name to be converted
       
   106 	 * @return the converted name
       
   107 	 */
       
   108 	public static String convertName(String name) {
       
   109 		StringBuffer sb;
       
   110 		if (name.length() > 0) {
       
   111 			boolean underscore = false;
       
   112 			sb = new StringBuffer(name);
       
   113 			if (Character.isDigit(name.charAt(0))) {
       
   114 				sb.insert(0, UNDERSCORE_CHAR);
       
   115 			}
       
   116 			for (int i = 0; i < sb.length(); i++) {
       
   117 				char c = sb.charAt(i);
       
   118 				if (!Character.isLetterOrDigit(c)) {
       
   119 					if (!underscore) {
       
   120 						sb.setCharAt(i, UNDERSCORE_CHAR);
       
   121 						underscore = true;
       
   122 					} else {
       
   123 						sb.deleteCharAt(i);
       
   124 						i--;
       
   125 					}
       
   126 				} else {
       
   127 					underscore = false;
       
   128 				}
       
   129 			}
       
   130 			if (sb.length() > 0) {
       
   131 				if (sb.charAt(sb.length() - 1) == UNDERSCORE_CHAR) {
       
   132 					sb.deleteCharAt(sb.length() - 1);
       
   133 				}
       
   134 			} else {
       
   135 				sb.append(UNDERSCORE);
       
   136 			}
       
   137 		} else {
       
   138 			sb = new StringBuffer();
       
   139 		}
       
   140 		// If parameter value is NULL, it would be used as name
       
   141 		String s = sb.toString();
       
   142 		if (s.equals("NULL")) { //$NON-NLS-1$
       
   143 			s = "_NULL"; //$NON-NLS-1$
       
   144 		}
       
   145 		return s;
       
   146 	}
       
   147 }