KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > util > ProjectProperties


1 package com.genimen.djeneric.tools.generator.util;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.BufferedWriter JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileReader JavaDoc;
7 import java.io.FileWriter JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
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 JavaDoc _outputPath;
21   private HashMap JavaDoc _defines;
22   private ArrayList JavaDoc _templateSets;
23   private Properties JavaDoc _templateProperties = new Properties JavaDoc();
24   private int _overwriteAction = OVERWRITE_ASK;
25   private boolean _openAfterGenerate = false;
26   public final static String JavaDoc 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 JavaDoc) _defines.clone();
49     dest._templateSets = (ArrayList JavaDoc) _templateSets.clone();
50     dest._templateProperties = (Properties JavaDoc) _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 JavaDoc getOutputPath()
79   {
80     return _outputPath;
81   }
82
83   public void setOutputPath(String JavaDoc outputPath)
84   {
85     _outputPath = outputPath;
86     setModified(true);
87   }
88
89   public void addDefine(String JavaDoc key, String JavaDoc value)
90   {
91     _defines.put(key, value);
92     setModified(true);
93   }
94
95   public void removeDefine(String JavaDoc key)
96   {
97     _defines.remove(key);
98     setModified(true);
99   }
100
101   public Iterator JavaDoc getDefineKeyIterator()
102   {
103     return _defines.keySet().iterator();
104   }
105
106   public String JavaDoc getDefine(String JavaDoc key)
107   {
108     return (String JavaDoc) _defines.get(key);
109   }
110
111   public Iterator JavaDoc getTemplateSetIterator()
112   {
113     return _templateSets.iterator();
114   }
115
116   public String JavaDoc[] getTemplateSets()
117   {
118     return (String JavaDoc[]) _templateSets.toArray(new String JavaDoc[0]);
119   }
120
121   public void addTemplate(String JavaDoc id)
122   {
123     _templateSets.add(id);
124     Collections.sort(_templateSets);
125   }
126
127   public void removeTemplate(String JavaDoc id)
128   {
129     _templateSets.remove(id);
130     setModified(true);
131   }
132
133   public void clear()
134   {
135     _outputPath = "";
136     _defines = new HashMap JavaDoc();
137     _templateSets = new ArrayList JavaDoc();
138     setModified(true);
139   }
140
141   public void save(String JavaDoc location) throws Exception JavaDoc
142   {
143     ProjectDocumentHandler pdh = new ProjectDocumentHandler();
144     BufferedWriter JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(location));
145     w.write(pdh.createXmlString(this));
146     w.close();
147     setModified(false);
148   }
149
150   public void load(String JavaDoc location) throws Exception JavaDoc
151   {
152     ProjectDocumentHandler pdh = new ProjectDocumentHandler(readFile(new File JavaDoc(location)));
153     clear();
154     pdh.load(this);
155     setModified(false);
156   }
157
158   private String JavaDoc readFile(File JavaDoc inFile) throws IOException JavaDoc
159   {
160     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
161     StringBuffer JavaDoc src = new StringBuffer JavaDoc(100);
162     String JavaDoc 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 JavaDoc moduleName, boolean shouldEnable)
173   {
174     _templateProperties.setProperty(moduleName + TEMPLATE_ENABLED_KEY, "" + shouldEnable);
175     setModified(true);
176   }
177
178   public boolean isTemplateEnabled(String JavaDoc moduleName)
179   {
180     return _templateProperties.getProperty(moduleName + TEMPLATE_ENABLED_KEY, "true").equals("true");
181   }
182
183   Properties JavaDoc getTemplateProperties()
184   {
185     return _templateProperties;
186   }
187 }
Popular Tags