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 / DefaultScriptingExternalFile.java @ 1215
History | View | Annotate | Download (3.8 KB)
1 |
|
---|---|
2 |
package org.gvsig.scripting.impl; |
3 |
|
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
import java.nio.charset.Charset; |
7 |
import java.util.ArrayList; |
8 |
import java.util.List; |
9 |
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
11 |
import org.apache.commons.io.Charsets; |
12 |
import org.apache.commons.io.FileUtils; |
13 |
import org.apache.tika.Tika; |
14 |
import org.gvsig.scripting.ScriptingExternalFile; |
15 |
import org.gvsig.scripting.ScriptingFolder; |
16 |
import org.gvsig.scripting.ScriptingManager; |
17 |
|
18 |
|
19 |
public class DefaultScriptingExternalFile extends AbstractUnit implements ScriptingExternalFile { |
20 |
private File externFile = null; |
21 |
private String mimeType = null; |
22 |
|
23 |
private static Tika tika = null; |
24 |
|
25 |
public DefaultScriptingExternalFile(ScriptingFolder parent, ScriptingManager manager, String id) { |
26 |
super(parent, ScriptingManager.UNIT_EXTERNALFILE, manager, id);
|
27 |
this.externFile = new File(parent.getFile(),id); |
28 |
} |
29 |
|
30 |
@Override
|
31 |
public void load(ScriptingFolder folder, String id) { |
32 |
} |
33 |
|
34 |
@Override
|
35 |
public boolean rename(String newId) { |
36 |
try {
|
37 |
File newf = new File(this.externFile.getParentFile(),newId); |
38 |
FileUtils.moveFile(this.externFile, newf);
|
39 |
return true; |
40 |
} catch (IOException ex) { |
41 |
throw new RuntimeException("Can't rename '"+this.id+"' to '"+newId+"'.", ex); |
42 |
} |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public void create(ScriptingFolder folder, String id) { |
47 |
File file = this.externFile; |
48 |
try {
|
49 |
file.createNewFile(); |
50 |
} catch (IOException e) { |
51 |
LOGGER.warn("Can't create inf file in '" + file.getAbsolutePath() + "'.", e); |
52 |
} |
53 |
} |
54 |
|
55 |
@Override
|
56 |
public void create(ScriptingFolder folder, String id, String language) { |
57 |
this.create(folder, id);
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public boolean remove() { |
62 |
return FileUtils.deleteQuietly(externFile);
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public boolean move(ScriptingFolder target) { |
67 |
File target_f = target.getFile();
|
68 |
File source_f = this.getExternalFile(); |
69 |
try {
|
70 |
FileUtils.copyFile(source_f, target_f); |
71 |
return true; |
72 |
} catch (IOException ex) { |
73 |
return false; |
74 |
} |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public String[] getIconNames() { |
79 |
return new String[]{ |
80 |
"scripting-icon-externalfile",
|
81 |
"scripting-icon-externalfile-open"
|
82 |
}; |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public File getExternalFile() { |
87 |
return this.externFile; |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public File getFile() { |
92 |
if (this.getId() == null) { |
93 |
return null; |
94 |
} |
95 |
if (this.getParent() == null) { |
96 |
return null; |
97 |
} |
98 |
if (this.getParent().getFile() == null) { |
99 |
return null; |
100 |
} |
101 |
return this.externFile; |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public List<File> getFiles() { |
106 |
List<File> l=new ArrayList<>(); |
107 |
l.add(this.externFile);
|
108 |
return l;
|
109 |
} |
110 |
|
111 |
|
112 |
@Override
|
113 |
public String getMimeType() { |
114 |
if( this.mimeType != null ) { |
115 |
return this.mimeType; |
116 |
} |
117 |
if( tika == null ) { |
118 |
tika = new Tika();
|
119 |
} |
120 |
try {
|
121 |
this.mimeType = tika.detect(externFile);
|
122 |
} catch (IOException ex) { |
123 |
return "application/octet-stream"; |
124 |
} |
125 |
return this.mimeType; |
126 |
} |
127 |
|
128 |
@Override
|
129 |
public void setContents(String text) { |
130 |
try {
|
131 |
FileUtils.write(externFile, text); |
132 |
} catch (IOException ex) { |
133 |
throw new RuntimeException(ex); |
134 |
} |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public String getContentsAsText() { |
139 |
try {
|
140 |
Charset encoding = Charsets.toCharset(EncodingUtils.getEncoding(externFile));
|
141 |
return FileUtils.readFileToString(externFile, encoding);
|
142 |
} catch (IOException ex) { |
143 |
throw new RuntimeException(ex); |
144 |
} |
145 |
} |
146 |
|
147 |
} |