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:
019: package org.apache.tools.ant.types.resources.selectors;
020:
021: import org.apache.tools.ant.Project;
022: import org.apache.tools.ant.ProjectHelper;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.ComponentHelper;
025: import org.apache.tools.ant.AntTypeDefinition;
026: import org.apache.tools.ant.types.Resource;
027:
028: /**
029: * InstanceOf ResourceSelector.
030: * @since Ant 1.7
031: */
032: public class InstanceOf implements ResourceSelector {
033: private static final String ONE_ONLY = "Exactly one of class|type must be set.";
034:
035: private Project project;
036: private Class clazz;
037: private String type;
038: private String uri;
039:
040: /**
041: * Set the Project instance for this InstanceOf selector.
042: * @param p the Project instance used for type comparisons.
043: */
044: public void setProject(Project p) {
045: project = p;
046: }
047:
048: /**
049: * Set the class to compare against.
050: * @param c the class.
051: */
052: public void setClass(Class c) {
053: if (clazz != null) {
054: throw new BuildException(
055: "The class attribute has already been set.");
056: }
057: clazz = c;
058: }
059:
060: /**
061: * Set the Ant type to compare against.
062: * @param s the type name.
063: */
064: public void setType(String s) {
065: type = s;
066: }
067:
068: /**
069: * Set the URI in which the Ant type, if specified, should be defined.
070: * @param u the URI.
071: */
072: public void setURI(String u) {
073: uri = u;
074: }
075:
076: /**
077: * Get the comparison class.
078: * @return the Class object.
079: */
080: public Class getCheckClass() {
081: return clazz;
082: }
083:
084: /**
085: * Get the comparison type.
086: * @return the String typename.
087: */
088: public String getType() {
089: return type;
090: }
091:
092: /**
093: * Get the type's URI.
094: * @return the String URI.
095: */
096: public String getURI() {
097: return uri;
098: }
099:
100: /**
101: * Return true if this Resource is selected.
102: * @param r the Resource to check.
103: * @return whether the Resource was selected.
104: * @throws BuildException if an error occurs.
105: */
106: public boolean isSelected(Resource r) {
107: if ((clazz == null) == (type == null)) {
108: throw new BuildException(ONE_ONLY);
109: }
110: Class c = clazz;
111: if (type != null) {
112: if (project == null) {
113: throw new BuildException(
114: "No project set for InstanceOf ResourceSelector; "
115: + "the type attribute is invalid.");
116: }
117: AntTypeDefinition d = ComponentHelper.getComponentHelper(
118: project).getDefinition(
119: ProjectHelper.genComponentName(uri, type));
120: if (d == null) {
121: throw new BuildException("type " + type + " not found.");
122: }
123: try {
124: c = d.innerGetTypeClass();
125: } catch (ClassNotFoundException e) {
126: throw new BuildException(e);
127: }
128: }
129: return c.isAssignableFrom(r.getClass());
130: }
131:
132: }
|