tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/document/FileDocumentMonitor.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) 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 * Document monitor implementation which reads all files from a directory
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.document;
       
    20 
       
    21 import java.io.File;
       
    22 import java.util.ArrayList;
       
    23 
       
    24 import com.nokia.tracecompiler.TraceCompilerGlobals;
       
    25 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages;
       
    26 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    27 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
       
    28 import com.nokia.tracecompiler.engine.source.SourceEngine;
       
    29 import com.nokia.tracecompiler.file.FileUtils;
       
    30 import com.nokia.tracecompiler.source.SourceDocumentFactory;
       
    31 import com.nokia.tracecompiler.source.SourceDocumentProcessor;
       
    32 import com.nokia.tracecompiler.utils.DocumentMonitorBase;
       
    33 
       
    34 /**
       
    35  * Document monitor implementation which reads files
       
    36  * 
       
    37  */
       
    38 public final class FileDocumentMonitor extends DocumentMonitorBase {
       
    39 
       
    40 	/**
       
    41 	 * Document factory
       
    42 	 */
       
    43 	private StringDocumentFactory documentFactory;
       
    44 
       
    45 	/**
       
    46 	 * Files to be processed
       
    47 	 */
       
    48 	private static String[] files;
       
    49 
       
    50 	/**
       
    51 	 * Sets the files to be used by the document monitor
       
    52 	 * 
       
    53 	 * @param files
       
    54 	 *            the files
       
    55 	 */
       
    56 	public static void setFiles(String[] files) {
       
    57 		FileDocumentMonitor.files = files;
       
    58 	}
       
    59 
       
    60 	/**
       
    61 	 * Constructor
       
    62 	 */
       
    63 	public FileDocumentMonitor() {
       
    64 	}
       
    65 
       
    66 	/*
       
    67 	 * (non-Javadoc)
       
    68 	 * 
       
    69 	 * @see com.nokia.tracecompiler.utils.DocumentMonitorBase#getFactory()
       
    70 	 */
       
    71 	@Override
       
    72 	public SourceDocumentFactory getFactory() {
       
    73 		if (documentFactory == null) {
       
    74 			documentFactory = new StringDocumentFactory();
       
    75 		}
       
    76 		return documentFactory;
       
    77 	}
       
    78 
       
    79 	/*
       
    80 	 * (non-Javadoc)
       
    81 	 * 
       
    82 	 * @see com.nokia.tracecompiler.utils.DocumentMonitorAdapter#
       
    83 	 *      startMonitor(com.nokia.tracecompiler.source.SourceDocumentProcessor)
       
    84 	 */
       
    85 	@Override
       
    86 	public void startMonitor(SourceDocumentProcessor processor) throws Exception {
       
    87 		
       
    88 		// Put source files and non-source files to same list
       
    89 		SourceEngine sourceEngine = TraceCompilerEngineGlobals.getSourceEngine();
       
    90 		ArrayList<String> list = sourceEngine.getNonSourceFiles();
       
    91 		for (int i=0; i < files.length; i++) {
       
    92 			String fileName = files[i];
       
    93 			if (!list.contains(fileName)) {
       
    94 				list.add(fileName);
       
    95 			}
       
    96 		}		
       
    97 		String[] fileArr = new String[list.size()];
       
    98 		list.toArray(fileArr);
       
    99 		String err = ""; //$NON-NLS-1$
       
   100 		
       
   101 		for (String fileName : fileArr) {
       
   102 			File file = new File(fileName);
       
   103 			if (file.exists() && !file.isDirectory()
       
   104 					&& FileUtils.isFileAllowed(file.getName())) {
       
   105 				try {
       
   106 					TraceCompilerEngineGlobals.getEvents().postInfoMessage(Messages.getString("FileDocumentMonitor.processingSourceFileText") + file.getAbsolutePath(), null); //$NON-NLS-1$
       
   107 					processor.sourceOpened(new FileDocument(file));
       
   108 				} catch (Exception e) {
       
   109 					// Error after processing one file
       
   110 					String msg = TraceCompilerEngineErrorMessages
       
   111 							.getErrorMessage(
       
   112 									TraceCompilerErrorCode.CANNOT_OPEN_SOURCE_FILE,
       
   113 									null);
       
   114 					TraceCompilerEngineGlobals.getEvents().postErrorMessage(msg,
       
   115 							file.getAbsolutePath(),true);
       
   116 					err = e.getMessage();
       
   117 				} finally {
       
   118 					if (TraceCompilerEngineGlobals.getEvents().hasErrorHappened() && !TraceCompilerGlobals.keepGoing())  {
       
   119 						throw new Exception("TraceCompiler stopping on error. " + err); //$NON-NLS-1$
       
   120 					}
       
   121 				}
       
   122 			}
       
   123 		}
       
   124 	}
       
   125 
       
   126 	/*
       
   127 	 * (non-Javadoc)
       
   128 	 * 
       
   129 	 * @see com.nokia.tracecompiler.utils.DocumentMonitorAdapter#stopMonitor()
       
   130 	 */
       
   131 	@Override
       
   132 	public void stopMonitor() {
       
   133 	}
       
   134 
       
   135 }