trace/traceviewer/com.nokia.trace.dictionary/src/com/nokia/trace/dictionary/model/DictionaryModelBuilder.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-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  * Dictionary Model Builder
       
    17  *
       
    18  */
       
    19 package com.nokia.trace.dictionary.model;
       
    20 
       
    21 import java.io.BufferedInputStream;
       
    22 import java.io.FileInputStream;
       
    23 import java.io.FileNotFoundException;
       
    24 import java.io.InputStream;
       
    25 import java.util.ArrayList;
       
    26 
       
    27 import com.nokia.trace.dictionary.TraceDictionaryEngine;
       
    28 import com.nokia.trace.dictionary.model.handlers.DictionaryHandler;
       
    29 import com.nokia.trace.eventrouter.TraceEvent;
       
    30 
       
    31 /**
       
    32  * Dictionary Model Builder
       
    33  */
       
    34 public class DictionaryModelBuilder {
       
    35 
       
    36 	/**
       
    37 	 * Allowed extension for files
       
    38 	 */
       
    39 	private static final String ALLOWED_EXTENSION = ".xml"; //$NON-NLS-1$
       
    40 
       
    41 	/**
       
    42 	 * Category for events
       
    43 	 */
       
    44 	private final static String EVENT_CATEGORY = "Dictionary Model Builder"; //$NON-NLS-1$
       
    45 
       
    46 	/**
       
    47 	 * Sleeping time when waiting model to be ready
       
    48 	 */
       
    49 	private static final long WAIT_FOR_MODEL_READY_TIME = 5;
       
    50 
       
    51 	/**
       
    52 	 * Model to build
       
    53 	 */
       
    54 	private DictionaryDecodeModel model;
       
    55 
       
    56 	/**
       
    57 	 * Current file opening
       
    58 	 */
       
    59 	private static String currentFile;
       
    60 
       
    61 	/**
       
    62 	 * Metadata handlers
       
    63 	 */
       
    64 	private static ArrayList<DictionaryHandler> metadataHandlers = new ArrayList<DictionaryHandler>();
       
    65 
       
    66 	/**
       
    67 	 * Constructor
       
    68 	 * 
       
    69 	 * @param model
       
    70 	 *            the trace model
       
    71 	 */
       
    72 	public DictionaryModelBuilder(DictionaryDecodeModel model) {
       
    73 		this.model = model;
       
    74 	}
       
    75 
       
    76 	/**
       
    77 	 * Opens decode file
       
    78 	 * 
       
    79 	 * @param filePath
       
    80 	 *            file path. Must end with .xml to be loaded
       
    81 	 * @param fileStream
       
    82 	 *            file stream. If null, file stream is created by opening a file
       
    83 	 *            from filePath variable
       
    84 	 * @param createNew
       
    85 	 *            true if creating new model, false if not
       
    86 	 */
       
    87 	public void openDecodeFile(String filePath, InputStream fileStream,
       
    88 			boolean createNew) {
       
    89 		// If there if parsing going on, wait for it to finish
       
    90 		waitForPreviousParse();
       
    91 
       
    92 		// Set current file and check the extension
       
    93 		currentFile = filePath;
       
    94 		if (!filePath.trim().toLowerCase().endsWith(ALLOWED_EXTENSION)) {
       
    95 			postErrorMessage(Messages
       
    96 					.getString("DictionaryModelBuilder.FileSkipped"), //$NON-NLS-1$
       
    97 					filePath);
       
    98 		} else {
       
    99 
       
   100 			// If file stream doesn't exist, create if from the file
       
   101 			if (fileStream == null) {
       
   102 				fileStream = createFileStream(filePath);
       
   103 			}
       
   104 
       
   105 			// File stream must now exist or quit
       
   106 			if (fileStream != null) {
       
   107 
       
   108 				// Creating new model so erase old one first
       
   109 				if (createNew) {
       
   110 					model.clearModel();
       
   111 				}
       
   112 
       
   113 				// Model is not valid anymore
       
   114 				model.setValid(false);
       
   115 
       
   116 				// Create new thread for parsing
       
   117 				DictionaryModelParserThread parseThread = new DictionaryModelParserThread(
       
   118 						fileStream, filePath, model, metadataHandlers);
       
   119 
       
   120 				// Start parsing
       
   121 				parseThread.start();
       
   122 			}
       
   123 		}
       
   124 	}
       
   125 
       
   126 	/**
       
   127 	 * Wait for previous parse
       
   128 	 */
       
   129 	private void waitForPreviousParse() {
       
   130 		try {
       
   131 			while (!model.isValid()) {
       
   132 
       
   133 				// Model not valid, wait
       
   134 				Thread.sleep(WAIT_FOR_MODEL_READY_TIME);
       
   135 			}
       
   136 		} catch (InterruptedException e) {
       
   137 		}
       
   138 	}
       
   139 
       
   140 	/**
       
   141 	 * Creates file stream from file path
       
   142 	 * 
       
   143 	 * @param filePath
       
   144 	 *            file path
       
   145 	 * @return file input stream
       
   146 	 */
       
   147 	private InputStream createFileStream(String filePath) {
       
   148 		BufferedInputStream bin = null;
       
   149 		try {
       
   150 			FileInputStream fin = new FileInputStream(filePath);
       
   151 			bin = new BufferedInputStream(fin);
       
   152 		} catch (FileNotFoundException e) {
       
   153 			postErrorMessage(e.toString(), filePath);
       
   154 			model.setValid(true);
       
   155 		}
       
   156 		return bin;
       
   157 	}
       
   158 
       
   159 	/**
       
   160 	 * Post error message
       
   161 	 * 
       
   162 	 * @param string
       
   163 	 *            error message
       
   164 	 * @param fileName
       
   165 	 *            file name
       
   166 	 */
       
   167 	private void postErrorMessage(String string, String fileName) {
       
   168 		TraceEvent event = new TraceEvent(TraceEvent.ERROR, string);
       
   169 		event.setCategory(EVENT_CATEGORY);
       
   170 		event.setSource(fileName);
       
   171 		TraceDictionaryEngine.postEvent(event);
       
   172 	}
       
   173 
       
   174 	/**
       
   175 	 * Gets the file path of the dictionary file that is currently being parsed
       
   176 	 * 
       
   177 	 * @return the file path of the dictionary file that is currently beging
       
   178 	 *         parsed
       
   179 	 */
       
   180 	public static String getCurrentFile() {
       
   181 		return currentFile;
       
   182 	}
       
   183 
       
   184 	/**
       
   185 	 * Adds a metadata handler
       
   186 	 * 
       
   187 	 * @param metadataHandler
       
   188 	 *            the handler to be added
       
   189 	 */
       
   190 	public static void addMetadataHandler(DictionaryHandler metadataHandler) {
       
   191 		metadataHandlers.add(metadataHandler);
       
   192 	}
       
   193 }