tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/document/FileDocumentMonitor.java
author hgs
Fri, 08 Oct 2010 14:56:39 +0300
changeset 56 aa2539c91954
parent 41 tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/document/FileDocumentMonitor.java@838cdffd57ce
permissions -rw-r--r--
201041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). 
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
* Document monitor implementation which reads all files from a directory
hgs
parents:
diff changeset
    17
*
hgs
parents:
diff changeset
    18
*/
hgs
parents:
diff changeset
    19
package com.nokia.tracecompiler.document;
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
import java.io.File;
hgs
parents:
diff changeset
    22
import java.util.ArrayList;
hgs
parents:
diff changeset
    23
hgs
parents:
diff changeset
    24
import com.nokia.tracecompiler.TraceCompilerGlobals;
hgs
parents:
diff changeset
    25
import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages;
hgs
parents:
diff changeset
    26
import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
hgs
parents:
diff changeset
    27
import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
hgs
parents:
diff changeset
    28
import com.nokia.tracecompiler.engine.source.SourceEngine;
hgs
parents:
diff changeset
    29
import com.nokia.tracecompiler.file.FileUtils;
hgs
parents:
diff changeset
    30
import com.nokia.tracecompiler.source.SourceDocumentFactory;
hgs
parents:
diff changeset
    31
import com.nokia.tracecompiler.source.SourceDocumentProcessor;
hgs
parents:
diff changeset
    32
import com.nokia.tracecompiler.utils.DocumentMonitorBase;
hgs
parents:
diff changeset
    33
hgs
parents:
diff changeset
    34
/**
hgs
parents:
diff changeset
    35
 * Document monitor implementation which reads files
hgs
parents:
diff changeset
    36
 * 
hgs
parents:
diff changeset
    37
 */
hgs
parents:
diff changeset
    38
