1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-05 16:15:25 +02:00

[209704] move code for getting code page converter from activator to CodePageConverterManager utility class

This commit is contained in:
David McKnight 2007-12-04 17:22:41 +00:00
parent e77a193982
commit 51a2ca0ee4
3 changed files with 156 additions and 127 deletions

View file

@ -57,7 +57,6 @@ import org.eclipse.dstore.core.model.DataStoreResources;
import org.eclipse.dstore.core.model.IDataStoreProvider;
import org.eclipse.rse.dstore.universal.miners.IUniversalDataStoreConstants;
import org.eclipse.rse.dstore.universal.miners.UniversalByteStreamHandler;
import org.eclipse.rse.internal.services.Activator;
import org.eclipse.rse.internal.services.dstore.ServiceResources;
import org.eclipse.rse.services.clientserver.FileTypeMatcher;
import org.eclipse.rse.services.clientserver.IMatcher;
@ -72,6 +71,7 @@ import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.dstore.AbstractDStoreService;
import org.eclipse.rse.services.dstore.util.DownloadListener;
import org.eclipse.rse.services.dstore.util.FileSystemMessageUtil;
import org.eclipse.rse.services.files.CodePageConverterManager;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.IFileServiceCodePageConverter;
import org.eclipse.rse.services.files.IHostFile;
@ -489,10 +489,7 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
int localLineSepLength = localLineSep.length();
IFileServiceCodePageConverter codePageConverter = Activator.getCodePageConverter(hostEncoding, this);
if (codePageConverter == null) {
codePageConverter = Activator.getDefaultCodePageConverter();
}
IFileServiceCodePageConverter codePageConverter = CodePageConverterManager.getCodePageConverter(hostEncoding, this);
// upload bytes while available
while (available > 0 && !isCancelled)
@ -788,12 +785,10 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_SUCCESS_TYPE))
{
if (!isBinary){ // do standard conversion if this is text!
String localEncoding = System.getProperty("file.encoding");
String localEncoding = System.getProperty("file.encoding"); //$NON-NLS-1$
IFileServiceCodePageConverter codePageConverter = Activator.getCodePageConverter(encoding, this);
if (codePageConverter == null) {
codePageConverter = Activator.getDefaultCodePageConverter();
}
IFileServiceCodePageConverter codePageConverter = CodePageConverterManager.getCodePageConverter(encoding, this);
codePageConverter.convertFileFromRemoteEncoding(localFile, encoding, localEncoding, this);
}
@ -1011,60 +1006,61 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer
List resultList = remoteElement.getNestedData();
DataElement resultChild = null;
for (int i = 0; i < resultList.size(); i++)
if (resultList != null)
{
resultChild = (DataElement) resultList.get(i);
if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_SUCCESS_TYPE))
for (int i = 0; i < resultList.size(); i++)
{
// do standard conversion if this is text!
if (!isBinaries[i]){ // do standard conversion if this is text!
String localEncoding = System.getProperty("file.encoding");
IFileServiceCodePageConverter codePageConverter = Activator.getCodePageConverter(hostEncodings[i], this);
if (codePageConverter == null) {
codePageConverter = Activator.getDefaultCodePageConverter();
resultChild = (DataElement) resultList.get(i);
if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_SUCCESS_TYPE))
{
// do standard conversion if this is text!
if (!isBinaries[i]){ // do standard conversion if this is text!
String localEncoding = System.getProperty("file.encoding"); //$NON-NLS-1$
IFileServiceCodePageConverter codePageConverter = CodePageConverterManager.getCodePageConverter(hostEncodings[i], this);
codePageConverter.convertFileFromRemoteEncoding(localFile, hostEncodings[i], localEncoding, this);
}
codePageConverter.convertFileFromRemoteEncoding(localFile, hostEncodings[i], localEncoding, this);
result = true;
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION))
{
localFile.delete();
SystemMessage msg = getMessage("RSEF1001").makeSubstitution(IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION); //$NON-NLS-1$
throw new SystemMessageException(msg);
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_UNSUPPORTED_ENCODING_EXCEPTION))
{
//SystemMessage msg = getMessage();
//throw new SystemMessageException(msg);
//UnsupportedEncodingException e = new UnsupportedEncodingException(resultChild.getName());
//UniversalSystemPlugin.logError(CLASSNAME + "." + "copy: " + "error reading file " + remotePath, e);
//throw new RemoteFileIOException(e);
result = false;
}
result = true;
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION))
{
localFile.delete();
SystemMessage msg = getMessage("RSEF1001").makeSubstitution(IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION); //$NON-NLS-1$
throw new SystemMessageException(msg);
//IOException e = new IOException(resultChild.getName());
//UniversalSystemPlugin.logError(CLASSNAME + "." + "copy: " + "error reading file " + remotePath, e);
//throw new RemoteFileIOException(e);
}
else
{
result = false;
}
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION))
if (monitor != null)
{
localFile.delete();
SystemMessage msg = getMessage("RSEF1001").makeSubstitution(IUniversalDataStoreConstants.DOWNLOAD_RESULT_FILE_NOT_FOUND_EXCEPTION); //$NON-NLS-1$
throw new SystemMessageException(msg);
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_UNSUPPORTED_ENCODING_EXCEPTION))
{
//SystemMessage msg = getMessage();
//throw new SystemMessageException(msg);
//UnsupportedEncodingException e = new UnsupportedEncodingException(resultChild.getName());
//UniversalSystemPlugin.logError(CLASSNAME + "." + "copy: " + "error reading file " + remotePath, e);
//throw new RemoteFileIOException(e);
result = false;
}
else if (resultChild.getType().equals(IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION))
{
localFile.delete();
SystemMessage msg = getMessage("RSEF1001").makeSubstitution(IUniversalDataStoreConstants.DOWNLOAD_RESULT_IO_EXCEPTION); //$NON-NLS-1$
throw new SystemMessageException(msg);
//IOException e = new IOException(resultChild.getName());
//UniversalSystemPlugin.logError(CLASSNAME + "." + "copy: " + "error reading file " + remotePath, e);
//throw new RemoteFileIOException(e);
}
else
{
result = false;
}
//monitor.done();
}
}
if (monitor != null)
{
//monitor.done();
}
}
return result;
}

