| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2017 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
* EclipseSource - ongoing development |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.internal.p2.metadata; |
| 16 |
|
| 17 |
import java.util.Map; |
| 18 |
import java.util.Map.Entry; |
| 19 |
import org.eclipse.equinox.p2.metadata.ITouchpointInstruction; |
| 20 |
import org.eclipse.equinox.p2.metadata.MetadataFactory; |
| 21 |
|
| 22 |
/** |
| 23 |
* A touchpoint instruction contains either a sequence of instruction statements |
| 24 |
* to be executed during a particular engine phase, or some simple string value |
| 25 |
* that is needed by a touchpoint to execute its phases. |
| 26 |
* <p> |
| 27 |
* The format of a touchpoint instruction statement sequence is as follows: |
| 28 |
* |
| 29 |
* statement-sequence : |
| 30 |
* | statement ';' |
| 31 |
* | statement-sequence statement |
| 32 |
* ; |
| 33 |
* |
| 34 |
*Where a statement is of the format: |
| 35 |
* |
| 36 |
* statement : |
| 37 |
* | actionName '(' parameters ')' |
| 38 |
* ; |
| 39 |
* |
| 40 |
* parameters : |
| 41 |
* | // empty |
| 42 |
* | parameter |
| 43 |
* | parameters ',' parameter |
| 44 |
* ; |
| 45 |
* |
| 46 |
* parameter : |
| 47 |
* | paramName ':' paramValue |
| 48 |
* ; |
| 49 |
* |
| 50 |
* actionName, paramName, paramValue : |
| 51 |
* | String |
| 52 |
* ; |
| 53 |
* |
| 54 |
* @noextend This class is not intended to be subclassed by clients. |
| 55 |
* @see MetadataFactory#createTouchpointInstruction(String, String) |
| 56 |
*/ |
| 57 |
public class TouchpointInstruction implements ITouchpointInstruction { |
| 58 |
|
| 59 |
private final String body; |
| 60 |
private final String importAttribute; |
| 61 |
|
| 62 |
/** |
| 63 |
* Encodes an action statement in string form. This method will |
| 64 |
* take care of escaping any illegal characters in function parameter values. |
| 65 |
* |
| 66 |
* @param actionName The name of the action. |
| 67 |
* @param parameters The function's parameters. This is a Map<String,String> |
| 68 |
* where the keys are parameter names, and the values are parameter values |
| 69 |
* @return An encoded touchpoint instruction statement |
| 70 |
*/ |
| 71 |
public static String encodeAction(String actionName, Map<String, String> parameters) { |
| 72 |
StringBuffer result = new StringBuffer(actionName); |
| 73 |
result.append('('); |
| 74 |
boolean first = true; |
| 75 |
for (Entry<String, String> entry : parameters.entrySet()) { |
| 76 |
if (first) |
| 77 |
first = false; |
| 78 |
else |
| 79 |
result.append(','); |
| 80 |
result.append(entry.getKey()); |
| 81 |
result.append(':'); |
| 82 |
appendEncoded(result, entry.getValue()); |
| 83 |
} |
| 84 |
result.append(')').append(';'); |
| 85 |
return result.toString(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Append the given value to the given buffer, encoding any illegal characters |
| 90 |
* with appropriate escape sequences. |
| 91 |
*/ |
| 92 |
private static void appendEncoded(StringBuffer buf, String value) { |
| 93 |
char[] chars = value.toCharArray(); |
| 94 |
for (char c : chars) { |
| 95 |
switch (c) { |
| 96 |
case '$' : |
| 97 |
case ',' : |
| 98 |
case ':' : |
| 99 |
case ';' : |
| 100 |
case '{' : |
| 101 |
case '}' : |
| 102 |
buf.append("${#").append(Integer.toString(c)).append('}'); //$NON-NLS-1$ |
| 103 |
break; |
| 104 |
default : |
| 105 |
buf.append(c); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Clients must use the factory method on {@link MetadataFactory}. |
| 112 |
*/ |
| 113 |
public TouchpointInstruction(String body, String importAttribute) { |
| 114 |
this.body = body; |
| 115 |
this.importAttribute = importAttribute; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public boolean equals(Object obj) { |
| 120 |
if (this == obj) |
| 121 |
return true; |
| 122 |
if (obj == null) |
| 123 |
return false; |
| 124 |
if (!(obj instanceof ITouchpointInstruction)) |
| 125 |
return false; |
| 126 |
ITouchpointInstruction other = (ITouchpointInstruction) obj; |
| 127 |
if (body == null) { |
| 128 |
if (other.getBody() != null) |
| 129 |
return false; |
| 130 |
} else if (!body.equals(other.getBody())) |
| 131 |
return false; |
| 132 |
if (importAttribute == null) { |
| 133 |
if (other.getImportAttribute() != null) |
| 134 |
return false; |
| 135 |
} else if (!importAttribute.equals(other.getImportAttribute())) |
| 136 |
return false; |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns the body of this touchpoint instruction. The body is either a sequence |
| 142 |
* of instruction statements, or a simple string value. |
| 143 |
* |
| 144 |
* @return The body of this touchpoint instruction |
| 145 |
*/ |
| 146 |
@Override |
| 147 |
public String getBody() { |
| 148 |
return body; |
| 149 |
} |
| 150 |
|
| 151 |
//TODO What is this? Please doc |
| 152 |
@Override |
| 153 |
public String getImportAttribute() { |
| 154 |
return importAttribute; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public int hashCode() { |
| 159 |
final int prime = 31; |
| 160 |
int result = 1; |
| 161 |
result = prime * result + ((body == null) ? 0 : body.hashCode()); |
| 162 |
result = prime * result + ((importAttribute == null) ? 0 : importAttribute.hashCode()); |
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Returns a string representation of this instruction for debugging purposes only. |
| 168 |
*/ |
| 169 |
@Override |
| 170 |
public String toString() { |
| 171 |
return "Instruction[" + body + ',' + importAttribute + ']'; //$NON-NLS-1$ |
| 172 |
} |
| 173 |
} |