diff options
Diffstat (limited to 'bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse')
27 files changed, 0 insertions, 3691 deletions
diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedIO.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedIO.java deleted file mode 100644 index a2d99983db..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedIO.java +++ /dev/null @@ -1,258 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; -import java.util.Properties; - -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.content.IContentDescription; -import org.eclipse.wst.sse.core.internal.encoding.util.Assert; -import org.eclipse.wst.sse.core.internal.encoding.util.Logger; -import org.osgi.framework.Bundle; - - -public abstract class CodedIO { - - private final boolean DEBUG = false; - - public static final int MAX_BUF_SIZE = 1024 * 8; - - public static final int MAX_MARK_SIZE = MAX_BUF_SIZE; - - public static final String NO_SPEC_DEFAULT = "NoSpecDefault"; //$NON-NLS-1$ - - private static Properties overridenCharsets = null; - - /** - * <p> - * There are two well known understood cases where the standard/default - * Java Mappings are not sufficient. (Thanks to Hirotaka Matsumoto for - * providing these two). I believe there are others that individual - * customers have requested to override on a case by case basis, but I've - * lost the details. TODO-future: document some of those use-cases. - * </p> - * <ul> - * <li>ISO-8859-8-I</li> - * <p> - * In the code conversion point of view, ISO-9959-8 and ISO-8859-8-I are - * the same. However. the representation on the browser is different. ( - * It's very very hard to explain this into the words, but once you will - * see, you will understand it :) Many BiDi HTML/JSPs use ISO-8859-8-I in - * META/page directive. So WSAD needs to support this encoding. - * </p> - * <li>X-SJIS</li> - * <p> - * Because Mosaic/Navigator 2.0 supported only X-SJIS/X-EUC-JP, lots of - * old HTML files used X-SJIS/X-EUC-JP so that the customers still want us - * to support this code conversion for HTML files. - * </p> - * </ul> - * - * @param detectedCharsetName - * @return the detectedCharsetName, if no overrides, otherwise the charset - * name that should be used instead of detectedCharsetName - */ - /** - * This method is deliberatly 'default access' since clients should not - * need to access this information directly. - */ - static public String checkMappingOverrides(String detectedCharsetName) { - // This method MUST return what was passed in, if - // there are no - // overrides. - String result = detectedCharsetName; - String newResult = getOverridenCharsets().getProperty(detectedCharsetName); - if (newResult != null) { - result = newResult; - } - return result; - } - - /** - * Note: once this instance is created, trace info still needs to be - * appended by caller, depending on the context its created. - */ - public static EncodingMemento createEncodingMemento(byte[] detectedBom, String javaCharsetName, String detectedCharsetName, String unSupportedName, String specDefaultEncoding, String reason) { - EncodingMemento result = new EncodingMemento(); - result.setJavaCharsetName(javaCharsetName); - result.setDetectedCharsetName(detectedCharsetName); - // TODO: if detectedCharset and spec default is - // null, need to use "work - // bench based" defaults. - if (specDefaultEncoding == null) - result.setAppropriateDefault(NO_SPEC_DEFAULT); - else - result.setAppropriateDefault(specDefaultEncoding); - if (unSupportedName != null) { - result.setInvalidEncoding(unSupportedName); - } - // check if valid - try { - Charset.isSupported(javaCharsetName); - } catch (IllegalCharsetNameException e) { - result.setInvalidEncoding(javaCharsetName); - } - - // check UTF83ByteBOMUsed and UnicodeStream - if (detectedBom != null) { - if (detectedBom.length == 2) - result.setUnicodeStream(true); - else if (detectedBom.length == 3) - result.setUTF83ByteBOMUsed(true); - } - return result; - } - - /** - * Note: once this instance is created, trace info still needs to be - * appended by caller, depending on the context its created. - */ - public static EncodingMemento createEncodingMemento(String detectedCharsetName) { - return createEncodingMemento(detectedCharsetName, null); - } - - /** - * Note: once this instance is created, trace info still needs to be - * appended by caller, depending on the context its created. - */ - public static EncodingMemento createEncodingMemento(String detectedCharsetName, String reason) { - return createEncodingMemento(detectedCharsetName, reason, null); - } - - /** - * Note: once this instance is created, trace info still needs to be - * appended by caller, depending on the context its created. - */ - public static EncodingMemento createEncodingMemento(String detectedCharsetName, String reason, String specDefaultEncoding) { - EncodingMemento result = new EncodingMemento(); - result = new EncodingMemento(); - String javaCharset = getAppropriateJavaCharset(detectedCharsetName); - result.setJavaCharsetName(javaCharset); - result.setDetectedCharsetName(detectedCharsetName); - // TODO: if detectedCharset and spec default is - // null, need to use "work - // bench based" defaults. - if (specDefaultEncoding == null) - result.setAppropriateDefault(NO_SPEC_DEFAULT); - else - result.setAppropriateDefault(specDefaultEncoding); - // check if valid - try { - Charset.isSupported(javaCharset); - } catch (IllegalCharsetNameException e) { - result.setInvalidEncoding(javaCharset); - } - - return result; - } - - /** - * This method can return null, if invalid charset name (in which case - * "appropriateDefault" should be used, if a name is really need for some - * "save anyway" cases). - * - * @param detectedCharsetName - * @return - */ - public static String getAppropriateJavaCharset(String detectedCharsetName) { - // we don't allow null argument (or risk NPE or - // IllegalArgumentException later at several - // points. - Assert.isNotNull(detectedCharsetName, "illegal charset argument. it can not be null"); //$NON-NLS-1$ - String result = detectedCharsetName; - // 1. Check explicit mapping overrides from - // property file - result = CodedIO.checkMappingOverrides(detectedCharsetName); - // 2. Use the "canonical" name from JRE mappings - // Note: see Charset JavaDoc, the name you get one - // with can be alias, - // the name you get back is "standard" name. - Charset javaCharset = null; - // Note: this will immediatly throw - // "UnsuppotedCharsetException" if it - // invalid. Issue: Is it more client friendly to - // eat that exception and return null? - javaCharset = Charset.forName(result); - if (javaCharset != null) { - result = javaCharset.name(); - } - return result; - } - - /** - * @return Returns the overridenCharsets. - */ - private static Properties getOverridenCharsets() { - if (overridenCharsets == null) { - overridenCharsets = new Properties(); - Bundle keyBundle = Platform.getBundle(ICodedResourcePlugin.ID); - IPath keyPath = new Path("config/override.properties"); //$NON-NLS-1$ - URL location = Platform.find(keyBundle, keyPath); - InputStream propertiesInputStream = null; - try { - propertiesInputStream = location.openStream(); - overridenCharsets.load(propertiesInputStream); - } catch (IOException e) { - // if can't read, just assume there's no - // overrides - // and repeated attempts will not occur, - // since they - // will be represented by an empty - // Properties object - } - } - return overridenCharsets; - } - - /** - * This class need not be instantiated (though its subclasses can be). - */ - protected CodedIO() { - super(); - } - - protected EncodingMemento createMemento(IContentDescription contentDescription) { - EncodingMemento result; - String appropriateDefault = contentDescription.getContentType().getDefaultCharset(); - String detectedCharset = (String) contentDescription.getProperty(IContentDescriptionExtended.DETECTED_CHARSET); - String unSupportedCharset = (String) contentDescription.getProperty(IContentDescriptionExtended.UNSUPPORTED_CHARSET); - String javaCharset = contentDescription.getCharset(); - // integrity checks for debugging - if (javaCharset == null) { - Logger.log(Logger.INFO_DEBUG, "charset equaled null!"); //$NON-NLS-1$ - } else if (javaCharset.length() == 0) { - Logger.log(Logger.INFO_DEBUG, "charset equaled emptyString!"); //$NON-NLS-1$ - } - byte[] BOM = (byte[]) contentDescription.getProperty(IContentDescription.BYTE_ORDER_MARK); - //result = (EncodingMemento) - // contentDescription.getProperty(IContentDescriptionExtended.ENCODING_MEMENTO); - result = createEncodingMemento(BOM, javaCharset, detectedCharset, unSupportedCharset, appropriateDefault, null); - if (!result.isValid()) { - result.setAppropriateDefault(appropriateDefault); - // integrity check for debugging "invalid" cases. - // the apprriate default we have, should equal what's in the - // detected field. (not sure this is always required) - if (DEBUG && appropriateDefault != null && !appropriateDefault.equals(detectedCharset)) { - Logger.log(Logger.INFO_DEBUG, "appropriate did not equal detected, as expected for invalid charset case"); //$NON-NLS-1$ - } - } - return result; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedReaderCreator.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedReaderCreator.java deleted file mode 100644 index 1bb216d8b3..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedReaderCreator.java +++ /dev/null @@ -1,533 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.nio.charset.CharsetDecoder; -import java.nio.charset.CodingErrorAction; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.content.IContentDescription; -import org.eclipse.core.runtime.content.IContentTypeManager; -import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.wst.sse.core.internal.SSECoreMessages; -import org.eclipse.wst.sse.core.internal.encoding.util.Assert; -import org.eclipse.wst.sse.core.internal.encoding.util.BufferedLimitedStream; -import org.eclipse.wst.sse.core.internal.encoding.util.Logger; -import org.eclipse.wst.sse.core.internal.encoding.util.NullInputStream; -import org.eclipse.wst.sse.core.internal.encoding.util.UnicodeBOMEncodingDetector; -import org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail; - - -/** - * The purpose of this class is to centralize analysis of a file to determine - * the most appropriate rules of decoding it. The intended use is to set the - * input, then get the reader for that input which will have its encoding set - * appropriately. Additionally, there is an EncodingMemento provided, which - * will be required, in some cases, to later determine the most appropriate - * form of encoded output. - */ -public class CodedReaderCreator extends CodedIO { - - - private boolean fClientSuppliedStream; - - - private EncodingMemento fEncodingMemento; - - private EncodingRule fEncodingRule; - - private String fFilename; - - private IFile fIFile; - - - private InputStream fInputStream; - - public CodedReaderCreator() { - - super(); - } - - public CodedReaderCreator(IFile file) throws CoreException, IOException { - - this(); - set(file); - setEncodingRule(EncodingRule.CONTENT_BASED); - } - - public CodedReaderCreator(IFile file, EncodingRule encodingRule) throws CoreException, IOException { - - this(); - set(file); - setEncodingRule(encodingRule); - } - - public CodedReaderCreator(String filename, InputStream inputStream) { - - this(); - set(filename, inputStream); - setEncodingRule(EncodingRule.CONTENT_BASED); - } - - public CodedReaderCreator(String filename, InputStream inputStream, EncodingRule encodingRule) { - - this(); - set(filename, inputStream); - setEncodingRule(encodingRule); - } - - private EncodingMemento checkForEncodingInContents(InputStream limitedStream) throws CoreException, IOException { - EncodingMemento result = null; - - // if encoding memento already set, then iFile must - // have been set, and no need to get again. - if (fEncodingMemento != null) { - result = fEncodingMemento; - } - else { - if (fClientSuppliedStream) { - try { - limitedStream.reset(); - IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); - IContentDescription contentDescription = contentTypeManager.getDescriptionFor(limitedStream, fFilename, IContentDescription.ALL); - if (contentDescription != null) { - fEncodingMemento = createMemento(contentDescription); - } - result = fEncodingMemento; - } - finally { - limitedStream.reset(); - } - } - else { - // throw new IllegalStateException("unexpected state: - // encodingMemento was null but no input stream supplied by - // client"); //$NON-NLS-1$ - result = null; - } - } - - if (result != null && !result.isValid() && !forceDefault()) { - throw new UnsupportedCharsetExceptionWithDetail(result); - } - - return result; - } - - /** - * @param resettableLimitedStream - */ - private EncodingMemento checkStreamForBOM(InputStream resettableLimitedStream) { - EncodingMemento result = null; - UnicodeBOMEncodingDetector unicodeBOMEncodingDetector = new UnicodeBOMEncodingDetector(); - unicodeBOMEncodingDetector.set(resettableLimitedStream); - result = unicodeBOMEncodingDetector.getEncodingMemento(); - return result; - } - - /** - * @param iFile - * @throws CoreException - * @throws IOException - */ - private EncodingMemento findMementoFromFileCase() throws CoreException, IOException { - EncodingMemento result = null; - IContentDescription contentDescription = null; - try { - // This method provides possible improved performance at the - // cost of sometimes returning null - if (fIFile.exists()) - contentDescription = fIFile.getContentDescription(); - } - catch (CoreException e) { - // Assume if core exception occurs, we can still try more - // expensive - // discovery options. - Logger.logException(e); - } - if (contentDescription == null && fIFile.isAccessible()) { - InputStream contents = null; - try { - contents = fIFile.getContents(); - contentDescription = Platform.getContentTypeManager().getDescriptionFor(contents, fIFile.getName(), IContentDescription.ALL); - } - catch (CoreException e1) { - // Assume if core exception occurs, we can't really do much - // with - // determining encoding, etc. - Logger.logException(e1); - throw e1; - } - catch (IOException e2) { - // We likely couldn't get the contents of the file, something - // is really wrong - Logger.logException(e2); - throw e2; - } - if (contents != null) { - try { - contents.close(); - } - catch (IOException e2) { - Logger.logException(e2); - } - } - } - if (contentDescription != null) { - result = createMemento(contentDescription); - } - - return result; - } - - /** - * The primary method which contains the highest level rules for how to - * decide appropriate decoding rules: 1. first check for unicode stream 2. - * then looked for encoding specified in content (according to the type of - * content that is it ... xml, html, jsp, etc. 3. then check for various - * settings: file settings first, if null check project settings, if null, - * check user preferences. 4. lastly (or, what is the last user - * preference) is to use "workbench defaults". - * - * @throws IOException - * @throws CoreException - */ - private EncodingMemento findMementoFromStreamCase() throws CoreException, IOException { - - EncodingMemento result = null; - InputStream resettableLimitedStream = null; - try { - resettableLimitedStream = getLimitedStream(getResettableStream()); - if (resettableLimitedStream != null) { - // first check for unicode stream - result = checkStreamForBOM(resettableLimitedStream); - // if not that, then check contents - if (result == null) { - resettableLimitedStream.reset(); - result = checkForEncodingInContents(resettableLimitedStream); - } - - } - else { - // stream null, may name's not. - if (fFilename != null) { - // filename not null - IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); - IContentDescription contentDescription = contentTypeManager.getDescriptionFor(new NullInputStream(), fFilename, IContentDescription.ALL); - if (contentDescription != null) { - result = createMemento(contentDescription); - } - } - } - } - finally { - if (resettableLimitedStream != null) { - handleStreamClose(resettableLimitedStream); - } - } - return result; - } - - private boolean forceDefault() { - - boolean result = false; - if (fEncodingRule != null && fEncodingRule == EncodingRule.FORCE_DEFAULT) - result = true; - return result; - } - - public Reader getCodedReader() throws CoreException, IOException { - - Reader result = null; - // we make a local copy of encoding memento so - // stream won't - // be accessed simultaneously. - EncodingMemento encodingMemento = getEncodingMemento(); - Assert.isNotNull(encodingMemento, "Appears reader requested before file or stream set"); //$NON-NLS-1$ - InputStream streamToReturn = getResettableStream(); - streamToReturn.reset(); - // if UTF 3 byte BOM is used (or UTF-16LE), the - // built in converters - // don't - // correct skip all three bytes ... so skip - // remaining one to leave - // stream transparently ready for client. - // see ... TODO look up bug number - if (encodingMemento.isUnicodeStream()) { - streamToReturn.skip(2); - } - else if (encodingMemento.isUTF83ByteBOMUsed()) { - streamToReturn.skip(3); - } - String charsetName = encodingMemento.getJavaCharsetName(); - if (charsetName == null) { - charsetName = encodingMemento.getDetectedCharsetName(); - } - if (!encodingMemento.isValid() && !forceDefault()) { - throw new UnsupportedCharsetExceptionWithDetail(encodingMemento); - } - - if (fEncodingRule == EncodingRule.FORCE_DEFAULT) { - charsetName = encodingMemento.getAppropriateDefault(); - } - Charset charset = Charset.forName(charsetName); - CharsetDecoder charsetDecoder = charset.newDecoder(); - if (fEncodingRule == EncodingRule.IGNORE_CONVERSION_ERROR) { - charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE); - charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE); - } - else { - charsetDecoder.onMalformedInput(CodingErrorAction.REPORT); - charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPORT); - } - // more efficient to be buffered, and I know of no - // reason not to return - // that directly. - result = new BufferedReader(new InputStreamReader(streamToReturn, charsetDecoder), CodedIO.MAX_BUF_SIZE); - result.mark(CodedIO.MAX_BUF_SIZE); - return result; - } - - public EncodingMemento getEncodingMemento() throws CoreException, IOException { - // figure out encoding memento from encoding strategy - if (fEncodingMemento == null) { - if (fClientSuppliedStream) { - fEncodingMemento = findMementoFromStreamCase(); - } - else if (fIFile != null) { - fEncodingMemento = findMementoFromFileCase(); - } - } - - // if encoding stratagy doesn't provide answer, - // then try file settings, project settings, - // user preferences, and - // finally workbench default. - // - if (fEncodingMemento == null || fEncodingMemento.getDetectedCharsetName() == null) { - fEncodingMemento = getEncodingMementoFromResourceAndPreference(); - } - - // use DefaultNameRules from NonContentBasedEncodingRules as the final - // default - if (fEncodingMemento == null) { - fEncodingMemento = handleNotProvidedFromContentCase(); - } - - return fEncodingMemento; - } - - /* - * This method is called only when encoding is not detected in the file. - * - * Here is encoding lookup order we will try: - try resource content - * description (Eclipse Text file encoding) - try resource content - * properties (for JSP only) - try content type encoding preferences (for - * HTML only) - try resource content description (Eclipse Text file - * encoding, implicit check) - * - * Note: This method appears in both CodedReaderCreator and - * CodedStreamCreator (with just a minor difference). They should be kept - * the same. - */ - private EncodingMemento getEncodingMementoFromResourceAndPreference() throws IOException, CoreException { - EncodingMemento encodingMemento = fEncodingMemento; - - // Follow Eclipse Platform's direction. Get the charset from IFile. - if (fIFile != null) { - String charset = fIFile.getCharset(); - encodingMemento = CodedIO.createEncodingMemento(charset); - } - - return encodingMemento; - } - - /** - * Ensures that an InputStream has mark/reset support, is readlimit is - * set, and that the stream is "limitable" (that is, reports "end of - * input" rather than allow going past mark). This is very specialized - * stream introduced to overcome - * https://bugs.eclipse.org/bugs/show_bug.cgi?id=67211. See also - * https://bugs.eclipse.org/bugs/show_bug.cgi?id=68565 - */ - private InputStream getLimitedStream(InputStream original) { - if (original == null) - return null; - if (original instanceof BufferedLimitedStream) - return original; - InputStream s = new BufferedLimitedStream(original, CodedIO.MAX_MARK_SIZE); - s.mark(CodedIO.MAX_MARK_SIZE); - return s; - } - - private InputStream getResettableStream() throws CoreException, IOException { - - InputStream resettableStream = null; - - if (fIFile != null) { - InputStream inputStream = null; - try { - // note we always get contents, even if out of synch - inputStream = fIFile.getContents(true); - } - catch (CoreException e) { - // SHOULD actually check for existence of - // fIStorage, but - // for now will just assume core exception - // means it - // doesn't exist on file system, yet. - // and we'll log, just in case its a noteable error - Logger.logException(e); - inputStream = new NullInputStream(); - } - resettableStream = new BufferedInputStream(inputStream, CodedIO.MAX_BUF_SIZE); - } - else { - if (fInputStream != null) { - if (fInputStream.markSupported()) { - resettableStream = fInputStream; - // try { - resettableStream.reset(); - // } - // catch (IOException e) { - // // assumed just hasn't been marked yet, so ignore - // } - } - else { - resettableStream = new BufferedInputStream(fInputStream, CodedIO.MAX_BUF_SIZE); - } - } - } - - if (resettableStream == null) { - resettableStream = new NullInputStream(); - } - - // mark this once, stream at "zero" position - resettableStream.mark(MAX_MARK_SIZE); - return resettableStream; - } - - private EncodingMemento handleNotProvidedFromContentCase() { - - EncodingMemento result = null; - String specDefault = null; - // try { - // specDefault = getEncodingDetector().getSpecDefaultEncoding(); - // } - // catch (CoreException e) { - // // If this exception occurs, assumes there is - // // no specDefault - // } - // catch (IOException e) { - // // If this exception occurs, assumes there is - // // no specDefault - // } - // finally { - // try { - // handleStreamClose(fEncodingDetectorStream); - // } - // catch (IOException e1) { - // // severe error, not much to do here - // } - // } - // this logic should be moved to 'detection' if not already - String charset = NonContentBasedEncodingRules.useDefaultNameRules(specDefault); - Assert.isNotNull(charset, "post condition failed"); //$NON-NLS-1$ - result = CodedIO.createEncodingMemento(charset); - return result; - } - - /** - * @param resettableInputStream - * @throws IOException - */ - private void handleStreamClose(InputStream resettableInputStream) throws IOException { - - if (resettableInputStream != null) { - if (fClientSuppliedStream) { - resettableInputStream.reset(); - } - else { - - resettableInputStream.close(); - } - } - } - - // TODO We just copy the content properties encoding to current resource's - // encoding for now. May improve the UI later by setting an informational - // message and/or disable the content properties encoding field. - // TODO: remake private else remove - void migrateContentPropertiesEncoding(String encoding) throws CoreException { - final IFile file = fIFile; - final String charset = encoding; - // TODO: externalize string later - Job migrater = new Job(SSECoreMessages.Migrate_Charset) { //$NON-NLS-1$ - protected IStatus run(IProgressMonitor monitor) { - if (file != null) { - try { - file.setCharset(charset, null); - } - catch (CoreException e) { - Logger.logException(e); - } - } - return Status.OK_STATUS; - } - }; - migrater.setSystem(true); - migrater.schedule(); - } - - private void resetAll() { - - fEncodingRule = null; - fIFile = null; - fFilename = null; - fInputStream = null; - fEncodingMemento = null; - fClientSuppliedStream = false; - } - - public void set(IFile iFile) throws CoreException, IOException { - Assert.isNotNull(iFile, "illegal argument"); //$NON-NLS-1$ - resetAll(); - fIFile = iFile; - } - - public void set(String filename, InputStream inputStream) { - - resetAll(); - fFilename = filename; - fInputStream = inputStream; - fClientSuppliedStream = true; - } - - public void setEncodingRule(EncodingRule encodingRule) { - - fEncodingRule = encodingRule; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedStreamCreator.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedStreamCreator.java deleted file mode 100644 index 07d319fcfc..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CodedStreamCreator.java +++ /dev/null @@ -1,509 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.CharArrayReader; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.StringReader; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; -import java.nio.charset.CodingErrorAction; -import java.nio.charset.UnmappableCharacterException; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.content.IContentDescription; -import org.eclipse.core.runtime.content.IContentTypeManager; -import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.wst.sse.core.internal.SSECoreMessages; -import org.eclipse.wst.sse.core.internal.SSECorePlugin; -import org.eclipse.wst.sse.core.internal.encoding.util.Assert; -import org.eclipse.wst.sse.core.internal.encoding.util.Logger; -import org.eclipse.wst.sse.core.internal.exceptions.CharConversionErrorWithDetail; -import org.eclipse.wst.sse.core.internal.exceptions.MalformedOutputExceptionWithDetail; -import org.eclipse.wst.sse.core.internal.exceptions.UnsupportedCharsetExceptionWithDetail; - - -public class CodedStreamCreator extends CodedIO { - - private final static int INITIAL_BUFFER_SIZE = 1024 * 16; - - // the 32 bytes used by default by ByteOutputStream is - // a little small - private static final String PROGRAM_ERROR__FAILED_TO_FIND_ANY_CHARSET_ANYWHERE_ = "Program error: failed to find any charset anywhere!"; //$NON-NLS-1$ - - private static final String UTF_16BE_CHARSET_NAME = "UTF-16BE"; //$NON-NLS-1$ - private static final String UTF_16LE_CHARSET_NAME = "UTF-16LE"; //$NON-NLS-1$ - // private static final String UTF_16_CHARSET_NAME = "UTF-16"; - // //$NON-NLS-1$ - - private static final String UTF_8_CHARSET_NAME = "UTF-8"; //$NON-NLS-1$ - - private boolean fClientSuppliedReader; - - // future_TODO: this 'checkConversion' can be a little - // pricey for large - // files, chould be a user preference, or something. - // private static final boolean checkConversion = true; - private EncodingMemento fCurrentEncodingMemento; - - private EncodingMemento fEncodingMemento; - - private String fFilename; - - private boolean fHasBeenAnalyzed; - - private IFile fIFile; - - private EncodingMemento fPreviousEncodingMemento; - - private Reader fReader; - - private Reader fResettableReader; - private byte[] UTF16BEBOM = new byte[]{(byte) 0xFE, (byte) 0xFF}; - - private byte[] UTF16LEBOM = new byte[]{(byte) 0xFF, (byte) 0xFE}; - private byte[] UTF3BYTEBOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; - - public CodedStreamCreator() { - super(); - } - - public CodedStreamCreator(String filename, char[] characterArray) { - super(); - fFilename = filename; - fReader = new CharArrayReader(characterArray); - } - - public CodedStreamCreator(String filename, Reader reader) { - super(); - fFilename = filename; - fReader = reader; - } - - public CodedStreamCreator(String filename, String textString) { - super(); - fFilename = filename; - fReader = new StringReader(textString); - } - - /** - * The primary method which contains the highest level rules for how to - * decide appropriate decoding rules: 1. first check for unicode stream 2. - * then looked for encoding specified in content (according to the type of - * content that is it ... xml, html, jsp, etc. 3. then check for various - * settings: file settings first, if null check project settings, if null, - * check user preferences. 4. lastly (or, what is the last user - * preference) is to use "workbench defaults". - */ - private void analyze() throws CoreException, IOException { - Reader resettableReader = getResettableReader(); - try { - if (fCurrentEncodingMemento == null) { - resettableReader.reset(); - fCurrentEncodingMemento = checkForEncodingInContents(); - } - // if encoding stratagy doesn't provide answer, - // then try file settings, project settings, - // user preferences, and - // finally workbench default. - // - if (fCurrentEncodingMemento == null || fCurrentEncodingMemento.getDetectedCharsetName() == null) { - resettableReader.reset(); - fCurrentEncodingMemento = getEncodingMementoFromResourceAndPreference(); - } - - // use DefaultNameRules from NonContentBasedEncodingRules as the - // final default - if (fEncodingMemento == null) { - handleNotProvidedFromContentCase(); - } - - fHasBeenAnalyzed = true; - } finally { - if (resettableReader != null) { - resettableReader.reset(); - } - } - } - - /** - * Need to check conversion early on. There's some danger than old - * contents of a file are set to empty, if an exception occurs. - * - * @param allText - * @param encoding - * @param encodingRule - * @throws java.io.UnsupportedEncodingException - * @throws MalformedOutputExceptionWithDetail - * @deprecated - we need to find "cheaper" way to to this functionality so - * likely to go away in future - */ - private void checkConversion(EncodingMemento memento, EncodingRule encodingRule) throws IOException { - String javaEncoding = memento.getJavaCharsetName(); - String detectedEncoding = memento.getDetectedCharsetName(); - Charset charset = Charset.forName(javaEncoding); - CharsetEncoder charsetEncoder = charset.newEncoder(); - charsetEncoder.onMalformedInput(CodingErrorAction.REPORT); - charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPORT); - Reader reader = getResettableReader(); - reader.reset(); - int currentChar = reader.read(); - int currentPos = 1; - try { - while (currentChar != -1) { - // note: this can probably be made more - // efficient later to - // check buffer by buffer, instead of - // character by character. - try { - boolean canConvert = charsetEncoder.canEncode((char) currentChar); - if (!canConvert) { - if (encodingRule == EncodingRule.IGNORE_CONVERSION_ERROR) { - // if we're told to ignore the - // encoding conversion - // error, - // notice we still want to detect - // and log it. We simply - // don't throw the exception, and - // we do continue with - // the - // save. - Logger.log(Logger.ERROR, "Encoding Conversion Error during save"); //$NON-NLS-1$ - } else { - throw new MalformedOutputExceptionWithDetail(javaEncoding, detectedEncoding, currentPos); - } - } - currentChar = reader.read(); - currentPos++; - } - // IBM's JRE seems to throw NPE when DBCS char is given to - // SBCS charsetEncoder - catch (NullPointerException e) { - throw new CharConversionErrorWithDetail(javaEncoding); //$NON-NLS-1$ - } - } - // if we get all the way through loop without throwing exception, - // then there must - // be an error not detectable when going character by character. - throw new CharConversionErrorWithDetail(javaEncoding); //$NON-NLS-1$ - } finally { - reader.reset(); - } - } - - private EncodingMemento checkForEncodingInContents() throws CoreException, IOException { - EncodingMemento result = null; - - // if encoding memento already set, and no need to get again. - if (fEncodingMemento != null) { - result = fEncodingMemento; - } else { - if (fClientSuppliedReader) { - fReader.reset(); - IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); - try { - IContentDescription contentDescription = contentTypeManager.getDescriptionFor(fReader, fFilename, IContentDescription.ALL); - if (contentDescription != null) { - fEncodingMemento = createMemento(contentDescription); - } else { - fEncodingMemento = CodedIO.createEncodingMemento("UTF-8"); //$NON-NLS-1$ - } - } catch (NullPointerException e) { - // TODO: work around for 5/14 bug in base, should be - // removed when move up to 5/21 - // just created a simple default one - fEncodingMemento = CodedIO.createEncodingMemento("UTF-8"); //$NON-NLS-1$ - } - result = fEncodingMemento; - } else { - throw new IllegalStateException("unexpected state: encodingMemento was null but no input stream supplied"); //$NON-NLS-1$ - } - } - // try { - // result = getEncodingDetector().getEncodingMemento(); - // if (result != null && !result.isValid() && !forceDefault()) { - // throw new UnsupportedCharsetExceptionWithDetail(result); - // } - // } - // finally { - // handleStreamClose(fEncodingDetectorStream); - // } - return result; - } - - - private void dump(OutputStream outputStream, EncodingRule encodingRule, boolean use3ByteBOMifUTF8) throws CoreException, IOException { - getCurrentEncodingMemento(); - String javaEncodingName = null; - if (encodingRule == EncodingRule.CONTENT_BASED) { - if (fCurrentEncodingMemento.isValid()) { - javaEncodingName = fCurrentEncodingMemento.getJavaCharsetName(); - } else { - throw new UnsupportedCharsetExceptionWithDetail(fCurrentEncodingMemento); - } - } else if (encodingRule == EncodingRule.IGNORE_CONVERSION_ERROR) - javaEncodingName = fCurrentEncodingMemento.getJavaCharsetName(); - else if (encodingRule == EncodingRule.FORCE_DEFAULT) - javaEncodingName = fCurrentEncodingMemento.getAppropriateDefault(); - // write appropriate "header" unicode BOM bytes - // Note: Java seems to write appropriate header for - // UTF-16, but not - // UTF-8 nor UTF-16BE. This - // may vary by JRE version, so need to test well. - // Note: javaEncodingName can be null in invalid - // cases, so we no hard - // to skip whole check if that's the case. - if (javaEncodingName != null) { - if ((javaEncodingName.equals(UTF_8_CHARSET_NAME) && use3ByteBOMifUTF8) || (javaEncodingName.equals(UTF_8_CHARSET_NAME) && fCurrentEncodingMemento.isUTF83ByteBOMUsed())) { - outputStream.write(UTF3BYTEBOM); - } else if (javaEncodingName.equals(UTF_16LE_CHARSET_NAME)) { - outputStream.write(UTF16LEBOM); - } else if (javaEncodingName.equals(UTF_16BE_CHARSET_NAME)) { - outputStream.write(UTF16BEBOM); - } - } - // TODO add back in line delimiter handling the - // "right" way (updating - // markers, not requiring string, etc. .. may need - // to move to document - // level) - //allTextBuffer = - // handleLineDelimiter(allTextBuffer, document); - Reader reader = getResettableReader(); - // be sure to test large "readers" ... we'll need - // to make sure they all - // can reset to initial position (StringReader, - // CharArrayReader, and - // DocumentReader should all work ok). - reader.reset(); - // There must be cleaner logic somehow, but the - // idea is that - // javaEncodingName can be null - // if original detected encoding is not valid (and - // if FORCE_DEFAULT was - // not specified). Hence, we WANT the first - // Charset.forName to - // throw appropriate exception. - Charset charset = null; - - // this call checks "override" properties file - javaEncodingName = CodedIO.getAppropriateJavaCharset(javaEncodingName); - - if (javaEncodingName == null) { - charset = Charset.forName(fCurrentEncodingMemento.getDetectedCharsetName()); - } else { - charset = Charset.forName(javaEncodingName); - } - CharsetEncoder charsetEncoder = charset.newEncoder(); - if (!(encodingRule == EncodingRule.IGNORE_CONVERSION_ERROR)) { - charsetEncoder.onMalformedInput(CodingErrorAction.REPORT); - charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPORT); - } else { - charsetEncoder.onMalformedInput(CodingErrorAction.REPLACE); - charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE); - - } - OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charsetEncoder); - //TODO: this may no longer be needed (and is at - // least wrong spot for - // it). - // if (checkConversion && (!(encodingRule == - // EncodingRule.IGNORE_CONVERSION_ERROR))) { - // checkConversion(fCurrentEncodingMemento, - // encodingRule); - // } - char[] charbuf = new char[CodedIO.MAX_BUF_SIZE]; - int nRead = 0; - try { - while (nRead != -1) { - nRead = reader.read(charbuf, 0, MAX_BUF_SIZE); - if (nRead > 0) { - outputStreamWriter.flush(); - outputStreamWriter.write(charbuf, 0, nRead); - } - } - } catch (UnmappableCharacterException e) { - checkConversion(fCurrentEncodingMemento, encodingRule); - } finally { - // since we don't own the original output stream, we - // won't close it ours. - // the caller who passed it to us must close original one - // when appropriate. - // (but we do flush to be sure all up-to-date) - outputStreamWriter.flush(); - } - } - - private boolean get3ByteBOMPreference() { - return SSECorePlugin.getDefault().getPluginPreferences().getBoolean(CommonEncodingPreferenceNames.USE_3BYTE_BOM_WITH_UTF8); - } - - public ByteArrayOutputStream getCodedByteArrayOutputStream() throws CoreException, IOException { - return getCodedByteArrayOutputStream(EncodingRule.CONTENT_BASED); - } - - public ByteArrayOutputStream getCodedByteArrayOutputStream(EncodingRule encodingRule) throws CoreException, IOException { - //Assert.isNotNull(fPreviousEncodingMemento, - // "previousEncodingMemento - // needs to be set first"); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE); - dump(byteArrayOutputStream, encodingRule, get3ByteBOMPreference()); - return byteArrayOutputStream; - } - - public EncodingMemento getCurrentEncodingMemento() throws CoreException, IOException { - //Assert.isNotNull(fPreviousEncodingMemento, - // "previousEncodingMemento - // needs to be set first"); - if (!fHasBeenAnalyzed) { - analyze(); - } - // post condition - Assert.isNotNull(fCurrentEncodingMemento, "illegal post condition state"); //$NON-NLS-1$ - // be sure to carry over appropriate encoding - // "state" that may be - // relevent. - if (fPreviousEncodingMemento != null) { - fCurrentEncodingMemento.setUTF83ByteBOMUsed(fPreviousEncodingMemento.isUTF83ByteBOMUsed()); - } - return fCurrentEncodingMemento; - } - - /* - * This method is called only when encoding is not detected in the file. - * - * Here is encoding lookup order we will try: - try resource content - * description (Eclipse Text file encoding) - try resource content - * properties (for JSP only) - try content type encoding preferences (for - * HTML only) - try resource content description (Eclipse Text file - * encoding, implicit check) - * - * Note: This method appears in both CodedReaderCreator and - * CodedStreamCreator (with just a minor difference). They should be kept - * the same. - */ - private EncodingMemento getEncodingMementoFromResourceAndPreference() throws IOException, CoreException { - EncodingMemento encodingMemento = fEncodingMemento; - - // Follow Eclipse Platform's direction. Get the charset from IFile. - if (fIFile != null) { - String charset = fIFile.getCharset(); - encodingMemento = CodedIO.createEncodingMemento(charset); - } - - return encodingMemento; - } - - private Reader getResettableReader() { - if (fResettableReader == null) { - if (fReader.markSupported()) { - fResettableReader = fReader; - } else { - fResettableReader = new BufferedReader(fReader); - try { - fResettableReader.mark(MAX_MARK_SIZE); - } catch (IOException e) { - // impossible, since we just checked if - // markable - throw new Error(e); - } - - } - } - return fResettableReader; - } - - protected void handleNotProvidedFromContentCase() { - // move to "detectors" if not already - String specDefault = null; - //specDefault = getEncodingDetector().getSpecDefaultEncoding(); - String charset = NonContentBasedEncodingRules.useDefaultNameRules(specDefault); - Assert.isNotNull(charset, PROGRAM_ERROR__FAILED_TO_FIND_ANY_CHARSET_ANYWHERE_); - fCurrentEncodingMemento = CodedIO.createEncodingMemento(charset); - } - - // TODO We just copy the content properties encoding to current resource's - // encoding for now. May improve the UI later by setting an informational - // message and/or disable the content properties encoding field. - // TODO make priviate if needed, else remove - void migrateContentPropertiesEncoding(String encoding) throws CoreException { - if (fIFile != null) - fIFile.setCharset(encoding, null); - final IFile file = fIFile; - final String charset = encoding; - // TODO: externalize string later - Job migrater = new Job(SSECoreMessages.Migrate_Charset) { //$NON-NLS-1$ - protected IStatus run(IProgressMonitor monitor) { - if (file != null) { - try { - file.setCharset(charset, null); - } catch (CoreException e) { - Logger.logException(e); - } - } - return Status.OK_STATUS; - } - }; - migrater.setSystem(true); - migrater.schedule(); - - } - - /** - * - */ - private void resetAll() { - fFilename = null; - fReader = null; - fPreviousEncodingMemento = null; - fCurrentEncodingMemento = null; - fHasBeenAnalyzed = false; - fClientSuppliedReader = false; - } - - public void set(IFile file, Reader reader) { - fIFile = file; - set(file.getName(), reader); - } - - public void set(String filename, char[] characterArray) { - resetAll(); - fFilename = filename; - fReader = new CharArrayReader(characterArray); - } - - public void set(String filename, Reader reader) { - resetAll(); - fFilename = filename; - fReader = reader; - fClientSuppliedReader = true; - } - - public void set(String filename, String textString) { - set(filename, new StringReader(textString)); - } - - public void setPreviousEncodingMemento(EncodingMemento previousEncodingMemento) { - fPreviousEncodingMemento = previousEncodingMemento; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CommonCharsetNames.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CommonCharsetNames.java deleted file mode 100644 index 10e08f95cc..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CommonCharsetNames.java +++ /dev/null @@ -1,258 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; -import java.nio.charset.UnsupportedCharsetException; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.Properties; -import java.util.PropertyResourceBundle; -import java.util.ResourceBundle; - -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Platform; -import org.eclipse.wst.sse.core.internal.encoding.util.Logger; -import org.osgi.framework.Bundle; - -/** - * CommonCharsets is a utility class to provide a central place to map some - * IANA charset name to a Java charset name. In 1.4 JRE's this mostly is a - * thin wrapper of existing Charset functionality. It does, however, allow - * some "overriding" of the built in JRE mappings in the event they are - * necessary. See CodedIO.checkMappingOverrides. - * <p> - * This class also provides some convenient human readable description for the - * charset names which can be used in user interfaces. The description is NL - * aware based on locale. The data is populated via the charset.properties - * file only once, based on static initialization of the hashtables. - * <p> - * The IANA tags are based on reference information found at the - * http://www.iana.org site. Specifically see - * http://www.iana.org/assignments/character-sets - */ -public final class CommonCharsetNames { - - private static Properties defaultIANAmappings = null; - - private static ArrayList encodings = null; - - private static Hashtable supportedEncodingDisplayNames = null; - - /** - * Returns list of commonly available encoding names. Suitable for - * populating a UI dialog or drop down. This list would be a subset of all - * possible charsets the VM supports (which can get into the hundreds). - * For the VM supported charsets, use - * <code>Charset.availableCharsets()</code> - * - * @return String[] - */ - public static String[] getCommonCharsetNames() { - String[] enc = new String[getEncodings().size()]; - getEncodings().toArray(enc); - return enc; - } - - /** - * @return Returns the defaultIANAmappings. - */ - private static Properties getDefaultIANAMappings() { - if (defaultIANAmappings == null) { - defaultIANAmappings = new Properties(); - Bundle keyBundle = Platform.getBundle(ICodedResourcePlugin.ID); - IPath keyPath = new Path("config/defaultIANA.properties"); //$NON-NLS-1$ - URL location = Platform.find(keyBundle, keyPath); - InputStream propertiesInputStream = null; - try { - propertiesInputStream = location.openStream(); - defaultIANAmappings.load(propertiesInputStream); - } - catch (IOException e) { - // if can't read, just assume there's no - // default IANA mappings - // and repeated attempts will not occur, - // since they - // will be represented by an empty - // Properties object - } - } - return defaultIANAmappings; - } - - /** - * Returns display (translated) string for encoding name. If there is no - * "custom" translated version available, it defers to ther VM's Charset - * support. It will return null if no display name is available. - * - * @param String - * charset name - * @return Human friendly display name - */ - public static String getDisplayString(String charsetName) { - if (charsetName == null) - return null; - String result = (String) getSupportedEncodingDisplayNames().get(charsetName); - if (result == null) { - // if we don't have a special one, just return - // what's provided by Charset - - try { - Charset charset = Charset.forName(charsetName); - result = charset.displayName(); - } - catch (UnsupportedCharsetException e) { - // if not supported, the display name is - // the least of clients concerns :) - } - } - return result; - } - - /** - * @return Returns the javaEncodings. - */ - private static ArrayList getEncodings() { - if (encodings == null) { - initHashTables(); - } - return encodings; - } - - public static String getIanaPreferredCharsetName(String charsetName) { - String preferredName = charsetName; - - try { - Charset charset = Charset.forName(charsetName); - if (charset.name() != null) { - preferredName = charset.name(); - } - } - catch (IllegalCharsetNameException e) { - // just return input if illegal - } - catch (UnsupportedCharsetException e) { - // just return input if illegal - } - return preferredName; - } - - /** - * Returns a default IANA name that is listed in CommonCharsetNames. Here - * is how it checks: 1. check to see if charsetName is in the - * CommonCharsetNames list and if so, just return it. 2. check to see if - * charsetName is listed in defaultIANAmappings which contains a mapping - * of more common encodings and the default IANA name they should map to. - * 3. return defaultIanaName if all else fails - */ - public static String getPreferredDefaultIanaName(String charsetName, String defaultIanaName) { - String preferredName = defaultIanaName; - String guessedName = charsetName; - try { - guessedName = CodedIO.getAppropriateJavaCharset(charsetName); - } - catch (IllegalCharsetNameException e) { - // just ignore if illegal - } - catch (UnsupportedCharsetException e) { - // just ignore if illegal - } - if (getEncodings().contains(guessedName)) - preferredName = guessedName; - else { - preferredName = getDefaultIANAMappings().getProperty(guessedName, preferredName); - } - - return preferredName; - } - - /** - * @return - */ - private static Hashtable getSupportedEncodingDisplayNames() { - if (supportedEncodingDisplayNames == null) { - initHashTables(); - } - return supportedEncodingDisplayNames; - } - - private static void initHashTables() { - if (supportedEncodingDisplayNames == null) { - // Initialize hash table for encoding table - supportedEncodingDisplayNames = new Hashtable(); - encodings = new ArrayList(); - - ResourceBundle bundle = null; - InputStream bundleStream = null; - try { - URL bundleURL = Platform.find(Platform.getBundle(ICodedResourcePlugin.ID), Path.fromOSString("$nl$/config/charset.properties")); //$NON-NLS-1$ - if (bundleURL != null) { - bundleStream = bundleURL.openStream(); - bundle = new PropertyResourceBundle(bundleStream); - } - - String totalNumString = bundle.getString("totalnumber");//$NON-NLS-1$ - int totalNum = 0; - if (totalNumString.length() != 0) { - try { - totalNum = Integer.valueOf(totalNumString).intValue(); - } - catch (NumberFormatException e) { - totalNum = 0; - } - } - - for (int i = 0; i < totalNum; i++) { - String iana = bundle.getString("codeset." + i + ".iana");//$NON-NLS-2$//$NON-NLS-1$ - String displayName = bundle.getString("codeset." + i + ".label");//$NON-NLS-2$//$NON-NLS-1$ - - encodings.add(iana); - supportedEncodingDisplayNames.put(iana, displayName); - } - } - catch (IOException e) { - Logger.logException("invalid install or configuration", e); //$NON-NLS-1$ - } - finally { - try { - if (bundleStream != null) - bundleStream.close(); - } - catch (IOException x) { - } - } - } - } - - public static void main(String[] args) { - // unit test only - String test = "Cp1252"; //$NON-NLS-1$ - String result = CommonCharsetNames.getIanaPreferredCharsetName(test); - System.out.println(test + " --> " + result); //$NON-NLS-1$ - - test = "MS932"; //$NON-NLS-1$ - result = CommonCharsetNames.getIanaPreferredCharsetName(test); - System.out.println(test + " --> " + result); //$NON-NLS-1$ - - } - - public CommonCharsetNames() { - super(); - initHashTables(); - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CommonEncodingPreferenceNames.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CommonEncodingPreferenceNames.java deleted file mode 100644 index 70354d505d..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/CommonEncodingPreferenceNames.java +++ /dev/null @@ -1,73 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -/** - * Common preference keys used to specify encoding and end of line. - */ -public class CommonEncodingPreferenceNames { - - private CommonEncodingPreferenceNames() { - // empty private constructor so users cannot instantiate class - } - - /** - * Constant to be used when referring to CR/MAC line delimiter - */ - public static final String CR = "EOL_Mac"; //$NON-NLS-1$ - /** - * Constant to be used when referring to CRLF/WINDOWS line delimiter - */ - public static final String CRLF = "EOL_Windows"; //$NON-NLS-1$ - /** - * The end-of-line character(s) to use. - */ - public static final String END_OF_LINE_CODE = "endOfLineCode";//$NON-NLS-1$ - /** - * The character code to use when reading a file. - */ - public static final String INPUT_CODESET = "inputCodeset";//$NON-NLS-1$ - - /** - * Constant to be used when referring to LF/UNIX line delimiter - */ - public static final String LF = "EOL_Unix"; //$NON-NLS-1$ - /** - * Constant to be used when referring to No translation of line delimiters - */ - public static final String NO_TRANSLATION = ""; //$NON-NLS-1$ - /** - * The character code to use when writing a file. - */ - public static final String OUTPUT_CODESET = "outputCodeset";//$NON-NLS-1$ - - /** - * String representation of CR/MAC line delimiter - */ - public static final String STRING_CR = "\r";//$NON-NLS-1$ - - /** - * String representation of CRLF/WINDOWS line delimiter - */ - public static final String STRING_CRLF = "\r\n";//$NON-NLS-1$ - - /** - * String representation of LF/UNIX line delimiter - */ - public static final String STRING_LF = "\n";//$NON-NLS-1$ - - /** - * String Use 3 byte BOM (Byte Order Mark) when saving UTF-8 encoded files - */ - public static final String USE_3BYTE_BOM_WITH_UTF8 = "Use3ByteBOMWithUTF8"; //$NON-NLS-1$ -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ContentBasedPreferenceGateway.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ContentBasedPreferenceGateway.java deleted file mode 100644 index 2d6ca30edf..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ContentBasedPreferenceGateway.java +++ /dev/null @@ -1,165 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Plugin; -import org.eclipse.core.runtime.content.IContentType; -import org.eclipse.core.runtime.preferences.DefaultScope; -import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.osgi.framework.Bundle; -import org.osgi.service.prefs.Preferences; - - -public class ContentBasedPreferenceGateway { - private static String DEFAULT_LOCATION = "org.eclipse.wst.sse.core"; //$NON-NLS-1$ - private static String RUNTIME_XML_ID = "org.eclipse.core.runtime.xml"; //$NON-NLS-1$ - private static String SSE_XML_ID = "org.eclipse.wst.xml.core.xmlsource"; //$NON-NLS-1$ - - /** - * @param pluginId - * @return - */ - private static boolean bundleExists(String pluginId) { - - // this just verifies there's really a plugin with this ID in "stack" - Bundle bundle = Platform.getBundle(pluginId); - return (!(bundle == null)); - } - - /** - * @param contentType - * @return - */ - private static String getContributorPluginId(IContentType contentType) { - // TODO: need to have registration info here, but for now, we'll use - // simple heuristic to cover the cases we know about. - String fullId = null; - if (contentType == null) { - fullId = DEFAULT_LOCATION; - } else { - fullId = contentType.getId(); - } - // only one known case, so far, of hard coded re-direction - // (not sure this is even needed, but just in case). - // We don't want to store/change runtime.xml preferences - if (RUNTIME_XML_ID.equals(fullId)) { - fullId = SSE_XML_ID; - } - String pluginId = inferPluginId(fullId); - return pluginId; - } - - private static Preferences getDefaultPreferences(IContentType contentType) { - IEclipsePreferences eclipsePreferences = Platform.getPreferencesService().getRootNode(); - // TODO: eventaully need extension mechanism to avoid these hard coded - // mechanism. - // The idea is to load/store based on plugin's preferences, where the - // content type was contributed - // Eventually, too, we could do more "dynamic lookup" to get parent - // types for defaults, etc. - - // Get default plugin preferences - String pluginPreferenceLocation = DefaultScope.SCOPE + IPath.SEPARATOR + getContributorPluginId(contentType); - Preferences pluginPreferences = eclipsePreferences.node(pluginPreferenceLocation); - - // the below code does not work at this time because content type - // preferences are stored in the place as plugin preferences - - // Preferences contentPreferences = null; - // if (contentType != null) { - // contentPreferences = pluginPreferences.node(contentType.getId()); - // } - // else { - // contentPreferences = pluginPreferences.node(DEFAULT_LOCATION ); - // } - // - // return contentPreferences; - - return pluginPreferences; - - } - - private static Preferences getDefaultPreferences(String contentTypeId) { - IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId); - return getDefaultPreferences(contentType); - } - - public static Preferences getPreferences(IContentType contentType) { - IEclipsePreferences eclipsePreferences = Platform.getPreferencesService().getRootNode(); - // TODO: eventaully need extension mechanism to avoid these hard coded - // mechanism. - // The idea is to load/store based on plugin's preferences, where the - // content type was contributed - // Eventually, too, we could do more "dynamic lookup" to get parent - // types for defaults, etc. - - // Get instance plugin preferences - String pluginPreferenceLocation = Plugin.PLUGIN_PREFERENCE_SCOPE + IPath.SEPARATOR + getContributorPluginId(contentType); - Preferences pluginPreferences = eclipsePreferences.node(pluginPreferenceLocation); - - // the below code does not work at this time because content type - // preferences are stored in the place as plugin preferences - - // Preferences contentPreferences = null; - // if (contentType != null) { - // contentPreferences = pluginPreferences.node(contentType.getId()); - // } - // else { - // contentPreferences = pluginPreferences.node(DEFAULT_LOCATION ); - // } - // - // return contentPreferences; - return pluginPreferences; - - } - - public static Preferences getPreferences(String contentTypeId) { - IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId); - return getPreferences(contentType); - } - - public static String getPreferencesString(IContentType contentType, String key) { - Preferences preferences = getPreferences(contentType); - String value = preferences.get(key, getDefaultPreferences(contentType).get(key, null)); - return value; - } - - public static String getPreferencesString(String contentTypeId, String key) { - Preferences preferences = getPreferences(contentTypeId); - String value = preferences.get(key, getDefaultPreferences(contentTypeId).get(key, null)); - return value; - } - - /** - * @param fullId - * @return - */ - private static String inferPluginId(String fullId) { - // simply trim off last "segment" from full ID. - int lastSegmentPos = fullId.lastIndexOf('.'); - String pluginId = null; - if (lastSegmentPos != -1) { - pluginId = fullId.substring(0, lastSegmentPos); - } else { - // weird case? We'll at least put/get them somewhere - pluginId = DEFAULT_LOCATION; - } - if (!bundleExists(pluginId)) { - // use default location - pluginId = DEFAULT_LOCATION; - } - return pluginId; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ContentTypeEncodingPreferences.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ContentTypeEncodingPreferences.java deleted file mode 100644 index 432ad24faf..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ContentTypeEncodingPreferences.java +++ /dev/null @@ -1,179 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.content.IContentType; -import org.eclipse.wst.sse.core.internal.encoding.util.Assert; - - -/** - * A convenience class to statically get preferenences. - */ - -public abstract class ContentTypeEncodingPreferences { - - // actually a null/empty string also means use workbench default so this - // constant might not really be necessary - public static final String WORKBENCH_DEFAULT = "WORKBENCH_DEFAULT"; //$NON-NLS-1$ - - - private static final String getJavaPlatformDefaultEncoding() { - // note: its important to use this system property, - // instead - // of - // ByteToCharConverter.getDefault().getCharacterEncoding() - // inorder to handle changes "on the fly". the - // ByteToCharConverter - // default is apparently set only when VM starts. - // There's not really any "cusomter scnererios" - // that change the - // default encoding "on the fly", but its at least - // used during - // our automated tests. - String enc = System.getProperty("file.encoding"); //$NON-NLS-1$ - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - return enc; - } - - - public static final String getPreferredNewLineDelimiter(String contentTypeId) { - String result = null; - String newLineCode = null; - newLineCode = ContentBasedPreferenceGateway.getPreferencesString(contentTypeId, CommonEncodingPreferenceNames.END_OF_LINE_CODE); - if (newLineCode == null) - result = null; - else if (newLineCode.equals(CommonEncodingPreferenceNames.CR)) - result = CommonEncodingPreferenceNames.STRING_CR; - else if (newLineCode.equals(CommonEncodingPreferenceNames.LF)) - result = CommonEncodingPreferenceNames.STRING_LF; - else if (newLineCode.equals(CommonEncodingPreferenceNames.CRLF)) - result = CommonEncodingPreferenceNames.STRING_CRLF; - return result; - } - - /** - * Returns current output encoding preference for contentTypeIdentifier - * (unique IANA encoding) - */ - public static final String getUserPreferredCharsetName(String contentTypeId) { - String prefEncoding = ContentBasedPreferenceGateway.getPreferencesString(contentTypeId, CommonEncodingPreferenceNames.OUTPUT_CODESET); - String encoding = prefEncoding; - // get workbench encoding preference if preference - // requests it - if (prefEncoding == null || prefEncoding.trim().length() == 0 || prefEncoding.equals(ContentTypeEncodingPreferences.WORKBENCH_DEFAULT)) { - encoding = ContentTypeEncodingPreferences.getWorkbenchPreferredCharsetName(); - } - return encoding; - } - - /** - * Utility method to get specified preference. Subclasses can't override, - * since we expect this to work in a consistent way. Note: this is - * specific to HTML and CSS and is intended to supply a "default spec" - * other than the workbench platform's default, only for those cases where - * there is no encoding specified anywhere else, e.g. in the file, or as a - * file or folder property. - */ - public static final String getUserSpecifiedDefaultEncodingPreference() { - String ContentTypeID_HTML = "org.eclipse.wst.html.core.htmlsource"; //$NON-NLS-1$ - return getUserSpecifiedDefaultEncodingPreference(ContentTypeID_HTML); - } - - public static final String getUserSpecifiedDefaultEncodingPreference(String contentTypeID) { - String enc = null; - - // first try to get base's default encoding for content type - IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeID); - if (contentType != null) { - enc = contentType.getDefaultCharset(); - } - - // next try to get sse's default encoding for content type - if (enc == null || enc.trim().length() == 0) { - enc = ContentBasedPreferenceGateway.getPreferencesString(contentTypeID, CommonEncodingPreferenceNames.INPUT_CODESET); - } - - // next, just try and use workbench encoding - if (enc == null || enc.trim().length() == 0) { - enc = getWorkbenchSpecifiedDefaultEncoding(); - } - - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - return enc; - } - - /** - * Returns Workbench encoding preference. Note: if workbench encoding is - * null, platform encoding will be returned. - */ - private static final String getWorkbenchPreferredCharsetName() { - String charset = ResourcesPlugin.getEncoding(); - charset = CommonCharsetNames.getIanaPreferredCharsetName(charset); - return charset; - } - - /** - * Returns Workbench encoding preference. Will return null if none - * specified. - */ - private static final String getWorkbenchSpecifiedDefaultEncoding() { - ResourcesPlugin resourcePlugin = ResourcesPlugin.getPlugin(); - String enc = resourcePlugin.getPluginPreferences().getString(ResourcesPlugin.PREF_ENCODING); - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - return enc; - } - - public static final String useDefaultNameRules(IResourceCharsetDetector encodingProvider) { - String result = null; - String enc = null; - enc = encodingProvider.getSpecDefaultEncoding(); - if (enc != null) { - result = enc; - } else { - enc = getUserSpecifiedDefaultEncodingPreference(); - if (enc != null && enc.trim().length() > 0) { - result = enc; - } else { - if (enc == null || enc.trim().length() == 0) { - enc = getWorkbenchSpecifiedDefaultEncoding(); - if (enc != null) { - result = enc; - } - } - if (enc == null || enc.trim().length() == 0) { - enc = getJavaPlatformDefaultEncoding(); - // enc should never be null (but we'll - // check anyway) - if (enc != null) { - result = enc; - } - } - } - } - Assert.isNotNull(enc, "post condition invalid"); //$NON-NLS-1$ - result = CodedIO.checkMappingOverrides(enc); - return result; - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/EncodingMemento.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/EncodingMemento.java deleted file mode 100644 index 853dfde27d..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/EncodingMemento.java +++ /dev/null @@ -1,249 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import org.eclipse.core.runtime.content.IContentDescription; - - -/** - * This class is to simply hold information and data about the type of - * encoding found for a resource. It not only includes names, etc., but also - * gives hints about the algorithm, or rule, that the encodng was determined. - * Having all this info in a central object, associated with the Document - * (technically, IStructuredDocument), allows for better user error messages, - * and better handling of knowing how to dump a file, given we know how it was - * loaded. - * - * Note: the data in this class is only valid if its has actually gone through - * the loading or dumping sequence. It is not accurate, for example, if a - * structuredDocument is simply created and then setText called. In this type - * of case, accuracy for loading and dumping is not required, since its all - * re-discovered. One limitation is that structuredDocument's created "from - * scratch" this way, don't have any encoding information to count on, and - * would have to arrange the processing to be done. (And it is done, - * automatically if going through loader or dumper, but perhaps not in future - * new uses. TODO: this can be inproved in future versions.) - * - * isInitialized is set when the loader or dumper processes have been used, - * but even this can't be counted on 100% if the document has been modified - * since. - * - */ -public class EncodingMemento implements Cloneable { - - public final static String CLONED = "cloned"; //$NON-NLS-1$ - public final static String DEFAULTS_ASSUMED_FOR_EMPTY_INPUT = "DefaultsAssumedForEmptyInput"; //$NON-NLS-1$ - public final static String DEFAULTS_USED_DUE_TO_SMALL_STREAM = "defaultsUsedDueToSmallStream"; //$NON-NLS-1$ - - - /* - * Strings to be used for tracing. TODO: need to clean this up, we no - * longer use all of them - */ - public final static String DETECTED_STANDARD_UNICODE_BYTES = "detectedStandardUnicodeBytes"; //$NON-NLS-1$ - public final static String FOUND_ENCODING_IN_CONTENT = "foundEncodingInContent"; //$NON-NLS-1$ - public final static String FOUND_ENCODING_IN_STREAM = "foundEncodingInStream"; //$NON-NLS-1$ - public final static String FOUND_ENCODING_IN_STRUCTURED_DOCUMENT = "foundEncodingInStructuredDocument"; //$NON-NLS-1$ - public final static String GUESSED_ENCODING_FROM_STREAM = "GuessEncodingFromStream"; //$NON-NLS-1$ - public final static String JAVA_NAME_FOUND_AS_IANA_NAME = "noMappingFoundButJavaNameFoundToBeIANAName"; //$NON-NLS-1$ - public final static String JAVA_NAME_FOUND_IN_ALIAS_NAME = "noMappingFoundButJavaNameFoundInAliasTable"; //$NON-NLS-1$ - public final static String NO_IANA_NAME_FOUND = "noMappingFoundFromJavaNameToIANAName"; //$NON-NLS-1$ - public final static String USED_CONTENT_TYPE_DEFAULT = "UsedContentTypeDefault"; //$NON-NLS-1$ - public final static String USED_JAVA_DEFAULT = "UsedJavaDefault"; //$NON-NLS-1$ - public final static String USED_MEMENTO_FROM_LOAD = "usedMementoFromLoad"; //$NON-NLS-1$ - public final static String USED_PROPERTY_SETTINGS = "USED_PROPERTY_SETTINGS"; //$NON-NLS-1$ - public final static String USED_USER_SPECIFIED_PREFERENCE = "UsedUserSpecifiedPreference"; //$NON-NLS-1$ - public final static String USED_WORKSPACE_DEFAULT = "UsedWorkspaceDefault"; //$NON-NLS-1$ - public final static String USER_IS_USING_JAVA_ENCODING = "UserIsUsingJavaEncoding"; //$NON-NLS-1$ - private String fAppropriateDefault; - private String fDetectedCharsetName; - private String fInvalidEncoding; - - - private String fJavaCharsetName; - private boolean fUnicodeStream; - private boolean fUTF83ByteBOMUsed; - - public EncodingMemento() { - super(); - } - - /** - * Returns a clone of this object. - */ - public Object clone() { - EncodingMemento object = null; - try { - object = (EncodingMemento) super.clone(); - } - catch (CloneNotSupportedException e) { - // impossible, since we're implementing here - } - - return object; - - } - - /** - * Returns the appropriateDefault. This is only set if an invalid encoding - * was found, and contains an charset appropriate to use as a default - * value, if, for example, the user decides to load the document anyway, - * even though the charset was found to be invalid. - * - * @return String - */ - public String getAppropriateDefault() { - if (fAppropriateDefault == null) { - fAppropriateDefault = NonContentBasedEncodingRules.useDefaultNameRules(null); - } - return fAppropriateDefault; - } - - /** - * Returns the charset name, if it is different from the charset name - * found in getJavaCharsetName. This can happen, for example, if there are - * differences in case. This method might return SHIFT_JIS, and the the - * getJavaCharsetName might return Shift_JIS -- if SHIFT_JIS was detected - * in file/document. If the original file contained the correct case, then - * this method would return null. The getJavaCharsetName is typically the - * one that should always be used, and this one only used for certain - * error conditions, or or if when creating a "duplicate" resource, it was - * desired to use exactly the charset name as in the original document. As - * an example of this later case, the original document might contain - * ISO-8859-9, but the detected charset name might contain ISO-8859-9-I. - * - * @return String - */ - public String getDetectedCharsetName() { - return fDetectedCharsetName; - } - - /** - * Returns a charset name that was detected, but not found to be a charset - * suppoorted by the VM. - * - * @return String - */ - public String getInvalidEncoding() { - return fInvalidEncoding; - } - - /** - * Returns the java cononical charset name. - * - * @return String - */ - public String getJavaCharsetName() { - return fJavaCharsetName; - } - - /** - * Note: we may be able to remove this method, if it turns out this work - * is done by "text" type. - * - * @deprecated - - */ - public byte[] getUnicodeBOM() { - byte[] bom = null; - if (isUTF83ByteBOMUsed()) - bom = IContentDescription.BOM_UTF_8; - else if (isUnicodeStream()) { - if (getJavaCharsetName().equals("UTF-16") || getJavaCharsetName().equals("UTF-16LE")) { //$NON-NLS-1$ //$NON-NLS-2$ - bom = IContentDescription.BOM_UTF_16LE; - } - else if (getJavaCharsetName().equals("UTF-16BE")) { //$NON-NLS-1$ - bom = IContentDescription.BOM_UTF_16BE; - } - - } - return bom; - } - - /** - * Note: in our implementation, the stream is a unicode stream if the - * charset is UTF-16, UTF-16LE, or UTF-16BE. A stream with 3 byte BOM is - * not considered unicode stream here. - * - * @return returns true if is a unicode (UTF-16) stream - */ - public boolean isUnicodeStream() { - return fUnicodeStream; - } - - /** - * Note: in our implementation, the stream is a unicode stream if the - * charset is UTF-16, UTF-16LE, or UTF-16BE. A stream with 3 byte BOM is - * not considered unicode stream here. - * - * Set during load, can be used by dumper to write 3 byte BOM, which Java - * does not normally do. This helps maintain compatibility with other - * programs (those that wrote the 3 byte BOM there to begin with. - * - * @return boolean - */ - public boolean isUTF83ByteBOMUsed() { - return fUTF83ByteBOMUsed; - } - - public boolean isValid() { - return getInvalidEncoding() == null; - } - - /** - * Sets the appropriateDefault. - * - * @param appropriateDefault - * The appropriateDefault to set - */ - public void setAppropriateDefault(String appropriateDefault) { - fAppropriateDefault = appropriateDefault; - } - - - public void setDetectedCharsetName(String detectedCharsetName) { - fDetectedCharsetName = detectedCharsetName; - } - - public void setInvalidEncoding(String invalidEncoding) { - fInvalidEncoding = invalidEncoding; - } - - /** - * Sets the javaEncodingName. - * - * @param javaEncodingName - * The javaEncodingName to set - */ - public void setJavaCharsetName(String javaCharsetName) { - fJavaCharsetName = javaCharsetName; - } - - /** - * @param b - */ - public void setUnicodeStream(boolean unicodeStream) { - fUnicodeStream = unicodeStream; - - } - - /** - * Sets the uTF83ByteBOMfound. - * - * @param uTF83ByteBOMfound - * The uTF83ByteBOMfound to set - */ - public void setUTF83ByteBOMUsed(boolean uTF83ByteBOMUsed) { - fUTF83ByteBOMUsed = uTF83ByteBOMUsed; - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/EncodingRule.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/EncodingRule.java deleted file mode 100644 index 53fbbe2d3d..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/EncodingRule.java +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - - -/** - * Class to provided "enumerated types" for encoding rule parameter. This is - * to be used by client to have some control over how encoding is determined. - */ -public class EncodingRule { - /** - * CONTENT_BASED means the class which uses the parameter (such as - * contentType Loaders) should use what ever rules it normally would. - * (Note, some content type loaders may not always literally use the file - * content to determine encoding, but the point is they should use what - * ever rules they normally would.) - */ - public static final EncodingRule CONTENT_BASED = new EncodingRule("CONTENT_BASED"); //$NON-NLS-1$ - /** - * FORCE_DEFAULT means the class which uses the parameter (such as - * contentType Loaders) should use what ever it defines as the default - * encoding. - */ - public static final EncodingRule FORCE_DEFAULT = new EncodingRule("FORCE_DEFAULT"); //$NON-NLS-1$ - - /** - * IGNORE_CONVERSION_ERROR means that the save operation should save even - * if it encounters conversion errors. This will result in some data loss, - * so should only be used after the user confirms that is indeed what they - * want to do. - */ - public static final EncodingRule IGNORE_CONVERSION_ERROR = new EncodingRule("IGNORE_CONVERSION_ERROR"); //$NON-NLS-1$ - - - private final String encodingRule; - - /** - * Constructor for EncodingRule is private, so no one can instantiate - * except this class itself. - */ - private EncodingRule(String ruleName) { - super(); - encodingRule = ruleName; - } - - public String toString() { - return encodingRule; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ICodedResourcePlugin.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ICodedResourcePlugin.java deleted file mode 100644 index 42b01de2de..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/ICodedResourcePlugin.java +++ /dev/null @@ -1,19 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - - -public interface ICodedResourcePlugin { - String ID = "org.eclipse.wst.sse.core"; //$NON-NLS-1$ - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IContentDescriptionExtended.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IContentDescriptionExtended.java deleted file mode 100644 index a778ad3ac9..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IContentDescriptionExtended.java +++ /dev/null @@ -1,42 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import org.eclipse.core.runtime.QualifiedName; - - -public interface IContentDescriptionExtended { - /** - * The APPROPRIATE_DEFAULT field is used only when the - * IContentType.getDefaultCharset returns null. Its typically set from - * user preferences. Known uses cases are HTML and CSS, where there is no - * "spec default" for those content types. - */ - public static final QualifiedName APPROPRIATE_DEFAULT = new QualifiedName(ICodedResourcePlugin.ID, "appropriateDefault"); //$NON-NLS-1$ - /** - * The DETECTED_CHARSET property should be set when the "detected" charset - * is different from the java charset, even though functionally - * equivelent. This can occur, for example, when the cases are different, - * or when an alias name is used instead of the conanical name. - */ - public final static QualifiedName DETECTED_CHARSET = new QualifiedName(ICodedResourcePlugin.ID, "detectedCharset"); //$NON-NLS-1$ - /** - * The UNSUPPORTED_CHARSET property holds the charset value, if its been - * found to be an unsuppoted charset. This is helpful in error messages, - * or in cases when even though the charset is invalid, the java charset - * is assumed to be the default. - */ - public final static QualifiedName UNSUPPORTED_CHARSET = new QualifiedName(ICodedResourcePlugin.ID, "unsupportedCharset"); //$NON-NLS-1$ - - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IResourceCharsetDetector.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IResourceCharsetDetector.java deleted file mode 100644 index 6995b517b7..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IResourceCharsetDetector.java +++ /dev/null @@ -1,21 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import org.eclipse.core.resources.IStorage; -import org.eclipse.core.runtime.CoreException; - -public interface IResourceCharsetDetector extends IStreamCharsetDetector { - void set(IStorage iStorage) throws CoreException; - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IStreamCharsetDetector.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IStreamCharsetDetector.java deleted file mode 100644 index f51aecccc3..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/IStreamCharsetDetector.java +++ /dev/null @@ -1,28 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; - -public interface IStreamCharsetDetector { - String getEncoding() throws IOException; - - String getSpecDefaultEncoding(); - - void set(InputStream inputStream); - - void set(Reader reader); - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/NonContentBasedEncodingRules.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/NonContentBasedEncodingRules.java deleted file mode 100644 index b7c9ae67ce..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/NonContentBasedEncodingRules.java +++ /dev/null @@ -1,139 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.content.IContentType; - -public class NonContentBasedEncodingRules { - - private static final String getJavaPlatformDefaultEncoding() { - String enc = System.getProperty("file.encoding"); //$NON-NLS-1$ - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - return enc; - } - - - public static String getUserSpecifiedDefaultForContentType(IFile iFile) { - String enc = null; - - IContentType contentType = null; - try { - contentType = iFile.getContentDescription().getContentType(); - - // first try to get base's default encoding for content type - if (contentType != null) { - enc = contentType.getDefaultCharset(); - } - - // next try to get sse's default encoding for content type - if (enc == null || enc.trim().length() == 0) { - enc = ContentBasedPreferenceGateway.getPreferencesString(contentType, CommonEncodingPreferenceNames.INPUT_CODESET); - } - - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - } catch (CoreException e) { - // if core exception occurs, assume no preference! - enc = null; - } - return enc; - } - - public static String getUserSpecifiedDefaultForContentType(String contentTypeId) { - String enc = null; - - // first try to get base's default encoding for content type - IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId); - if (contentType != null) { - enc = contentType.getDefaultCharset(); - } - - // next try to get sse's default encoding for content type - if (enc == null || enc.trim().length() == 0) { - enc = ContentBasedPreferenceGateway.getPreferencesString(contentTypeId, CommonEncodingPreferenceNames.INPUT_CODESET); - } - - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - return enc; - } - - private static final String getWorkbenchSpecifiedDefaultEncoding() { - ResourcesPlugin resourcePlugin = ResourcesPlugin.getPlugin(); - String enc = resourcePlugin.getPluginPreferences().getString(ResourcesPlugin.PREF_ENCODING); - // return blank as null - if (enc != null && enc.trim().length() == 0) { - enc = null; - } - return enc; - } - - /** - * @param specDefault - * This is the default charset name that would ordinarily be - * used for a particular type of content. Null may be - * specififed for those types with no spec default. If the spec - * default is known (and passed in), then it will be returned - * after being checked to see if there's be any user specified - * "override" for that charset (which would be rare). In other - * words, if the spec is known, there's little reason to use - * this method. - * @return the charset that should be used according to the rules - * established by this class. - */ - public static final String useDefaultNameRules(String specDefault) { - String enc = null; - String result = null; - enc = specDefault; - if (enc != null) { - result = enc; - } else { - enc = ContentTypeEncodingPreferences.getUserSpecifiedDefaultEncodingPreference(); - if (enc != null && enc.trim().length() > 0) { - result = enc.trim(); - } else { - if (enc == null || enc.trim().length() == 0) { - enc = getWorkbenchSpecifiedDefaultEncoding(); - if (enc != null) { - result = enc.trim(); - } - } - if (enc == null || enc.trim().length() == 0) { - enc = getJavaPlatformDefaultEncoding(); - // enc should never be null (but we'll - // check anyway) - if (enc != null) { - result = enc; - } - } - } - } - result = CodedIO.checkMappingOverrides(result); - return result; - } - - private NonContentBasedEncodingRules() { - super(); - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/Assert.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/Assert.java deleted file mode 100644 index 85f18dda40..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/Assert.java +++ /dev/null @@ -1,120 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -/** - * <code>Assert</code> is useful for for embedding runtime sanity checks in - * code. The predicate methods all test a condition and throw some type of - * unchecked exception if the condition does not hold. - * <p> - * Assertion failure exceptions, like most runtime exceptions, are thrown when - * something is misbehaving. Assertion failures are invariably unspecified - * behavior; consequently, clients should never rely on these being thrown - * (and certainly should not being catching them specifically). - * </p> - */ -public final class Assert { - - /** - * <code>AssertionFailedException</code> is a runtime exception thrown - * by some of the methods in <code>Assert</code>. - * <p> - * This class is not declared public to prevent some misuses; programs - * that catch or otherwise depend on assertion failures are susceptible to - * unexpected breakage when assertions in the code are added or removed. - * </p> - */ - static class AssertionFailedException extends RuntimeException { - /** - * Comment for <code>serialVersionUID</code> - */ - private static final long serialVersionUID = 1L; - - /** - * Constructs a new exception. - */ - public AssertionFailedException() { - super(); - } - - /** - * Constructs a new exception with the given message. - */ - public AssertionFailedException(String detail) { - super(detail); - } - } - - /** - * Asserts that an argument is legal. If the given boolean is not - * <code>true</code>, an <code>IllegalArgumentException</code> is - * thrown. The given message is included in that exception, to aid - * debugging. - * - * @param expression - * the outcode of the check - * @param message - * the message to include in the exception - * @return <code>true</code> if the check passes (does not return if the - * check fails) - * @exception IllegalArgumentException - * if the legality test failed - */ - public static boolean isLegal(boolean expression, String message) { - if (!expression) - throw new AssertionFailedException(message); - return expression; - } - - /** - * Asserts that the given object is not <code>null</code>. If this is - * not the case, some kind of unchecked exception is thrown. The given - * message is included in that exception, to aid debugging. - * - * @param object - * the value to test - * @param message - * the message to include in the exception - * @exception IllegalArgumentException - * if the object is <code>null</code> - */ - public static void isNotNull(Object object, String message) { - if (object == null) { - throw new AssertionFailedException(); - } - } - - /** - * Asserts that the given boolean is <code>true</code>. If this is not - * the case, some kind of unchecked exception is thrown. The given message - * is included in that exception, to aid debugging. - * - * @param expression - * the outcode of the check - * @param message - * the message to include in the exception - * @return <code>true</code> if the check passes (does not return if the - * check fails) - */ - public static boolean isTrue(boolean expression, String message) { - if (!expression) { - throw new AssertionFailedException(); - } - return expression; - } - - /* This class is not intended to be instantiated. */ - private Assert() { - super(); - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/BufferedLimitedReader.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/BufferedLimitedReader.java deleted file mode 100644 index f026bdab4b..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/BufferedLimitedReader.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.Reader; - -/** - * This is a pretty limited implementation, sort of specific - * to the way its used by tokenizers (JFlex). To really - * be general purpose, would need more work. - * - */ - - -public class BufferedLimitedReader extends BufferedReader { - private int limitedCount; - private int nRead; - - public BufferedLimitedReader(Reader reader, int size) { - super(reader, size); - if (reader.markSupported()) { - try { - mark(size); - } - catch (IOException e) { - // impossible - e.printStackTrace(); - } - } - limitedCount = size; - } - - public int read() throws IOException { - int result = 0; - nRead++; - if (nRead > limitedCount) { - result = -1; - } - else { - result = super.read(); - } - return result; - - } - - public int read(char cbuf[], int off, int len) throws IOException { - int result = 0; - if (nRead + len > limitedCount) { - result = -1; - } - else { - result = super.read(cbuf, off, len); - nRead = nRead + result; - } - return result; - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/BufferedLimitedStream.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/BufferedLimitedStream.java deleted file mode 100644 index 7e20cb9357..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/BufferedLimitedStream.java +++ /dev/null @@ -1,79 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; - -public class BufferedLimitedStream extends BufferedInputStream { - - private int limitedCount; - - public BufferedLimitedStream(InputStream inStream, int size) { - super(inStream, size); - mark(size); - try { - limitedCount = Math.min(size, inStream.available()); - } catch (IOException e) { - // unlikely - limitedCount = 0; - } - } - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#available() - */ - public synchronized int available() throws IOException { - - return limitedCount - pos; - } - - /** - * copied down from super class - */ - private void ensureOpen() throws IOException { - if (in == null) - throw new IOException("Stream closed"); //$NON-NLS-1$ - } - - /** - * copied down from super class then, changed to simiulate EOF if goes - * beyond buffered amount - */ - public synchronized int read() throws IOException { - ensureOpen(); - // for this special stream, indicate "end of file" when buffer is - // full - if (pos >= limitedCount) { - return -1; - } - return super.read(); - } - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#read(byte[], int, int) - */ - public synchronized int read(byte[] b, int off, int len) throws IOException { - // for this special stream, indicate "end of file" when buffer is - // full - if (pos >= limitedCount) { - return -1; - } - return super.read(b, off, len); - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/ByteReader.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/ByteReader.java deleted file mode 100644 index e3dc503993..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/ByteReader.java +++ /dev/null @@ -1,109 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; - -import org.eclipse.wst.sse.core.internal.encoding.CodedIO; - -/** - * This is an "adapter" class, simply to get in input stream to act like a - * reader. We could not use InputStreamReader directly because its internal - * buffers are not controllable, and it sometimes pulls too much out of input - * stream (even when it wasn't needed for our purposes). - * - * The use of this class is highly specialized and by not means meant to be - * general purpose. Its use is restricted to those cases where the input - * stream can be regarded as ascii just long enough to determine what the real - * encoding should be. - */ - -public class ByteReader extends Reader { - - - public static final int DEFAULT_BUFFER_SIZE = CodedIO.MAX_BUF_SIZE; - - protected byte[] fBuffer; - - protected InputStream fInputStream; - - protected ByteReader() { - super(); - } - - public ByteReader(InputStream inputStream) { - this(inputStream, DEFAULT_BUFFER_SIZE); - if (!inputStream.markSupported()) { - throw new IllegalArgumentException("ByteReader is required to have a resettable stream"); //$NON-NLS-1$ - } - } - - public ByteReader(InputStream inputStream, int size) { - this.fInputStream = inputStream; - if (!inputStream.markSupported()) { - throw new IllegalArgumentException("ByteReader is required to have a resettable stream"); //$NON-NLS-1$ - } - this.fBuffer = new byte[size]; - - } - - public void close() throws IOException { - this.fInputStream.close(); - } - - public void mark(int readAheadLimit) { - this.fInputStream.mark(readAheadLimit); - } - - public boolean markSupported() { - return true; - } - - public int read() throws IOException { - int b0 = this.fInputStream.read(); - return (b0 & 0x00FF); - } - - public int read(char ch[], int offset, int length) throws IOException { - if (length > this.fBuffer.length) { - length = this.fBuffer.length; - } - - int count = this.fInputStream.read(this.fBuffer, 0, length); - - for (int i = 0; i < count; i++) { - int b0 = this.fBuffer[i]; - // the 0x00FF is to "lose" the negative bits filled in the byte to - // int conversion - // (and which would be there if cast directly from byte to char). - char c0 = (char) (b0 & 0x00FF); - ch[offset + i] = c0; - } - return count; - } - - public boolean ready() throws IOException { - return this.fInputStream.available() > 0; - } - - public void reset() throws IOException { - this.fInputStream.reset(); - } - - public long skip(long n) throws IOException { - return this.fInputStream.skip(n); - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/CodedResourcePlugin.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/CodedResourcePlugin.java deleted file mode 100644 index 32c0b70744..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/CodedResourcePlugin.java +++ /dev/null @@ -1,55 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import org.eclipse.core.resources.IWorkspace; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.Plugin; -import org.eclipse.core.runtime.Preferences; -import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames; -import org.eclipse.wst.sse.core.internal.encoding.ICodedResourcePlugin; - -/** - * The main plugin class to be used in the desktop. - */ -public class CodedResourcePlugin extends Plugin implements ICodedResourcePlugin { - //The shared instance. - private static CodedResourcePlugin plugin; - - /** - * Returns the shared instance. - */ - public static CodedResourcePlugin getDefault() { - return plugin; - } - - /** - * Returns the workspace instance. - */ - public static IWorkspace getWorkspace() { - return ResourcesPlugin.getWorkspace(); - } - - /** - * The constructor. - */ - public CodedResourcePlugin() { - super(); - plugin = this; - } - - protected void initializeDefaultPluginPreferences() { - Preferences prefs = getPluginPreferences(); - prefs.setDefault(CommonEncodingPreferenceNames.USE_3BYTE_BOM_WITH_UTF8, false); - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/Logger.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/Logger.java deleted file mode 100644 index 0e10147170..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/Logger.java +++ /dev/null @@ -1,158 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import com.ibm.icu.util.StringTokenizer; - -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; -import org.osgi.framework.Bundle; - - -/** - * Small convenience class to log messages to plugin's log file and also, if - * desired, the console. This class should only be used by classes in this - * plugin. Other plugins should make their own copy, with appropriate ID. - */ -public class Logger { - private static final String PLUGIN_ID = "org.eclipse.wst.sse.core.internal.encoding"; //$NON-NLS-1$ - - public static final int ERROR = IStatus.ERROR; // 4 - public static final int ERROR_DEBUG = 200 + ERROR; - public static final int INFO = IStatus.INFO; // 1 - public static final int INFO_DEBUG = 200 + INFO; - - public static final int OK = IStatus.OK; // 0 - - public static final int OK_DEBUG = 200 + OK; - - private static final String TRACEFILTER_LOCATION = "/debug/tracefilter"; //$NON-NLS-1$ - public static final int WARNING = IStatus.WARNING; // 2 - public static final int WARNING_DEBUG = 200 + WARNING; - - /** - * Adds message to log. - * - * @param level - * severity level of the message (OK, INFO, WARNING, ERROR, - * OK_DEBUG, INFO_DEBUG, WARNING_DEBUG, ERROR_DEBUG) - * @param message - * text to add to the log - * @param exception - * exception thrown - */ - protected static void _log(int level, String message, Throwable exception) { - if (level == OK_DEBUG || level == INFO_DEBUG || level == WARNING_DEBUG || level == ERROR_DEBUG) { - if (!isDebugging()) - return; - } - - int severity = IStatus.OK; - switch (level) { - case INFO_DEBUG : - case INFO : - severity = IStatus.INFO; - break; - case WARNING_DEBUG : - case WARNING : - severity = IStatus.WARNING; - break; - case ERROR_DEBUG : - case ERROR : - severity = IStatus.ERROR; - } - message = (message != null) ? message : "null"; //$NON-NLS-1$ - Status statusObj = new Status(severity, PLUGIN_ID, severity, message, exception); - Bundle bundle = Platform.getBundle(PLUGIN_ID); - if (bundle != null) - Platform.getLog(bundle).log(statusObj); - } - - /** - * Prints message to log if category matches /debug/tracefilter option. - * - * @param message - * text to print - * @param category - * category of the message, to be compared with - * /debug/tracefilter - */ - protected static void _trace(String category, String message, Throwable exception) { - if (isTracing(category)) { - message = (message != null) ? message : "null"; //$NON-NLS-1$ - Status statusObj = new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, exception); - Bundle bundle = Platform.getBundle(PLUGIN_ID); - if (bundle != null) - Platform.getLog(bundle).log(statusObj); - } - } - - /** - * @return true if the platform is debugging - */ - public static boolean isDebugging() { - return Platform.inDebugMode(); - } - - /** - * Determines if currently tracing a category - * - * @param category - * @return true if tracing category, false otherwise - */ - public static boolean isTracing(String category) { - if (!isDebugging()) - return false; - - String traceFilter = Platform.getDebugOption(PLUGIN_ID + TRACEFILTER_LOCATION); - if (traceFilter != null) { - StringTokenizer tokenizer = new StringTokenizer(traceFilter, ","); //$NON-NLS-1$ - while (tokenizer.hasMoreTokens()) { - String cat = tokenizer.nextToken().trim(); - if (category.equals(cat)) { - return true; - } - } - } - return false; - } - - public static void log(int level, String message) { - _log(level, message, null); - } - - public static void log(int level, String message, Throwable exception) { - _log(level, message, exception); - } - - public static void logException(String message, Throwable exception) { - _log(ERROR, message, exception); - } - - public static void logException(Throwable exception) { - _log(ERROR, exception.getMessage(), exception); - } - - public static void trace(String category, String message) { - _trace(category, message, null); - } - - public static void traceException(String category, String message, Throwable exception) { - _trace(category, message, exception); - } - - public static void traceException(String category, Throwable exception) { - _trace(category, exception.getMessage(), exception); - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/NullInputStream.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/NullInputStream.java deleted file mode 100644 index 43f3aa277a..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/NullInputStream.java +++ /dev/null @@ -1,69 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import java.io.IOException; -import java.io.InputStream; - - -public class NullInputStream extends InputStream { - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#mark(int) - */ - public synchronized void mark(int readlimit) { - // nothing to do - } - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#markSupported() - */ - public boolean markSupported() { - // we can mark nothing. - // and, we are using this Null class specifically for - // a "fake" resettable stream. - return true; - } - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#read() - */ - public int read() throws IOException { - - return -1; - } - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#reset() - */ - public synchronized void reset() throws IOException { - // nothing to do - } - - /* - * (non-Javadoc) - * - * @see java.io.InputStream#skip(long) - */ - public long skip(long n) throws IOException { - return 0; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/ResourceBundleHelper.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/ResourceBundleHelper.java deleted file mode 100644 index e57c7ed5f0..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/ResourceBundleHelper.java +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Locale; -import java.util.ResourceBundle; - -import org.eclipse.core.runtime.Platform; - -//TODO: rework this with new platform/runtime APIs (if still needed). - -public class ResourceBundleHelper { - - public static ResourceBundle getResourceBundle(String resourceURI) throws MalformedURLException, IOException { - return getResourceBundle(resourceURI, Locale.getDefault()); - } - - public static ResourceBundle getResourceBundle(String resourceURI, Locale targetLocale) throws MalformedURLException, IOException { - // try to load bundle from the location specified in the resourceURI - // we make the assumption that the resourceURI points to the local - // file system - - int index = resourceURI.lastIndexOf("/"); //$NON-NLS-1$ - if (index == -1) { - throw new IllegalArgumentException("Invalid resourceURI"); //$NON-NLS-1$ - } - - // Below we set 'resourceDirectory' so that it ends with a '/'. - // Here's an excerpt from the ClassLoader Javadoc ... - // Any URL that ends with a '/' is assumed to refer to a directory. - // Otherwise, the URL is assumed - // to refer to a JAR file which will be opened as needed. - // - String resourceDirectory = resourceURI.substring(0, index + 1); - String resourceBundleName = resourceURI.substring(index + 1); - - // create a class loader with a class path that points to the resource - // bundle's location - // - URL[] classpath = new URL[1]; - classpath[0] = Platform.resolve(new URL(resourceDirectory)); - ClassLoader resourceLoader = new URLClassLoader(classpath, null); - - return ResourceBundle.getBundle(resourceBundleName, targetLocale, resourceLoader); - } -} - diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/UnicodeBOMEncodingDetector.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/UnicodeBOMEncodingDetector.java deleted file mode 100644 index 6cc5194745..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/encoding/util/UnicodeBOMEncodingDetector.java +++ /dev/null @@ -1,213 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.encoding.util; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.nio.charset.Charset; - -import org.eclipse.core.resources.IStorage; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.wst.sse.core.internal.encoding.CodedIO; -import org.eclipse.wst.sse.core.internal.encoding.EncodingMemento; -import org.eclipse.wst.sse.core.internal.encoding.IResourceCharsetDetector; - - -/** - * This is a "common function" class to decide if an input stream, is a - * unicode stream. - */ -public class UnicodeBOMEncodingDetector implements IResourceCharsetDetector { - - //private static final String UTF_16_CHARSET_NAME = "UTF-16"; - // //$NON-NLS-1$ - - public static class NotEnoughInputForBOMException extends IOException { - - /** - * Default <code>serialVersionUID</code> - */ - private static final long serialVersionUID = 1L; - - public NotEnoughInputForBOMException() { - super(); - } - - public NotEnoughInputForBOMException(String s) { - super(s); - } - - } - - private final static byte BB = (byte) 0xBB; - private final static byte BF = (byte) 0xBF; - private final static byte EF = (byte) 0xEF; - private final static byte FE = (byte) -2; - - private final static byte FF = (byte) -1; - private static final String UTF_16BE_CHARSET_NAME = "UTF-16BE"; //$NON-NLS-1$ - private static final String UTF_16LE_CHARSET_NAME = "UTF-16LE"; //$NON-NLS-1$ - - private static final String UTF_8_CHARSET_NAME = "UTF-8"; //$NON-NLS-1$ - - private InputStream fInputStream = null; - private boolean fNoBOMPossible; - - private EncodingMemento checkForBOM(InputStream inputStream) { - EncodingMemento result = null; - - try { - byte b1 = getNextByte(inputStream); - byte b2 = getNextByte(inputStream); - if (b1 == FE && b2 == FF) { - result = createEncodingMemento(UTF_16BE_CHARSET_NAME); - result.setUnicodeStream(true); - } else { - if (b1 == FF && b2 == FE) { - result = createEncodingMemento(UTF_16LE_CHARSET_NAME); - result.setUnicodeStream(true); - } else { - byte b3 = getNextByte((inputStream)); - if (b1 == EF && b2 == BB && b3 == BF) { - result = createEncodingMemento(UTF_8_CHARSET_NAME); - result.setUTF83ByteBOMUsed(true); - } - } - } - } catch (NotEnoughInputForBOMException e) { - // This is sort of unexpected for normal cases, but can occur for - // empty - // streams. And, this can occur "normally" for non-BOM streams - // that - // have only two - // bytes, and for which those two bytes match the first two bytes - // of UTF-8 - // BOM In any case, we'll simply return null; - result = null; - } catch (IOException e) { - // other errors should be impossible - throw new Error(e); - } - - return result; - } - - private EncodingMemento createEncodingMemento(String javaEncodingName) { - EncodingMemento encodingMemento = new EncodingMemento(); - encodingMemento.setJavaCharsetName(javaEncodingName); - String ianaName = Charset.forName(javaEncodingName).name(); - encodingMemento.setDetectedCharsetName(ianaName); - if (javaEncodingName.equals(UTF_8_CHARSET_NAME)) { - encodingMemento.setUTF83ByteBOMUsed(true); - } - return encodingMemento; - } - - public String getEncoding() throws IOException { - - return getEncodingMemento().getDetectedCharsetName(); - } - - /** - * Returns IANA encoding name if BOM detected in stream. If a BOM is - * detected, the stream is left positioned after readying the BOM. If a - * BOM is not detected, the steam is reset. - * - * 0xFEFF UTF-16, big-endian 0xFFFE UTF-16, little-endian 0xEFBBBF UTF-8 - * (BOM is optional) - * - * @param inputStream - - * must be a resetable (mark supported) stream so it can be - * reset, if not BOM encoded stream - * @return String - IANA encodingname (may not work well on 1.3, but 1.4 - * seems to have good support for IANA names) - */ - public EncodingMemento getEncodingMemento() { - - EncodingMemento result = null; - if (!fNoBOMPossible) { - - if (fInputStream == null) - throw new IllegalStateException("input must be set before use"); //$NON-NLS-1$ - - if (!fInputStream.markSupported()) { - throw new IllegalArgumentException("inputStream must be resetable"); //$NON-NLS-1$ - } - - result = checkForBOM(fInputStream); - } - - return result; - - } - - private byte getNextByte(InputStream inputStream) throws IOException { - - int byteCharAsInt = -1; - // be sure we won't block - if (inputStream.available() > 0) { - byteCharAsInt = inputStream.read(); - byteCharAsInt = byteCharAsInt & 0XFF; - } - // to avoid confustion over meaning of returned byte, - // throw exception if EOF reached. - if (byteCharAsInt == -1) - throw new NotEnoughInputForBOMException("typically not an error"); //$NON-NLS-1$ - return (byte) byteCharAsInt; - } - - /** - * - */ - - public String getSpecDefaultEncoding() { - // There is no default for this case - return null; - } - - /** - * - */ - private void resetAll() { - fNoBOMPossible = false; - fInputStream = null; - - } - - /** - * - */ - - public void set(InputStream inputStream) { - resetAll(); - fInputStream = inputStream; - } - - public void set(IStorage iStorage) throws CoreException { - set(new BufferedInputStream(iStorage.getContents(), CodedIO.MAX_BUF_SIZE)); - - } - - public void set(Reader reader) { - if (reader instanceof ByteReader) { - ByteReader byteReader = (ByteReader) reader; - fInputStream = byteReader.fInputStream; - } else { - fNoBOMPossible = true; - } - - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/CharConversionErrorWithDetail.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/CharConversionErrorWithDetail.java deleted file mode 100644 index 8d5253c172..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/CharConversionErrorWithDetail.java +++ /dev/null @@ -1,43 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.exceptions; - -import java.nio.charset.CharacterCodingException; - - -public class CharConversionErrorWithDetail extends CharacterCodingException { - /** - * Comment for <code>serialVersionUID</code> - */ - private static final long serialVersionUID = 1L; - private String fCharsetName; - - public CharConversionErrorWithDetail() { - super(); - } - - /** - * @param s - */ - public CharConversionErrorWithDetail(String charsetName) { - super(); - fCharsetName = charsetName; - } - - /** - * @return Returns the fCharsetName. - */ - public String getCharsetName() { - return fCharsetName; - } -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/MalformedInputExceptionWithDetail.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/MalformedInputExceptionWithDetail.java deleted file mode 100644 index 629567d8b3..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/MalformedInputExceptionWithDetail.java +++ /dev/null @@ -1,103 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.exceptions; - -import java.nio.charset.CharacterCodingException; - - -/** - * Intended to be a more precise form of the MalformedInputException, where - * character position and attempted encoding can be attempted. - */ -public class MalformedInputExceptionWithDetail extends CharacterCodingException { - - /** - * Default <code>serialVersionUID</code> - */ - private static final long serialVersionUID = 1L; - private int fCharPosition; - private String fDetectedCharsetName; - private boolean fExceededMax = false; - private String fJavaCharsetName; - private int fMaxBuffer; - - /** - * Disallow default constructor. If attemptedEncoding and charPostion can - * not be provided, use one of java's MalformedException. - */ - protected MalformedInputExceptionWithDetail() { - // Nothing to do - } - - public MalformedInputExceptionWithDetail(String encodingName, int charPostion) { - this.fJavaCharsetName = encodingName; - this.fDetectedCharsetName = encodingName; - this.fCharPosition = charPostion; - } - - public MalformedInputExceptionWithDetail(String attemptedJavaEncoding, String attemptedIANAEncoding, int charPostion) { - this.fJavaCharsetName = attemptedJavaEncoding; - this.fDetectedCharsetName = attemptedIANAEncoding; - this.fCharPosition = charPostion; - } - - /** - * If charPosition = -1 this could be because the character position - * exceeded the maximum buffer size, maxBuffer, then exceededMax = true. - */ - public MalformedInputExceptionWithDetail(String attemptedJavaEncoding, String attemptedIANAEncoding, int charPostion, boolean exceededMax, int maxBuffer) { - this.fJavaCharsetName = attemptedJavaEncoding; - this.fDetectedCharsetName = attemptedIANAEncoding; - this.fCharPosition = charPostion; - this.fExceededMax = exceededMax; - this.fMaxBuffer = maxBuffer; - } - - /** - */ - public java.lang.String getAttemptedIANAEncoding() { - return fDetectedCharsetName; - } - - /** - */ - public java.lang.String getAttemptedJavaEncoding() { - return fJavaCharsetName; - } - - /** - * @return int - */ - public int getCharPosition() { - return fCharPosition; - } - - /** - * Returns the maxBuffer. - * - * @return int - */ - public int getMaxBuffer() { - return fMaxBuffer; - } - - /** - * Returns the exceededMax. - * - * @return boolean - */ - public boolean isExceededMax() { - return fExceededMax; - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/MalformedOutputExceptionWithDetail.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/MalformedOutputExceptionWithDetail.java deleted file mode 100644 index 6cf9f7c05f..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/MalformedOutputExceptionWithDetail.java +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.exceptions; - - -public class MalformedOutputExceptionWithDetail extends MalformedInputExceptionWithDetail { - - /** - * Default <code>serialVersionUID</code> - */ - private static final long serialVersionUID = 1L; - - /** - * Disallow default constructor. If attemptedEncoding and charPostion can - * not be provided, use sun.io.MalformedException. - */ - private MalformedOutputExceptionWithDetail() { - // default constructor is disallowed, since if - // extra info can not be provided, the regular - // Malformed exception should be thrown - } - - /** - * Constructor for MalformedOutputExceptionWithDetail. - * - * @param attemptedJavaEncoding - * @param attemptedIANAEncoding - * @param charPostion - */ - public MalformedOutputExceptionWithDetail(String attemptedJavaEncoding, String attemptedIANAEncoding, int charPostion) { - super(attemptedJavaEncoding, attemptedIANAEncoding, charPostion); - } - -} diff --git a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/UnsupportedCharsetExceptionWithDetail.java b/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/UnsupportedCharsetExceptionWithDetail.java deleted file mode 100644 index 7cdc40b5e6..0000000000 --- a/bundles/org.eclipse.wst.sse.core/src-encoding/org/eclipse/wst/sse/core/internal/exceptions/UnsupportedCharsetExceptionWithDetail.java +++ /dev/null @@ -1,47 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 2004 IBM Corporation 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: - * IBM Corporation - initial API and implementation - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.sse.core.internal.exceptions; - -import java.nio.charset.UnsupportedCharsetException; - -import org.eclipse.wst.sse.core.internal.encoding.EncodingMemento; - - -/** - * This is intended for same purpose as it super class, but simply provides - * more information about than the name in error. This is especially useful - * for "UIs" which can present users with the error, and the - * "appropriateDefault" that can be used for a particular input. - */ -public class UnsupportedCharsetExceptionWithDetail extends UnsupportedCharsetException { - - /** - * Default <code>serialVersionUID</code> - */ - private static final long serialVersionUID = 1L; - private EncodingMemento fEncodingMementio; - - public UnsupportedCharsetExceptionWithDetail(EncodingMemento encodingMemento) { - this(encodingMemento.getDetectedCharsetName()); - fEncodingMementio = encodingMemento; - } - - protected UnsupportedCharsetExceptionWithDetail(String charsetName) { - super(charsetName); - } - - public EncodingMemento getEncodingMemento() { - return fEncodingMementio; - } - -} |