tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/project/SortedProperties.java
changeset 56 aa2539c91954
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 * Sorted properties
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine.project;
       
    20 
       
    21 import java.io.BufferedWriter;
       
    22 import java.io.IOException;
       
    23 import java.io.OutputStream;
       
    24 import java.io.OutputStreamWriter;
       
    25 import java.util.Collections;
       
    26 import java.util.Enumeration;
       
    27 import java.util.Properties;
       
    28 import java.util.Vector;
       
    29 
       
    30 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    31 
       
    32 /**
       
    33  * Sorted properties
       
    34  * 
       
    35  */
       
    36 public final class SortedProperties extends Properties {
       
    37 
       
    38 	/**
       
    39 	 * UID
       
    40 	 */
       
    41 	private static final long serialVersionUID = 746540919659257261L;
       
    42 
       
    43 	/*
       
    44 	 * (non-Javadoc)
       
    45 	 * 
       
    46 	 * @see java.util.Hashtable#keys()
       
    47 	 */
       
    48 	@Override
       
    49 	@SuppressWarnings("unchecked")
       
    50 	public synchronized Enumeration<Object> keys() {
       
    51 		Enumeration<Object> keysEnum = super.keys();
       
    52 		Vector keyList = new Vector();
       
    53 		while (keysEnum.hasMoreElements()) {
       
    54 			keyList.add(keysEnum.nextElement());
       
    55 		}
       
    56 		Collections.sort(keyList);
       
    57 		return keyList.elements();
       
    58 	}
       
    59 
       
    60 	/*
       
    61 	 * (non-Javadoc)
       
    62 	 * 
       
    63 	 * @see java.util.Properties#store(java.io.OutputStream, java.lang.String)
       
    64 	 */
       
    65 	@Override
       
    66 	public synchronized void store(OutputStream out, String comments)
       
    67 			throws IOException {
       
    68 		BufferedWriter awriter;
       
    69 		awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1")); //$NON-NLS-1$
       
    70 		String licence = TraceCompilerEngineGlobals.getDefaultLicence(false);
       
    71 		if (licence != null) {
       
    72 			out.write(licence.getBytes());
       
    73 		}
       
    74 		if (comments != null)
       
    75 			writeln(awriter, "#" + comments); //$NON-NLS-1$
       
    76 
       
    77 		for (Enumeration<Object> e = keys(); e.hasMoreElements();) {
       
    78 			String key = (String) e.nextElement();
       
    79 			String val = (String) get(key);
       
    80 			key = saveConvert(key, true);
       
    81 
       
    82 			/*
       
    83 			 * No need to escape embedded and trailing spaces for value, hence
       
    84 			 * pass false to flag.
       
    85 			 */
       
    86 			val = saveConvert(val, false);
       
    87 			writeln(awriter, key + "=" + val); //$NON-NLS-1$
       
    88 		}
       
    89 		awriter.flush();
       
    90 	}
       
    91 
       
    92 	/**
       
    93 	 * Converts unicodes to encoded &#92;uxxxx and escapes special characters
       
    94 	 * with a preceding slash
       
    95 	 * 
       
    96 	 * @param theString
       
    97 	 *            the string to be convert
       
    98 	 * @param escapeSpace
       
    99 	 *            the escape space flag
       
   100 	 * @return the coverted string
       
   101 	 */
       
   102 	private String saveConvert(String theString, boolean escapeSpace) { // CodForChk_Dis_ComplexFunc
       
   103 		int len = theString.length();
       
   104 		int bufLen = len * 2; // CodForChk_Dis_Magic
       
   105 		if (bufLen < 0) {
       
   106 			bufLen = Integer.MAX_VALUE;
       
   107 		}
       
   108 		StringBuffer outBuffer = new StringBuffer(bufLen);
       
   109 
       
   110 		for (int x = 0; x < len; x++) {
       
   111 			char aChar = theString.charAt(x);
       
   112 			// Handle common case first, selecting largest block that
       
   113 			// avoids the specials below
       
   114 			if ((aChar > 61) && (aChar < 127)) { // CodForChk_Dis_Magic
       
   115 				if (aChar == '\\') {
       
   116 					outBuffer.append('\\');
       
   117 					outBuffer.append('\\');
       
   118 					continue;
       
   119 				}
       
   120 				outBuffer.append(aChar);
       
   121 				continue;
       
   122 			}
       
   123 			switch (aChar) {
       
   124 			case ' ':
       
   125 				if (x == 0 || escapeSpace)
       
   126 					outBuffer.append('\\');
       
   127 				outBuffer.append(' ');
       
   128 				break;
       
   129 			case '\t':
       
   130 				outBuffer.append('\\');
       
   131 				outBuffer.append('t');
       
   132 				break;
       
   133 			case '\n':
       
   134 				outBuffer.append('\\');
       
   135 				outBuffer.append('n');
       
   136 				break;
       
   137 			case '\r':
       
   138 				outBuffer.append('\\');
       
   139 				outBuffer.append('r');
       
   140 				break;
       
   141 			case '\f':
       
   142 				outBuffer.append('\\');
       
   143 				outBuffer.append('f');
       
   144 				break;
       
   145 			case '=': // Fall through
       
   146 			case ':': // Fall through
       
   147 			case '#': // Fall through
       
   148 			case '!':
       
   149 				outBuffer.append('\\');
       
   150 				outBuffer.append(aChar);
       
   151 				break;
       
   152 			default:
       
   153 				if ((aChar < 0x0020) || (aChar > 0x007e)) { // CodForChk_Dis_Magic
       
   154 					outBuffer.append('\\');
       
   155 					outBuffer.append('u');
       
   156 					outBuffer.append(toHex((aChar >> 12) & 0xF)); // CodForChk_Dis_Magic
       
   157 					outBuffer.append(toHex((aChar >> 8) & 0xF)); // CodForChk_Dis_Magic
       
   158 					outBuffer.append(toHex((aChar >> 4) & 0xF)); // CodForChk_Dis_Magic
       
   159 					outBuffer.append(toHex(aChar & 0xF)); // CodForChk_Dis_Magic
       
   160 				} else {
       
   161 					outBuffer.append(aChar);
       
   162 				}
       
   163 			}
       
   164 		}
       
   165 		return outBuffer.toString();
       
   166 	}
       
   167 
       
   168 	/**
       
   169 	 * Convert a nibble to a hex character
       
   170 	 * 
       
   171 	 * @param nibble
       
   172 	 *            the nibble to convert
       
   173 	 * @return the hex character
       
   174 	 */
       
   175 	private static char toHex(int nibble) {
       
   176 		return hexDigit[(nibble & 0xF)]; // CodForChk_Dis_Magic
       
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * A table of hex digits
       
   181 	 */
       
   182 	private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6',
       
   183 			'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
       
   184 
       
   185 	/**
       
   186 	 * Write line
       
   187 	 * 
       
   188 	 * @param bw
       
   189 	 *            the buffered writer that used to write
       
   190 	 * @param s
       
   191 	 *            the string what to write
       
   192 	 * @throws IOException
       
   193 	 */
       
   194 	private static void writeln(BufferedWriter bw, String s) throws IOException {
       
   195 		bw.write(s);
       
   196 		bw.newLine();
       
   197 	}
       
   198 }