View Javadoc

1   /*
2   Karma core - Core of the Karma application
3   Copyright (C) 2004  Toolforge <www.toolforge.nl>
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  package nl.toolforge.karma.core.module;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.io.BufferedWriter;
25  import java.io.File;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.io.Writer;
29  
30  /***
31   * @author D.A. Smedes
32   * @version $Id: ModuleDescriptor.java,v 1.5 2004/11/10 22:25:11 hippe Exp $
33   */
34  public class ModuleDescriptor {
35  
36    protected static Log logger = LogFactory.getLog(ModuleDescriptor.class);
37  
38    private Module.Type moduleType = null;
39  
40    public ModuleDescriptor(Module module) {
41  
42      if (module == null) {
43        throw new IllegalArgumentException("Module type cannot be null.");
44      }
45  
46      // todo should be implemented by a method overridden by specific module types
47      //
48      if (module instanceof LibModule) {
49        this.moduleType = Module.LIBRARY_MODULE;
50      } else if (module instanceof SourceModule) {
51        this.moduleType = Module.JAVA_SOURCE_MODULE;
52      } else if (module instanceof JavaEnterpriseApplicationModule) {
53        this.moduleType = Module.JAVA_ENTERPRISE_APPLICATION;
54      } else if (module instanceof JavaWebApplicationModule) {
55        this.moduleType = Module.JAVA_WEB_APPLICATION;
56      } else if (module instanceof OtherModule) {
57        this.moduleType = Module.OTHER_MODULE;
58      }
59    }
60  
61    /***
62     * Writes the <code>module-descriptor.xml</code> file to <code>dir</code>.
63     *
64     * @param dir An existing directory where the <code>module-descriptor.xml</code> file should be created.
65     */
66    public void createFile(File dir) throws IOException {
67  
68      StringBuffer buffer = new StringBuffer();
69  
70      buffer.append("<?xml version=\"1.0\"?>\n");
71  
72      buffer.append("<module-descriptor version=\"1-0\">\n");
73      buffer.append("  <type>").append(moduleType.getType()).append("</type>\n");
74      buffer.append("  <layout-specification/> <!-- for future usage -->\n");
75      buffer.append("</module-descriptor>\n");
76  
77      Writer writer = new BufferedWriter(new FileWriter(new File(dir, "module-descriptor.xml")));
78      writer.write(buffer.toString());
79  
80      logger.debug("Creating `module-descriptor.xml` for type : " + moduleType.getType());
81  
82      writer.flush();
83    }
84  
85  }