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  import org.apache.tools.ant.types.DataType;
26  import org.apache.tools.ant.types.Reference;
27  
28  import com.github.maven_nar.cpptasks.CUtil;
29  
30  /**
31   * Set of preprocessor macro defines and undefines.
32   *
33   * @author Mark A Russell <a
34   *         href="mailto:mark_russell@csgsystems.com">mark_russell@csg_systems.
35   *         com
36   *         </a>
37   * @author Adam Murdoch
38   */
39  public class DefineSet extends DataType {
40    private final Vector<UndefineArgument> defineList = new Vector<>();
41    private String ifCond = null;
42    private String unlessCond = null;
43  
44    /**
45     * 
46     * Adds a define element.
47     * 
48     * @throws BuildException
49     *           if reference
50     */
51    public void addDefine(final DefineArgument arg) throws BuildException {
52      if (isReference()) {
53        throw noChildrenAllowed();
54      }
55      this.defineList.addElement(arg);
56    }
57  
58    /** Adds defines/undefines. */
59    private void addDefines(final String[] defs, final boolean isDefine) {
60      for (final String def2 : defs) {
61        UndefineArgument def;
62        if (isDefine) {
63          def = new DefineArgument();
64        } else {
65          def = new UndefineArgument();
66        }
67        def.setName(def2);
68        this.defineList.addElement(def);
69      }
70    }
71  
72    /**
73     * 
74     * Adds an undefine element.
75     * 
76     * @throws BuildException
77     *           if reference
78     */
79    public void addUndefine(final UndefineArgument arg) throws BuildException {
80      if (isReference()) {
81        throw noChildrenAllowed();
82      }
83      this.defineList.addElement(arg);
84    }
85  
86    public void execute() throws org.apache.tools.ant.BuildException {
87      throw new org.apache.tools.ant.BuildException("Not an actual task, but looks like one for documentation purposes");
88    }
89  
90    /** Returns the defines and undefines in this set. */
91    public UndefineArgument[] getDefines() throws BuildException {
92      if (isReference()) {
93        final DefineSet defset = (DefineSet) getCheckedRef(DefineSet.class, "DefineSet");
94        return defset.getDefines();
95      } else {
96        if (isActive()) {
97          final UndefineArgument[] defs = new UndefineArgument[this.defineList.size()];
98          this.defineList.copyInto(defs);
99          return defs;
100       } else {
101         return new UndefineArgument[0];
102       }
103     }
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() throws BuildException {
114     return CUtil.isActive(getProject(), this.ifCond, this.unlessCond);
115   }
116 
117   /**
118    * A comma-separated list of preprocessor macros to define. Use nested
119    * define elements to define macro values.
120    * 
121    * @param defList
122    *          comma-separated list of preprocessor macros
123    * @throws BuildException
124    *           throw if defineset is a reference
125    */
126   public void setDefine(final CUtil.StringArrayBuilder defList) throws BuildException {
127     if (isReference()) {
128       throw tooManyAttributes();
129     }
130     addDefines(defList.getValue(), true);
131   }
132 
133   /**
134    * Sets a description of the current data type.
135    */
136   @Override
137   public void setDescription(final String desc) {
138     super.setDescription(desc);
139   }
140 
141   /**
142    * Sets an id that can be used to reference this element.
143    * 
144    * @param id
145    *          id
146    */
147   public void setId(final String id) {
148     //
149     // this is actually accomplished by a different
150     // mechanism, but we can document it
151     //
152   }
153 
154   /**
155    * Sets the property name for the 'if' condition.
156    * 
157    * The define will be ignored unless the property is defined.
158    * 
159    * The value of the property is insignificant, but values that would imply
160    * misinterpretation ("false", "no") will throw an exception when
161    * evaluated.
162    * 
163    * @param propName
164    *          property name
165    */
166   public final void setIf(final String propName) {
167     this.ifCond = propName;
168   }
169 
170   /**
171    * Specifies that this element should behave as if the content of the
172    * element with the matching id attribute was inserted at this location. If
173    * specified, no other attributes or child content should be specified,
174    * other than "description".
175    * 
176    */
177   @Override
178   public void setRefid(final Reference r) throws BuildException {
179     if (!this.defineList.isEmpty()) {
180       throw tooManyAttributes();
181     }
182     super.setRefid(r);
183   }
184 
185   /**
186    * A comma-separated list of preprocessor macros to undefine.
187    * 
188    * @param undefList
189    *          comma-separated list of preprocessor macros
190    * @throws BuildException
191    *           throw if defineset is a reference
192    */
193   public void setUndefine(final CUtil.StringArrayBuilder undefList) throws BuildException {
194     if (isReference()) {
195       throw tooManyAttributes();
196     }
197     addDefines(undefList.getValue(), false);
198   }
199 
200   /**
201    * Set the property name for the 'unless' condition.
202    * 
203    * If named property is set, the define will be ignored.
204    * 
205    * The value of the property is insignificant, but values that would imply
206    * misinterpretation ("false", "no") of the behavior will throw an
207    * exception when evaluated.
208    * 
209    * @param propName
210    *          name of property
211    */
212   public final void setUnless(final String propName) {
213     this.unlessCond = propName;
214   }
215 }