/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.all;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.texteditor.MarkerAnnotation;
public class InvalidCharacterCompletionProposal implements ICompletionProposal {
private static List<String> encodingsToTest = new ArrayList<String>();
static {
encodingsToTest.add("UTF-8");
encodingsToTest.add("ISO-8859-1");
encodingsToTest.add("ISO-8859-15");
encodingsToTest.add("CP1252");
}
private MarkerAnnotation _markerAnnotation;
private String _correctionProposal;
public InvalidCharacterCompletionProposal(MarkerAnnotation markerAnnotation) {
_markerAnnotation = markerAnnotation;
_correctionProposal = findCorrectionProposal(_markerAnnotation);
}
public void apply(IDocument document) {
int linenumber = _markerAnnotation.getMarker().getAttribute(IMarker.LINE_NUMBER, -1);
if (linenumber != -1) {
try {
int offset = document.getLineInformation(linenumber).getOffset();
int length = document.getLineInformation(linenumber).getLength();
document.replace(offset, length, _correctionProposal);
} catch (BadLocationException e) {
}
}
}
public String getAdditionalProposalInfo() {
return null;
}
public IContextInformation getContextInformation() {
return null;
}
public String getDisplayString() {
return "change to '" + _correctionProposal.trim() + "'";
}
public Image getImage() {
return null;
}
public Point getSelection(IDocument document) {
return null;
}
public static String findCorrectionProposal(MarkerAnnotation markerAnnotation) {
LineNumberReader read = null;
try {
int linenumber = (Integer) markerAnnotation.getMarker().getAttribute(IMarker.LINE_NUMBER);
File input = markerAnnotation.getMarker().getResource().getLocation().toFile();
for (String encoding : encodingsToTest) {
read = new LineNumberReader(new InputStreamReader(new FileInputStream(input), encoding));
String line;
while ((line = read.readLine()) != null) {
if (read.getLineNumber()-1 == linenumber) {
break;
}
}
if (line != null && !InvalidEncodingMarkingHandler.containsInvalidChar(line)) {
return line;
}
}
} catch (Exception e) {
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
}
}
}
return null;
}
public static boolean canCorrect(MarkerAnnotation markerAnnotation) {
return findCorrectionProposal(markerAnnotation) != null;
}
}