blob: c564553c1d12872dd4db18c955a56f44a6d2dfea (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*******************************************************************************
* Copyright (c) 2014 BestSolution.at and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
*******************************************************************************/
package org.eclipse.fx.code.compensator.editor.internal;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.fx.code.compensator.editor.ContentTypeProvider;
import org.eclipse.fx.code.compensator.editor.Input;
import org.eclipse.fx.code.compensator.editor.TextEditor;
import org.eclipse.fx.code.compensator.editor.URIProvider;
import org.eclipse.fx.code.compensator.editor.services.ContentTypeDetector;
import org.eclipse.fx.core.URI;
import org.eclipse.fx.core.di.Service;
public class FileInput implements Input<String>, ContentTypeProvider, URIProvider {
private Path path;
private String data;
private final List<ContentTypeDetector> detectorList;
@Inject
public FileInput(@Named(TextEditor.DOCUMENT_URL) String url, @Service List<ContentTypeDetector> detectorList) {
this.path = Paths.get(java.net.URI.create(url));
this.detectorList = detectorList;
}
@Override
public URI getURI() {
return URI.create(path.toUri().toString());
}
@Override
public String getData() {
if( data == null ) {
try(BufferedReader reader = Files.newBufferedReader(path)) {
StringBuilder b = new StringBuilder();
String line;
while( (line = reader.readLine()) != null ) {
//FIXME We need to replace TABs for now
b.append(line.replaceAll("\t", " ")+"\n");
}
reader.close();
data = b.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;
}
@Override
public void setData(String data) {
this.data = data;
}
@Override
public void persist() {
try(BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.write(data);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void reset() {
this.data = null;
getData();
}
@Override
public void dispose() {
this.data = null;
this.path = null;
}
@Override
public String getContentType() {
if( path.toString().endsWith(".java") ) {
return ContentTypeProvider.JAVA;
} else if( path.toString().endsWith(".js") ) {
return ContentTypeProvider.JAVASCRIPT;
}
for( ContentTypeDetector d : detectorList ) {
String ct = d.getContentType(this);
if( ct != null ) {
return ct;
}
}
return null;
}
}
|