summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-08-16 09:52:50 (EDT)
committer pelder2006-08-16 09:52:50 (EDT)
commit1843312d6ccaba52252e6e497bf450694468672c (patch)
treeee7738e6702ea1c44a3869c59da4ee5bf9f9b699
parent4bfe9cdfa5fa1708440dc619b991b10974a7dfcb (diff)
downloadorg.eclipse.jet-1843312d6ccaba52252e6e497bf450694468672c.zip
org.eclipse.jet-1843312d6ccaba52252e6e497bf450694468672c.tar.gz
org.eclipse.jet-1843312d6ccaba52252e6e497bf450694468672c.tar.bz2
[153930] Avoid filebuffers APIs as there are extenders who assume the API is only used in the UI thread.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/TagUtil.java44
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java80
2 files changed, 108 insertions, 16 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/TagUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/TagUtil.java
index a8ef90e..b9f644d 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/TagUtil.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/TagUtil.java
@@ -16,11 +16,15 @@
*/
package org.eclipse.jet.taglib;
-import org.eclipse.core.filebuffers.FileBuffers;
-import org.eclipse.core.filebuffers.ITextFileBufferManager;
+import java.net.MalformedURLException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jet.internal.InternalJET2Platform;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
/**
* Utility class with various functions useful to tag implementations
@@ -36,17 +40,33 @@ public class TagUtil
super();
}
+ /**
+ * Return the contents of the text file refered to by location.
+ * @param location the full file system location of the resource.
+ * @return the contents as a string
+ * @throws CoreException if an error occurs while reading the file
+ * @throws IllegalArgumentException if <code>location</code> does not refer to an existing workspace file.
+ */
public static String getContents(IPath location) throws CoreException
{
- final ITextFileBufferManager mgr = FileBuffers.getTextFileBufferManager();
- mgr.connect(location, new NullProgressMonitor());
- try
- {
- return mgr.getTextFileBuffer(location).getDocument().get();
- }
- finally
- {
- mgr.disconnect(location, new NullProgressMonitor());
+ final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(location);
+ if(file != null) {
+ String encoding = file.getCharset();
+ try
+ {
+ return ActionsUtil.readTextFile(file.getLocationURI().toURL(), encoding);
+ }
+ catch (JET2TagException e)
+ {
+ final Exception cause = (Exception)e.getCause();
+ throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, cause.getMessage(), cause));
+ }
+ catch (MalformedURLException e)
+ {
+ throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, e.getMessage(), e));
+ }
+ } else {
+ throw new IllegalArgumentException(location.toString());
}
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
index 93ff010..037b094 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
@@ -28,6 +28,7 @@ import java.io.SequenceInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.MessageFormat;
+import java.util.Arrays;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
@@ -58,6 +59,16 @@ public final class ActionsUtil
{
/**
+ * Constant that identifies the UTF-8 character encoding schema.
+ */
+ private static final String CHARSET_UTF_8 = "UTF-8"; //$NON-NLS-1$
+ /**
+ * Constant that identifies the Byte-Order-Mark for contents encoded with
+ * the UTF-8 character encoding scheme.
+ */
+ private final static byte[] BOM_UTF_8 = { (byte)0xEF, (byte)0xBB, (byte)0xBF };
+
+ /**
*
*/
private ActionsUtil()
@@ -164,7 +175,7 @@ public final class ActionsUtil
boolean hasBOM = false;
IContentDescription description = file.getContentDescription();
hasBOM = description != null && description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
- if(hasBOM && "UTF-8".equals(file.getCharset())) { //$NON-NLS-1$
+ if(hasBOM && CHARSET_UTF_8.equals(file.getCharset())) {
source = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), source);
}
monitor.worked(1);
@@ -294,13 +305,17 @@ public final class ActionsUtil
*/
public static String readTextFile(URL url, String encoding) throws JET2TagException
{
+ InputStream input = null;
+ Reader reader = null;
try
{
- final InputStream input = url.openStream();
- StringBuffer inputContents = new StringBuffer();
- Reader reader;
+ input = url.openStream();
if (encoding != null)
{
+ if(encoding.equals(CHARSET_UTF_8))
+ {
+ input = stripByteOrderMark(input);
+ }
reader = new InputStreamReader(input, encoding);
}
else
@@ -308,6 +323,7 @@ public final class ActionsUtil
reader = new InputStreamReader(input);
}
+ StringBuffer inputContents = new StringBuffer();
char readChars[] = new char [1024];
for (int read = reader.read(readChars); read != -1; read = reader.read(readChars))
{
@@ -321,6 +337,62 @@ public final class ActionsUtil
{
throw new JET2TagException(e);
}
+ finally
+ {
+ // ensure stream and reader are closed.
+ if(reader != null) {
+ try
+ {
+ reader.close();
+ }
+ catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ if(input != null) {
+ try
+ {
+ input.close();
+ }
+ catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+ }
+
+ /**
+ * Remove the optional Byte Order Mark from UTF-8 input streams. A JRE bug means that Java doesn't do this
+ * automatically for UTF-8 files.
+ * @param input an input stream positioned at the start of a UTF-8 encoded file.
+ * @return an input stream positioned immediately after any UTF-8 BOM
+ * @throws IOException if an IO Error occurs
+ */
+ private static InputStream stripByteOrderMark(InputStream input) throws IOException
+ {
+ // need to look for and remove any Byte-order-mark - a JRE bug means that Java doesn't do this for UTF-8.
+ if(!input.markSupported()) {
+ // wrap with a buffered input stream, we'll need to rewind if we don't find the BOM.
+ input = new BufferedInputStream(input);
+ }
+ input.mark(BOM_UTF_8.length + 1);
+ int bomLength = BOM_UTF_8.length;
+ byte[] bomStore = new byte [bomLength];
+ int bytesRead = 0;
+ do
+ {
+ int bytes = input.read(bomStore, bytesRead, bomLength - bytesRead);
+ if (bytes == -1)
+ throw new IOException();
+ bytesRead += bytes;
+ } while (bytesRead < bomLength);
+ if(!Arrays.equals(BOM_UTF_8, bomStore))
+ {
+ input.reset(); // wasn't a bom, rewind to our mark and continue.
+ }
+ return input;
}
/**