Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b7115dfc585001dd87f0b3b8d5ce2f42d83eccd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
 * Copyright (c) 2007, 2008 Symbian Software Limited and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.templateengine.process.processes;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
import org.eclipse.cdt.core.templateengine.process.ProcessHelper;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;


/**
 * Append the contents to the file. 
 */
public class Append extends ProcessRunner {
	
	/**
	 * This method Appends the contents to a file.
	 */
	@Override
	public void process(TemplateCore template, ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {
		ProcessArgument[][] files = args[0].getComplexArrayValue();
		for(int i=0; i<files.length; i++) {
			ProcessArgument[] file = files[i];
			String sourcePath = file[0].getSimpleValue();
			URL sourceURL;
			try {
				sourceURL = TemplateEngineHelper.getTemplateResourceURLRelativeToTemplate(template, sourcePath);
				if (sourceURL == null) {
					throw new ProcessFailureException(getProcessMessage(processId, IStatus.ERROR, Messages.getString("Append.0") + sourcePath)); //$NON-NLS-1$
				}
			} catch (IOException e1) {
				throw new ProcessFailureException(Messages.getString("Append.1") + sourcePath); //$NON-NLS-1$
			}
			File targetFile = new File(file[1].getSimpleValue());
			boolean replaceable = file[2].getSimpleValue().equals("true"); //$NON-NLS-1$
			String fileContents;
			try {
				fileContents = ProcessHelper.readFromFile(sourceURL);
			} catch (IOException e1) {
				throw new ProcessFailureException(Messages.getString("Append.3") + sourcePath); //$NON-NLS-1$
			}
			if (replaceable) {
				fileContents = ProcessHelper.getValueAfterExpandingMacros(fileContents, ProcessHelper.getReplaceKeys(fileContents), template.getValueStore());
			}
			try {
				ProcessHelper.appendFile(fileContents, targetFile);
			} catch (IOException e) {
				throw new ProcessFailureException(Messages.getString("Append.4"), e); //$NON-NLS-1$
			}
		}
	}
}

Back to the top