public final class FileDocumentMonitor extends DocumentMonitorBase {
hgs
parents:
diff changeset
    39
hgs
parents:
diff changeset
    40
	/**
hgs
parents:
diff changeset
    41
	 * Document factory
hgs
parents:
diff changeset
    42
	 */
hgs
parents:
diff changeset
    43
	private StringDocumentFactory documentFactory;
hgs
parents:
diff changeset
    44
hgs
parents:
diff changeset
    45
	/**
hgs
parents:
diff changeset
    46
	 * Files to be processed
hgs
parents:
diff changeset
    47
	 */
hgs
parents:
diff changeset
    48
	private static String[] files;
hgs
parents:
diff changeset
    49
hgs
parents:
diff changeset
    50
	/**
hgs
parents:
diff changeset
    51
	 * Sets the files to be used by the document monitor
hgs
parents:
diff changeset
    52
	 * 
hgs
parents:
diff changeset
    53
	 * @param files
hgs
parents:
diff changeset
    54
	 *            the files
hgs
parents:
diff changeset
    55
	 */
hgs
parents:
diff changeset
    56
	public static void setFiles(String[] files) {
hgs
parents:
diff changeset
    57
		FileDocumentMonitor.files = files;
hgs
parents:
diff changeset
    58
	}
hgs
parents:
diff changeset
    59
hgs
parents:
diff changeset
    60
	/**
hgs
parents:
diff changeset
    61
	 * Constructor
hgs
parents:
diff changeset
    62
	 */
hgs
parents:
diff changeset
    63
	public FileDocumentMonitor() {
hgs
parents:
diff changeset
    64
	}
hgs
parents:
diff changeset
    65
hgs
parents:
diff changeset
    66
	/*
hgs
parents:
diff changeset
    67
	 * (non-Javadoc)
hgs
parents:
diff changeset
    68
	 * 
hgs
parents:
diff changeset
    69
	 * @see com.nokia.tracecompiler.utils.DocumentMonitorBase#getFactory()
hgs
parents:
diff changeset
    70
	 */
hgs
parents:
diff changeset
    71
	@Override
hgs
parents:
diff changeset
    72
	public SourceDocumentFactory getFactory() {
hgs
parents:
diff changeset
    73
		if (documentFactory == null) {
hgs
parents:
diff changeset
    74
			documentFactory = new StringDocumentFactory();
hgs
parents:
diff changeset
    75
		}
hgs
parents:
diff changeset
    76
		return documentFactory;
hgs
parents:
diff changeset
    77
	}
hgs
parents:
diff changeset
    78
hgs
parents:
diff changeset
    79
	/*
hgs
parents:
diff changeset
    80
	 * (non-Javadoc)
hgs
parents:
diff changeset
    81
	 * 
hgs
parents:
diff changeset
    82
	 * @see com.nokia.tracecompiler.utils.DocumentMonitorAdapter#
hgs
parents:
diff changeset
    83
	 *      startMonitor(com.nokia.tracecompiler.source.SourceDocumentProcessor)
hgs
parents:
diff changeset
    84
	 */
hgs
parents:
diff changeset
    85
	@Override
hgs
parents:
diff changeset
    86
	public void startMonitor(SourceDocumentProcessor processor) throws Exception {
hgs
parents:
diff changeset
    87
		
hgs
parents:
diff changeset
    88
		// Put source files and non-source files to same list
hgs
parents:
diff changeset
    89
		SourceEngine sourceEngine = TraceCompilerEngineGlobals.getSourceEngine();
hgs
parents:
diff changeset
    90
		ArrayList<String> list = sourceEngine.getNonSourceFiles();
hgs
parents:
diff changeset
    91
		for (int i=0; i < files.length; i++) {
hgs
parents:
diff changeset
    92
			String fileName = files[i];
hgs
parents:
diff changeset
    93
			if (!list.contains(fileName)) {
hgs
parents:
diff changeset
    94
				list.add(fileName);
hgs
parents:
diff changeset
    95
			}
hgs
parents:
diff changeset
    96
		}		
hgs
parents:
diff changeset
    97
		String[] fileArr = new String[list.size()];
hgs
parents:
diff changeset
    98
		list.toArray(fileArr);
hgs
parents:
diff changeset
    99
		String err = ""; //$NON-NLS-1$
hgs
parents:
diff changeset
   100
		
hgs
parents:
diff changeset
   101
		for (String fileName : fileArr) {
hgs
parents:
diff changeset
   102
			File file = new File(fileName);
hgs
parents:
diff changeset
   103
			if (file.exists() && !file.isDirectory()
hgs
parents:
diff changeset
   104
					&& FileUtils.isFileAllowed(file.getName())) {
hgs
parents:
diff changeset
   105
				try {
hgs
parents:
diff changeset
   106
					TraceCompilerEngineGlobals.getEvents().postInfoMessage(Messages.getString("FileDocumentMonitor.processingSourceFileText") + file.getAbsolutePath(), null); //$NON-NLS-1$
hgs
parents:
diff changeset
   107
					processor.sourceOpened(new FileDocument(file));
hgs
parents:
diff changeset
   108
				} catch (Exception e) {
hgs
parents:
diff changeset
   109
					// Error after processing one file
hgs
parents:
diff changeset
   110
					String msg = TraceCompilerEngineErrorMessages
hgs
parents:
diff changeset
   111
							.getErrorMessage(
hgs
parents:
diff changeset
   112
									TraceCompilerErrorCode.CANNOT_OPEN_SOURCE_FILE,
hgs
parents:
diff changeset
   113
									null);
hgs
parents:
diff changeset
   114
					TraceCompilerEngineGlobals.getEvents().postErrorMessage(msg,
hgs
parents:
diff changeset
   115
							file.getAbsolutePath(),true);
hgs
parents:
diff changeset
   116
					err = e.getMessage();
hgs
parents:
diff changeset
   117
				} finally {
hgs
parents:
diff changeset
   118
					if (TraceCompilerEngineGlobals.getEvents().hasErrorHappened() && !TraceCompilerGlobals.keepGoing())  {
hgs
parents:
diff changeset
   119
						throw new Exception("TraceCompiler stopping on error. " + err); //$NON-NLS-1$
hgs
parents:
diff changeset
   120
					}
hgs
parents:
diff changeset
   121
				}
hgs
parents:
diff changeset
   122
			}
hgs
parents:
diff changeset
   123
		}
hgs
parents:
diff changeset
   124
	}
hgs
parents:
diff changeset
   125
hgs
parents:
diff changeset
   126
	/*
hgs
parents:
diff changeset
   127
	 * (non-Javadoc)
hgs
parents:
diff changeset
   128
	 * 
hgs
parents:
diff changeset
   129
	 * @see com.nokia.tracecompiler.utils.DocumentMonitorAdapter#stopMonitor()
hgs
parents:
diff changeset
   130
	 */
hgs
parents:
diff changeset
   131
	@Override
hgs
parents:
diff changeset
   132
	public void stopMonitor() {
hgs
parents:
diff changeset
   133
	}
hgs
parents:
diff changeset
   134
hgs
parents:
diff changeset
   135
}