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