Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 82ef4701392aac31909319f88ae02690207af0dd (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
 * Copyright (c) 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.tcf.te.launch.core.persistence.filetransfer;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.tcf.te.launch.core.interfaces.IFileTransferItem;
import org.eclipse.tcf.te.launch.core.nls.Messages;

/**
 * FileTransferItemValidator
 */
public class FileTransferItemValidator {

	public static final Map<String,String> validate(IFileTransferItem item) {
		Assert.isNotNull(item);

		Map<String,String> invalid = new HashMap<String,String>();

		String host = item.getStringProperty(IFileTransferItem.PROPERTY_HOST);
		String target = item.getStringProperty(IFileTransferItem.PROPERTY_TARGET);
		int direction = (item.getProperty(IFileTransferItem.PROPERTY_DIRECTION) != null ?
						item.getIntProperty(IFileTransferItem.PROPERTY_DIRECTION) : IFileTransferItem.HOST_TO_TARGET);

		if (host == null || host.trim().length() == 0) {
			if (direction == IFileTransferItem.HOST_TO_TARGET) {
				invalid.put(IFileTransferItem.PROPERTY_HOST, Messages.FileTransferItemValidator_missingFile);
			}
			else {
				invalid.put(IFileTransferItem.PROPERTY_HOST, Messages.FileTransferItemValidator_missingFileOrDirectory);
			}
		}
		else {
			IPath hostPath = new Path(host);
			File hostFile = hostPath.toFile();
			if (direction == IFileTransferItem.HOST_TO_TARGET) {
				if (!hostFile.exists() || !hostFile.isFile() || !hostFile.canRead()) {
					invalid.put(IFileTransferItem.PROPERTY_HOST, Messages.FileTransferItemValidator_notExistingFile);
				}
			}
			else {
				if ((hostFile.isFile() && !hostFile.canWrite()) || (hostFile.isDirectory() && (!hostFile.exists() || !hostFile.canWrite()))) {
					invalid.put(IFileTransferItem.PROPERTY_HOST, Messages.FileTransferItemValidator_notExistingFileOrDirectory);
				}
			}
		}

		if (target == null || target.trim().length() == 0) {
			if (direction == IFileTransferItem.HOST_TO_TARGET) {
				invalid.put(IFileTransferItem.PROPERTY_TARGET, Messages.FileTransferItemValidator_missingFileOrDirectory);
			}
			else {
				invalid.put(IFileTransferItem.PROPERTY_TARGET, Messages.FileTransferItemValidator_missingFile);
			}
		}
		else {
			IPath targetPath = new Path(target);
			if (direction == IFileTransferItem.HOST_TO_TARGET) {
				if (!targetPath.isValidPath(target)) {
					invalid.put(IFileTransferItem.PROPERTY_TARGET, Messages.FileTransferItemValidator_invalidFileOrDirectory);
				}
			}
			else {
				if (!targetPath.isValidPath(target)) {
					invalid.put(IFileTransferItem.PROPERTY_TARGET, Messages.FileTransferItemValidator_invalidFile);
				}
			}
		}

		return invalid.isEmpty() ? null : invalid;
	}
}

Back to the top