Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2009-01-07 09:24:23 -0500
committerpelder2009-01-07 09:24:23 -0500
commit4d470a8b67649d26acfcef7707ed4f9f62d00fa0 (patch)
treeb7d3f68d6df7ed4b3edc0d4a7ae799c9eb762acf
parent39f2e9857387aabd5f9d945c17cf520ba23160aa (diff)
downloadorg.eclipse.jet-4d470a8b67649d26acfcef7707ed4f9f62d00fa0.tar.gz
org.eclipse.jet-4d470a8b67649d26acfcef7707ed4f9f62d00fa0.tar.xz
org.eclipse.jet-4d470a8b67649d26acfcef7707ed4f9f62d00fa0.zip
[260103] NullPointerException in ActionsUtil
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java61
1 files changed, 41 insertions, 20 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 df7fffa..319129f 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2008 IBM Corporation and others.
+ * Copyright (c) 2006, 2009 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
@@ -24,6 +24,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.SequenceInputStream;
+import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.text.MessageFormat;
@@ -38,14 +39,18 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.content.IContentDescription;
+import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jet.BufferedJET2Writer;
import org.eclipse.jet.IWriterListener;
import org.eclipse.jet.IWriterListenerExtension;
import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.internal.InternalJET2Platform;
import org.eclipse.jet.internal.l10n.JET2Messages;
import org.eclipse.jet.taglib.JET2TagException;
@@ -173,7 +178,7 @@ public final class ActionsUtil
return false;
}
- // Step 1: make sure file exists - necessary for proper encoding handling...
+ // Step 1 (work = 1): make sure folder exists
if (!file.exists())
{
// Step 1a: ensure containing folder exists...
@@ -186,17 +191,14 @@ public final class ActionsUtil
monitor.worked(1);
}
}
- else
- {
- monitor.worked(1);
- }
- final IContentDescription description =
- file.exists() ? file.getContentDescription() :
- Platform.getContentTypeManager().findContentTypeFor(file.getName()).getDefaultDescription();
+ // Step 2 (work = 1): convert the contents to a byte stream of the proper type
+
+ // get the content description to see if it provides a hint on charset to use...
+ final IContentDescription contentDescription = getContentDescription(file, contents);
- // Step 3: convert the contents to a byte stream of the proper type
- String sourceEncoding = encoding != null ? encoding : description.getCharset();
+ String sourceEncoding = encoding != null ? encoding :
+ contentDescription != null ? contentDescription.getCharset() : null;
if(sourceEncoding == null) {
sourceEncoding = ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset();
}
@@ -204,20 +206,24 @@ public final class ActionsUtil
// 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;
-
- hasBOM = description != null && description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
- if (hasBOM && CHARSET_UTF_8.equals(sourceEncoding))
+ // Get default content description (based on file name) to see whether this type of file
+ // requires a UTF-8 BOM
+ final IContentType defaultContentType = Platform.getContentTypeManager().findContentTypeFor(file.getName());
+ final IContentDescription defaultDescription =
+ defaultContentType != null ? defaultContentType.getDefaultDescription() : null;
+ final boolean requiresBOM = defaultDescription != null
+ && defaultDescription.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
+ if (requiresBOM && 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
+ // Step 3 (work = 3): write the contents - creating the file if needed
if(!file.exists() && encoding == null) {
+ // if we don't need to specify the encoding, just create the file...
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
@@ -232,8 +238,7 @@ public final class ActionsUtil
file.setContents(source, false, true, new SubProgressMonitor(monitor, 1));
}
- // RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
- // Step 6: Apply generated file properties...
+ // Step 4: Apply generated file properties...
file.setDerived(derived);
return true;
@@ -252,10 +257,26 @@ public final class ActionsUtil
}
}
+ private static IContentDescription getContentDescription(IFile file, final String contents)
+ {
+ try
+ {
+ return Platform.getContentTypeManager().getDescriptionFor(new StringReader(contents), file.getName(),
+ new QualifiedName[] {IContentDescription.CHARSET});
+ }
+ catch (IOException e)
+ {
+ // should happen. Log and return
+ InternalJET2Platform.getDefault().getLog().log(
+ InternalJET2Platform.newStatus(IStatus.ERROR, "IO error determining content type for generated content", e)); //$NON-NLS-1$
+ return null;
+ }
+ }
+
/**
* Ensure that a folder exists by creating it and any of its parents if necessary.
* @param folder the folder to ensure exists.
- * @param monitor a progress montiro
+ * @param monitor a progress monitor
* @return <code>true</code> if folders were created...
* @throws JET2TagException if the folder cannot be created
*/

Back to the top