Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 77385f1e728f3ef936905d07db0fb55e7d7b1aff (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
 * Copyright (c) 2002, 2007 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 * Martin Oberhuber (Wind River) - [168870] refactor org.eclipse.rse.core package of the UI plugin
 *******************************************************************************/
package org.eclipse.rse.internal.useractions.ui.compile;

import org.eclipse.rse.ui.view.ISystemRemoteElementAdapter;
import org.eclipse.rse.ui.view.SystemAdapterHelpers;

public class SystemCompileRemoteObjectMatcher {
	private String ssfId, nameFilter, typeFilter;
	private boolean allSsfs, allNames, allTypes;
	private boolean genericSsfStart, genericNamesStart, genericTypesStart;
	private boolean genericSsfEnd, genericNamesEnd, genericTypesEnd;
	private String ssfIdPart, nameFilterPart, typeFilterPart;

	public SystemCompileRemoteObjectMatcher(String ssfId, String nameFilter, String typeFilter) {
		this.ssfId = ssfId;
		this.nameFilter = nameFilter;
		this.typeFilter = typeFilter;
		if (ssfId == null) {
			ssfId = "*"; //$NON-NLS-1$
		}
		if (nameFilter == null) {
			nameFilter = "*"; //$NON-NLS-1$
		}
		if (typeFilter == null) {
			typeFilter = "*"; //$NON-NLS-1$
		}
		// determine if any attribute is totally generic
		this.allSsfs = ssfId.equals("*"); //$NON-NLS-1$
		this.allNames = nameFilter.equals("*"); //$NON-NLS-1$
		this.allTypes = typeFilter.equals("*"); //$NON-NLS-1$
		// determine if any attribute value starts with asterisk
		this.genericSsfStart = !allSsfs && startsWithAsterisk(ssfId);
		this.genericNamesStart = !allNames && startsWithAsterisk(nameFilter);
		this.genericTypesStart = !allTypes && startsWithAsterisk(typeFilter);
		// determine if any attribute value ends with asterisk
		this.genericSsfEnd = !allSsfs && endsWithAsterisk(ssfId);
		this.genericNamesEnd = !allNames && endsWithAsterisk(nameFilter);
		this.genericTypesEnd = !allTypes && endsWithAsterisk(typeFilter);
		// strip of leading asterisk if there is one
		if (genericSsfStart) {
			ssfIdPart = stripLeadingAsterisk(ssfId);
		}
		if (genericNamesStart) {
			nameFilterPart = stripLeadingAsterisk(nameFilter);
		}
		if (genericTypesStart) {
			typeFilterPart = stripLeadingAsterisk(typeFilter);
		}
		// strip of trailing asterisk if there is one
		if (genericSsfEnd) {
			ssfIdPart = stripTrailingAsterisk(ssfId);
		}
		if (genericNamesEnd) {
			nameFilterPart = stripTrailingAsterisk(nameFilter);
		}
		if (genericTypesEnd) {
			typeFilterPart = stripTrailingAsterisk(typeFilter);
		}
	}

	/**
	 * Helper method.
	 * Returns true if given name starts with an asterisk.
	 */
	protected boolean startsWithAsterisk(String name) {
		return name.startsWith("*"); //$NON-NLS-1$
	}

	/**
	 * Helper method.
	 * Returns true if given name ends with an asterisk.
	 */
	protected boolean endsWithAsterisk(String name) {
		return name.endsWith("*"); //$NON-NLS-1$
	}

	/**
	 * Helper method.
	 * Strips off the leading asterisk.
	 */
	protected String stripLeadingAsterisk(String name) {
		return name.substring(1);
	}

	/**
	 * Helper method.
	 * Strips off the trailing asterisk.
	 */
	protected String stripTrailingAsterisk(String name) {
		return name.substring(0, name.length() - 1);
	}

	/**
	 * Getter method.
	 * Return what was specified for the <samp>subsystemconfigurationid</samp> xml attribute.
	 */
	public String getSubSystemFactoryId() {
		return ssfId;
	}

	/**
	 * Getter method.
	 * Return what was specified for the <samp>namefilter</samp> xml attribute.
	 */
	public String getNameFilter() {
		return nameFilter;
	}

	/**
	 * Getter method.
	 * Return what was specified for the <samp>typefilter</samp> xml attribute.
	 */
	public String getTypeFilter() {
		return typeFilter;
	}

	/**
	 * Returns true if the current selection matches all the given filtering criteria, false otherwise.
	 */
	public boolean appliesTo(Object element) {
		ISystemRemoteElementAdapter adapter = SystemAdapterHelpers.getRemoteAdapter(element);
		if (adapter == null) {
			return false;
		}
		boolean applies = true;
		// must match on all attributes
		// check for match on subsystem factory id
		boolean ssfIdMatch = true;
		if (!allSsfs) {
			String subsystem = adapter.getSubSystemConfigurationId(element);
			if (ssfId == null) {
				ssfIdMatch = false;
			} else if (!genericSsfStart && !genericSsfEnd) {
				ssfIdMatch = subsystem.equals(ssfId);
			} else if (genericSsfStart) {
				ssfIdMatch = subsystem.endsWith(ssfIdPart);
			} else if (genericSsfEnd) {
				ssfIdMatch = subsystem.startsWith(ssfIdPart);
			}
		}
		if (!ssfIdMatch) {
			return false;
		}
		// check for match on name filter
		boolean nameMatch = true;
		if (!allNames) {
			String name = adapter.getName(element);
			if (name == null || !genericNamesStart) {
				nameMatch = false;
			} else if (!genericNamesStart && !genericNamesEnd) {
				nameMatch = name.equals(nameFilter);
			} else if (genericNamesStart) {
				nameMatch = name.endsWith(nameFilterPart);
			} else if (genericNamesEnd) {
				nameMatch = name.startsWith(nameFilterPart);
			}
		}
		if (!nameMatch) {
			return false;
		}
		// check for match on type filter
		boolean typeMatch = true;
		if (!allTypes) {
			String type = adapter.getRemoteSourceType(element);
			if (type == null) {
				typeMatch = false;
			} else if (!genericTypesStart && !genericTypesEnd) {
				typeMatch = type.equals(typeFilter);
			} else if (genericTypesStart) {
				typeMatch = type.endsWith(typeFilterPart);
			} else if (genericTypesEnd) {
				typeMatch = type.startsWith(typeFilterPart);
			}
		}
		if (!typeMatch) {
			return false;
		}
		return applies;
	}
}

Back to the top