KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > BaseSelector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors;
20
21 import java.io.File JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.types.DataType;
25
26 /**
27  * A convenience base class that you can subclass Selectors from. It
28  * provides some helpful common behaviour. Note that there is no need
29  * for Selectors to inherit from this class, it is only necessary that
30  * they implement FileSelector.
31  *
32  * @since 1.5
33  */

34 public abstract class BaseSelector extends DataType implements FileSelector {
35
36     private String JavaDoc errmsg = null;
37
38
39     /**
40      * Do nothing constructor.
41      */

42     public BaseSelector() {
43     }
44
45     /**
46      * Allows all selectors to indicate a setup error. Note that only
47      * the first error message is recorded.
48      *
49      * @param msg The error message any BuildException should throw.
50      */

51     public void setError(String JavaDoc msg) {
52         if (errmsg == null) {
53             errmsg = msg;
54         }
55     }
56
57     /**
58      * Returns any error messages that have been set.
59      *
60      * @return the error condition
61      */

62     public String JavaDoc getError() {
63         return errmsg;
64     }
65
66
67     /**
68      * <p>Subclasses can override this method to provide checking of their
69      * state. So long as they call validate() from isSelected(), this will
70      * be called automatically (unless they override validate()).</p>
71      * <p>Implementations should check for incorrect settings and call
72      * setError() as necessary.</p>
73      */

74     public void verifySettings() {
75         if (isReference()) {
76             ((BaseSelector) getCheckedRef()).verifySettings();
77         }
78     }
79
80
81     /**
82      * Subclasses can use this to throw the requisite exception
83      * in isSelected() in the case of an error condition.
84      */

85     public void validate() {
86         if (getError() == null) {
87             verifySettings();
88         }
89         if (getError() != null) {
90             throw new BuildException(errmsg);
91         }
92     }
93
94     /**
95      * Method that each selector will implement to create their
96      * selection behaviour. If there is a problem with the setup
97      * of a selector, it can throw a BuildException to indicate
98      * the problem.
99      *
100      * @param basedir A java.io.File object for the base directory
101      * @param filename The name of the file to check
102      * @param file A File object for this filename
103      * @return whether the file should be selected or not
104      */

105     public abstract boolean isSelected(File JavaDoc basedir, String JavaDoc filename,
106                                        File JavaDoc file);
107
108 }
109
110
111
Popular Tags