summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-05-03 10:04:32 (EDT)
committer pelder2007-05-03 10:04:32 (EDT)
commit65efd4900bfacfec944d9602e3947f27c95cd532 (patch)
tree7d1746ddf24c92239137a3587ff4ef7fef3dcc70
parentaead3fe5ef17d87f2e59e1459a0ef89fe2c7f3e1 (diff)
downloadorg.eclipse.jet-65efd4900bfacfec944d9602e3947f27c95cd532.zip
org.eclipse.jet-65efd4900bfacfec944d9602e3947f27c95cd532.tar.gz
org.eclipse.jet-65efd4900bfacfec944d9602e3947f27c95cd532.tar.bz2
[163256] Change text file contents to use the appropriate line delimiter. First try project, then Workspace, then JRE delimiter.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java105
1 files changed, 102 insertions, 3 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 65b8234..a9d184b 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
@@ -34,15 +34,20 @@ import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IncrementalProjectBuilder;
+import org.eclipse.core.resources.ProjectScope;
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.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.content.IContentDescription;
+import org.eclipse.core.runtime.preferences.IScopeContext;
+import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jet.IWriterListener;
import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.internal.core.NewLineUtil;
import org.eclipse.jet.internal.l10n.JET2Messages;
import org.eclipse.jet.taglib.JET2TagException;
@@ -68,6 +73,8 @@ public final class ActionsUtil
*/
private final static byte[] BOM_UTF_8 = { (byte)0xEF, (byte)0xBB, (byte)0xBF };
+ private static final String NL = System.getProperty("line.separator"); //$NON-NLS-1$
+
/**
*
*/
@@ -79,7 +86,7 @@ public final class ActionsUtil
/**
* Write the file with the specified options from a JET writer.
* <p>
- * This method delegates to {@link #writeTextFile(IFile, boolean, String, boolean, String, IProgressMonitor) writeTextFile}
+ * This method delegates to {@link #writeTextFile(IFile, boolean, String, boolean, String, String, IProgressMonitor) writeTextFile}
* after finalizing the writer's content and notifying writer listeners via {@link IWriterListener#finalizeContent(JET2Writer, Object)}.
* If the content is written, then the writer listeners are informed of this after the write via
* {@link IWriterListener#postCommitContent(JET2Writer, Object)}.
@@ -104,9 +111,41 @@ public final class ActionsUtil
JET2Writer writer,
IProgressMonitor monitor) throws JET2TagException
{
+ return writeTextFileFromWriter(file, replace, encoding, derived, writer, getLineSeparator(file), monitor);
+ }
+
+ /**
+ * Write the file with the specified options from a JET writer.
+ * <p>
+ * This method delegates to {@link #writeTextFile(IFile, boolean, String, boolean, String, String, IProgressMonitor) writeTextFile}
+ * after finalizing the writer's content and notifying writer listeners via {@link IWriterListener#finalizeContent(JET2Writer, Object)}.
+ * If the content is written, then the writer listeners are informed of this after the write via
+ * {@link IWriterListener#postCommitContent(JET2Writer, Object)}.
+ * </p>
+ * @param file the file to write
+ * @param replace <code>true</code> if existing verions of the file should be overwritten.
+ * @param encoding the file encoding or <code>null</code> for the default encoding.
+ * @param derived <code>true</code> if the file is derived (and will be ignored by the team environment.
+ * @param writer the writer containing the file's content.
+ * @param lineSeparator the line separator to use for the content or <code>null</code> for the system line separator
+ * @param monitor a progress monitor
+ * @return <code>true</code> if the file was written, <code>false</code> if <code>replace</code> is <code>true</code>
+ * and the file already exists..
+ * @throws JET2TagException if the file cannot be created or written, or the encoding is invalid.
+ * @see IWriterListener#finalizeContent(JET2Writer, Object)
+ * @see IWriterListener#postCommitContent(JET2Writer, Object)
+ */
+ public static boolean writeTextFileFromWriter(
+ IFile file,
+ boolean replace,
+ String encoding,
+ boolean derived,
+ JET2Writer writer,
+ String lineSeparator, IProgressMonitor monitor) throws JET2TagException
+ {
finalizeContent(writer, file);
- boolean written = writeTextFile(file, replace, encoding, derived, writer.toString(), monitor);
+ boolean written = writeTextFile(file, replace, encoding, derived, writer.toString(), lineSeparator, monitor);
if (written)
{
@@ -153,6 +192,29 @@ public final class ActionsUtil
final String contents,
IProgressMonitor monitor) throws JET2TagException
{
+ return writeTextFile(file, replace, encoding, derived, contents, getLineSeparator(file), monitor);
+ }
+
+ /**
+ * Write the specified text file from a string.
+ * @param file the file to write
+ * @param replace <code>true</code> if existing verions of the file should be overwritten.
+ * @param encoding the file encoding or <code>null</code> for the default encoding.
+ * @param derived <code>true</code> if the file is derived (and will be ignored by the team environment.
+ * @param contents the contents to write.
+ * @param lineSeparator the line separator to use for the content or <code>null</code> for the system line separator
+ * @param monitor a progress monitor
+ * @return <code>false</code> <code>replace</code> was <code>true</code> and the file existed; <code>true</code> otherwise.
+ * @throws JET2TagException if the file cannot be created or written, or the encoding is invalid.
+ */
+ public static boolean writeTextFile(
+ IFile file,
+ boolean replace,
+ String encoding,
+ boolean derived,
+ final String contents,
+ String lineSeparator, IProgressMonitor monitor) throws JET2TagException
+ {
try
{
final String fileMessage = MessageFormat.format(
@@ -199,7 +261,8 @@ public final class ActionsUtil
}
// Step 3: convert the contents to a byte stream of the proper type
- InputStream source = new ByteArrayInputStream(contents.getBytes(file.getCharset()));
+ String nlContents = NewLineUtil.setLineTerminator(contents, lineSeparator != null ? lineSeparator : getLineSeparator(file));
+ InputStream source = new ByteArrayInputStream(nlContents.getBytes(file.getCharset()));
// work around SUN bug with UTF-8 encoding and byte-order marks.
// See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
@@ -599,4 +662,40 @@ public final class ActionsUtil
}
}
+
+ /**
+ * Return the workspace defined default separator for this file.
+ * @param file an existing IFile
+ * @return Returns the new line separator.
+ */
+ private static String getLineSeparator(IFile file)
+ {
+ // check for the platform defined line separator for this file...
+ String lineDelimiter = null;
+ if (file != null && file.getProject() != null)
+ {
+ // try project preference
+ lineDelimiter = Platform.getPreferencesService().getString(
+ Platform.PI_RUNTIME,
+ Platform.PREF_LINE_SEPARATOR,
+ null,
+ new IScopeContext []{ new ProjectScope(file.getProject()) });
+ }
+ if (lineDelimiter == null)
+ {
+ // try the workspace preference
+ lineDelimiter = Platform.getPreferencesService().getString(
+ Platform.PI_RUNTIME,
+ Platform.PREF_LINE_SEPARATOR,
+ null,
+ new IScopeContext []{ new InstanceScope() });
+ }
+ if (lineDelimiter == null)
+ {
+ // fall back the the JRE default
+ lineDelimiter = NL;
+ }
+ return lineDelimiter;
+ }
+
}