1 16 package scriptella.tools.template; 17 18 import scriptella.util.IOUtils; 19 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.Writer ; 26 import java.text.MessageFormat ; 27 import java.util.Map ; 28 29 36 public class TemplateManager { 37 private static final String DEFAULT_ETL_XML = "default.etl.xml"; 38 private static final String DEFAULT_ETL_PROPS = "default.etl.properties"; 39 private Map <String , String > properties; 40 private static final String DEFAULT_BASE_NAME = "etl"; 41 private static final String XML_EXT = ".xml"; 42 private static final String PROPS_EXT = ".properties"; 43 44 public TemplateManager() { 45 } 46 47 50 public TemplateManager(Map <String , String > properties) { 51 this.properties = properties; 52 } 53 54 55 60 public void create() throws IOException { 61 InputStream xml = TemplateManager.class.getResourceAsStream(DEFAULT_ETL_XML); 63 if (xml == null) { 64 throw new IllegalArgumentException ("Resource " + DEFAULT_ETL_XML + " not found"); 65 } 66 InputStream props = getClass().getResourceAsStream(DEFAULT_ETL_PROPS); 67 if (props == null) { 68 throw new IllegalArgumentException ("Resource " + DEFAULT_ETL_PROPS + " not found"); 69 } 70 71 String baseName = defineName(); 72 String xmlName = baseName + XML_EXT; 73 String propsName = baseName + PROPS_EXT; 74 String xmlTemplate = IOUtils.toString(new InputStreamReader (xml)); 75 Writer w = newFileWriter(xmlName); 76 77 w.write(MessageFormat.format(xmlTemplate, propsName)); 78 w.close(); 79 w = newFileWriter(propsName); 80 String propsTemplate = IOUtils.toString(new InputStreamReader (props)); 81 w.write(propsTemplate); 82 w.close(); 83 System.out.println("Files " + xmlName + ", " + propsName + " have been successfully created."); 84 } 85 86 89 String defineName() { 90 for (int i = 0; i < 10; i++) { 91 String 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 ("Too many templates generated. Remove unused."); 97 } 98 99 102 protected Writer newFileWriter(String fileName) throws IOException { 103 return new FileWriter (fileName); 104 } 105 106 111 protected boolean checkFile(String name) { 112 File f = new File (name); 113 return !f.exists(); 114 } 115 } 116 | Popular Tags |