Skip to main content
summaryrefslogtreecommitdiffstats
blob: f3922da2d95026be58191e70aaeee5e021439457 (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
/*******************************************************************************
 *  Copyright (c) 2008, 2017 IBM Corporation and others.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *     EclipseSource - ongoing development
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata;

import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.equinox.p2.metadata.ITouchpointInstruction;
import org.eclipse.equinox.p2.metadata.MetadataFactory;

/**
 * A touchpoint instruction contains either a sequence of instruction statements
 * to be executed during a particular engine phase, or some simple string value
 * that is needed by a touchpoint to execute its phases.
 * <p>
 * The format of a touchpoint instruction statement sequence is as follows:
 * 
 *   statement-sequence :
 *     | statement ';'
 *      | statement-sequence statement
 *      ;
 *
 *Where a statement is of the format:
 *
 *  statement :
 *      | actionName '(' parameters ')'
 *      ;
 *
 *  parameters :
 *      | // empty
 *      | parameter
 *      | parameters ',' parameter
 *      ;
 *
 *   parameter : 
 *      | paramName ':' paramValue
 *      ;
 *
 * actionName, paramName, paramValue :
 *      | String 
 *      ;
 *
 * @noextend This class is not intended to be subclassed by clients.
 * @see MetadataFactory#createTouchpointInstruction(String, String)
 */
public class TouchpointInstruction implements ITouchpointInstruction {

	private final String body;
	private final String importAttribute;

	/**
	 * Encodes an action statement in string form. This method will
	 * take care of escaping any illegal characters in function parameter values.
	 * 
	 * @param actionName The name of the action.
	 * @param parameters The function's parameters. This is a Map<String,String>
	 * where the keys are parameter names, and the values are parameter values
	 * @return An encoded touchpoint instruction statement
	 */
	public static String encodeAction(String actionName, Map<String, String> parameters) {
		StringBuffer result = new StringBuffer(actionName);
		result.append('(');
		boolean first = true;
		for (Entry<String, String> entry : parameters.entrySet()) {
			if (first)
				first = false;
			else
				result.append(',');
			result.append(entry.getKey());
			result.append(':');
			appendEncoded(result, entry.getValue());
		}
		result.append(')').append(';');
		return result.toString();
	}

	/**
	 * Append the given value to the given buffer, encoding any illegal characters
	 * with appropriate escape sequences.
	 */
	private static void appendEncoded(StringBuffer buf, String value) {
		char[] chars = value.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			switch (chars[i]) {
				case '$' :
				case ',' :
				case ':' :
				case ';' :
				case '{' :
				case '}' :
					buf.append("${#").append(Integer.toString(chars[i])).append('}'); //$NON-NLS-1$
					break;
				default :
					buf.append(chars[i]);
			}
		}
	}

	/**
	 * Clients must use the factory method on {@link MetadataFactory}.
	 */
	public TouchpointInstruction(String body, String importAttribute) {
		this.body = body;
		this.importAttribute = importAttribute;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof ITouchpointInstruction))
			return false;
		ITouchpointInstruction other = (ITouchpointInstruction) obj;
		if (body == null) {
			if (other.getBody() != null)
				return false;
		} else if (!body.equals(other.getBody()))
			return false;
		if (importAttribute == null) {
			if (other.getImportAttribute() != null)
				return false;
		} else if (!importAttribute.equals(other.getImportAttribute()))
			return false;
		return true;
	}

	/**
	 * Returns the body of this touchpoint instruction. The body is either a sequence
	 * of instruction statements, or a simple string value.
	 * 
	 * @return The body of this touchpoint instruction
	 */
	@Override
	public String getBody() {
		return body;
	}

	//TODO What is this? Please doc
	@Override
	public String getImportAttribute() {
		return importAttribute;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((body == null) ? 0 : body.hashCode());
		result = prime * result + ((importAttribute == null) ? 0 : importAttribute.hashCode());
		return result;
	}

	/**
	 * Returns a string representation of this instruction for debugging purposes only.
	 */
	@Override
	public String toString() {
		return "Instruction[" + body + ',' + importAttribute + ']'; //$NON-NLS-1$
	}
}

Back to the top