001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.build.ant;
038:
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.Stack;
042: import org.apache.tools.ant.Project;
043: import org.apache.tools.ant.Task;
044:
045: /**
046: * This class is an ant task which is capable of setting a property value basing on
047: * either a supplied value or a value of another property.
048: *
049: * @author Kirill Sorokin
050: */
051: public class SetProperty extends Task {
052: private List<String> resolving;
053: /////////////////////////////////////////////////////////////////////////////////
054: // Instance
055: /**
056: * Name of the target property whose value should be set.
057: */
058: private String property;
059:
060: /**
061: * Name of the source property whose value should be evaluated and set as the
062: * value of the target property.
063: */
064: private String source;
065:
066: /**
067: * String which should be set as the value for the target property.
068: */
069: private String value;
070:
071: // setters //////////////////////////////////////////////////////////////////////
072: /**
073: * Setter for the 'property' property.
074: *
075: * @param property New value for the 'property' property.
076: */
077: public void setProperty(final String property) {
078: this .property = property;
079: }
080:
081: /**
082: * Setter for the 'source' property.
083: *
084: * @param source New value for the 'source' property.
085: */
086: public void setSource(final String source) {
087: this .source = source;
088: }
089:
090: /**
091: * Setter for the 'value' property.
092: *
093: * @param value New value for the 'value' property.
094: */
095: public void setValue(final String value) {
096: this .value = value;
097: }
098:
099: // execution ////////////////////////////////////////////////////////////////////
100: /**
101: * Executes the task. If the source property was specified, its value is
102: * evaluated and set as the value of the target property. Otherwise the literal
103: * string value is used.
104: */
105: public void execute() {
106: resolving = new ArrayList<String>();
107: final Project project = getProject();
108: final String string = (source != null) ? project
109: .getProperty(source) : value;
110: final String resolved = resolveString(string);
111: log("Setting " + property + " to " + resolved);
112: project.setProperty(property, resolved);
113: }
114:
115: private String resolveString(final String string) {
116: if (string == null || string.equals("")) {
117: return string;
118: }
119: StringBuilder result = new StringBuilder(string.length());
120: StringBuilder buffer = null;
121: Stack<StringBuilder> started = new Stack<StringBuilder>();
122: boolean inside = false;
123:
124: for (int i = 0; i < string.length(); i++) {
125: char c = string.charAt(i);
126: switch (c) {
127: case '$':
128: if (i + 1 < string.length()
129: && string.charAt(i + 1) == '{') {
130: if (inside) {
131: started.push(buffer);
132: }
133: i++;
134: inside = true;
135: buffer = new StringBuilder();
136: } else {
137: (inside ? buffer : result).append("$");
138: }
139: break;
140: case '}':
141: if (inside) {
142: final String propName = buffer.toString();
143: String propValue;
144:
145: if (resolving.contains(propName)) {
146: propValue = null;
147: } else {
148: resolving.add(propName);
149: propValue = resolveString(getProject()
150: .getProperty(propName));
151: resolving.remove(propName);
152: }
153: final String resolved = propValue != null ? propValue
154: : "${" + propName + "}";
155: if (!started.empty()) {
156: buffer = started.pop().append(resolved);
157: } else {
158: result.append(resolved);
159: inside = false;
160: }
161: } else {
162: result.append(c);
163: }
164: break;
165: default:
166: (inside ? buffer : result).append(c);
167: }
168: }
169:
170: if (inside) {
171: do {
172: if (!started.empty()) {
173: buffer = started.pop().append("${").append(buffer);
174: } else {
175: result.append("${").append(buffer);
176: buffer = null;
177: }
178: } while (buffer != null);
179: }
180: return result.toString();
181: }
182: }
|