Revision 1066 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

View differences:

DefaultScriptingFolder.java
6 6
import java.util.ArrayList;
7 7
import java.util.List;
8 8
import java.util.Arrays;
9
import java.util.Collection;
9 10
import java.util.Collections;
10 11
import java.util.Comparator;
11 12
import java.util.HashSet;
......
18 19
import org.gvsig.scripting.ScriptingManager;
19 20
import org.gvsig.scripting.ScriptingUnit;
20 21
import org.gvsig.tools.exception.BaseException;
22
import static org.gvsig.tools.util.FileTools.relativizeFile;
21 23
import org.gvsig.tools.visitor.Visitor;
22 24
import org.ini4j.Ini;
23 25
import org.slf4j.Logger;
......
28 30
    private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingFolder.class);
29 31
    protected File folder;
30 32
    private File infFile;
31

  
33
    private File linkTarget;
34
    private String[] iconNames = new String[]{"scripting-icon-folder", "scripting-icon-folder-open"};
35
    
32 36
    protected DefaultScriptingFolder(ScriptingManager manager) {
33 37
        super(null, ScriptingManager.UNIT_FOLDER, manager, null);
34 38
    }
......
36 40
    public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager, File folder) {
37 41
        super(parent, ScriptingManager.UNIT_FOLDER, manager, null);
38 42
        this.folder = new File(FilenameUtils.normalizeNoEndSeparator(folder.getAbsolutePath(),true));
39
        this.infFile = null;
40
        if( folder.isFile() ) {
41
            if( !folder.getName().toLowerCase().endsWith(".inf") ) {
42
                throw new IllegalArgumentException("folder argument can be a folder.");
43
            }
44
            if( !folder.exists() ) {
45
                throw new IllegalArgumentException("Can't access to folder inf file ("+folder.getAbsolutePath()+").");
46
            }
47
            this.loadInf(folder);
48
        } else {
49
            this.setId(folder.getName());
50
            File f = new File(folder.getParentFile(),this.getId() + ".inf");
51
            if (f.isFile()) {
52
                this.loadInf(f);
53
            }
43
        this.infFile = null;        
44
        if( folder.isFile() && !FilenameUtils.isExtension(folder.getName(), "inf") ) {
45
            throw new IllegalArgumentException("folder argument can be a folder.");
54 46
        }
47
        this.load(parent, folder.getName());
55 48
   }
56 49
    
57 50
    @Override
58 51
    public void create(ScriptingFolder folder, String id) {
59 52
        File f = new File(folder.getFile(), id);
60
        f.mkdir();
61
        this.load(folder, id);    }
53
        this.load(folder, id);
54
        if( !this.isLink() ) {
55
            f.mkdir();
56
        }
57
    }
62 58

  
63 59
    @Override
64 60
    public void create(ScriptingFolder folder, String id, String language) {
......
67 63
    
68 64
    @Override
69 65
    public File getFile() {
70
        return this.folder;
66
        if( this.linkTarget==null ) {
67
            return this.folder;
68
        }
69
        return this.linkTarget;
71 70
    }
72 71

  
73 72
    @Override
74 73
    public List<File> getFiles() {
75 74
        List<File> l = new ArrayList<>();
76
        l.add(this.getFile());
75
        l.add(this.folder);
76
        if( this.infFile!=null ) {
77
            l.add(this.infFile);
78
        }
77 79
        return l;
78 80
    }
79 81

  
......
83 85
        if( parent != null ) {
84 86
            return parent;
85 87
        }
86
        File f = this.infFile;
87
        if( f==null ) {
88
            // Is a link
89
            f = this.folder;
90
        } 
88
        File f = this.folder;
89
        if( this.isLink() ) {
90
            f = this.infFile.getParentFile();
91
        }
91 92
        if( FileUtils.isSameFile(this.manager.getRootUserFolder(), f) ) {
92 93
            return null;
93 94
        }
94 95
        if( FileUtils.isRootFile(f) ) {
95 96
            return null;
96 97
        }
97
//        if( !DefaultScriptingManager.isSubfolder(this.manager.getRootUserFolder(), f) ) {
98
//            return null;
99
//        }        
100

  
101 98
        return manager.getFolder(f.getParentFile());
102 99
    }
103 100

  
......
158 155
        return unit;
159 156
    }
160 157

  
158
    private boolean containsAny(Set<File> files, Collection<File> other) {
159
        for (File file : other) {
160
            if( files.contains(file) ) {
161
                return true;
162
            }
163
        }
164
        return false;
165
    }
166
    
161 167
    @Override
