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.modules.project.ant.task;
043:
044: import java.beans.PropertyVetoException;
045: import java.io.File;
046: import java.io.IOException;
047: import org.apache.tools.ant.BuildException;
048: import org.apache.tools.ant.Task;
049: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
050: import org.openide.filesystems.*;
051:
052: // XXX should this task also do XML Schema validation of project.xml?
053:
054: /**
055: * Ant task to regenerate project metadata files (build scripts).
056: * Currently semantics are identical to that of opening the project
057: * in the IDE's GUI: i.e. both <code>build.xml</code> and <code>build-impl.xml</code>
058: * will be regenerated if they are missing, or if they are out of date with
059: * respect to either <code>project.xml</code> or the stylesheet (and are
060: * not modified according to <code>genfiles.properties</code>).
061: * @author Jesse Glick
062: */
063: public final class RegenerateFilesTask extends Task {
064:
065: /** Standard constructor. */
066: public RegenerateFilesTask() {
067: }
068:
069: private File buildXsl;
070:
071: /**
072: * Set the stylesheet to use for the main build script.
073: * @param f a <code>build.xsl</code> file
074: */
075: public void setBuildXsl(File f) {
076: // XXX could also support jar: URIs etc.
077: buildXsl = f;
078: }
079:
080: private File buildImplXsl;
081:
082: /**
083: * Set the stylesheet to use for the automatic build script.
084: * @param f a <code>build-impl.xsl</code> file
085: */
086: public void setBuildImplXsl(File f) {
087: buildImplXsl = f;
088: }
089:
090: private File projectDir;
091:
092: /**
093: * Set the project directory to regenerate files in.
094: * @param f the top directory of an Ant-based project
095: */
096: public void setProject(File f) {
097: projectDir = f;
098: }
099:
100: public void execute() throws BuildException {
101: if (projectDir == null) {
102: throw new BuildException("Must set 'project' attr",
103: getLocation());
104: }
105: // XXX later may provide more control here...
106: if (buildXsl == null && buildImplXsl == null) {
107: throw new BuildException(
108: "Must set either 'buildxsl' or 'buildimplxsl' attrs or both",
109: getLocation());
110: }
111: try {
112: // Might be running inside IDE, in which case already have a mount...
113: FileObject projectFO = FileUtil.toFileObject(projectDir);
114: if (projectFO == null) {
115: // Probably not running inside IDE, so mount it.
116: // XXX for some reason including masterfs.jar in <taskdef> does not work. Why?
117: // Possibly a bug in AntClassLoader.getResources(String)?
118: LocalFileSystem lfs = new LocalFileSystem();
119: lfs.setRootDirectory(projectDir);
120: Repository.getDefault().addFileSystem(lfs);
121: projectFO = lfs.getRoot();
122: assert projectFO != null;
123: }
124: GeneratedFilesHelper h = new GeneratedFilesHelper(projectFO);
125: if (buildXsl != null
126: && h.refreshBuildScript(
127: GeneratedFilesHelper.BUILD_XML_PATH,
128: buildXsl.toURI().toURL(), true)) {
129: log("Regenerating "
130: + new File(projectDir,
131: GeneratedFilesHelper.BUILD_XML_PATH
132: .replace('/',
133: File.separatorChar))
134: .getAbsolutePath());
135: }
136: if (buildImplXsl != null
137: && h.refreshBuildScript(
138: GeneratedFilesHelper.BUILD_IMPL_XML_PATH,
139: buildImplXsl.toURI().toURL(), true)) {
140: log("Regenerating "
141: + new File(
142: projectDir,
143: GeneratedFilesHelper.BUILD_IMPL_XML_PATH
144: .replace('/',
145: File.separatorChar))
146: .getAbsolutePath());
147: }
148: } catch (IOException e) {
149: throw new BuildException(e, getLocation());
150: } catch (PropertyVetoException e) {
151: throw new BuildException(e, getLocation());
152: }
153: }
154:
155: }
|