1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Thomas Fletcher added new method replacePersistenttarget()

This commit is contained in:
Alain Magloire 2003-07-16 14:29:40 +00:00
parent dab6ba3ae7
commit 088b72acaf

View file

@ -15,133 +15,163 @@ import org.eclipse.core.runtime.QualifiedName;
public class MakeUtil { public class MakeUtil {
final static String MAKE_GOALS = "goals"; final static String MAKE_GOALS = "goals";
final static String MAKE_DIR = "buildir"; final static String MAKE_DIR = "buildir";
final static String TARGET_ID = "org.eclipse.cdt.make"; final static String TARGET_ID = "org.eclipse.cdt.make";
public static String [] decodeTargets (String property) { public static String[] decodeTargets(String property) {
BufferedReader reader= new BufferedReader(new StringReader(property)); BufferedReader reader = new BufferedReader(new StringReader(property));
ArrayList l= new ArrayList(5); ArrayList l = new ArrayList(5);
try { try {
String line= reader.readLine(); String line = reader.readLine();
while (line != null && !"".equals(line)) { while (line != null && !"".equals(line)) {
l.add(line); l.add(line);
line = reader.readLine(); line = reader.readLine();
} }
} catch (IOException e) { } catch (IOException e) {
// this should not happen, we're reading from a string. // this should not happen, we're reading from a string.
} }
String[]result = new String[l.size ()]; String[] result = new String[l.size()];
return (String[])l.toArray(result); return (String[]) l.toArray(result);
} }
public static String encodeTargets(String[] targets) { public static String encodeTargets(String[] targets) {
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
for (int i= 0; i < targets.length; i++) { for (int i = 0; i < targets.length; i++) {
if (targets[i] != null) { if (targets[i] != null) {
buf.append(targets[i]); buf.append(targets[i]);
buf.append("\n"); buf.append("\n");
} }
} }
return (buf.length () == 0) ? null : buf.toString(); return (buf.length() == 0) ? null : buf.toString();
} }
public static QualifiedName getQualifiedNameTarget () { public static QualifiedName getQualifiedNameTarget() {
return new QualifiedName(TARGET_ID, MAKE_GOALS); return new QualifiedName(TARGET_ID, MAKE_GOALS);
} }
public static QualifiedName getQualifiedNameDir () { public static QualifiedName getQualifiedNameDir() {
return new QualifiedName(TARGET_ID, MAKE_DIR); return new QualifiedName(TARGET_ID, MAKE_DIR);
} }
public static String getSessionTarget(IResource resource) { public static String getSessionTarget(IResource resource) {
try { try {
String property = (String)resource.getSessionProperty(getQualifiedNameTarget()); String property = (String) resource.getSessionProperty(getQualifiedNameTarget());
if (property != null) if (property != null)
return property; return property;
} catch (CoreException e) { } catch (CoreException e) {
} }
return new String(); return new String();
} }
public static void setSessionTarget(IResource resource, String target) { public static void setSessionTarget(IResource resource, String target) {
try { try {
resource.setSessionProperty(getQualifiedNameTarget(), target); resource.setSessionProperty(getQualifiedNameTarget(), target);
} catch (CoreException e) { } catch (CoreException e) {
} }
} }
public static void removeSessionTarget(IResource resource) { public static void removeSessionTarget(IResource resource) {
setSessionTarget (resource, null); setSessionTarget(resource, null);
} }
public static String getSessionBuildDir(IResource resource) { public static String getSessionBuildDir(IResource resource) {
try { try {
String dir = (String)resource.getSessionProperty(getQualifiedNameDir()); String dir = (String) resource.getSessionProperty(getQualifiedNameDir());
if (dir != null) if (dir != null)
return dir; return dir;
} catch (CoreException e) { } catch (CoreException e) {
} }
return new String (); return new String();
} }
public static void setSessionBuildDir(IResource resource, String dir) { public static void setSessionBuildDir(IResource resource, String dir) {
try { try {
resource.setSessionProperty(getQualifiedNameDir(), dir); resource.setSessionProperty(getQualifiedNameDir(), dir);
} catch (CoreException e) { } catch (CoreException e) {
} }
} }
public static void removeSessionBuildDir(IResource resource) { public static void removeSessionBuildDir(IResource resource) {
setSessionBuildDir (resource, null); setSessionBuildDir(resource, null);
} }
public static String[] getPersistentTargets(IResource resource) { public static String[] getPersistentTargets(IResource resource) {
try { try {
String property = resource.getPersistentProperty(getQualifiedNameTarget()); String property = resource.getPersistentProperty(getQualifiedNameTarget());
if (property != null) if (property != null)
return decodeTargets (property); return decodeTargets(property);
} catch (CoreException e) { } catch (CoreException e) {
} }
return new String[0]; return new String[0];
} }
public static void setPersistentTargets(IResource resource, String[] targets) { public static void setPersistentTargets(IResource resource, String[] targets) {
String property= null; String property = null;
if (targets != null) if (targets != null)
property = encodeTargets(targets); property = encodeTargets(targets);
//System.out.println ("PROPERTY " + property); //System.out.println ("PROPERTY " + property);
try { try {
resource.setPersistentProperty(getQualifiedNameTarget(), property); resource.setPersistentProperty(getQualifiedNameTarget(), property);
} catch (CoreException e) { } catch (CoreException e) {
} }
} }
public static void addPersistentTarget(IResource resource, String target) { public static void addPersistentTarget(IResource resource, String target) {
String[] targets = MakeUtil.getPersistentTargets (resource); String[] targets = MakeUtil.getPersistentTargets(resource);
for (int i = 0; i < targets.length; i++) { for (int i = 0; i < targets.length; i++) {
if (targets[i].equals (target)) { if (targets[i].equals(target)) {
return; return;
} }
} }
String[] newTargets = new String[targets.length + 1]; String[] newTargets = new String[targets.length + 1];
System.arraycopy (targets, 0, newTargets, 0, targets.length); System.arraycopy(targets, 0, newTargets, 0, targets.length);
newTargets[targets.length] = target; newTargets[targets.length] = target;
MakeUtil.setPersistentTargets (resource, newTargets); MakeUtil.setPersistentTargets(resource, newTargets);
} }
public static void removePersistentTarget (IResource resource, String target) { public static void removePersistentTarget(IResource resource, String target) {
String[] targets = MakeUtil.getPersistentTargets (resource); String[] targets = MakeUtil.getPersistentTargets(resource);
String[] newTargets = new String[targets.length]; String[] newTargets = new String[targets.length];
for (int i = 0; i < targets.length; i++) { for (int i = 0; i < targets.length; i++) {
if (! targets[i].equals (target)) { if (!targets[i].equals(target)) {
newTargets[i] = targets[i]; newTargets[i] = targets[i];
} }
} }
MakeUtil.setPersistentTargets (resource, newTargets); MakeUtil.setPersistentTargets(resource, newTargets);
} }
private MakeUtil() { /**
} * Replace a tag on a resource. Functionally equivalent to
* removePersistantTag(resource, oldtarget)
* addPersistantTag(resource, newtarget)
* If the oldtarget doesn't exist, the newtarget is added.
* If the newtarget is null, the oldtarget is removed.
* @param resource The resource the tag applies to
* @param oldtarget The oldtarget tag
* @param newtarget The newtarget tag to replace the old target tag or null. If
* newtarget is null then this call is the same as removePersistantTarget(resource, oldtarget)
*/
public static void replacePersistentTarget(IResource resource, String oldtarget, String newtarget) {
if (newtarget == null) {
removePersistentTarget(resource, oldtarget);
return;
}
String[] targets = getPersistentTargets(resource);
for (int i = 0; i < targets.length; i++) {
if (targets[i].equals(oldtarget)) {
targets[i] = newtarget;
setPersistentTargets(resource, targets);
return;
}
}
//The target wasn't found, create a new one
addPersistentTarget(resource, newtarget);
}
private MakeUtil() {
}
} }