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