KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > tools > template > TemplateManager


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.tools.template;
17
18 import scriptella.util.IOUtils;
19
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * ETL files template manager.
31  * <p>TODO Add support for DB migration script templates
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 public class TemplateManager {
37     private static final String JavaDoc DEFAULT_ETL_XML = "default.etl.xml";
38     private static final String JavaDoc DEFAULT_ETL_PROPS = "default.etl.properties";
39     private Map JavaDoc<String JavaDoc, String JavaDoc> properties;
40     private static final String JavaDoc DEFAULT_BASE_NAME = "etl";
41     private static final String JavaDoc XML_EXT = ".xml";
42     private static final String JavaDoc PROPS_EXT = ".properties";
43
44     public TemplateManager() {
45     }
46
47     /**
48      * Creates a template manager using configuration properties.
49      */

50     public TemplateManager(Map JavaDoc<String JavaDoc, String JavaDoc> properties) {
51         this.properties = properties;
52     }
53
54
55     /**
56      * Produce template files.
57      *
58      * @throws IOException if output fails.
59      */

60     public void create() throws IOException JavaDoc {
61         //Only default template supported yet
62
InputStream JavaDoc xml = TemplateManager.class.getResourceAsStream(DEFAULT_ETL_XML);
63         if (xml == null) {
64             throw new IllegalArgumentException JavaDoc("Resource " + DEFAULT_ETL_XML + " not found");
65         }
66         InputStream JavaDoc props = getClass().getResourceAsStream(DEFAULT_ETL_PROPS);
67         if (props == null) {
68             throw new IllegalArgumentException JavaDoc("Resource " + DEFAULT_ETL_PROPS + " not found");
69         }
70
71         String JavaDoc baseName = defineName();
72         String JavaDoc xmlName = baseName + XML_EXT;
73         String JavaDoc propsName = baseName + PROPS_EXT;
74         String JavaDoc xmlTemplate = IOUtils.toString(new InputStreamReader JavaDoc(xml));
75         Writer JavaDoc w = newFileWriter(xmlName);
76
77         w.write(MessageFormat.format(xmlTemplate, propsName));
78         w.close();
79         w = newFileWriter(propsName);
80         String JavaDoc propsTemplate = IOUtils.toString(new InputStreamReader JavaDoc(props));
81         w.write(propsTemplate);
82         w.close();
83         System.out.println("Files " + xmlName + ", " + propsName + " have been successfully created.");
84     }
85
86     /**
87      * Defines base name for ETL.
88      */

89     String JavaDoc defineName() {
90         for (int i = 0; i < 10; i++) {
91             String JavaDoc name = DEFAULT_BASE_NAME + ((i > 0) ? ("[" + i + "]") : "");
92             if (checkFile(name + XML_EXT) && checkFile(name + PROPS_EXT)) {
93                 return name;
94             }
95         }
96         throw new IllegalStateException JavaDoc("Too many templates generated. Remove unused.");
97     }
98
99     /**
100      * Template factory method for writers.
101      */

102     protected Writer JavaDoc newFileWriter(String JavaDoc fileName) throws IOException JavaDoc {
103         return new FileWriter JavaDoc(fileName);
104     }
105
106     /**
107      * Returns true if file doesn't exist.
108      *
109      * @param name file name.
110      */

111     protected boolean checkFile(String JavaDoc name) {
112         File JavaDoc f = new File JavaDoc(name);
113         return !f.exists();
114     }
115 }
116
Popular Tags