Added
Link Here
|
1 |
package org.eclipse.jdt.internal.ui.compare; |
2 |
|
3 |
import java.io.ByteArrayInputStream; |
4 |
import java.io.ByteArrayOutputStream; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.io.UnsupportedEncodingException; |
8 |
import java.util.Map; |
9 |
|
10 |
import org.eclipse.compare.IEncodedStreamContentAccessor; |
11 |
import org.eclipse.compare.IStreamContentAccessor; |
12 |
import org.eclipse.compare.ITypedElement; |
13 |
import org.eclipse.compare.structuremergeviewer.ICompareInput; |
14 |
import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener; |
15 |
import org.eclipse.jdt.core.JavaCore; |
16 |
import org.eclipse.jdt.core.ToolFactory; |
17 |
import org.eclipse.jdt.core.formatter.CodeFormatter; |
18 |
|
19 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
20 |
|
21 |
import org.eclipse.jface.text.Document; |
22 |
import org.eclipse.swt.graphics.Image; |
23 |
import org.eclipse.text.edits.TextEdit; |
24 |
|
25 |
import org.eclipse.core.runtime.CoreException; |
26 |
import org.eclipse.core.runtime.IStatus; |
27 |
import org.eclipse.core.runtime.Status; |
28 |
|
29 |
|
30 |
|
31 |
public class FormattedCompareInput implements ICompareInput{ |
32 |
class StringInput implements ITypedElement, |
33 |
IEncodedStreamContentAccessor { |
34 |
|
35 |
private String fContents; |
36 |
|
37 |
private String fType; |
38 |
|
39 |
private String fName; |
40 |
|
41 |
public StringInput(final String type, final String name, final String contents) { |
42 |
this.fType = type; |
43 |
this.fName = name; |
44 |
this.fContents = contents; |
45 |
} |
46 |
|
47 |
public Image getImage() { |
48 |
return null; |
49 |
} |
50 |
|
51 |
public String getName() { |
52 |
return this.fName; |
53 |
} |
54 |
|
55 |
public String getType() { |
56 |
return this.fType; |
57 |
} |
58 |
|
59 |
public String getCharset() throws CoreException { |
60 |
return "UTF-16"; //$NON-NLS-1$ |
61 |
} |
62 |
|
63 |
public InputStream getContents() throws CoreException { |
64 |
byte[] bytes; |
65 |
try { |
66 |
bytes = this.fContents.getBytes("UTF-16"); //$NON-NLS-1$ |
67 |
} catch (final UnsupportedEncodingException e) { |
68 |
bytes = this.fContents.getBytes(); |
69 |
} |
70 |
return new ByteArrayInputStream(bytes); |
71 |
} |
72 |
|
73 |
} |
74 |
|
75 |
private ICompareInput fCompareInput; |
76 |
|
77 |
|
78 |
public FormattedCompareInput(final ICompareInput compareInput) { |
79 |
this.fCompareInput = compareInput; |
80 |
} |
81 |
|
82 |
public void addCompareInputChangeListener( |
83 |
final ICompareInputChangeListener listener) { |
84 |
this.fCompareInput.addCompareInputChangeListener(listener); |
85 |
} |
86 |
|
87 |
public void copy(final boolean leftToRight) { |
88 |
this.fCompareInput.copy(leftToRight); |
89 |
} |
90 |
|
91 |
public ITypedElement getAncestor() { |
92 |
return this.fCompareInput.getAncestor(); |
93 |
} |
94 |
|
95 |
public Image getImage() { |
96 |
return this.fCompareInput.getImage(); |
97 |
} |
98 |
|
99 |
public int getKind() { |
100 |
return this.fCompareInput.getKind(); |
101 |
} |
102 |
|
103 |
public ITypedElement getLeft() { |
104 |
final ITypedElement leftElement = this.fCompareInput.getLeft(); |
105 |
|
106 |
return format(leftElement); |
107 |
} |
108 |
|
109 |
public String getName() { |
110 |
return this.fCompareInput.getName(); |
111 |
} |
112 |
|
113 |
public ITypedElement getRight() { |
114 |
final ITypedElement rightElement = this.fCompareInput.getRight(); |
115 |
|
116 |
return format(rightElement); |
117 |
} |
118 |
|
119 |
public void removeCompareInputChangeListener( |
120 |
final ICompareInputChangeListener listener) { |
121 |
this.fCompareInput.removeCompareInputChangeListener(listener); |
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
private ITypedElement format(final ITypedElement elementToFormat) { |
127 |
try { |
128 |
if (elementToFormat instanceof IStreamContentAccessor) { |
129 |
final String contentsString = fromIStreamContentAccessorToString((IStreamContentAccessor) elementToFormat); |
130 |
final Map options = JavaCore.getOptions(); |
131 |
|
132 |
final CodeFormatter codeFormatter = ToolFactory |
133 |
.createCodeFormatter(options); |
134 |
final TextEdit tmpOutputFromFormatter = codeFormatter.format( |
135 |
CodeFormatter.K_COMPILATION_UNIT, contentsString, 0, contentsString |
136 |
.length(), 0, null); |
137 |
if (tmpOutputFromFormatter != null) { |
138 |
//to convert from TextEdit to String we must pass it to a Document |
139 |
final Document tempDoc = new Document(contentsString); |
140 |
tmpOutputFromFormatter.apply(tempDoc); |
141 |
final String formattedText = tempDoc.get(); |
142 |
final StringInput toReturn = new StringInput(elementToFormat |
143 |
.getType(), elementToFormat.getName(), formattedText); |
144 |
return toReturn; |
145 |
} else { |
146 |
JavaPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,JavaPlugin.getPluginId(), 0, "Cannot format the source code",null)); //$NON-NLS-1$ |
147 |
return elementToFormat; |
148 |
} |
149 |
|
150 |
} |
151 |
|
152 |
} catch (final Exception e) { |
153 |
JavaPlugin.getDefault().getLog().log(new Status(IStatus.WARNING,JavaPlugin.getPluginId(), 0, "Cannot format the source code",e)); //$NON-NLS-1$ |
154 |
} |
155 |
return elementToFormat; |
156 |
} |
157 |
|
158 |
private String fromIStreamContentAccessorToString(IStreamContentAccessor isca) throws CoreException, IOException { |
159 |
String charset = null; |
160 |
if (isca instanceof IEncodedStreamContentAccessor) { //try to get the charset from the inputstream |
161 |
charset = ((IEncodedStreamContentAccessor)isca).getCharset(); |
162 |
} |
163 |
if (charset == null || "".equals(charset)) { //$NON-NLS-1$ |
164 |
//set default charset to UTF-8 |
165 |
charset = "UTF-8"; //$NON-NLS-1$ |
166 |
} |
167 |
final InputStream contentIs = isca.getContents(); |
168 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
169 |
byte[] buffer = new byte[4096]; |
170 |
int readBytes = 0; |
171 |
while((readBytes = contentIs.read(buffer)) >= 0) { |
172 |
bos.write(buffer,0,readBytes); |
173 |
} |
174 |
return new String(bos.toByteArray(),charset); |
175 |
} |
176 |
|
177 |
|
178 |
|
179 |
} |