1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Bug 573797: Refactor code to use Charset instead of passing around Strings

This simplifies error handling as once you have a Charset you don't
have to worry about whether or not an UnsupportedEncodingException can
be thrown anymore. In addition it is a little easier on type safety.

Change-Id: I4292878a7c621f9d05fdb98f5c26a0ae8bfec062
This commit is contained in:
Jonah Graham 2021-05-27 01:02:20 -04:00
parent 385d302d70
commit d465e83836
9 changed files with 142 additions and 48 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2 Bundle-ManifestVersion: 2
Bundle-Name: %pluginName Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.tm.terminal.control; singleton:=true Bundle-SymbolicName: org.eclipse.tm.terminal.control; singleton:=true
Bundle-Version: 5.2.100.qualifier Bundle-Version: 5.3.0.qualifier
Bundle-Activator: org.eclipse.tm.internal.terminal.control.impl.TerminalPlugin Bundle-Activator: org.eclipse.tm.internal.terminal.control.impl.TerminalPlugin
Bundle-Vendor: %providerName Bundle-Vendor: %providerName
Bundle-Localization: plugin Bundle-Localization: plugin

View file

@ -17,6 +17,7 @@
package org.eclipse.tm.internal.terminal.control; package org.eclipse.tm.internal.terminal.control;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.eclipse.swt.dnd.Clipboard; import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Font;
@ -38,18 +39,38 @@ public interface ITerminalViewControl {
* *
* @see ITerminalControl#setEncoding(String) * @see ITerminalControl#setEncoding(String)
* @since org.eclipse.tm.terminal 2.0 * @since org.eclipse.tm.terminal 2.0
* @deprecated Use {@link #setCharset(Charset)} and do the error handling in the UI code.
*/ */
@Deprecated
void setEncoding(String encoding) throws UnsupportedEncodingException; void setEncoding(String encoding) throws UnsupportedEncodingException;
/**
* Set the charset that the Terminal uses to decode byte streams into
* characters.
*
* @see ITerminalControl#setCharset(Charset)
* @since 5.3
*/
void setCharset(Charset charset);
/** /**
* Get the Terminal's current encoding. * Get the Terminal's current encoding.
* *
* @return the current Encoding of the Terminal. * @return the current Encoding of the Terminal.
* @see ITerminalControl#getEncoding() * @see ITerminalControl#getEncoding()
* @since org.eclipse.tm.terminal 2.0 * @since org.eclipse.tm.terminal 2.0
* @deprecated Use {@link #getCharset()} and call {@link Charset#name()} on the result
*/ */
@Deprecated
String getEncoding(); String getEncoding();
/**
* @return the non-<code>null</code> current Charset of the Terminal.
* @see ITerminalControl#getCharset()
* @since 5.3
*/
Charset getCharset();
boolean isEmpty(); boolean isEmpty();
/** /**

View file

@ -156,8 +156,7 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
private boolean connectOnEnterIfClosed = true; private boolean connectOnEnterIfClosed = true;
PipedInputStream fInputStream; PipedInputStream fInputStream;
private static final String defaultEncoding = Charset.defaultCharset().name(); private Charset fCharset = Charset.defaultCharset();
private String fEncoding = defaultEncoding;
private InputStreamReader fInputStreamReader; private InputStreamReader fInputStreamReader;
private ICommandInputField fCommandInputField; private ICommandInputField fCommandInputField;
@ -230,38 +229,43 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
fTerminalModel.setMaxHeight(1000); fTerminalModel.setMaxHeight(1000);
fInputStream = new PipedInputStream(8 * 1024); fInputStream = new PipedInputStream(8 * 1024);
fTerminalText = new VT100Emulator(fTerminalModel, this, null); fTerminalText = new VT100Emulator(fTerminalModel, this, null);
try { // Use Default Charset as start, until setCharset() is called
// Use Default Encoding as start, until setEncoding() is called setCharset(Charset.defaultCharset());
setEncoding(null);
} catch (UnsupportedEncodingException e) {
// Should never happen
e.printStackTrace();
// Fall back to local Platform Default Encoding
fEncoding = defaultEncoding;
fInputStreamReader = new InputStreamReader(fInputStream);
fTerminalText.setInputStreamReader(fInputStreamReader);
}
setupTerminal(wndParent); setupTerminal(wndParent);
} }
@Override @Override
@Deprecated
public void setEncoding(String encoding) throws UnsupportedEncodingException { public void setEncoding(String encoding) throws UnsupportedEncodingException {
Charset charset;
if (encoding == null) { if (encoding == null) {
// TODO better use a standard remote-to-local encoding? charset = Charset.defaultCharset();
encoding = "ISO-8859-1"; //$NON-NLS-1$ } else {
// TODO or better use the local default encoding? charset = Charset.forName(encoding);
// encoding = defaultEncoding;
} }
fInputStreamReader = new InputStreamReader(fInputStream, encoding);
// remember encoding if above didn't throw an exception // remember encoding if above didn't throw an exception
fEncoding = encoding; setCharset(charset);
}
@Override
public void setCharset(Charset charset) {
if (charset == null) {
charset = Charset.defaultCharset();
}
fInputStreamReader = new InputStreamReader(fInputStream, charset);
fCharset = charset;
fTerminalText.setInputStreamReader(fInputStreamReader); fTerminalText.setInputStreamReader(fInputStreamReader);
} }
@Override @Override
@Deprecated
public String getEncoding() { public String getEncoding() {
return fEncoding; return fCharset.name();
}
@Override
public Charset getCharset() {
return fCharset;
} }
@Override @Override
@ -547,7 +551,7 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
// TODO: Find a way to force this to use the ISO Latin-1 encoding. // TODO: Find a way to force this to use the ISO Latin-1 encoding.
// TODO: handle Encoding Errors in a better way // TODO: handle Encoding Errors in a better way
getOutputStream().write(string.getBytes(fEncoding)); getOutputStream().write(string.getBytes(fCharset));
getOutputStream().flush(); getOutputStream().flush();
} catch (SocketException socketException) { } catch (SocketException socketException) {
displayTextInTerminal(socketException.getMessage()); displayTextInTerminal(socketException.getMessage());
@ -591,7 +595,7 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
// //
// TODO: Make the ESCAPE-vs-highbit behavior user configurable. // TODO: Make the ESCAPE-vs-highbit behavior user configurable.
byte[] bytesToSend = String.valueOf(chKey).getBytes(fEncoding); byte[] bytesToSend = String.valueOf(chKey).getBytes(fCharset);
StringBuilder b = new StringBuilder("sending ESC"); //$NON-NLS-1$ StringBuilder b = new StringBuilder("sending ESC"); //$NON-NLS-1$
for (int i = 0; i < bytesToSend.length; i++) { for (int i = 0; i < bytesToSend.length; i++) {
if (i != 0) if (i != 0)
@ -602,7 +606,7 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
os.write('\u001b'); os.write('\u001b');
os.write(bytesToSend); os.write(bytesToSend);
} else { } else {
byte[] bytesToSend = String.valueOf(chKey).getBytes(fEncoding); byte[] bytesToSend = String.valueOf(chKey).getBytes(fCharset);
StringBuilder b = new StringBuilder("sending"); //$NON-NLS-1$ StringBuilder b = new StringBuilder("sending"); //$NON-NLS-1$
for (int i = 0; i < bytesToSend.length; i++) { for (int i = 0; i < bytesToSend.length; i++) {
if (i != 0) if (i != 0)
@ -826,10 +830,7 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC
private void writeToTerminal(String text) { private void writeToTerminal(String text) {
try { try {
getRemoteToTerminalOutputStream().write(text.getBytes(fEncoding)); getRemoteToTerminalOutputStream().write(text.getBytes(fCharset));
} catch (UnsupportedEncodingException e) {
// should never happen!
e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
// should never happen! // should never happen!
e.printStackTrace(); e.printStackTrace();

View file

@ -18,6 +18,7 @@ package org.eclipse.tm.internal.terminal.provisional.api;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
@ -83,9 +84,39 @@ public interface ITerminalControl {
* LANG on UNIX systems. * LANG on UNIX systems.
* *
* @since org.eclipse.tm.terminal 2.0 * @since org.eclipse.tm.terminal 2.0
* @deprecated Use {@link #setCharset(Charset)} and do the error handling in the UI code.
*/ */
@Deprecated
void setEncoding(String encoding) throws UnsupportedEncodingException; void setEncoding(String encoding) throws UnsupportedEncodingException;
/**
* Set the charset that the Terminal uses to decode bytes from the
* Terminal-to-remote-Stream into Unicode Characters used in Java; or, to
* encode Characters typed by the user into bytes sent over the wire to the
* remote.
*
* By default, the local Platform Default charset is used. Also note that
* the encoding must not be applied in case the terminal stream is processed
* by some data transfer protocol which requires binary data.
*
* Validity of the charset set here is not checked. Since some encodings do
* not cover the entire range of Unicode characters, it can happen that a
* particular Unicode String typed in by the user can not be encoded into a
* byte Stream with the encoding specified. and UnsupportedEncodingException
* will be thrown in this case at the time the String is about to be
* processed.
*
* The concrete encoding to use can either be specified manually by a user,
* by means of a dialog, or a connector can try to obtain it automatically
* from the remote side e.g. by evaluating an environment variable such as
* LANG on UNIX systems.
*
* @param charset Charset to use, or <code>null</code> for platform's default charset.
*
* @since 5.3
*/
void setCharset(Charset charset);
/** /**
* Return the current encoding. That's interesting when the previous * Return the current encoding. That's interesting when the previous
* setEncoding() call failed and the fallback default encoding should be * setEncoding() call failed and the fallback default encoding should be
@ -94,9 +125,19 @@ public interface ITerminalControl {
* *
* @return the current Encoding of the Terminal. * @return the current Encoding of the Terminal.
* @since org.eclipse.tm.terminal 2.0 * @since org.eclipse.tm.terminal 2.0
* @deprecated Use {@link #getCharset()} and call {@link Charset#name()} on the result
*/ */
@Deprecated
String getEncoding(); String getEncoding();
/**
* Return the current charset.
*
* @return the non-<code>null</code> current charset of the Terminal
* @since 5.3
*/
Charset getCharset();
/** /**
* Show a text in the terminal. If puts newlines at the beginning and the * Show a text in the terminal. If puts newlines at the beginning and the
* end. * end.

View file

@ -17,6 +17,7 @@
package org.eclipse.tm.internal.terminal.connector; package org.eclipse.tm.internal.terminal.connector;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
@ -53,11 +54,20 @@ public class TerminalConnectorFactoryTest extends TestCase {
public void setEncoding(String encoding) { public void setEncoding(String encoding) {
} }
@Override
public void setCharset(Charset charset) {
}
@Override @Override
public String getEncoding() { public String getEncoding() {
return "ISO-8859-1"; //$NON-NLS-1$ return "ISO-8859-1"; //$NON-NLS-1$
} }
@Override
public Charset getCharset() {
return Charset.defaultCharset();
}
@Override @Override
public void displayTextInTerminal(String text) { public void displayTextInTerminal(String text) {
} }

View file

@ -17,6 +17,7 @@
package org.eclipse.tm.internal.terminal.connector; package org.eclipse.tm.internal.terminal.connector;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Shell;
@ -53,11 +54,20 @@ public class TerminalConnectorTest extends TestCase {
public void setEncoding(String encoding) { public void setEncoding(String encoding) {
} }
@Override
public void setCharset(Charset charset) {
}
@Override @Override
public String getEncoding() { public String getEncoding() {
return "ISO-8859-1"; //$NON-NLS-1$ return "ISO-8859-1"; //$NON-NLS-1$
} }
@Override
public Charset getCharset() {
return Charset.defaultCharset();
}
@Override @Override
public void displayTextInTerminal(String text) { public void displayTextInTerminal(String text) {
} }

View file

@ -11,8 +11,6 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.tm.terminal.view.ui.actions; package org.eclipse.tm.terminal.view.ui.actions;
import java.io.UnsupportedEncodingException;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.IAction; import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.ImageDescriptor;
@ -53,14 +51,10 @@ public class SelectEncodingAction extends AbstractTerminalAction {
return; return;
EncodingSelectionDialog dialog = new EncodingSelectionDialog(null); EncodingSelectionDialog dialog = new EncodingSelectionDialog(null);
dialog.setEncoding(target.getEncoding()); dialog.setCharset(target.getCharset());
if (dialog.open() == Window.OK) { if (dialog.open() == Window.OK) {
try { target.setCharset(dialog.getCharset());
target.setEncoding(dialog.getEncoding()); tabFolderManager.updateStatusLine();
tabFolderManager.updateStatusLine();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} }
} }

View file

@ -11,6 +11,9 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.tm.terminal.view.ui.internal.dialogs; package org.eclipse.tm.terminal.view.ui.internal.dialogs;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.TrayDialog; import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
@ -33,7 +36,7 @@ public class EncodingSelectionDialog extends TrayDialog {
private String contextHelpId = null; private String contextHelpId = null;
// The selected encoding or null // The selected encoding or null
/* default */ String encoding = null; private String encoding;
// Reference to the encodings panel // Reference to the encodings panel
private EncodingPanel encodingPanel = null; private EncodingPanel encodingPanel = null;
@ -194,16 +197,25 @@ public class EncodingSelectionDialog extends TrayDialog {
} }
/** /**
* Set the encoding to default to on creating the dialog. * Set the charset to default to on creating the dialog.
*/ */
public final void setEncoding(String encoding) {
this.encoding = encoding; public void setCharset(Charset charset) {
this.encoding = charset == null ? null : charset.name();
} }
/** /**
* Returns the selected encoding or <code>null</code>. * Returns the selected charset or <code>null</code>.
*/ */
public final String getEncoding() { public final Charset getCharset() {
return encoding; if (encoding == null) {
return null;
}
try {
return Charset.forName(encoding);
} catch (UnsupportedCharsetException e) {
return null;
}
} }
} }

View file

@ -11,7 +11,8 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.tm.terminal.view.ui.tabs; package org.eclipse.tm.terminal.view.ui.tabs;
import java.io.UnsupportedEncodingException; import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -279,8 +280,12 @@ public class TabFolderManager extends PlatformObject implements ISelectionProvid
new TerminalControlSelectionListener(terminal); new TerminalControlSelectionListener(terminal);
// Configure the terminal encoding // Configure the terminal encoding
try { try {
terminal.setEncoding(encoding); Charset charset = null;
} catch (UnsupportedEncodingException e) { if (encoding != null) {
charset = Charset.forName(encoding);
}
terminal.setCharset(charset);
} catch (UnsupportedCharsetException e) {
/* ignored on purpose */ /* ignored on purpose */
} }
// Associated the terminal with the tab item // Associated the terminal with the tab item