gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.lib / org.gvsig.scripting.lib.impl / src / main / java / org / gvsig / scripting / impl / AbstractUnit.java @ 468
History | View | Annotate | Download (9.16 KB)
1 |
package org.gvsig.scripting.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.List; |
6 |
import org.apache.commons.io.FileUtils; |
7 |
|
8 |
import org.ini4j.Ini; |
9 |
import org.slf4j.Logger; |
10 |
import org.slf4j.LoggerFactory; |
11 |
import org.apache.commons.io.FilenameUtils; |
12 |
import org.gvsig.scripting.ScriptingFolder; |
13 |
import org.gvsig.scripting.ScriptingManager; |
14 |
import org.gvsig.scripting.ScriptingScript; |
15 |
import org.gvsig.scripting.ScriptingUnit; |
16 |
|
17 |
public abstract class AbstractUnit implements ScriptingUnit, Unit { |
18 |
|
19 |
private static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class); |
20 |
|
21 |
protected DefaultScriptingManager manager;
|
22 |
|
23 |
protected String id; |
24 |
protected String name = null; |
25 |
protected String description; |
26 |
protected String createdBy; |
27 |
protected String version; |
28 |
protected ScriptingFolder parent;
|
29 |
protected String typename; |
30 |
|
31 |
public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id) { |
32 |
this.parent = parent;
|
33 |
this.manager = (DefaultScriptingManager) manager;
|
34 |
this.typename = typename;
|
35 |
this.id = id;
|
36 |
} |
37 |
|
38 |
public String getTypeName() { |
39 |
return typename;
|
40 |
} |
41 |
|
42 |
/* (non-Javadoc)
|
43 |
* @see org.gvsig.scripting.impl.Unit#load(org.gvsig.scripting.ScriptingFolder, java.lang.String)
|
44 |
*/
|
45 |
public abstract void load(ScriptingFolder folder, String id); |
46 |
|
47 |
public String toString() { |
48 |
if (this.getName() == null) { |
49 |
return "(" + this.getClass().getSimpleName() + ")"; |
50 |
} |
51 |
return this.getName(); |
52 |
} |
53 |
|
54 |
protected void setParent(ScriptingFolder parent) { |
55 |
this.parent = parent;
|
56 |
} |
57 |
|
58 |
public ScriptingManager getManager() {
|
59 |
return this.manager; |
60 |
} |
61 |
|
62 |
protected File getFileResource(String extension) { |
63 |
return new File(this.getParent().getFile(), this.id + extension).getAbsoluteFile(); |
64 |
} |
65 |
|
66 |
/* (non-Javadoc)
|
67 |
* @see org.gvsig.scripting.impl.Unit#getDescription()
|
68 |
*/
|
69 |
public String getDescription() { |
70 |
return this.description; |
71 |
} |
72 |
|
73 |
public String getCreatedBy() { |
74 |
return this.createdBy; |
75 |
} |
76 |
|
77 |
public String getVersion() { |
78 |
return this.version; |
79 |
} |
80 |
|
81 |
/* (non-Javadoc)
|
82 |
* @see org.gvsig.scripting.impl.Unit#getId()
|
83 |
*/
|
84 |
public String getId() { |
85 |
return this.id; |
86 |
} |
87 |
|
88 |
/* (non-Javadoc)
|
89 |
* @see org.gvsig.scripting.impl.Unit#getName()
|
90 |
*/
|
91 |
public String getName() { |
92 |
if (this.name == null) { |
93 |
return this.id; |
94 |
} |
95 |
return this.name; |
96 |
} |
97 |
|
98 |
/* (non-Javadoc)
|
99 |
* @see org.gvsig.scripting.impl.Unit#setDescription(java.lang.String)
|
100 |
*/
|
101 |
public void setDescription(String description) { |
102 |
this.description = description;
|
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void setCreatedBy(String createdBy) { |
107 |
this.createdBy = createdBy;
|
108 |
} |
109 |
|
110 |
@Override
|
111 |
public void setVersion(String version) { |
112 |
this.version = version;
|
113 |
} |
114 |
|
115 |
/* (non-Javadoc)
|
116 |
* @see org.gvsig.scripting.impl.Unit#setId(java.lang.String)
|
117 |
*/
|
118 |
@Override
|
119 |
public void setId(String id) { |
120 |
if (this instanceof ScriptingFolder) { |
121 |
this.id = id;
|
122 |
} else {
|
123 |
this.id = FilenameUtils.getBaseName(id);
|
124 |
} |
125 |
} |
126 |
|
127 |
/* (non-Javadoc)
|
128 |
* @see org.gvsig.scripting.impl.Unit#setName(java.lang.String)
|
129 |
*/
|
130 |
@Override
|
131 |
public void setName(String name) { |
132 |
this.name = name;
|
133 |
} |
134 |
|
135 |
/* (non-Javadoc)
|
136 |
* @see org.gvsig.scripting.impl.Unit#getParent()
|
137 |
*/
|
138 |
public ScriptingFolder getParent() {
|
139 |
return this.parent; |
140 |
} |
141 |
|
142 |
/* (non-Javadoc)
|
143 |
* @see org.gvsig.scripting.impl.Unit#move(org.gvsig.scripting.ScriptingFolder)
|
144 |
*/
|
145 |
private boolean moveFiles(ScriptingFolder folder, String id) { |
146 |
|
147 |
if (manager.validateUnitId(folder, id)) {
|
148 |
String oldId = this.getId(); |
149 |
String fileName = null; |
150 |
String s[] = null; |
151 |
String extension = null; |
152 |
List<ScriptingUnit> units = this.getParent().getUnits(); |
153 |
for (int i = 0; i < units.size(); i++) { |
154 |
fileName = (units.get(i)).getId(); |
155 |
s = fileName.split("\\.");
|
156 |
extension = "";
|
157 |
if (s.length > 1) { |
158 |
extension = "." + s[s.length - 1]; |
159 |
fileName = fileName.substring(0, fileName.length() - extension.length());
|
160 |
} |
161 |
if (extension.equals("") && this instanceof ScriptingScript) { |
162 |
extension = manager.getExtensionOfLanguage(((ScriptingScript) this).getLangName());
|
163 |
} |
164 |
if (fileName.equals(oldId)) {
|
165 |
//renombramos fichero
|
166 |
File f = new File(this.getParent().getFile(), fileName + extension); |
167 |
File fDest = new File(folder.getFile(), id + extension); |
168 |
if (this instanceof ScriptingScript) { |
169 |
String code;
|
170 |
try {
|
171 |
code = FileUtils.readFileToString(f); |
172 |
((ScriptingScript) this).setCode(code);
|
173 |
} catch (IOException ex) { |
174 |
|
175 |
} |
176 |
} |
177 |
f.renameTo(fDest); |
178 |
|
179 |
File fInf = new File(this.getParent().getFile(), fileName + ".inf"); |
180 |
if (fInf.exists()) {
|
181 |
File fInfDest = new File(folder.getFile(), id + ".inf"); |
182 |
fInf.renameTo(fInfDest); |
183 |
} |
184 |
File fDialog = new File(this.getParent().getFile(), fileName + ".dlg"); |
185 |
if (fDialog.exists()) {
|
186 |
File fDialogDest = new File(folder.getFile(), id + ".dlg"); |
187 |
fDialog.renameTo(fDialogDest); |
188 |
} |
189 |
} |
190 |
} |
191 |
this.setParent(folder);
|
192 |
this.setId(id);
|
193 |
return true; |
194 |
} |
195 |
return false; |
196 |
} |
197 |
|
198 |
public boolean move(ScriptingFolder target) { |
199 |
return moveFiles(target, this.getId()); |
200 |
} |
201 |
|
202 |
/* (non-Javadoc)
|
203 |
* @see org.gvsig.scripting.impl.Unit#rename(java.lang.String)
|
204 |
*/
|
205 |
public boolean rename(String newId) { |
206 |
return moveFiles(this.getParent(), newId); |
207 |
} |
208 |
|
209 |
private String toStringNotNull(String s) { |
210 |
if (s == null) { |
211 |
return ""; |
212 |
} |
213 |
return s;
|
214 |
} |
215 |
|
216 |
protected void save(Ini prefs) { |
217 |
prefs.put("Unit", "type", toStringNotNull(this.getTypeName())); |
218 |
prefs.put("Unit", "name", this.getName()); |
219 |
prefs.put("Unit", "description", toStringNotNull(this.getDescription())); |
220 |
prefs.put("Unit", "createdBy", toStringNotNull(this.getCreatedBy())); |
221 |
prefs.put("Unit", "version", toStringNotNull(this.getVersion())); |
222 |
|
223 |
try {
|
224 |
prefs.store(); |
225 |
} catch (IOException e) { |
226 |
File f = prefs.getFile();
|
227 |
String fname = (f == null) ? "(null)" : f.getAbsolutePath(); |
228 |
logger.warn("Can't save inf file '" + fname + "'."); |
229 |
} |
230 |
|
231 |
} |
232 |
|
233 |
protected void loadInf(Ini prefs) { |
234 |
String typename = getInfString(prefs, "Unit", "type", this.getTypeName()); |
235 |
if (!this.getTypeName().equalsIgnoreCase(typename)) { |
236 |
File f = prefs.getFile();
|
237 |
String fname = (f == null) ? "(null)" : f.getAbsolutePath(); |
238 |
logger.warn("inconsistent type in inf file '" + fname + "'. Curent type '" + this.getTypeName() + "', type from inf file '" + typename + "'."); |
239 |
} |
240 |
this.setName(getInfString(prefs, "Unit", "name", this.getName())); |
241 |
this.setDescription(getInfString(prefs, "Unit", "description", null)); |
242 |
this.setCreatedBy(getInfString(prefs, "Unit", "createdBy", null)); |
243 |
this.setVersion(getInfString(prefs, "Unit", "version", null)); |
244 |
|
245 |
} |
246 |
|
247 |
protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue) { |
248 |
Object r = prefs.get(section, option);
|
249 |
if (r == null) { |
250 |
return defaultValue;
|
251 |
} else {
|
252 |
return r;
|
253 |
} |
254 |
} |
255 |
|
256 |
private String getInfString(Ini prefs, String section, String option, Object defaultValue) { |
257 |
String s = (String) getInfValue(prefs, section, option, defaultValue); |
258 |
if (s != null && s.trim().length() < 1) { |
259 |
return null; |
260 |
} |
261 |
return s;
|
262 |
} |
263 |
|
264 |
protected void console_println(String s) { |
265 |
// When running from the composer messages of stdout shows in the console tab.
|
266 |
logger.info(s); |
267 |
System.out.println(s);
|
268 |
} |
269 |
|
270 |
public void create(ScriptingFolder folder, String id) { |
271 |
this.setParent(folder);
|
272 |
this.setId(id);
|
273 |
|
274 |
File file = new File(folder.getFile(), id + ".inf"); |
275 |
try {
|
276 |
file.createNewFile(); |
277 |
} catch (IOException e) { |
278 |
logger.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e); |
279 |
} |
280 |
} |
281 |
|
282 |
public File getFile() { |
283 |
if (this.getId() == null) { |
284 |
return null; |
285 |
} |
286 |
if (this.getParent() == null) { |
287 |
return null; |
288 |
} |
289 |
if (this.getParent().getFile() == null) { |
290 |
return null; |
291 |
} |
292 |
return new File(getParent().getFile(), this.getId() + ".inf"); |
293 |
} |
294 |
} |