KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ant > task > RegenerateFilesTask


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ant.task;
21
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
28 import org.openide.filesystems.*;
29
30 // XXX should this task also do XML Schema validation of project.xml?
31

32 /**
33  * Ant task to regenerate project metadata files (build scripts).
34  * Currently semantics are identical to that of opening the project
35  * in the IDE's GUI: i.e. both <code>build.xml</code> and <code>build-impl.xml</code>
36  * will be regenerated if they are missing, or if they are out of date with
37  * respect to either <code>project.xml</code> or the stylesheet (and are
38  * not modified according to <code>genfiles.properties</code>).
39  * @author Jesse Glick
40  */

41 public final class RegenerateFilesTask extends Task {
42     
43     /** Standard constructor. */
44     public RegenerateFilesTask() {}
45     
46     private File JavaDoc buildXsl;
47     /**
48      * Set the stylesheet to use for the main build script.
49      * @param f a <code>build.xsl</code> file
50      */

51     public void setBuildXsl(File JavaDoc f) {
52         // XXX could also support jar: URIs etc.
53
buildXsl = f;
54     }
55     
56     private File JavaDoc buildImplXsl;
57     /**
58      * Set the stylesheet to use for the automatic build script.
59      * @param f a <code>build-impl.xsl</code> file
60      */

61     public void setBuildImplXsl(File JavaDoc f) {
62         buildImplXsl = f;
63     }
64     
65     private File JavaDoc projectDir;
66     /**
67      * Set the project directory to regenerate files in.
68      * @param f the top directory of an Ant-based project
69      */

70     public void setProject(File JavaDoc f) {
71         projectDir = f;
72     }
73     
74     public void execute() throws BuildException {
75         if (projectDir == null) {
76             throw new BuildException("Must set 'project' attr", getLocation());
77         }
78         // XXX later may provide more control here...
79
if (buildXsl == null && buildImplXsl == null) {
80             throw new BuildException("Must set either 'buildxsl' or 'buildimplxsl' attrs or both", getLocation());
81         }
82         try {
83             // Might be running inside IDE, in which case already have a mount...
84
FileObject projectFO = FileUtil.toFileObject(projectDir);
85             if (projectFO == null) {
86                 // Probably not running inside IDE, so mount it.
87
// XXX for some reason including masterfs.jar in <taskdef> does not work. Why?
88
// Possibly a bug in AntClassLoader.getResources(String)?
89
LocalFileSystem lfs = new LocalFileSystem();
90                 lfs.setRootDirectory(projectDir);
91                 Repository.getDefault().addFileSystem(lfs);
92                 projectFO = lfs.getRoot();
93                 assert projectFO != null;
94             }
95             GeneratedFilesHelper h = new GeneratedFilesHelper(projectFO);
96             if (buildXsl != null && h.refreshBuildScript(GeneratedFilesHelper.BUILD_XML_PATH, buildXsl.toURI().toURL(), true)) {
97                 log("Regenerating " + new File JavaDoc(projectDir, GeneratedFilesHelper.BUILD_XML_PATH.replace('/', File.separatorChar)).getAbsolutePath());
98             }
99             if (buildImplXsl != null && h.refreshBuildScript(GeneratedFilesHelper.BUILD_IMPL_XML_PATH, buildImplXsl.toURI().toURL(), true)) {
100                 log("Regenerating " + new File JavaDoc(projectDir, GeneratedFilesHelper.BUILD_IMPL_XML_PATH.replace('/', File.separatorChar)).getAbsolutePath());
101             }
102         } catch (IOException JavaDoc e) {
103             throw new BuildException(e, getLocation());
104         } catch (PropertyVetoException JavaDoc e) {
105             throw new BuildException(e, getLocation());
106         }
107     }
108     
109 }
110
Popular Tags