View file

@ -20,7 +20,6 @@ package org.eclipse.rse.internal.services;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
@ -36,8 +35,7 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.eclipse.rse.services.clientserver.archiveutils.ArchiveHandlerManager;
import org.eclipse.rse.services.files.DefaultFileServiceCodePageConverter;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.CodePageConverterManager;
import org.eclipse.rse.services.files.IFileServiceCodePageConverter;
/**
@ -47,9 +45,7 @@ public class Activator extends Plugin {
//The shared instance.
private static Activator plugin;
private static IFileServiceCodePageConverter _defaultCodePageConverter;
protected static Vector _codePageConverters;
/**
* The constructor.
@ -130,79 +126,35 @@ public class Activator extends Plugin {
private void registerCodePageConverters()
{
if (_codePageConverters == null) {
// retrieve all extension points
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint ep = registry.getExtensionPoint("org.eclipse.rse.services", "codePageConverters"); //$NON-NLS-1$
if (ep != null){
IExtension[] extensions = ep.getExtensions();
_codePageConverters = new Vector();
for (int i = 0; i < extensions.length; i++) {
IExtension extension = extensions[i];
IConfigurationElement[] configElements = extension.getConfigurationElements();
for (int j = 0; j < configElements.length; j++) {
IConfigurationElement element = configElements[j];
if (element.getName().equalsIgnoreCase("codePageConverter")) {
try {
Object codePageConverter = element.createExecutableExtension("class");
if (codePageConverter!=null && codePageConverter instanceof IFileServiceCodePageConverter){
// only save extension point which implement the correct interface
_codePageConverters.add(codePageConverter);
}
}
catch (CoreException e) {
//shouldn't get here....
e.printStackTrace();
// retrieve all extension points
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint ep = registry.getExtensionPoint("org.eclipse.rse.services", "codePageConverters"); //$NON-NLS-1$ //$NON-NLS-2$
if (ep != null){
IExtension[] extensions = ep.getExtensions();
for (int i = 0; i < extensions.length; i++) {
IExtension extension = extensions[i];
IConfigurationElement[] configElements = extension.getConfigurationElements();
for (int j = 0; j < configElements.length; j++) {
IConfigurationElement element = configElements[j];
if (element.getName().equalsIgnoreCase("codePageConverter")) { //$NON-NLS-1$
try {
Object codePageConverter = element.createExecutableExtension("class"); //$NON-NLS-1$
if (codePageConverter!=null && codePageConverter instanceof IFileServiceCodePageConverter){
// only save extension point which implement the correct interface
CodePageConverterManager.registerCodePageConverter((IFileServiceCodePageConverter)codePageConverter);
}
}
catch (CoreException e) {
//shouldn't get here....
e.printStackTrace();
}
}
}
}
}
if (_defaultCodePageConverter == null){
_defaultCodePageConverter = new DefaultFileServiceCodePageConverter();
}
}
public static IFileServiceCodePageConverter getDefaultCodePageConverter()
{
return _defaultCodePageConverter;
}
/**
* Retrieves the first codepage converter provided via the codePageConverter extension point for the specified
* encoding
* @param serverEncoding The server encoding for which to retrieve a code page converter
* @return A code page converter for the specified encoding, or null if no converter was found for that encoding.
*/
public static IFileServiceCodePageConverter getCodePageConverter(String serverEncoding, IFileService fileService) {
IFileServiceCodePageConverter matchingCodePageConverter = null;
if (_codePageConverters != null)
{
//scan through the available converters and return the first valid one for the specified encoding for this
// subsystem implementation
for (int i=0; i<_codePageConverters.size(); i++) {
IFileServiceCodePageConverter codePageConverter = (IFileServiceCodePageConverter)_codePageConverters.elementAt(i);
if (codePageConverter.isServerEncodingSupported(serverEncoding, fileService))
{
if (matchingCodePageConverter != null){
int matchingPriority = matchingCodePageConverter.getPriority(serverEncoding, fileService);
int newPriority = codePageConverter.getPriority(serverEncoding, fileService);
if (newPriority < matchingPriority){
matchingCodePageConverter = codePageConverter;
}
}
else {
matchingCodePageConverter = codePageConverter;
}
}
}
}
return matchingCodePageConverter;
}
/**
* Logs an throwable to the log for this plugin.

View file

@ -0,0 +1,81 @@
/********************************************************************************
* Copyright (c) 2007 IBM Corporation. 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
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight.
*
* Contributors:
* {Name} (company) - description of contribution.
* David McKnight (IBM) - [209704] [api] Ability to override default encoding conversion needed.
********************************************************************************/
package org.eclipse.rse.services.files;
import java.util.Vector;
/**
* Utility class for getting a file service code page converter for a given server encoding and file service
*/
public class CodePageConverterManager {
private static IFileServiceCodePageConverter _defaultCodePageConverter;
protected static Vector _codePageConverters = new Vector();
public static void registerCodePageConverter(IFileServiceCodePageConverter converter)
{
_codePageConverters.add(converter);
}
public static IFileServiceCodePageConverter getDefaultCodePageConverter()
{
if (_defaultCodePageConverter == null){
_defaultCodePageConverter = new DefaultFileServiceCodePageConverter();
}
return _defaultCodePageConverter;
}
/**
* Retrieves the first codepage converter provided via the codePageConverter extension point for the specified
* encoding
* @param serverEncoding The server encoding for which to retrieve a code page converter
* @return A code page converter for the specified encoding, or null if no converter was found for that encoding.
*/
public static IFileServiceCodePageConverter getCodePageConverter(String serverEncoding, IFileService fileService) {
IFileServiceCodePageConverter matchingCodePageConverter = null;
if (_codePageConverters != null)
{
//scan through the available converters and return the first valid one for the specified encoding for this
// subsystem implementation
for (int i=0; i<_codePageConverters.size(); i++) {
IFileServiceCodePageConverter codePageConverter = (IFileServiceCodePageConverter)_codePageConverters.elementAt(i);
if (codePageConverter.isServerEncodingSupported(serverEncoding, fileService))
{
if (matchingCodePageConverter != null){
int matchingPriority = matchingCodePageConverter.getPriority(serverEncoding, fileService);
int newPriority = codePageConverter.getPriority(serverEncoding, fileService);
if (newPriority < matchingPriority){
matchingCodePageConverter = codePageConverter;
}
}
else {
matchingCodePageConverter = codePageConverter;
}
}
}
}
if (matchingCodePageConverter == null)
{
matchingCodePageConverter = _defaultCodePageConverter;
}
return matchingCodePageConverter;
}
}