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 / DefaultScriptingFolder.java @ 442
History | View | Annotate | Download (5.47 KB)
1 |
package org.gvsig.scripting.impl; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FilenameFilter; |
5 |
import java.io.IOException; |
6 |
import java.net.URL; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import java.util.Arrays; |
12 |
import java.util.Comparator; |
13 |
|
14 |
import org.apache.commons.io.FileUtils; |
15 |
import org.apache.commons.io.FilenameUtils; |
16 |
import org.gvsig.scripting.ScriptingFolder; |
17 |
import org.gvsig.scripting.ScriptingManager; |
18 |
import org.gvsig.scripting.ScriptingScript; |
19 |
import org.gvsig.scripting.ScriptingUnit; |
20 |
import org.ini4j.Ini; |
21 |
import org.python.core.util.FileUtil; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
|
26 |
public class DefaultScriptingFolder extends AbstractUnit implements ScriptingFolder{ |
27 |
|
28 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingFolder.class); |
29 |
protected File folder; |
30 |
|
31 |
public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager) {
|
32 |
this(parent, manager,(File)null); |
33 |
} |
34 |
|
35 |
public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager, File folder) { |
36 |
super(parent, ScriptingManager.UNIT_FOLDER, manager, null); |
37 |
this.folder = folder;
|
38 |
if( folder != null ) { |
39 |
this.setId(folder.getName());
|
40 |
} |
41 |
} |
42 |
|
43 |
public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager, URL folder) { |
44 |
super(parent, ScriptingManager.UNIT_FOLDER, manager, null); |
45 |
if(!folder.getProtocol().equalsIgnoreCase("file")){ |
46 |
throw new RuntimeException("Protocol URL not supported"); |
47 |
} |
48 |
File f = new File(folder.getPath()); |
49 |
this.setId(f.getName());
|
50 |
this.folder = f;
|
51 |
} |
52 |
|
53 |
public void create(ScriptingFolder folder, String id) { |
54 |
this.getFile().mkdir();
|
55 |
} |
56 |
|
57 |
public File getFile(){ |
58 |
return this.folder; |
59 |
} |
60 |
|
61 |
public ScriptingFolder getParent() {
|
62 |
ScriptingFolder parent = super.getParent();
|
63 |
if( parent != null ) { |
64 |
return parent;
|
65 |
} |
66 |
return new DefaultScriptingFolder(null, manager, this.folder.getParentFile()); |
67 |
} |
68 |
|
69 |
private String getTypenameFromInf(File file) { |
70 |
Ini inf = null;
|
71 |
try {
|
72 |
inf = new Ini(file);
|
73 |
} catch (Exception e) { |
74 |
logger.warn("Can't load 'inf' file '"+file.getAbsolutePath()+"'.",e); |
75 |
} |
76 |
String typename = inf.get("Unit", "type"); |
77 |
if( typename == null ) { |
78 |
typename = ScriptingManager.UNIT_SCRIPT; |
79 |
} |
80 |
return typename;
|
81 |
} |
82 |
|
83 |
public Unit getUnit(File afile) { |
84 |
Unit unit=null;
|
85 |
|
86 |
File file = null; |
87 |
if( afile.isAbsolute() ) {
|
88 |
file = afile.getAbsoluteFile(); |
89 |
} else {
|
90 |
file = new File( this.getFile(),afile.getPath()); |
91 |
} |
92 |
|
93 |
String id = FilenameUtils.getBaseName(afile.getAbsolutePath());
|
94 |
ScriptingFolder parent = null;
|
95 |
if( FilenameUtils.equalsNormalizedOnSystem(this.getFile().getAbsolutePath(), file.getParentFile().getAbsolutePath() ) ) { |
96 |
parent = this;
|
97 |
} else {
|
98 |
parent = new DefaultScriptingFolder(null, manager, file.getParentFile()); |
99 |
} |
100 |
|
101 |
if (file.isDirectory()){
|
102 |
unit = (Unit) this.manager.createUnit(ScriptingManager.UNIT_FOLDER, parent, id);
|
103 |
|
104 |
} else if( file.isFile() ) { |
105 |
if( !file.getName().endsWith(".inf") ) { |
106 |
file = new File( file.getParentFile(), |
107 |
FilenameUtils.getBaseName(file.getName())+".inf");
|
108 |
} |
109 |
if( !file.exists() ) {
|
110 |
logger.warn("Can't get Unit, file '"+file.getAbsolutePath()+"' not exists."); |
111 |
return null; |
112 |
} |
113 |
unit = (Unit) this.manager.createUnit(getTypenameFromInf(file), parent, id);
|
114 |
} |
115 |
return unit;
|
116 |
} |
117 |
|
118 |
public List<ScriptingUnit> getUnits() { |
119 |
// Listar los ficheros del directorio y pasarlo a un vector para ordenarlo
|
120 |
List<ScriptingUnit> ol = new ArrayList<ScriptingUnit>(); |
121 |
File[] files = this.folder.listFiles(new FilenameFilter() { |
122 |
public boolean accept(File arg0, String arg1) { |
123 |
File f = new File(arg0,arg1); |
124 |
if( !f.canRead() ) {
|
125 |
return false; |
126 |
} |
127 |
if( f.isHidden() ) {
|
128 |
return false; |
129 |
} |
130 |
if( f.isDirectory() ) {
|
131 |
return true; |
132 |
} |
133 |
String ext = FilenameUtils.getExtension(f.getAbsolutePath());
|
134 |
return ext.equalsIgnoreCase("inf"); |
135 |
} |
136 |
}); |
137 |
if (files != null){ |
138 |
Arrays.sort( files, new Comparator<File>() { |
139 |
public int compare(File f1, File f2) { |
140 |
if (f1.isDirectory() && !f2.isDirectory()) {
|
141 |
return -1; |
142 |
} else if (!f1.isDirectory() && f2.isDirectory()) { |
143 |
return 1; |
144 |
} else {
|
145 |
return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
|
146 |
} |
147 |
} |
148 |
}); |
149 |
for (int i = 0; i < files.length; i++){ |
150 |
File f = files[i];
|
151 |
ScriptingUnit unit = null;
|
152 |
try {
|
153 |
unit = this.getUnit(f);
|
154 |
} catch(Exception ex) { |
155 |
logger.warn("Can't create unit from file '"+f.getAbsolutePath()+"'."); |
156 |
} |
157 |
if (unit!=null){ |
158 |
ol.add(unit); |
159 |
} |
160 |
} |
161 |
} |
162 |
return ol;
|
163 |
} |
164 |
|
165 |
public void add(ScriptingUnit unit) { |
166 |
unit.move(this);
|
167 |
} |
168 |
|
169 |
public void remove(ScriptingUnit unit) { |
170 |
unit.remove(); |
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void load(ScriptingFolder folder, String id) { |
175 |
DefaultScriptingFolder parent = (DefaultScriptingFolder) folder; |
176 |
this.setParent(folder);
|
177 |
this.setId(id);
|
178 |
this.folder = new File(parent.getFile(),id); |
179 |
} |
180 |
|
181 |
public String[] getIconNames() { |
182 |
return new String[]{"folder", "folder-drag-accept"}; |
183 |
} |
184 |
|
185 |
public boolean remove() { |
186 |
boolean r = true; |
187 |
File folder = this.getParent().getFile(); |
188 |
File f = null; |
189 |
try {
|
190 |
f = new File(folder,this.getId()); |
191 |
FileUtils.forceDelete(f); |
192 |
} catch (IOException e) { |
193 |
logger.warn("Can't remove project '"+f.getAbsolutePath()+"'.",e); |
194 |
r = false;
|
195 |
} |
196 |
return r;
|
197 |
} |
198 |
|
199 |
public void create(ScriptingFolder folder, String id, String language) { |
200 |
File f = new File(folder.getFile(),id); |
201 |
f.mkdir(); |
202 |
this.load(folder, id);
|
203 |
} |
204 |
} |