162 168
    public List<ScriptingUnit> getUnits() {
163 169
        List<ScriptingUnit> ol = new ArrayList<>();
164
        File[] files = this.folder.listFiles(new FilenameFilter() {
170
        //
171
        // Buscamos los directorios y ficheros ".inf".
172
        File[] files = this.getFile().listFiles(new FilenameFilter() {
165 173
            @Override
166 174
            public boolean accept(File arg0, String arg1) {
167 175
                File f = new File(arg0, arg1);
......
171 179
                if (f.isDirectory()) {
172 180
                    return true;
173 181
                }
182
                if( "#folder.inf".equals(f.getName()) ) {
183
                    return false;
184
                }
174 185
                String ext = FilenameUtils.getExtension(f.getName());
175 186
                return "inf".equalsIgnoreCase(ext);
176 187
            }
177 188
        });
178 189
        Set<File> filesAdded = new HashSet<>();
179 190
        if (files != null) {
180
//            Arrays.sort(files, new Comparator<File>() {
181
//                @Override
182
//                public int compare(File f1, File f2) {
183
//                    if (f1.isDirectory() && !f2.isDirectory()) {
184
//                        return -1;
185
//                    } else if (!f1.isDirectory() && f2.isDirectory()) {
186
//                        return 1;
187
//                    } else {
188
//                        return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
189
//                    }
190
//                }
191
//            });
192 191
            for (File f : files) {
193 192
                try {
194 193
                    ScriptingUnit unit = this.getUnit(f);
195
                    filesAdded.addAll(unit.getFiles());
196
                    ol.add(unit);
194
                    if( !containsAny(filesAdded, unit.getFiles()) ) {
195
                        filesAdded.addAll(unit.getFiles());
196
                        ol.add(unit);
197
                    }
197 198
                } catch (Exception ex) {
198 199
                    logger.warn("Can't create unit from file '" + f.getAbsolutePath() + "'.",ex);
199 200
                }
200 201
            }
201 202
        }
202
        files = this.folder.listFiles(new FilenameFilter() {
203
        
204
        
205
        // Ahora buscamos los ficheros de tipo ExternalFile, no son 
206
        // directorios, no tienen ".inf".
207
        files = this.getFile().listFiles(new FilenameFilter() {
203 208
            @Override
204 209
            public boolean accept(File arg0, String arg1) {
205 210
                File f = new File(arg0, arg1);
......
207 212
                    return false;
208 213
                }
209 214
                if (f.isDirectory()) {
210
                    return true;
215
                    return false;
211 216
                }
217
                if( "#folder.inf".equals(f.getName()) ) {
218
                    return false;
219
                }
212 220
                return true;
213 221
            }
214 222
        });
......
249 257
    @Override
250 258
    public List<ScriptingFolder> getUnitFolders() {
251 259
        List<ScriptingFolder> ol = new ArrayList<>();
252
        File[] files = this.folder.listFiles(new FilenameFilter() {
260
        File[] files = this.getFile().listFiles(new FilenameFilter() {
253 261
            @Override
254 262
            public boolean accept(File arg0, String arg1) {
255 263
                File f = new File(arg0, arg1);
......
301 309
    }
302 310

  
303 311
    @Override
312
    public File getFileResource(String extension) {
313
        if( this.isLink() ) {
314
            return new File(
315
                    this.infFile.getParentFile(), 
316
                    FilenameUtils.getBaseName(this.infFile.getName()) + extension
317
                ).getAbsoluteFile();
318
        }
319
        return new File(
320
                this.getFile().getParentFile(), 
321
                this.getFile().getName() + extension
322
            ).getAbsoluteFile();
323
    }
324
    
325

  
326
    @Override
327
    public void save() {
328
        File f = this.infFile;
329
        if( f==null ) {
330
            f = new File(folder,"#folder.inf");
331
        }
332
        try {        
333
            Ini prefs = new Ini();
334
            prefs.setFile(f);
335
            if( this.linkTarget!=null ) {
336
                prefs.put(
337
                        ScriptingManager.UNIT_FOLDER, 
338
                        "path", 
339
                        relativizeFile(this.infFile.getParentFile(), this.linkTarget)
340
                );
341
            }
342
            if( this.iconNames!=null ) {
343
                prefs.put(ScriptingManager.UNIT_FOLDER, "iconName", this.iconNames[0]);
344
                prefs.put(ScriptingManager.UNIT_FOLDER, "iconNameOpen", this.iconNames[1]);
345
            }
346
            this.save(prefs);
347
        } catch (Throwable e) {
348
            String msg = "Can't save 'inf' file '" + f.getAbsolutePath() + "'.";
349
            logger.warn(msg, e);
350
            throw new RuntimeException(msg, e);
351
        }
352
    }
353
    
354
    @Override
304 355
    public void load(ScriptingFolder parent, String id) {
305
        File f = new File(parent.getFile(), id+".inf");
306
        if (f.isFile()) {
307
            this.loadInf(f);
308
        } else {
309
            this.setParent(parent);
310
            this.setId(id);
356
        this.infFile = null;
357
        this.linkTarget = null;
358
        this.iconNames = new String[]{"scripting-icon-folder", "scripting-icon-folder-open"};
359
        this.setId(id);
360
        this.setParent(parent);
361

  
362
        File fileInf = getFileResource(".inf");
363
        if (!fileInf.isFile()) {
364
            fileInf = new File(this.folder, "#folder.inf");
365
            if (!fileInf.isFile()) {
366
                return;
367
            }
311 368
        }
312
    }
313
        
314
    private void loadInf(File f) {   
315 369
        Ini prefs;
316 370
        try {
317
            prefs = new Ini(f);
371
            prefs = new Ini(fileInf);
318 372
            loadInf(prefs);
319
            String spath = getInfString(
320
                    prefs, 
321
                    ScriptingManager.UNIT_FOLDER, 
373
            String iconName = getInfString(
374
                    prefs,
375
                    ScriptingManager.UNIT_FOLDER,
376
                    "iconName",
377
                    null
378
            );
379
            String iconNameOpen = getInfString(
380
                    prefs,
381
                    ScriptingManager.UNIT_FOLDER,
382
                    "iconNameOpen",
383
                    null
384
            );
385
            if( iconName!=null ) {
386
                this.iconNames = new String[] {
387
                    iconName,
388
                    iconNameOpen==null? iconName:iconNameOpen
389
                };
390
            }
391
            String targetPath = getInfString(
392
                    prefs,
393
                    ScriptingManager.UNIT_FOLDER,
322 394
                    "path",
323 395
                    null
324 396
            );
325
            if( StringUtils.isEmpty(spath) ) {
326
                this.folder = FileUtils.removeExtension(f);
327
                this.infFile = null;
328
            } else {
329
                File path = new File(spath);
330
                if( path.isAbsolute() ) {
331
                    this.folder = path;
397
            if( !StringUtils.isEmpty(targetPath) ) {
398
                File target = new File(targetPath);
399
                if (target.isAbsolute()) {
400
                    this.linkTarget = target;
332 401
                } else {
333
                    this.folder = new File(FilenameUtils.concat(f.getParent(), spath));
402
                    this.linkTarget = new File(FilenameUtils.concat(fileInf.getParent(), targetPath));
334 403
                }
335
                this.infFile = FileUtils.getAbsoluteFile(f);
336
                this.manager.addLink(this.infFile, this.folder);
404
                this.manager.addLink(this.infFile, this.linkTarget);
337 405
            }
338
            this.folder = FileUtils.getAbsoluteFile(this.folder);
339
            this.setId(FileUtils.getBaseName(f));
340
            this.setParent(manager.getFolder(f.getParentFile()));
406
            this.infFile = FileUtils.getAbsoluteFile(fileInf);
341 407
        } catch (Exception e) {
342
            logger.warn("Can't load 'inf' file '" + f.getAbsolutePath() + "'.", e);
408
            logger.warn("Can't load 'inf' file '" + fileInf.getAbsolutePath() + "'.", e);
343 409
        }
410

  
344 411
    }
345 412

  
346 413
    @Override
347 414
    public String[] getIconNames() {
348
        return new String[]{"folder", "folder-drag-accept"};
415
        return this.iconNames;
349 416
    }
350 417

  
351 418
    @Override
......
432 499
    
433 500
    @Override
434 501
    public boolean isLink() {
435
        return this.infFile!=null;
502
        return this.linkTarget!=null;
436 503
    }
437 504
    
438 505
    @Override
......
451 518
    }
452 519

  
453 520
    @Override
454
    public void save() {
455
        File f = this.infFile;
456
        if( f==null ) {
457
            f = new File(folder.getParentFile(),this.getId() + ".inf");
521
    public String getRawInf() {
522
        if( this.infFile == null ) {
523
            return null;
458 524
        }
459
        try {        
460
            Ini prefs = new Ini(f);
461
            this.save(prefs);
462
        } catch (Throwable e) {
463
            String msg = "Can't save 'inf' file '" + f.getAbsolutePath() + "'.";
464
            logger.warn(msg, e);
465
            throw new RuntimeException(msg, e);
525
        try {
526
            String rawInf = org.apache.commons.io.FileUtils.readFileToString(infFile);
527
            return rawInf;
528
        } catch (IOException ex) {
529
            // TODO: 
466 530
        }
531
        return null;
467 532
    }
533
    
534
    @Override
535
    public void setRawInf(String rawInf) {
536
        if( this.infFile == null ) {
537
            this.infFile = new File(folder,"#folder.inf");
538
        }
539
        try {
540
            org.apache.commons.io.FileUtils.write(infFile, rawInf);
541
            this.reload();
542
        } catch (IOException ex) {
543
            // TODO: 
544
        }
545
    }
468 546
}

Also available in: Unified diff