Debian Codegraph @

src/main/java/net/sf/antcontrib/property/PathToFileSet.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.property;
17
18 import java.io.File;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.types.FileSet;
23 import org.apache.tools.ant.types.Path;
24 import org.apache.tools.ant.util.FileUtils;
25
26 public class PathToFileSet
27 extends Task
28 {
29 private File dir;
30 private String name;
31 private String pathRefId;
32 private boolean ignoreNonRelative = false;
33
34 private static FileUtils fileUtils = FileUtils.newFileUtils();
35
36 public void setDir(File dir) {
37 this.dir = dir;
38 }
39
40 public void setName(String name) {
41 this.name = name;
42 }
43
44 public void setPathRefId(String pathRefId) {
45 this.pathRefId = pathRefId;
46 }
47
48 public void setIgnoreNonRelative(boolean ignoreNonRelative) {
49 this.ignoreNonRelative = ignoreNonRelative;
50 }
51
52 public void execute() {
53 if (dir == null)
54 throw new BuildException("missing dir");
55 if (name == null)
56 throw new BuildException("missing name");
57 if (pathRefId == null)
58 throw new BuildException("missing pathrefid");
59
60 if (! dir.isDirectory())
61 throw new BuildException(
62 dir.toString() + " is not a directory");
63
64 Object path = getProject().getReference(pathRefId);
65 if (path == null)
66 throw new BuildException("Unknown reference " + pathRefId);
67 if (! (path instanceof Path))
68 throw new BuildException(pathRefId + " is not a path");
69
70
71 String[] sources = ((Path) path).list();
72
73 FileSet fileSet = new FileSet();
74 fileSet.setProject(getProject());
75 fileSet.setDir(dir);
76 String dirNormal =
77 fileUtils.normalize(dir.getAbsolutePath()).getAbsolutePath();
78 if (! dirNormal.endsWith(File.separator)) {
79 dirNormal += File.separator;
80 }
81
82
83 boolean atLeastOne = false;
84 for (int i = 0; i < sources.length; ++i) {
85 File sourceFile = new File(sources[i]);
86 if (! sourceFile.exists())
87 continue;
88 String relativeName = getRelativeName(dirNormal, sourceFile);
89 if (relativeName == null && !ignoreNonRelative) {
90 throw new BuildException(
91 sources[i] + " is not relative to " + dir.getAbsolutePath());
92 }
93 if (relativeName == null)
94 continue;
95 fileSet.createInclude().setName(relativeName);
96 atLeastOne = true;
97 }
98
99 if (! atLeastOne) {
100 // need to make an empty fileset
101 fileSet.createInclude().setName("a:b:c:d//THis si &&& not a file !!! ");
102 }
103 getProject().addReference(name, fileSet);
104 }
105
106 private String getRelativeName(String dirNormal, File file) {
107 String fileNormal =
108 fileUtils.normalize(file.getAbsolutePath()).getAbsolutePath();
109 if (! fileNormal.startsWith(dirNormal))
110 return null;
111 return fileNormal.substring(dirNormal.length());
112 }
113 }
114