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 java.util.Vector;
23  
24  import org.apache.tools.ant.BuildException;
25  
26  import com.github.maven_nar.cpptasks.CUtil;
27  
28  /**
29   * Preprocessor macro undefinition.
30   *
31   * @author Mark A Russell <a
32   *         href="mailto:mark_russell@csgsystems.com">mark_russell@csg_systems.
33   *         com
34   *         </a>
35   */
36  public class UndefineArgument {
37    /**
38     * This method returns an array of UndefineArgument and DefineArgument's by
39     * merging a base list with an override list.
40     * 
41     * Any define in the base list with a name that appears in the override
42     * list is suppressed. All entries in the override list are preserved
43     * 
44     */
45    public static UndefineArgument[] merge(final UndefineArgument[] base, final UndefineArgument[] override) {
46      if (base.length == 0) {
47        final UndefineArgument[] overrideClone = override.clone();
48        return overrideClone;
49      }
50      if (override.length == 0) {
51        final UndefineArgument[] baseClone = base.clone();
52        return baseClone;
53      }
54      final Vector<UndefineArgument> unduplicated = new Vector<>(base.length);
55      for (final UndefineArgument current : base) {
56        final String currentName = current.getName();
57        boolean match = false;
58        if (currentName == null) {
59          match = true;
60        } else {
61          for (final UndefineArgument over : override) {
62            final String overName = over.getName();
63            if (overName != null && overName.equals(currentName)) {
64              match = true;
65              break;
66            }
67          }
68        }
69        if (!match) {
70          unduplicated.addElement(current);
71        }
72      }
73      final UndefineArgument[] combined = new UndefineArgument[unduplicated.size() + override.length];
74      unduplicated.copyInto(combined);
75      final int offset = unduplicated.size();
76      System.arraycopy(override, 0, combined, offset + 0, override.length);
77      return combined;
78    }
79  
80    private boolean define = false;
81    private String ifCond;
82    private String name;
83    private String unlessCond;
84  
85    public UndefineArgument() {
86    }
87  
88    protected UndefineArgument(final boolean isDefine) {
89      this.define = isDefine;
90    }
91  
92    public void execute() throws org.apache.tools.ant.BuildException {
93      throw new org.apache.tools.ant.BuildException("Not an actual task, but looks like one for documentation purposes");
94    }
95  
96    /** Returns the name of the define */
97    public final String getName() {
98      return this.name;
99    }
100 
101   /** Returns the value of the define */
102   public String getValue() {
103     return null;
104   }
105 
106   /**
107    * Returns true if the define's if and unless conditions (if any) are
108    * satisfied.
109    * 
110    * @exception BuildException
111    *              throws build exception if name is not set
112    */
113   public final boolean isActive(final org.apache.tools.ant.Project p) throws BuildException {
114     if (this.name == null) {
115       throw new BuildException("<define> is missing name attribute");
116     }
117     return CUtil.isActive(p, this.ifCond, this.unlessCond);
118   }
119 
120   /** Returns true if this is a define, false if an undefine. */
121   public final boolean isDefine() {
122     return this.define;
123   }
124 
125   /**
126    * Sets the property name for the 'if' condition.
127    * 
128    * The define will be ignored unless the property is defined.
129    * 
130    * The value of the property is insignificant, but values that would imply
131    * misinterpretation ("false", "no") will throw an exception when
132    * evaluated.
133    * 
134    * @param propName
135    *          property name
136    */
137   public final void setIf(final String propName) {
138     this.ifCond = propName;
139   }
140 
141   /** Set the name attribute */
142   public final void setName(final String name) {
143     this.name = name;
144   }
145 
146   /**
147    * Set the property name for the 'unless' condition.
148    * 
149    * If named property is set, the define will be ignored.
150    * 
151    * The value of the property is insignificant, but values that would imply
152    * misinterpretation ("false", "no") of the behavior will throw an
153    * exception when evaluated.
154    * 
155    * @param propName
156    *          name of property
157    */
158   public final void setUnless(final String propName) {
159     this.unlessCond = propName;
160   }
161 }