]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*\r |
2 | * \r | |
3 | * Copyright 2001-2004 The Ant-Contrib project\r | |
4 | *\r | |
5 | * Licensed under the Apache License, Version 2.0 (the "License");\r | |
6 | * you may not use this file except in compliance with the License.\r | |
7 | * You may obtain a copy of the License at\r | |
8 | *\r | |
9 | * http://www.apache.org/licenses/LICENSE-2.0\r | |
10 | *\r | |
11 | * Unless required by applicable law or agreed to in writing, software\r | |
12 | * distributed under the License is distributed on an "AS IS" BASIS,\r | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r | |
14 | * See the License for the specific language governing permissions and\r | |
15 | * limitations under the License.\r | |
16 | */\r | |
17 | package net.sf.antcontrib.cpptasks.types;\r | |
18 | import java.util.Vector;\r | |
19 | \r | |
20 | import net.sf.antcontrib.cpptasks.CUtil;\r | |
21 | \r | |
22 | import org.apache.tools.ant.BuildException;\r | |
23 | /**\r | |
24 | * Preprocessor macro undefinition.\r | |
25 | * \r | |
26 | * @author Mark A Russell <a\r | |
27 | * href="mailto:mark_russell@csgsystems.com">mark_russell@csg_systems.com\r | |
28 | * </a>\r | |
29 | */\r | |
30 | public class UndefineArgument {\r | |
31 | /**\r | |
32 | * This method returns an array of UndefineArgument and DefineArgument's by\r | |
33 | * merging a base list with an override list.\r | |
34 | * \r | |
35 | * Any define in the base list with a name that appears in the override\r | |
36 | * list is suppressed. All entries in the override list are preserved\r | |
37 | * \r | |
38 | */\r | |
39 | public static UndefineArgument[] merge(UndefineArgument[] base,\r | |
40 | UndefineArgument[] override) {\r | |
41 | if (base.length == 0) {\r | |
42 | UndefineArgument[] overrideClone = (UndefineArgument[]) override\r | |
43 | .clone();\r | |
44 | return overrideClone;\r | |
45 | }\r | |
46 | if (override.length == 0) {\r | |
47 | UndefineArgument[] baseClone = (UndefineArgument[]) base.clone();\r | |
48 | return baseClone;\r | |
49 | }\r | |
50 | Vector unduplicated = new Vector(base.length);\r | |
51 | for (int i = 0; i < base.length; i++) {\r | |
52 | UndefineArgument current = base[i];\r | |
53 | String currentName = current.getName();\r | |
54 | boolean match = false;\r | |
55 | if (currentName == null) {\r | |
56 | match = true;\r | |
57 | } else {\r | |
58 | for (int j = 0; j < override.length; j++) {\r | |
59 | UndefineArgument over = override[j];\r | |
60 | String overName = over.getName();\r | |
61 | if (overName != null && overName.equals(currentName)) {\r | |
62 | match = true;\r | |
63 | break;\r | |
64 | }\r | |
65 | }\r | |
66 | }\r | |
67 | if (!match) {\r | |
68 | unduplicated.addElement(current);\r | |
69 | }\r | |
70 | }\r | |
71 | UndefineArgument[] combined = new UndefineArgument[unduplicated.size()\r | |
72 | + override.length];\r | |
73 | unduplicated.copyInto(combined);\r | |
74 | int offset = unduplicated.size();\r | |
75 | for (int i = 0; i < override.length; i++) {\r | |
76 | combined[offset + i] = override[i];\r | |
77 | }\r | |
78 | return combined;\r | |
79 | }\r | |
80 | private boolean define = false;\r | |
81 | private String ifCond;\r | |
82 | private String name;\r | |
83 | private String unlessCond;\r | |
84 | public UndefineArgument() {\r | |
85 | }\r | |
86 | protected UndefineArgument(boolean isDefine) {\r | |
87 | this.define = isDefine;\r | |
88 | }\r | |
89 | public void execute() throws org.apache.tools.ant.BuildException {\r | |
90 | throw new org.apache.tools.ant.BuildException(\r | |
91 | "Not an actual task, but looks like one for documentation purposes");\r | |
92 | }\r | |
93 | /** Returns the name of the define */\r | |
94 | public final String getName() {\r | |
95 | return name;\r | |
96 | }\r | |
97 | /** Returns the value of the define */\r | |
98 | public String getValue() {\r | |
99 | return null;\r | |
100 | }\r | |
101 | /**\r | |
102 | * Returns true if the define's if and unless conditions (if any) are\r | |
103 | * satisfied.\r | |
104 | * \r | |
105 | * @exception BuildException\r | |
106 | * throws build exception if name is not set\r | |
107 | */\r | |
108 | public final boolean isActive(org.apache.tools.ant.Project p)\r | |
109 | throws BuildException {\r | |
110 | if (name == null) {\r | |
111 | throw new BuildException("<define> is missing name attribute");\r | |
112 | }\r | |
113 | return CUtil.isActive(p, ifCond, unlessCond);\r | |
114 | }\r | |
115 | /** Returns true if this is a define, false if an undefine. */\r | |
116 | public final boolean isDefine() {\r | |
117 | return define;\r | |
118 | }\r | |
119 | /**\r | |
120 | * Sets the property name for the 'if' condition.\r | |
121 | * \r | |
122 | * The define will be ignored unless the property is defined.\r | |
123 | * \r | |
124 | * The value of the property is insignificant, but values that would imply\r | |
125 | * misinterpretation ("false", "no") will throw an exception when\r | |
126 | * evaluated.\r | |
127 | * \r | |
128 | * @param propName\r | |
129 | * property name\r | |
130 | */\r | |
131 | public final void setIf(String propName) {\r | |
132 | ifCond = propName;\r | |
133 | }\r | |
134 | /** Set the name attribute */\r | |
135 | public final void setName(String name) {\r | |
136 | this.name = name;\r | |
137 | }\r | |
138 | /**\r | |
139 | * Set the property name for the 'unless' condition.\r | |
140 | * \r | |
141 | * If named property is set, the define will be ignored.\r | |
142 | * \r | |
143 | * The value of the property is insignificant, but values that would imply\r | |
144 | * misinterpretation ("false", "no") of the behavior will throw an\r | |
145 | * exception when evaluated.\r | |
146 | * \r | |
147 | * @param propName\r | |
148 | * name of property\r | |
149 | */\r | |
150 | public final void setUnless(String propName) {\r | |
151 | unlessCond = propName;\r | |
152 | }\r | |
153 | }\r |