View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.types;
21  
22  import org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.Project;
24  import org.apache.tools.ant.types.AbstractFileSet;
25  import org.apache.tools.ant.types.FileSet;
26  
27  import com.github.maven_nar.cpptasks.CUtil;
28  
29  /**
30   * An Ant FileSet object augmented with if and unless conditions.
31   *
32   * @author Curt Arnold
33   */
34  public class ConditionalFileSet extends FileSet {
35    private String ifCond;
36    private String unlessCond;
37  
38    public ConditionalFileSet() {
39    }
40  
41    public void execute() throws org.apache.tools.ant.BuildException {
42      throw new org.apache.tools.ant.BuildException("Not an actual task, but looks like one for documentation purposes");
43    }
44  
45    /**
46     * overrides FileSet's implementation which would throw an exception since
47     * the referenced object isn't this type.
48     */
49    @Override
50    protected AbstractFileSet getRef(final Project p) {
51      return (AbstractFileSet) this.ref.getReferencedObject(p);
52    }
53  
54    /**
55     * Returns true if the Path's if and unless conditions (if any) are
56     * satisfied.
57     */
58    public boolean isActive() throws BuildException {
59      final Project p = getProject();
60      if (p == null) {
61        throw new java.lang.IllegalStateException("setProject() should have been called");
62      }
63      return CUtil.isActive(p, this.ifCond, this.unlessCond);
64    }
65  
66    /**
67     * Sets the property name for the 'if' condition.
68     * 
69     * The fileset will be ignored unless the property is defined.
70     * 
71     * The value of the property is insignificant, but values that would imply
72     * misinterpretation ("false", "no") will throw an exception when
73     * evaluated.
74     */
75    public void setIf(final String propName) {
76      this.ifCond = propName;
77    }
78  
79    /**
80     * Set the property name for the 'unless' condition.
81     * 
82     * If named property is set, the fileset will be ignored.
83     * 
84     * The value of the property is insignificant, but values that would imply
85     * misinterpretation ("false", "no") of the behavior will throw an
86     * exception when evaluated.
87     * 
88     * @param propName
89     *          name of property
90     */
91    public void setUnless(final String propName) {
92      this.unlessCond = propName;
93    }
94  }