KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > SystemVariableResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.core;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.variables.IDynamicVariable;
20 import org.eclipse.core.variables.IDynamicVariableResolver;
21
22 /**
23  * Resolves common system variables. The current set of variables
24  * supported (referenced as an argument to this variable) are:
25  * <ul>
26  * <li>ARCH - value of <code>BootLoader.getOSArch()</code></li>
27  * <li>ECLIPSE_HOME - location of the Eclipse installation</li>
28  * <li>NL - value of <code>BootLoader.getNL()</code></li>
29  * <li>OS - value of <code>BootLoader.getOS()</code></li>
30  * <li>WS - value of <code>BootLoader.getWS()</code></li>
31  * </ul>
32  * @since 3.0
33  */

34 public class SystemVariableResolver implements IDynamicVariableResolver {
35     /* (non-Javadoc)
36      * @see org.eclipse.core.variables.IDynamicVariableResolver#resolveValue(org.eclipse.core.variables.IDynamicVariable, java.lang.String)
37      */

38     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException {
39         if ("ARCH".equals(argument)) { //$NON-NLS-1$
40
return Platform.getOSArch();
41         } else if ("ECLIPSE_HOME".equals(argument)) { //$NON-NLS-1$
42
URL JavaDoc installURL = Platform.getInstallLocation().getURL();
43             IPath ppath = new Path(installURL.getFile()).removeTrailingSeparator();
44             return getCorrectPath(ppath.toOSString());
45         } else if ("NL".equals(argument)) { //$NON-NLS-1$
46
return Platform.getNL();
47         } else if ("OS".equals(argument)) { //$NON-NLS-1$
48
return Platform.getOS();
49         } else if ("WS".equals(argument)) { //$NON-NLS-1$
50
return Platform.getWS();
51         }
52         return null;
53     }
54     
55     private static String JavaDoc getCorrectPath(String JavaDoc path) {
56         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
57         for (int i = 0; i < path.length(); i++) {
58             char c = path.charAt(i);
59             if (Platform.getOS().equals("win32")) { //$NON-NLS-1$
60
if (i == 0 && c == '/')
61                     continue;
62             }
63             // Some VMs may return %20 instead of a space
64
if (c == '%' && i + 2 < path.length()) {
65                 char c1 = path.charAt(i + 1);
66                 char c2 = path.charAt(i + 2);
67                 if (c1 == '2' && c2 == '0') {
68                     i += 2;
69                     buf.append(" "); //$NON-NLS-1$
70
continue;
71                 }
72             }
73             buf.append(c);
74         }
75         return buf.toString();
76     }
77 }
78
Popular Tags