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.template;
20  
21  import nl.toolforge.karma.core.KarmaRuntimeException;
22  import nl.toolforge.karma.core.module.SourceModule;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import java.io.BufferedReader;
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.io.InputStreamReader;
31  import java.io.Reader;
32  
33  /***
34   * @author D.A. Smedes
35   * @version $Id: BaseModuleLayoutTemplate.java,v 1.1 2004/11/10 22:25:11 hippe Exp $
36   */
37  public abstract class BaseModuleLayoutTemplate implements ModuleLayoutTemplate {
38  
39    protected static Log logger = LogFactory.getLog(BaseModuleLayoutTemplate.class);
40  
41    public abstract FileTemplate[] getFileElements();
42  
43    public abstract String[] getDirectoryElements();
44  
45    public final void createLayout(File baseDir) throws IOException {
46  
47      FileTemplate[] fileTemplates = getFileElements();
48      String[] templateFiles = new String[fileTemplates.length];
49  
50      for (int i = 0; i < fileTemplates.length; i++) {
51  
52        FileTemplate fileTemplate = fileTemplates[i];
53        logger.debug("Write template '" + fileTemplate.getSource() + "' to '" + fileTemplate.getTarget() + "'.");
54        Reader input = new BufferedReader(new InputStreamReader(SourceModule.class.getResourceAsStream(fileTemplate.getSource().toString().replace('//','/'))));
55  
56        File outputFile = new File(baseDir + File.separator + fileTemplate.getTarget());
57        outputFile.getParentFile().mkdirs();
58        outputFile.createNewFile();
59  
60        FileOutputStream output = new FileOutputStream(outputFile);
61  
62        while (input.ready()) {
63          output.write(input.read());
64        }
65        templateFiles[i] = fileTemplate.getTarget().getPath();
66        logger.debug("Wrote template.");
67      }
68  
69      // Create directories.
70      //
71      String[] dirs = getDirectoryElements();
72  
73      for (int i=0; i < dirs.length; i++) {
74  
75        File dirToAdd = new File(baseDir, dirs[i]);
76  
77        // Try to create the dirs
78        if (!dirToAdd.mkdirs() && !dirToAdd.exists()) {
79          // Failed to create the dirs and they do not exist yet.
80          //
81          throw new KarmaRuntimeException("Error while creating module layout.");
82        }
83      }
84    }
85  }