Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java63
1 files changed, 37 insertions, 26 deletions
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 398eb9a..df7fffa 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
@@ -39,6 +39,7 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.jet.BufferedJET2Writer;
@@ -164,14 +165,14 @@ public final class ActionsUtil
final String fileMessage = MessageFormat.format(
JET2Messages.WsFileFromWriterAction_WritingFile,
new Object []{ file.getFullPath().toString() });
-
+
monitor.beginTask(fileMessage, 5);
-
+
if (file.exists() && !replace)
{
return false;
}
-
+
// Step 1: make sure file exists - necessary for proper encoding handling...
if (!file.exists())
{
@@ -184,47 +185,57 @@ public final class ActionsUtil
{
monitor.worked(1);
}
-
- // can't set encoding until the file exists, create the file with null-contents
- // now, and set it's contents later.
- file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(2);
- }
-
- // Step 2: set the encoding...
- if (encoding != null)
- {
- file.setCharset(encoding.length() > 0 ? encoding : null, new SubProgressMonitor(monitor, 1));
}
else
{
monitor.worked(1);
}
-
+
+ final IContentDescription description =
+ file.exists() ? file.getContentDescription() :
+ Platform.getContentTypeManager().findContentTypeFor(file.getName()).getDefaultDescription();
+
// Step 3: convert the contents to a byte stream of the proper type
- InputStream source = new ByteArrayInputStream(contents.getBytes(file.getCharset()));
-
+ String sourceEncoding = encoding != null ? encoding : description.getCharset();
+ if(sourceEncoding == null) {
+ sourceEncoding = ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset();
+ }
+ InputStream source = new ByteArrayInputStream(contents.getBytes(sourceEncoding));
+
// work around SUN bug with UTF-8 encoding and byte-order marks.
// See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
boolean hasBOM = false;
- IContentDescription description = file.getContentDescription();
+
hasBOM = description != null && description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
- if (hasBOM && CHARSET_UTF_8.equals(file.getCharset()))
+ if (hasBOM && CHARSET_UTF_8.equals(sourceEncoding))
{
source = new SequenceInputStream(new ByteArrayInputStream(IContentDescription.BOM_UTF_8), source);
}
monitor.worked(1);
-
+
// Step 5: write the contents - we created the file in step 1, so we only need to set its proper contents
- file.setContents(source, false, true, new SubProgressMonitor(monitor, 2));
-
+ if(!file.exists() && encoding == null) {
+ file.create(source, false, new SubProgressMonitor(monitor, 3));
+ } else {
+ // Step 5a: set the encoding...
+ if (!file.exists())
+ {
+ // can't set encoding until the file exists, create the file with null-contents
+ // now, and set it's contents later.
+ file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
+ file.setCharset(encoding.length() > 0 ? encoding : null, new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(2);
+ }
+ file.setContents(source, false, true, new SubProgressMonitor(monitor, 1));
+ }
+
// RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
// Step 6: Apply generated file properties...
file.setDerived(derived);
-
+
return true;
}
catch (UnsupportedEncodingException e)

Back to the top