Revision 441 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
AbstractUnit.java | ||
---|---|---|
5 | 5 |
import java.util.List; |
6 | 6 |
|
7 | 7 |
import org.ini4j.Ini; |
8 |
|
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
import org.apache.commons.io.FilenameUtils; |
|
9 | 11 |
import org.gvsig.scripting.ScriptingFolder; |
10 | 12 |
import org.gvsig.scripting.ScriptingManager; |
11 | 13 |
import org.gvsig.scripting.ScriptingScript; |
... | ... | |
13 | 15 |
|
14 | 16 |
public abstract class AbstractUnit implements ScriptingUnit, Unit{ |
15 | 17 |
|
18 |
private static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class); |
|
19 |
|
|
16 | 20 |
protected DefaultScriptingManager manager; |
17 | 21 |
|
18 | 22 |
protected String id; |
... | ... | |
20 | 24 |
protected String description; |
21 | 25 |
protected String createdBy; |
22 | 26 |
protected String version; |
23 |
protected ScriptingFolder parent; |
|
27 |
protected ScriptingFolder parent; |
|
28 |
protected String typename; |
|
24 | 29 |
|
25 |
public AbstractUnit(ScriptingManager manager){ |
|
30 |
public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id){ |
|
31 |
this.parent = parent; |
|
26 | 32 |
this.manager = (DefaultScriptingManager) manager; |
33 |
this.typename = typename; |
|
34 |
this.id = id; |
|
27 | 35 |
} |
28 | 36 |
|
37 |
public String getTypeName() { |
|
38 |
return typename; |
|
39 |
} |
|
40 |
|
|
29 | 41 |
/* (non-Javadoc) |
30 | 42 |
* @see org.gvsig.scripting.impl.Unit#load(org.gvsig.scripting.ScriptingFolder, java.lang.String) |
31 | 43 |
*/ |
32 | 44 |
public abstract void load(ScriptingFolder folder, String id); |
33 | 45 |
|
34 |
@Override |
|
35 | 46 |
public String toString(){ |
47 |
if( this.getName() == null ) { |
|
48 |
return "("+this.getClass().getSimpleName()+")"; |
|
49 |
} |
|
36 | 50 |
return this.getName(); |
37 | 51 |
} |
38 | 52 |
|
... | ... | |
41 | 55 |
} |
42 | 56 |
|
43 | 57 |
protected File getFileResource(String extension){ |
44 |
return new File(this.getParent().getPathFile(),this.id + extension);
|
|
58 |
return new File(this.getParent().getFile(),this.id + extension).getAbsoluteFile();
|
|
45 | 59 |
} |
46 | 60 |
|
47 | 61 |
/* (non-Javadoc) |
... | ... | |
98 | 112 |
if(this instanceof ScriptingFolder){ |
99 | 113 |
this.id = id; |
100 | 114 |
} else { |
101 |
String s[] = id.split("\\."); |
|
102 |
if (s.length<2){ |
|
103 |
this.id = id; |
|
104 |
} else{ |
|
105 |
String extension ="." + s[s.length-1]; |
|
106 |
this.id = id.substring(0,id.length()-extension.length()); |
|
107 |
} |
|
115 |
this.id = FilenameUtils.getBaseName(id); |
|
108 | 116 |
} |
109 | 117 |
} |
110 | 118 |
|
... | ... | |
147 | 155 |
} |
148 | 156 |
if(fileName.equals(oldId)){ |
149 | 157 |
//renombramos fichero |
150 |
File f = new File(this.getParent().getPath()+File.separator+fileName+extension);
|
|
151 |
File fDest = new File(folder.getPath()+File.separator+id+extension);
|
|
158 |
File f = new File(this.getParent().getFile(),fileName+extension);
|
|
159 |
File fDest = new File(folder.getFile(),id+extension);
|
|
152 | 160 |
if(this instanceof ScriptingScript){ |
153 | 161 |
String code = DefaultScriptingManager.getStringFromFile(f.getPath()); |
154 | 162 |
((ScriptingScript)this).setCode(code); |
155 | 163 |
} |
156 | 164 |
f.renameTo(fDest); |
157 | 165 |
|
158 |
File fInf = new File(this.getParent().getPath()+File.separator+fileName+".inf");
|
|
166 |
File fInf = new File(this.getParent().getFile(),fileName+".inf");
|
|
159 | 167 |
if(fInf.exists()){ |
160 |
File fInfDest = new File(folder.getPath()+File.separator+id+".inf");
|
|
168 |
File fInfDest = new File(folder.getFile(),id+".inf");
|
|
161 | 169 |
fInf.renameTo(fInfDest); |
162 | 170 |
} |
163 |
File fDialog = new File(this.getParent().getPath()+File.separator+fileName+".dlg");
|
|
171 |
File fDialog = new File(this.getParent().getFile(),fileName+".dlg");
|
|
164 | 172 |
if(fDialog.exists()){ |
165 |
File fDialogDest = new File(folder.getPath()+File.separator+id+".dlg");
|
|
173 |
File fDialogDest = new File(folder.getFile(),id+".dlg");
|
|
166 | 174 |
fDialog.renameTo(fDialogDest); |
167 | 175 |
} |
168 | 176 |
} |
... | ... | |
185 | 193 |
return moveFiles(this.getParent(), newId); |
186 | 194 |
} |
187 | 195 |
|
196 |
private String toStringNotNull(String s) { |
|
197 |
if( s==null ) { |
|
198 |
return ""; |
|
199 |
} |
|
200 |
return s; |
|
201 |
} |
|
202 |
|
|
188 | 203 |
protected void save(Ini prefs){ |
204 |
prefs.put("Unit", "type",toStringNotNull(this.getTypeName())); |
|
189 | 205 |
prefs.put("Unit", "name",this.getName()); |
190 |
if (this.getDescription() == null){ |
|
191 |
prefs.put("Unit", "description",""); |
|
192 |
}else { |
|
193 |
prefs.put("Unit", "description",this.getDescription()); |
|
194 |
} |
|
206 |
prefs.put("Unit", "description",toStringNotNull(this.getDescription())); |
|
207 |
prefs.put("Unit", "createdBy",toStringNotNull(this.getCreatedBy())); |
|
208 |
prefs.put("Unit", "version",toStringNotNull(this.getVersion())); |
|
195 | 209 |
|
196 |
if (this.getCreatedBy() == null){ |
|
197 |
prefs.put("Unit", "createdBy",""); |
|
198 |
}else { |
|
199 |
prefs.put("Unit", "createdBy",this.getCreatedBy()); |
|
200 |
} |
|
201 |
|
|
202 |
if (this.getVersion() == null){ |
|
203 |
prefs.put("Unit", "version",""); |
|
204 |
}else { |
|
205 |
prefs.put("Unit", "version",this.getVersion()); |
|
206 |
} |
|
207 |
|
|
208 | 210 |
try { |
209 | 211 |
prefs.store(); |
210 | 212 |
} catch (IOException e) { |
211 |
// TODO Auto-generated catch block |
|
212 |
e.printStackTrace(); |
|
213 |
File f = prefs.getFile(); |
|
214 |
String fname = (f==null)? "(null)":f.getAbsolutePath(); |
|
215 |
logger.warn("Can't save inf file '"+fname+"'."); |
|
213 | 216 |
} |
214 | 217 |
|
215 | 218 |
} |
216 | 219 |
|
217 | 220 |
protected void loadInf(Ini prefs){ |
218 |
|
|
219 |
this.setName((String)getInfValue(prefs,"Unit","name", this.getName()));
|
|
220 |
String description = (String) getInfValue(prefs,"Unit","description", null);
|
|
221 |
if (description != null && description.length() == 0){
|
|
222 |
description = null;
|
|
221 |
String typename = getInfString(prefs,"Unit","type", this.getTypeName()); |
|
222 |
if( !this.getTypeName().equalsIgnoreCase(typename) ) {
|
|
223 |
File f = prefs.getFile();
|
|
224 |
String fname = (f==null)? "(null)":f.getAbsolutePath();
|
|
225 |
logger.warn("inconsistent type in inf file '"+fname+"'. Curent type '"+this.getTypeName()+"', type from inf file '"+typename+"'.");
|
|
223 | 226 |
} |
224 |
this.setDescription(description); |
|
225 |
|
|
226 |
String createdBy = (String) getInfValue(prefs,"Unit","createdBy", null); |
|
227 |
if (createdBy != null && createdBy.length() == 0){ |
|
228 |
createdBy = null; |
|
229 |
} |
|
230 |
this.setCreatedBy(createdBy); |
|
231 |
|
|
232 |
String version = (String) getInfValue(prefs,"Unit","version", null); |
|
233 |
if (version != null && version.length() == 0){ |
|
234 |
version = null; |
|
235 |
} |
|
236 |
this.setVersion(version); |
|
227 |
this.setName(getInfString(prefs,"Unit","name", this.getName())); |
|
228 |
this.setDescription(getInfString(prefs,"Unit","description", null)); |
|
229 |
this.setCreatedBy(getInfString(prefs,"Unit","createdBy", null)); |
|
230 |
this.setVersion(getInfString(prefs,"Unit","version", null)); |
|
237 | 231 |
|
238 | 232 |
} |
239 | 233 |
|
240 | 234 |
protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue){ |
241 | 235 |
Object r = prefs.get(section,option); |
242 |
if (r==null) |
|
236 |
if (r==null) {
|
|
243 | 237 |
return defaultValue; |
244 |
else
|
|
238 |
} else {
|
|
245 | 239 |
return r; |
240 |
} |
|
246 | 241 |
} |
242 |
|
|
243 |
private String getInfString(Ini prefs, String section, String option, Object defaultValue) { |
|
244 |
String s = (String) getInfValue(prefs, section, option, defaultValue); |
|
245 |
if( s!=null && s.trim().length()<1) { |
|
246 |
return null; |
|
247 |
} |
|
248 |
return s; |
|
249 |
} |
|
250 |
|
|
251 |
protected void console_println(String s) { |
|
252 |
// When running from the composer messages of stdout shows in the console tab. |
|
253 |
logger.info(s); |
|
254 |
System.out.println(s); |
|
255 |
} |
|
256 |
|
|
257 |
public void create(ScriptingFolder folder, String id) { |
|
258 |
this.setParent(folder); |
|
259 |
this.setId(id); |
|
260 |
|
|
261 |
File file = new File(folder.getFile(),id +".inf"); |
|
262 |
try { |
|
263 |
file.createNewFile(); |
|
264 |
} catch (IOException e) { |
|
265 |
logger.warn("Can't create inf file in '"+file.getAbsolutePath()+"'.",e); |
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
public File getFile() { |
|
270 |
if( this.getId()==null ) { |
|
271 |
return null; |
|
272 |
} |
|
273 |
if( this.getParent()==null ) { |
|
274 |
return null; |
|
275 |
} |
|
276 |
if( this.getParent().getFile()==null ) { |
|
277 |
return null; |
|
278 |
} |
|
279 |
return new File(getParent().getFile(),this.getId()+".inf"); |
|
280 |
} |
|
247 | 281 |
} |
Also available in: Unified diff