tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/decodeplugins/dictionary/encoder/TagWriter.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     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 * Tag writer
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.decodeplugins.dictionary.encoder;
       
    20 
       
    21 import java.io.BufferedOutputStream;
       
    22 import java.io.FileOutputStream;
       
    23 import java.io.IOException;
       
    24 import java.io.OutputStream;
       
    25 import java.io.OutputStreamWriter;
       
    26 
       
    27 /**
       
    28  * Tag writer
       
    29  * 
       
    30  */
       
    31 class TagWriter {
       
    32 
       
    33 	/**
       
    34 	 * 
       
    35 	 */
       
    36 	private static final String OUTPUT_FORMAT_UTF8 = "UTF-8"; //$NON-NLS-1$
       
    37 
       
    38 	/**
       
    39 	 * 
       
    40 	 */
       
    41 	private static OutputStreamWriter m_writer = null;
       
    42 
       
    43 	/**
       
    44 	 * 
       
    45 	 */
       
    46 	static final String XML_START = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
       
    47 
       
    48 	/**
       
    49 	 * 
       
    50 	 */
       
    51 	static final String CLASSIFICATION_ATTR = "classification="; //$NON-NLS-1$
       
    52 
       
    53 	/**
       
    54 	 * 
       
    55 	 */
       
    56 	static final String CLASSNAME_ATTR = "classname="; //$NON-NLS-1$
       
    57 
       
    58 	/**
       
    59 	 * 
       
    60 	 */
       
    61 	static final String COMPONENT_START_OPEN = "<component "; //$NON-NLS-1$
       
    62 
       
    63 	/**
       
    64 	 * 
       
    65 	 */
       
    66 	static final String COMPONENT_END = "</component>"; //$NON-NLS-1$
       
    67 
       
    68 	/**
       
    69 	 * 
       
    70 	 */
       
    71 	static final String DATA_REF_ATTR = "data-ref="; //$NON-NLS-1$
       
    72 
       
    73 	/**
       
    74 	 * 
       
    75 	 */
       
    76 	static final String DATASTORE_START = "<datastore>"; //$NON-NLS-1$
       
    77 
       
    78 	/**
       
    79 	 * 
       
    80 	 */
       
    81 	static final String DATASTORE_END = "<datastore>"; //$NON-NLS-1$
       
    82 
       
    83 	/**
       
    84 	 * 
       
    85 	 */
       
    86 	static final String DEF_END = "</def>"; //$NON-NLS-1$
       
    87 
       
    88 	/**
       
    89 	 * 
       
    90 	 */
       
    91 	static final String DEF_START_OPEN = "<def "; //$NON-NLS-1$
       
    92 
       
    93 	/**
       
    94 	 * 
       
    95 	 */
       
    96 	static final String DICTIONARY_START = "<tracedictionary xmlns:xsi=" + //$NON-NLS-1$
       
    97 			"\"http://www.w3.org/2001/XMLSchema-instance\" " + //$NON-NLS-1$
       
    98 			"xsi:noNamespaceSchemaLocation=\"DictionarySchema.xsd\">";//$NON-NLS-1$
       
    99 
       
   100 	/**
       
   101 	 * 
       
   102 	 */
       
   103 	static final String DICTIONARY_END = "</tracedictionary>"; //$NON-NLS-1$
       
   104 
       
   105 	/**
       
   106 	 * 
       
   107 	 */
       
   108 	static final String END_TAG_OPEN = "</"; //$NON-NLS-1$
       
   109 
       
   110 	/**
       
   111 	 * 
       
   112 	 */
       
   113 	static final String EXTERNAL_DEF_END = "</external-def>"; //$NON-NLS-1$
       
   114 
       
   115 	/**
       
   116 	 * 
       
   117 	 */
       
   118 	static final String EXTERNAL_DEF_START = "<external-def>"; //$NON-NLS-1$
       
   119 
       
   120 	/**
       
   121 	 * 
       
   122 	 */
       
   123 	static final String FILE_START_OPEN = "<file "; //$NON-NLS-1$
       
   124 
       
   125 	/**
       
   126 	 * 
       
   127 	 */
       
   128 	static final String FILE_END = "</file>"; //$NON-NLS-1$
       
   129 
       
   130 	/**
       
   131 	 * 
       
   132 	 */
       
   133 	static final String FORMATCHAR_ATTR = "formatchar="; //$NON-NLS-1$
       
   134 
       
   135 	/**
       
   136 	 * 
       
   137 	 */
       
   138 	static final String GROUP_END = "</group>"; //$NON-NLS-1$
       
   139 
       
   140 	/**
       
   141 	 * 
       
   142 	 */
       
   143 	static final String GROUP_START_OPEN = "<group "; //$NON-NLS-1$
       
   144 
       
   145 	/**
       
   146 	 * 
       
   147 	 */
       
   148 	static final String ID_ATTR = "id="; //$NON-NLS-1$
       
   149 
       
   150 	/**
       
   151 	 * 
       
   152 	 */
       
   153 	static final String INSTANCE_START_OPEN = "<instance "; //$NON-NLS-1$
       
   154 
       
   155 	/**
       
   156 	 * 
       
   157 	 */
       
   158 	static final String INSTANCE_END = "</instance>"; //$NON-NLS-1$
       
   159 
       
   160 	/**
       
   161 	 * 
       
   162 	 */
       
   163 	static final String LINE_ATTR = "line="; //$NON-NLS-1$
       
   164 
       
   165 	/**
       
   166 	 * 
       
   167 	 */
       
   168 	static final String LOC_REF_ATTR = "loc-ref="; //$NON-NLS-1$
       
   169 
       
   170 	/**
       
   171 	 * 
       
   172 	 */
       
   173 	static final String LOCATIONS_START = "<locations>"; //$NON-NLS-1$
       
   174 
       
   175 	/**
       
   176 	 * 
       
   177 	 */
       
   178 	static final String LOCATIONS_END = "</locations>"; //$NON-NLS-1$
       
   179 
       
   180 	/**
       
   181 	 * 
       
   182 	 */
       
   183 	static final String METHODNAME_ATTR = "methodname="; //$NON-NLS-1$
       
   184 
       
   185 	/**
       
   186 	 * 
       
   187 	 */
       
   188 	static final String METADATA_START = "<metadata>"; //$NON-NLS-1$
       
   189 
       
   190 	/**
       
   191 	 * 
       
   192 	 */
       
   193 	static final String METADATA_END = "</metadata>"; //$NON-NLS-1$
       
   194 
       
   195 	/**
       
   196 	 * 
       
   197 	 */
       
   198 	static final String NAME_ATTR = "name="; //$NON-NLS-1$
       
   199 
       
   200 	/**
       
   201 	 * 
       
   202 	 */
       
   203 	static final String NAME_START = "<name>"; //$NON-NLS-1$
       
   204 
       
   205 	/**
       
   206 	 * 
       
   207 	 */
       
   208 	static final String NAME_END = "</name>"; //$NON-NLS-1$
       
   209 
       
   210 	/**
       
   211 	 * 
       
   212 	 */
       
   213 	static final String OBJECT_END = "</object>"; //$NON-NLS-1$
       
   214 
       
   215 	/**
       
   216 	 * 
       
   217 	 */
       
   218 	static final String OBJECT_START = "<object>"; //$NON-NLS-1$
       
   219 
       
   220 	/**
       
   221 	 * 
       
   222 	 */
       
   223 	static final String OBJECT_START_OPEN = "<object "; //$NON-NLS-1$
       
   224 
       
   225 	/**
       
   226 	 * 
       
   227 	 */
       
   228 	static final String OPTIONS_START = "<options>"; //$NON-NLS-1$
       
   229 
       
   230 	/**
       
   231 	 * 
       
   232 	 */
       
   233 	static final String OPTIONS_START_OPEN = "<options "; //$NON-NLS-1$
       
   234 
       
   235 	/**
       
   236 	 * 
       
   237 	 */
       
   238 	static final String OPTIONS_END = "</options>"; //$NON-NLS-1$
       
   239 
       
   240 	/**
       
   241 	 * 
       
   242 	 */
       
   243 	static final String PATH_START_OPEN = "<path "; //$NON-NLS-1$
       
   244 
       
   245 	/**
       
   246 	 * 
       
   247 	 */
       
   248 	static final String PATH_END = "</path>"; //$NON-NLS-1$
       
   249 
       
   250 	/**
       
   251 	 * 
       
   252 	 */
       
   253 	static final String PREFIX_ATTR = "prefix="; //$NON-NLS-1$
       
   254 
       
   255 	/**
       
   256 	 * 
       
   257 	 */
       
   258 	static final String QUOTE = "\""; //$NON-NLS-1$
       
   259 
       
   260 	/**
       
   261 	 * 
       
   262 	 */
       
   263 	static final String ROW_CHANGE = "\n"; //$NON-NLS-1$
       
   264 
       
   265 	/**
       
   266 	 * 
       
   267 	 */
       
   268 	static final String SIZE_ATTR = "size="; //$NON-NLS-1$
       
   269 
       
   270 	/**
       
   271 	 * 
       
   272 	 */
       
   273 	static final String SPACE = " "; //$NON-NLS-1$
       
   274 
       
   275 	/**
       
   276 	 * 
       
   277 	 */
       
   278 	static final String START_TAG_OPEN = "<"; //$NON-NLS-1$
       
   279 
       
   280 	/**
       
   281 	 * 
       
   282 	 */
       
   283 	static final String SUFFIX_ATTR = "suffix="; //$NON-NLS-1$
       
   284 
       
   285 	/**
       
   286 	 * 
       
   287 	 */
       
   288 	static final String TAB = "\t"; //$NON-NLS-1$
       
   289 
       
   290 	/**
       
   291 	 * 
       
   292 	 */
       
   293 	static final String TAG_CLOSE = ">"; //$NON-NLS-1$
       
   294 
       
   295 	/**
       
   296 	 * 
       
   297 	 */
       
   298 	static final String TAG_END = "/>"; //$NON-NLS-1$
       
   299 
       
   300 	/**
       
   301 	 * 
       
   302 	 */
       
   303 	static final String TRACE_END = "</trace>"; //$NON-NLS-1$
       
   304 
       
   305 	/**
       
   306 	 * 
       
   307 	 */
       
   308 	static final String TRACE_START = "<trace>"; //$NON-NLS-1$
       
   309 
       
   310 	/**
       
   311 	 * 
       
   312 	 */
       
   313 	static final String TRACE_START_OPEN = "<trace "; //$NON-NLS-1$
       
   314 
       
   315 	/**
       
   316 	 * 
       
   317 	 */
       
   318 	static final String TRACE_DATA_END = "</data>"; //$NON-NLS-1$
       
   319 
       
   320 	/**
       
   321 	 * 
       
   322 	 */
       
   323 	static final String TRACE_DATA_START = "<data>"; //$NON-NLS-1$
       
   324 
       
   325 	/**
       
   326 	 * 
       
   327 	 */
       
   328 	static final String TYPE_ATTR = "type="; //$NON-NLS-1$
       
   329 
       
   330 	/**
       
   331 	 * 
       
   332 	 */
       
   333 	static final String TYPEDEFS_START = "<typedefs>"; //$NON-NLS-1$
       
   334 
       
   335 	/**
       
   336 	 * 
       
   337 	 */
       
   338 	static final String TYPEDEFS_END = "</typedefs>"; //$NON-NLS-1$
       
   339 
       
   340 	/**
       
   341 	 * 
       
   342 	 */
       
   343 	static final String TYPEMEMBER_END = "</typemember>"; //$NON-NLS-1$
       
   344 
       
   345 	/**
       
   346 	 * 
       
   347 	 */
       
   348 	static final String TYPEMEMBER_START = "<typemember>"; //$NON-NLS-1$
       
   349 
       
   350 	/**
       
   351 	 * 
       
   352 	 */
       
   353 	static final String TYPEMEMBER_START_OPEN = "<typemember "; //$NON-NLS-1$
       
   354 
       
   355 	/**
       
   356 	 * 
       
   357 	 */
       
   358 	static final String VAL_ATTR = "val="; //$NON-NLS-1$
       
   359 
       
   360 	/**
       
   361 	 * 
       
   362 	 */
       
   363 	static final String VALUE_ATTR = "value="; //$NON-NLS-1$
       
   364 
       
   365 	/**
       
   366 	 * 
       
   367 	 */
       
   368 	static final String VALUE_START = "<value>"; //$NON-NLS-1$
       
   369 
       
   370 	/**
       
   371 	 * 
       
   372 	 */
       
   373 	static final String VALUE_END = "</value>"; //$NON-NLS-1$
       
   374 
       
   375 	/**
       
   376 	 * 
       
   377 	 */
       
   378 	static final int INDENT_0 = 0;
       
   379 
       
   380 	/**
       
   381 	 * 
       
   382 	 */
       
   383 	static final int INDENT_1 = 1;
       
   384 
       
   385 	/**
       
   386 	 * 
       
   387 	 */
       
   388 	static final int INDENT_2 = 2; // CodForChk_Dis_Magic
       
   389 
       
   390 	/**
       
   391 	 * 
       
   392 	 */
       
   393 	static final int INDENT_3 = 3;// CodForChk_Dis_Magic
       
   394 
       
   395 	/**
       
   396 	 * 
       
   397 	 */
       
   398 	static final int INDENT_4 = 4;// CodForChk_Dis_Magic
       
   399 
       
   400 	/**
       
   401 	 * 
       
   402 	 */
       
   403 	static final int INDENT_5 = 5;// CodForChk_Dis_Magic
       
   404 
       
   405 	/**
       
   406 	 * 
       
   407 	 */
       
   408 	static final int INDENT_6 = 6;// CodForChk_Dis_Magic
       
   409 
       
   410 	/**
       
   411 	 * Initializes TagWriter
       
   412 	 * 
       
   413 	 * @param filePath
       
   414 	 *            name of the output file
       
   415 	 */
       
   416 	static void initialize(String filePath) {
       
   417 		try {
       
   418 			OutputStream fout = new FileOutputStream(filePath);
       
   419 			OutputStream bout = new BufferedOutputStream(fout);
       
   420 			m_writer = new OutputStreamWriter(bout, OUTPUT_FORMAT_UTF8);
       
   421 		} catch (java.io.IOException e) {
       
   422 			e.printStackTrace();
       
   423 		}
       
   424 	}
       
   425 
       
   426 	/**
       
   427 	 * Writes tag to XML file
       
   428 	 * 
       
   429 	 * @param tag
       
   430 	 *            name of the tag
       
   431 	 * @param indent
       
   432 	 *            amount of tabs for indent
       
   433 	 */
       
   434 	static void write(String tag, int indent) {
       
   435 		try {
       
   436 			StringBuffer sb = new StringBuffer();
       
   437 			for (int i = 0; i < indent; i++) {
       
   438 				sb.append(TAB);
       
   439 			}
       
   440 			sb.append(tag);
       
   441 			sb.append(ROW_CHANGE);
       
   442 			m_writer.write(sb.toString());
       
   443 		} catch (IOException e) {
       
   444 			e.printStackTrace();
       
   445 		}
       
   446 	}
       
   447 
       
   448 	/**
       
   449 	 * Appends attribute to given StringBuffer
       
   450 	 * 
       
   451 	 * @param sb
       
   452 	 *            buffer to be updated
       
   453 	 * @param attribute
       
   454 	 *            attribute to append
       
   455 	 * @param value
       
   456 	 *            value of the attribute
       
   457 	 * @param withSpace
       
   458 	 *            is there leading space before attribute
       
   459 	 */
       
   460 	static void appendAttribute(StringBuffer sb, String attribute,
       
   461 			String value, boolean withSpace) {
       
   462 		if (value != null) {
       
   463 			if (withSpace) {
       
   464 				sb.append(SPACE);
       
   465 			}
       
   466 			sb.append(attribute);
       
   467 			sb.append(QUOTE);
       
   468 			sb.append(value);
       
   469 			sb.append(QUOTE);
       
   470 		}
       
   471 	}
       
   472 
       
   473 	/**
       
   474 	 * Closes TagWriter
       
   475 	 */
       
   476 	static void close() {
       
   477 		try {
       
   478 			m_writer.flush();
       
   479 		} catch (IOException e) {
       
   480 			e.printStackTrace();
       
   481 		}
       
   482 
       
   483 		try {
       
   484 			m_writer.close();
       
   485 		} catch (IOException e) {
       
   486 			e.printStackTrace();
       
   487 		}
       
   488 	}
       
   489 }