KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > io > ExistsJob


1 package org.oddjob.io;
2
3 import java.io.File JavaDoc;
4 import java.io.Serializable JavaDoc;
5
6
7 /**
8  * @oddjob.description Test if a file exists. This job will flag
9  * complete if the file exists, not complete if it doesn't, and
10  * will signal an exception if the path to the file does not exist.
11  *
12  * @oddjob.example
13  *
14  * <pre>
15  * &lt;sequential&gt;
16  * &lt;exists id="exists" file="data/demo*.rep"/&gt;
17  * &lt;foreach id="found" values="${exists.exists}"&gt;
18  * &lt;child&gt;
19  * &lt;echo text="found ${found.current}" /&gt;
20  * &lt;/child&gt;
21  * &lt;/foreach&gt;
22  * &lt;/sequential&gt;
23  * </pre>
24  */

25
26 public class ExistsJob implements Runnable JavaDoc, Serializable JavaDoc {
27     private static final long serialVersionUID = 20060117;
28
29     /**
30      * @oddjob.property
31      * @oddjob.description A name, can be any text.
32      * @oddjob.required No.
33      */

34     private String JavaDoc name;
35
36     /**
37      * @oddjob.property
38      * @oddjob.description The file, can contain wildcard characters.
39      * @oddjob.required Yes.
40      */

41     private File JavaDoc file;
42
43     /**
44      * @oddjob.property
45      * @oddjob.description The files that match the file spec.
46      * @oddjob.required R/O.
47      */

48     private File JavaDoc[] exists;
49         
50     /**
51      * Get the name.
52      *
53      * @return The name.
54      */

55     public String JavaDoc getName() {
56         return name;
57     }
58     
59     /**
60      * Set the name
61      *
62      * @param name The name.
63      */

64     public void setName(String JavaDoc name) {
65         this.name = name;
66     }
67     
68     /**
69      * Get the file.
70      *
71      * @return The file.
72      */

73     public File JavaDoc getFile() {
74         return file;
75     }
76     
77     /**
78      * Set the file.
79      *
80      * @param The file.
81      */

82     public void setFile(File JavaDoc file) {
83         this.file = file;
84     }
85
86     public File JavaDoc[] getExists() {
87         return exists;
88     }
89     /**
90      * Get the result. Used to set complete/not state.
91      *
92      * @return 0 if file exists.
93      */

94     public int getResult() {
95         if (exists == null) {
96             return -1;
97         }
98         return exists.length > 0 ? 0 : 1;
99     }
100     
101     /*
102      * (non-Javadoc)
103      * @see java.lang.Runnable#run()
104      */

105     public void run() {
106         if (file == null) {
107             throw new IllegalStateException JavaDoc("File must be specified.");
108         }
109         WildcardSpec wild = new WildcardSpec(file);
110         exists = wild.findFiles();
111     }
112     
113     /*
114      * (non-Javadoc)
115      * @see java.lang.Object#toString()
116      */

117     public String JavaDoc toString() {
118         if (name == null) {
119             return "Check File Exists";
120         }
121         return name;
122     }
123 }
124
Popular Tags