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
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.nbbuild;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import org.apache.tools.ant.*;
047: import org.apache.tools.ant.types.*;
048:
049: /**
050: * @author Michal Zlamal
051: */
052: public class GetModuleName extends Task {
053: String name = null;
054: File root = null;
055:
056: // XXX this is a lousy attr name; conventional for such attrs to
057: // end in 'property' so you realize they refer to a property name
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: /** Root directory of the whole project - ${nb_all} */
063: public void setRoot(File root) {
064: this .root = root;
065: }
066:
067: public void execute() throws BuildException {
068: if (name == null)
069: throw new BuildException(
070: "You must set the property name, where to store the module name",
071: this .getLocation());
072: if (root == null)
073: throw new BuildException("You must set the root dir", this
074: .getLocation());
075: try {
076: File dir = this .getProject().getBaseDir();
077: if (dir.toString().endsWith(
078: java.io.File.separatorChar + "test")) {
079: // when looking for base dir for tests
080: dir = dir.getParentFile();
081: }
082: String rootdir = root.getCanonicalPath();
083: StringBuffer modulename = new StringBuffer();
084: while (dir != null) {
085: if (dir.getCanonicalPath().equals(rootdir)) {
086: break;
087: }
088: if (modulename.length() > 0) {
089: modulename.insert(0, '/');
090: }
091: modulename.insert(0, dir.getName());
092: dir = dir.getParentFile();
093: }
094:
095: //log("Basedir: " + basedir + " rootdir: " + rootdir);
096: if (dir == null) {
097: throw new BuildException("This module ("
098: + this .getProject().getBaseDir()
099: + ") is on different path than the root dir",
100: this .getLocation());
101: }
102: this .getProject().setProperty(name, modulename.toString()); // XXX should be setNewProperty, when that is possible
103: } catch (IOException ex) {
104: throw new BuildException(
105: "Root dir or module's base dir wasn't recognized",
106: ex, this.getLocation());
107: }
108: }
109:
110: }
|