1 package com.genimen.djeneric.tools.generator.util; 2 3 import java.io.BufferedReader ; 4 import java.io.BufferedWriter ; 5 import java.io.File ; 6 import java.io.FileReader ; 7 import java.io.FileWriter ; 8 import java.io.IOException ; 9 import java.util.ArrayList ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 15 public class ProjectProperties 16 { 17 public final static int OVERWRITE_ASK = 0; 18 public final static int OVERWRITE_ALWAYS = 1; 19 public final static int OVERWRITE_NEVER = 2; 20 private String _outputPath; 21 private HashMap _defines; 22 private ArrayList _templateSets; 23 private Properties _templateProperties = new Properties (); 24 private int _overwriteAction = OVERWRITE_ASK; 25 private boolean _openAfterGenerate = false; 26 public final static String TEMPLATE_ENABLED_KEY = ".enabled"; 27 private boolean _modified = false; 28 29 public ProjectProperties() 30 { 31 clear(); 32 setModified(false); 33 } 34 35 public void setModified(boolean b) 36 { 37 _modified = b; 38 } 39 40 public boolean isModified() 41 { 42 return _modified; 43 } 44 45 public void copyInto(ProjectProperties dest) 46 { 47 dest._outputPath = _outputPath; 48 dest._defines = (HashMap ) _defines.clone(); 49 dest._templateSets = (ArrayList ) _templateSets.clone(); 50 dest._templateProperties = (Properties ) _templateProperties.clone(); 51 dest._overwriteAction = _overwriteAction; 52 dest._openAfterGenerate = _openAfterGenerate; 53 dest.setModified(true); 54 } 55 56 public boolean isOpenAfterGenerate() 57 { 58 return _openAfterGenerate; 59 } 60 61 public void setOpenAfterGenerate(boolean openAfterGenerate) 62 { 63 _openAfterGenerate = openAfterGenerate; 64 setModified(true); 65 } 66 67 public int getOverwriteAction() 68 { 69 return _overwriteAction; 70 } 71 72 public void setOverwriteAction(int action) 73 { 74 _overwriteAction = action; 75 setModified(true); 76 } 77 78 public String getOutputPath() 79 { 80 return _outputPath; 81 } 82 83 public void setOutputPath(String outputPath) 84 { 85 _outputPath = outputPath; 86 setModified(true); 87 } 88 89 public void addDefine(String key, String value) 90 { 91 _defines.put(key, value); 92 setModified(true); 93 } 94 95 public void removeDefine(String key) 96 { 97 _defines.remove(key); 98 setModified(true); 99 } 100 101 public Iterator getDefineKeyIterator() 102 { 103 return _defines.keySet().iterator(); 104 } 105 106 public String getDefine(String key) 107 { 108 return (String ) _defines.get(key); 109 } 110 111 public Iterator getTemplateSetIterator() 112 { 113 return _templateSets.iterator(); 114 } 115 116 public String [] getTemplateSets() 117 { 118 return (String []) _templateSets.toArray(new String [0]); 119 } 120 121 public void addTemplate(String id) 122 { 123 _templateSets.add(id); 124 Collections.sort(_templateSets); 125 } 126 127 public void removeTemplate(String id) 128 { 129 _templateSets.remove(id); 130 setModified(true); 131 } 132 133 public void clear() 134 { 135 _outputPath = ""; 136 _defines = new HashMap (); 137 _templateSets = new ArrayList (); 138 setModified(true); 139 } 140 141 public void save(String location) throws Exception 142 { 143 ProjectDocumentHandler pdh = new ProjectDocumentHandler(); 144 BufferedWriter w = new BufferedWriter (new FileWriter (location)); 145 w.write(pdh.createXmlString(this)); 146 w.close(); 147 setModified(false); 148 } 149 150 public void load(String location) throws Exception 151 { 152 ProjectDocumentHandler pdh = new ProjectDocumentHandler(readFile(new File (location))); 153 clear(); 154 pdh.load(this); 155 setModified(false); 156 } 157 158 private String readFile(File inFile) throws IOException 159 { 160 BufferedReader br = new BufferedReader (new FileReader (inFile)); 161 StringBuffer src = new StringBuffer (100); 162 String ln; 163 while ((ln = br.readLine()) != null) 164 { 165 src.append(ln); 166 src.append("\n"); 167 } 168 br.close(); 169 return src.toString(); 170 } 171 172 public void enableTemplate(String moduleName, boolean shouldEnable) 173 { 174 _templateProperties.setProperty(moduleName + TEMPLATE_ENABLED_KEY, "" + shouldEnable); 175 setModified(true); 176 } 177 178 public boolean isTemplateEnabled(String moduleName) 179 { 180 return _templateProperties.getProperty(moduleName + TEMPLATE_ENABLED_KEY, "true").equals("true"); 181 } 182 183 Properties getTemplateProperties() 184 { 185 return _templateProperties; 186 } 187 } | Popular Tags |