KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > core > AntCoreUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.ant.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.core.variables.IStringVariableManager;
27 import org.eclipse.core.variables.VariablesPlugin;
28 import org.osgi.framework.BundleContext;
29
30 public class AntCoreUtil {
31     
32     private static BundleContext fgContext= null;
33     
34     public static void setBundleContext(BundleContext context) {
35         fgContext= context;
36     }
37     
38     public static BundleContext getBundleContext() {
39         return fgContext;
40     }
41     
42     /*
43      * Helper method to ensure an array is converted into an ArrayList.
44      */

45     public static ArrayList JavaDoc getArrayList(String JavaDoc[] args) {
46         if (args == null) {
47             return null;
48         }
49         // We could be using Arrays.asList() here, but it does not specify
50
// what kind of list it will return. We need a list that
51
// implements the method List.remove(Object) and ArrayList does.
52
ArrayList JavaDoc result = new ArrayList JavaDoc(args.length);
53         for (int i = 0; i < args.length; i++) {
54             result.add(args[i]);
55         }
56         return result;
57     }
58     
59     /*
60      * From a command line list, get the argument for the given parameter.
61      * The parameter and its argument are removed from the list.
62      *
63      * @return <code>null</code> if the parameter is not found
64      * or an empty String if no arguments are found
65      */

66     public static String JavaDoc getArgument(List JavaDoc commands, String JavaDoc param) {
67         if (commands == null) {
68             return null;
69         }
70         int index = commands.indexOf(param);
71         if (index == -1) {
72             return null;
73         }
74         commands.remove(index);
75         if (index == commands.size()) {// if this is the last command
76
return ""; //$NON-NLS-1$
77
}
78         
79         String JavaDoc command = (String JavaDoc) commands.get(index);
80         if (command.startsWith("-")) { //new parameter //$NON-NLS-1$
81
return ""; //$NON-NLS-1$
82
}
83         commands.remove(index);
84         return command;
85     }
86     
87     public static void processMinusDProperties(List JavaDoc commands, Map JavaDoc userProperties) {
88         Iterator JavaDoc iter= commands.iterator();
89         while (iter.hasNext()) {
90             String JavaDoc arg = (String JavaDoc) iter.next();
91             if (arg.startsWith("-D")) { //$NON-NLS-1$
92
String JavaDoc name = arg.substring(2, arg.length());
93                 String JavaDoc value = null;
94                 int posEq = name.indexOf("="); //$NON-NLS-1$
95
if (posEq == 0) {
96                     value= name.substring(1);
97                     name= ""; //$NON-NLS-1$
98
} else if (posEq > 0 && posEq != name.length() - 1) {
99                     value = name.substring(posEq + 1).trim();
100                     name = name.substring(0, posEq);
101                 }
102                 
103                 if (value == null) {
104                     //the user has specified something like "-Debug"
105
continue;
106                 }
107     
108                 userProperties.put(name, value);
109                 iter.remove();
110             }
111         }
112     }
113     
114     public static File JavaDoc getFileRelativeToBaseDir(String JavaDoc fileName, String JavaDoc base, String JavaDoc buildFileLocation) {
115         IPath path= new Path(fileName);
116         if (!path.isAbsolute()) {
117             if (base != null) {
118                 File JavaDoc baseDir= new File JavaDoc(base);
119                 //relative to the base dir
120
path= new Path(baseDir.getAbsolutePath());
121             } else {
122                 //relative to the build file location
123
path= new Path(buildFileLocation);
124                 path= path.removeLastSegments(1);
125             }
126             path= path.addTrailingSeparator();
127             path= path.append(fileName);
128         }
129         
130         return path.toFile();
131     }
132     
133     /**
134      * Returns a list of Properties contained in the list of fileNames.
135      */

136     public static List JavaDoc loadPropertyFiles(List JavaDoc fileNames, String JavaDoc base, String JavaDoc buildFileLocation) throws IOException JavaDoc {
137         List JavaDoc allProperties= new ArrayList JavaDoc(fileNames.size());
138         for (int i = 0; i < fileNames.size(); i++) {
139             String JavaDoc filename = (String JavaDoc) fileNames.get(i);
140             File JavaDoc file= getFileRelativeToBaseDir(filename, base, buildFileLocation);
141             Properties JavaDoc props = new Properties JavaDoc();
142             FileInputStream JavaDoc fis = null;
143             try {
144                 fis = new FileInputStream JavaDoc(file);
145                 props.load(fis);
146             } finally {
147                 if (fis != null) {
148                     try {
149                         fis.close();
150                     } catch (IOException JavaDoc e){
151                     }
152                 }
153             }
154             Enumeration JavaDoc propertyNames = props.propertyNames();
155             while (propertyNames.hasMoreElements()) {
156                 String JavaDoc name = (String JavaDoc) propertyNames.nextElement();
157                 
158                 String JavaDoc value= props.getProperty(name);
159                 props.remove(name);
160                 IStringVariableManager stringVariableManager = VariablesPlugin.getDefault().getStringVariableManager();
161                 try {
162                     name= stringVariableManager.performStringSubstitution(name);
163                     value= stringVariableManager.performStringSubstitution(value);
164                 } catch (CoreException e) {
165                 }
166                 props.setProperty(name, value);
167             }
168             allProperties.add(props);
169         }
170         return allProperties;
171     }
172 }
173
Popular Tags