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.io.File;
040: import java.io.IOException;
041: import java.util.Date;
042: import org.apache.tools.ant.BuildException;
043: import org.apache.tools.ant.Task;
044: import org.netbeans.installer.infra.build.ant.utils.Utils;
045:
046: /**
047: * This class is an ant task which creates a product package descriptor based on the
048: * existing project properties and writes it to the specified file.
049: *
050: * @author Kirill Sorokin
051: */
052: public class ProductDescriptor extends Task {
053: /////////////////////////////////////////////////////////////////////////////////
054: // Instance
055: /**
056: * File to which the product descriptor should be written.
057: */
058: private File file;
059:
060: // setters //////////////////////////////////////////////////////////////////////
061: /**
062: * Setter for the 'file' property.
063: *
064: * @param path The new value of the 'file' property.
065: */
066: public void setFile(final String path) {
067: file = new File(path);
068: if (!file.equals(file.getAbsoluteFile())) {
069: file = new File(getProject().getBaseDir(), path);
070: }
071: }
072:
073: // execution ////////////////////////////////////////////////////////////////////
074: /**
075: * Executes the task. This method writes the product package descriptor xml code
076: * to the specified file.
077: *
078: * @throws org.apache.tools.ant.BuildException if a I/O error occurs.
079: */
080: public void execute() throws BuildException {
081: Utils.setProject(getProject());
082:
083: StringBuilder xml = new StringBuilder();
084:
085: // header ///////////////////////////////////////////////////////////////////
086: xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); // NOI18N
087: xml.append("<registry "
088: + // NOI18N
089: "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
090: + // NOI18N
091: "xsi:noNamespaceSchemaLocation=\"registry.xsd\">\n"); // NOI18N
092: xml.append(" <components>\n"); // NOI18N
093:
094: // core data ////////////////////////////////////////////////////////////////
095: String uid = get("product.uid"); // NOI18N
096: final String version = get("product.version"); // NOI18N
097: final String platform = get("product.platforms"); // NOI18N
098: final String status = get("product.status"); // NOI18N
099: final String offset = get("product.offset"); // NOI18N
100: final String expand = get("product.expand"); // NOI18N
101: final String visible = get("product.visible"); // NOI18N
102: final String features = get("product.features"); // NOI18N
103:
104: xml.append(" <product uid=\"" + uid + "\" " + // NOI18N
105: "version=\"" + version + "\" " + // NOI18N
106: "platforms=\"" + platform + "\" " + // NOI18N
107: "status=\"" + status + "\" " + // NOI18N
108: "offset=\"" + offset + "\" " + // NOI18N
109: "expand=\"" + expand + "\" " + // NOI18N
110: "built=\"" + new Date().getTime() + "\" " + // NOI18N
111: "visible=\"" + visible + "\" " + // NOI18N
112: "features=\"" + features + "\">\n"); // NOI18N
113:
114: // locales //////////////////////////////////////////////////////////////////
115: final String locales = get("product.locales.list").trim(); // NOI18N
116:
117: // display name /////////////////////////////////////////////////////////////
118: xml.append(" <display-name>\n"); // NOI18N
119: xml
120: .append(" <default><![CDATA["
121: + // NOI18N
122: get("product.display.name.default")
123: + "]]></default>\n"); // NOI18N
124: if (!locales.equals("")) { // NOI18N
125: for (String locale : locales.split(" ")) { // NOI18N
126: xml.append(" <localized locale=\"" + // NOI18N
127: locale + "\"><![CDATA[" + // NOI18N
128: get("product.display.name." + locale) + // NOI18N
129: "]]></localized>\n"); // NOI18N
130: }
131: }
132: xml.append(" </display-name>\n"); // NOI18N
133:
134: // description //////////////////////////////////////////////////////////////
135: xml.append(" <description>\n"); // NOI18N
136: xml.append(" <default><![CDATA[" + // NOI18N
137: get("product.description.default") + "]]></default>\n"); // NOI18N
138: if (!locales.equals("")) { // NOI18N
139: for (String locale : locales.split(" ")) { // NOI18N
140: xml.append(" <localized locale=\"" + // NOI18N
141: locale + "\"><![CDATA[" + // NOI18N
142: get("product.description." + locale) + // NOI18N
143: "]]></localized>\n"); // NOI18N
144: }
145: }
146: xml.append(" </description>\n"); // NOI18N
147:
148: // icon /////////////////////////////////////////////////////////////////////
149: String size = get("product.icon.size"); // NOI18N
150: String md5 = get("product.icon.md5"); // NOI18N
151: String uri = get("product.icon.correct.uri"); // NOI18N
152:
153: xml.append(" <icon " + // NOI18N
154: "size=\"" + size + "\" " + // NOI18N
155: "md5=\"" + md5 + "\">\n"); // NOI18N
156: xml.append(" <default-uri>" + // NOI18N
157: uri.replace(" ", "%20") + "</default-uri>\n"); // NOI18N
158: xml.append(" </icon>\n"); // NOI18N
159:
160: // properties ///////////////////////////////////////////////////////////////
161: if (getInt("product.properties.length") > 0) { // NOI18N
162: xml.append(" <properties>\n"); // NOI18N
163: for (int i = 1; i <= getInt("product.properties.length"); i++) { // NOI18N
164: String name = get("product.properties." + i + ".name"); // NOI18N
165: String value = get("product.properties." + i + ".value"); // NOI18N
166: xml.append(" <property name=\"" + name + // NOI18N
167: "\"><![CDATA[" + value + "]]></property>\n"); // NOI18N
168: }
169: xml.append(" </properties>\n"); // NOI18N
170: }
171:
172: // configuration logic //////////////////////////////////////////////////////
173: xml.append(" <configuration-logic>\n"); // NOI18N
174: for (int i = 1; i <= getInt("product.logic.length"); i++) { // NOI18N
175: size = get("product.logic." + i + ".size"); // NOI18N
176: md5 = get("product.logic." + i + ".md5"); // NOI18N
177: uri = get("product.logic." + i + ".correct.uri"); // NOI18N
178:
179: xml.append(" <file " + // NOI18N
180: "size=\"" + size + "\" " + // NOI18N
181: "md5=\"" + md5 + "\">\n"); // NOI18N
182:
183: xml.append(" <default-uri>" + // NOI18N
184: uri.replace(" ", "%20") + // NOI18N
185: "</default-uri>\n"); // NOI18N
186: xml.append(" </file>\n"); // NOI18N
187: }
188: xml.append(" </configuration-logic>\n"); // NOI18N
189:
190: // installation data ////////////////////////////////////////////////////////
191: xml.append(" <installation-data>\n"); // NOI18N
192: for (int i = 1; i <= getInt("product.data.length"); i++) { // NOI18N
193: size = get("product.data." + i + ".size"); // NOI18N
194: md5 = get("product.data." + i + ".md5"); // NOI18N
195: uri = get("product.data." + i + ".correct.uri"); // NOI18N
196:
197: xml.append(" <file " + // NOI18N
198: "size=\"" + size + "\" " + // NOI18N
199: "md5=\"" + md5 + "\">\n"); // NOI18N
200: xml.append(" <default-uri>" + // NOI18N
201: uri.replace(" ", "%20") + // NOI18N
202: "</default-uri>\n"); // NOI18N
203: xml.append(" </file>\n"); // NOI18N
204: }
205: xml.append(" </installation-data>\n"); // NOI18N
206:
207: // requirements /////////////////////////////////////////////////////////////
208: xml.append(" <system-requirements>\n"); // NOI18N
209: xml.append(" <disk-space>" + // NOI18N
210: get("product.disk.space") + "</disk-space>\n"); // NOI18N
211: xml.append(" </system-requirements>\n"); // NOI18N
212:
213: // dependencies /////////////////////////////////////////////////////////////
214: if (getInt("product.requirements.length") + // NOI18N
215: getInt("product.conflicts.length") + // NOI18N
216: getInt("product.install-afters.length") > 0) { // NOI18N
217: xml.append(" <dependencies>\n"); // NOI18N
218:
219: for (int i = 1; i <= getInt("product.requirements.length"); i++) { // NOI18N
220: uid = get("product.requirements." + i + ".uid"); // NOI18N
221:
222: String lower = get("product.requirements." + i + // NOI18N
223: ".version-lower"); // NOI18N
224: String upper = get("product.requirements." + i + // NOI18N
225: ".version-upper"); // NOI18N
226: int alternativeRequirements = getInt("product.requirements."
227: + i + // NOI18N
228: ".alternatives.length");// NOI18N
229:
230: if (alternativeRequirements == 0) {
231: xml.append(" <requirement " + // NOI18N
232: "uid=\"" + uid + "\" " + // NOI18N
233: "version-lower=\"" + lower + "\" " + // NOI18N
234: "version-upper=\"" + upper + "\"/>\n"); // NOI18N
235: } else {
236: xml.append(" <requirement " + // NOI18N
237: "uid=\"" + uid + "\" " + // NOI18N
238: "version-lower=\"" + lower + "\" " + // NOI18N
239: "version-upper=\"" + upper + "\">\n"); // NOI18N
240:
241: for (int j = 1; j <= alternativeRequirements; j++) {
242: int reqs = getInt("product.requirements." + i
243: + ".alternatives." + j
244: + ".requirements.length");
245:
246: if (reqs > 0) {
247: xml.append(" <or>\n");
248: for (int k = 1; k <= reqs; k++) {
249: String prefix = "product.requirements."
250: + i + ".alternatives." + j
251: + ".requirements." + k + ".";
252: uid = get(prefix + "uid"); // NOI18N
253: lower = get(prefix + "version-lower"); // NOI18N
254: upper = get(prefix + "version-upper"); // NOI18N
255:
256: xml
257: .append(" <requirement "
258: + // NOI18N
259: "uid=\""
260: + uid
261: + "\" "
262: + // NOI18N
263: "version-lower=\""
264: + lower
265: + "\" "
266: + // NOI18N
267: "version-upper=\""
268: + upper + "\"/>\n"); // NOI18N
269: }
270: xml.append(" </or>\n");
271: }
272: }
273:
274: xml.append(" </requirement>\n");
275: }
276: }
277:
278: for (int i = 1; i <= getInt("product.conflicts.length"); i++) { // NOI18N
279: uid = get("product.conflicts." + i + ".uid"); // NOI18N
280:
281: String lower = get("product.conflicts." + i
282: + ".version-lower"); // NOI18N
283: String upper = get("product.conflicts." + i
284: + ".version-upper"); // NOI18N
285:
286: xml.append(" <conflict " + // NOI18N
287: "uid=\"" + uid + "\" " + // NOI18N
288: "version-lower=\"" + lower + // NOI18N
289: "\" version-upper=\"" + upper + "\"/>\n"); // NOI18N
290: }
291:
292: for (int i = 1; i <= getInt("product.install-afters.length"); i++) { // NOI18N
293: uid = get("product.install-afters." + i + ".uid"); // NOI18N
294:
295: xml.append(" <install-after " + "uid=\""
296: + // NOI18N
297: uid + "\"/>\n"); // NOI18N
298: }
299:
300: xml.append(" </dependencies>\n"); // NOI18N
301: }
302:
303: xml.append(" </product>\n"); // NOI18N
304: xml.append(" </components>\n"); // NOI18N
305: xml.append("</registry>\n"); // NOI18N
306:
307: try {
308: Utils.write(file, xml);
309: } catch (IOException e) {
310: throw new BuildException(e);
311: }
312: }
313:
314: // private //////////////////////////////////////////////////////////////////////
315: /**
316: * Gets a project's property's string value.
317: *
318: * @param name Name of the property whose value is required.
319: * @return The value of the property as a string.
320: */
321: private String get(String name) {
322: return getProject().getProperty(name);
323: }
324:
325: /**
326: * Gets a project's property's integer value.
327: *
328: * @param name Name of the property whose value is required.
329: * @return The value of the property as an integer.
330: */
331: private int getInt(String name) {
332: if (get(name) == null) {
333: return 0;
334: } else {
335: return Integer.parseInt(get(name));
336: }
337: }
338: }
|