KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > ShorterPaths


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.nbbuild;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31 import org.apache.tools.ant.*;
32 import org.apache.tools.ant.types.*;
33
34 /**
35  * Replace paths prefixes with variables.
36  * Designed for netbeans.dest dir and test.dist.dir variables
37  */

38 public class ShorterPaths extends Task {
39     
40    
41     /** dir is prefix and name is name of varaible
42      */

43     public static class Replacement {
44         String JavaDoc name;
45         File JavaDoc dir;
46         
47         public void setName(String JavaDoc name) {
48             this.name = name;
49         }
50         public void setDir(File JavaDoc dir) {
51             this.dir = dir;
52         }
53     }
54     private List JavaDoc<Replacement> replacements = new LinkedList JavaDoc<Replacement>(); // List<Nestme>
55
public Replacement createReplacement() {
56         Replacement r = new Replacement();
57         replacements.add(r);
58         return r;
59     }
60     // Or:
61
public void addReplacement(Replacement r) {
62         replacements.add(r);
63     }
64     // <shorterpaths in="inputpropname" out="outpropNames">
65
// <replacement name="property_name" dir="directory"/>
66
// </shorterpaths>
67

68      
69     private Path in;
70     public void setIn(Path p) {
71         if (in == null) {
72             in = p.createPath();
73         }
74         in.append(p);
75     }
76     public Path createIn () {
77         if (in == null) {
78             in = new Path(getProject());
79         }
80         return in;
81     }
82     public void setinRef(Reference r) {
83         createIn().setRefid(r);
84     }
85     // <customtask path="foo:bar"/>
86
// <customtask>
87
// <path>
88
// <pathelement location="foo"/>
89
// </path>
90
// </customtask>
91
// Etc.
92

93     String JavaDoc out;
94     public void setOut(String JavaDoc out) {
95         this.out = out;
96     }
97     
98     String JavaDoc extraLibs;
99     public void setExtraLibs(String JavaDoc extraLibs) {
100         this.extraLibs = extraLibs;
101     }
102     File JavaDoc extraLibsDir;
103     public void setExtraLibsDir(File JavaDoc extraLibsDir) {
104         this.extraLibsDir = extraLibsDir;
105     }
106     
107     File JavaDoc testProperties;
108     public void setTestProperties(File JavaDoc testProperties) {
109         this.testProperties = testProperties;
110     }
111     
112     
113
114     public void execute() throws BuildException {
115         // TODO code here what the task actually does:
116
String JavaDoc paths[] = in.list();
117         StringBuffer JavaDoc nbLibBuff = new StringBuffer JavaDoc() ;
118 // Path nbLibPath = new Path(getProject());
119
StringBuffer JavaDoc externalLibBuf = new StringBuffer JavaDoc();
120         try {
121             for (int i = 0 ; i < paths.length ; i++) {
122                 String JavaDoc path = paths[i];
123                 File JavaDoc file = new File JavaDoc(path);
124                 // check if file exists
125
if (file.exists()) {
126                     // add it on classpath
127
path = file.getCanonicalPath();
128                     simplyPath(path, externalLibBuf, nbLibBuff);
129                 } else {
130                     log("Path element "+ file + " doesn't exist.",Project.MSG_VERBOSE);
131                 }
132             }
133             if (out != null) {
134                 define(out, nbLibBuff.toString());
135             }
136             if (this.extraLibs != null) {
137                 define(extraLibs,externalLibBuf.toString());
138             }
139
140             if (testProperties != null) {
141                 // create properties file
142
PrintWriter JavaDoc pw = new PrintWriter JavaDoc(testProperties);
143                 
144                 // copy extra unit.test.properties
145
String JavaDoc extraProp = "test-unit-sys-prop";
146                 Hashtable JavaDoc properties = getProject().getProperties();
147                 StringBuffer JavaDoc outProp = new StringBuffer JavaDoc();
148                 for (Iterator JavaDoc it = properties.keySet().iterator(); it.hasNext();) {
149                     String JavaDoc name = (String JavaDoc) it.next();
150                     if (name.startsWith(extraProp)) {
151                         if (name.equals("test-unit-sys-prop.xtest.data")) {
152                             // ignore overring xtest.data.dir, data.zip placed to standard location
153
continue;
154                         }
155                        //
156
outProp.setLength(0);
157                        StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(properties.get(name).toString(), ":;");
158                        String JavaDoc nextToken = null;
159                        while (nextToken != null || tokenizer.hasMoreTokens()) {
160                            String JavaDoc token = nextToken ;
161                            nextToken = null;
162                            if (token == null) {
163                                token = tokenizer.nextToken();
164                            }
165                            if (tokenizer.hasMoreTokens()) {
166                                nextToken = tokenizer.nextToken();
167                            }
168                            // check if <disk drive>:\path is property"
169
String JavaDoc path = token + ":" + nextToken;
170                            if (new File JavaDoc(path).exists()) {
171                                nextToken = null;
172                            } else {
173                                path = token;
174                            }
175
176                            simplyPath(path,externalLibBuf,outProp);
177                        }
178                        pw.println(name + "=" + outProp);
179                     }
180                 }
181                 pw.println("extra.test.libs.dir=" + externalLibBuf.toString());
182                 pw.println("test.unit.run.cp=" + nbLibBuff.toString());
183                 pw.close();
184             }
185         } catch (IOException JavaDoc ex) {
186             throw new BuildException(ex);
187         }
188     }
189
190     private void simplyPath(String JavaDoc path, final StringBuffer JavaDoc externalLibBuf, final StringBuffer JavaDoc nbLibBuff) throws IOException JavaDoc {
191         boolean bAppend = false;
192         File JavaDoc file = new File JavaDoc(path);
193         if (file.exists()) {
194             // file exists, try to to replace the path with ${a.prop}/relpath
195
//
196
path = file.getAbsolutePath();
197            for (Replacement repl: replacements) {
198                 String JavaDoc dirCan = repl.dir.getCanonicalPath();
199                 if (path.startsWith(dirCan)) {
200                     if (nbLibBuff.length() > 0 ) {
201                         nbLibBuff.append(":\\\n");
202                     }
203
204                     nbLibBuff.append("${" + repl.name + "}");
205                     // postfix + unify file separators to '/'
206
nbLibBuff.append(path.substring(dirCan.length()).replace(File.separatorChar,'/'));
207                     bAppend = true;
208                     break;
209                 }
210             }
211             if (!bAppend) {
212                 String JavaDoc fName = copyExtraLib(path);
213                 if (fName != null) {
214                     if (externalLibBuf.length() > 0 ) {
215                         externalLibBuf.append(":\\\n");
216                     }
217                    externalLibBuf.append("${extra.test.libs}/" + fName);
218                 }
219             }
220            
221         } else {
222             if (nbLibBuff.length() > 0 ) {
223                 nbLibBuff.append(":\\\n");
224             }
225             nbLibBuff.append(path);
226         }
227             
228     }
229     
230     private void define(String JavaDoc prop, String JavaDoc val) {
231         log("Setting " + prop + "=" + val, Project.MSG_VERBOSE);
232         String JavaDoc old = getProject().getProperty(prop);
233         if (old != null && !old.equals(val)) {
234             getProject().log("Warning: " + prop + " was already set to " + old, Project.MSG_WARN);
235         }
236         getProject().setNewProperty(prop, val);
237     }
238
239     private String JavaDoc copyExtraLib(String JavaDoc path) throws IOException JavaDoc{
240         String JavaDoc name = null;
241         File JavaDoc file = new File JavaDoc(path);
242         if (this.extraLibsDir != null && extraLibsDir.isDirectory() && file.isFile()) {
243             
244             name = file.getName();
245             byte buff[] = new byte[100000];
246             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(path);
247             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(new File JavaDoc (extraLibsDir,name));
248             int size = 0;
249             while ((size = fis.read(buff)) > 0 ) {
250                 fos.write(buff,0,size);
251             }
252             fos.close();
253             fis.close();
254         }
255         return name;
256     }
257  
258     
259 }
260
261     
262
Popular Tags