001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.resources.selectors;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.types.Resource;
022: import org.apache.tools.ant.types.EnumeratedAttribute;
023:
024: /**
025: * Type file/dir ResourceSelector.
026: * @since Ant 1.7
027: */
028: public class Type implements ResourceSelector {
029:
030: private static final String FILE_ATTR = "file";
031: private static final String DIR_ATTR = "dir";
032:
033: /** Static file type selector. */
034: public static final Type FILE = new Type(new FileDir(FILE_ATTR));
035:
036: /** Static dir type selector. */
037: public static final Type DIR = new Type(new FileDir(DIR_ATTR));
038:
039: /**
040: * Implements the type attribute.
041: */
042: public static class FileDir extends EnumeratedAttribute {
043: private static final String[] VALUES = new String[] {
044: FILE_ATTR, DIR_ATTR };
045:
046: /**
047: * Default constructor.
048: */
049: public FileDir() {
050: }
051:
052: /**
053: * Convenience constructor.
054: * @param value the String EnumeratedAttribute value.
055: */
056: public FileDir(String value) {
057: setValue(value);
058: }
059:
060: /**
061: * Return the possible values.
062: * @return a String array.
063: */
064: public String[] getValues() {
065: return VALUES;
066: }
067: }
068:
069: private FileDir type = null;
070:
071: /**
072: * Default constructor.
073: */
074: public Type() {
075: }
076:
077: /**
078: * Convenience constructor.
079: * @param fd the FileDir type.
080: */
081: public Type(FileDir fd) {
082: setType(fd);
083: }
084:
085: /**
086: * Set type; file|dir.
087: * @param fd a FileDir object.
088: */
089: public void setType(FileDir fd) {
090: type = fd;
091: }
092:
093: /**
094: * Return true if this Resource is selected.
095: * @param r the Resource to check.
096: * @return whether the Resource was selected.
097: */
098: public boolean isSelected(Resource r) {
099: if (type == null) {
100: throw new BuildException("The type attribute is required.");
101: }
102: int i = type.getIndex();
103: return r.isDirectory() ? i == 1 : i == 0;
104: }
105:
106: }
|