diff options
author | Szymon Ptaszkiewicz | 2012-07-18 08:25:58 +0000 |
---|---|---|
committer | Szymon Ptaszkiewicz | 2012-07-18 08:25:58 +0000 |
commit | 032f18c757aa1bfbe5d0f387268138235ec75065 (patch) | |
tree | 8b9d68179c75473bb2e1b7b710296f1966fec3d5 | |
parent | 1ad052306f6886a1fcfed04bd57b773344a114d8 (diff) | |
download | eclipse.platform.resources-032f18c757aa1bfbe5d0f387268138235ec75065.tar.gz eclipse.platform.resources-032f18c757aa1bfbe5d0f387268138235ec75065.tar.xz eclipse.platform.resources-032f18c757aa1bfbe5d0f387268138235ec75065.zip |
Bug 384150 - .project files created with wrong line delimitersv20120718-082558I20120911-1000I20120904-0800I20120828-0800I20120821-0800I20120814-0800I20120812-1736I20120810-1300I20120808-2000I20120808-0800I20120807-2000I20120807-0800I20120806-2000I20120806-0800I20120805-2000I20120801-1800I20120731-0800I20120725-1800I20120724-0800
8 files changed, 151 insertions, 55 deletions
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java index 674f58c2c..69f72468c 100644 --- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java +++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 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 @@ -617,8 +617,9 @@ public class FileSystemResourceManager implements ICoreConstants, IManager, Pref //write the model to a byte array ByteArrayOutputStream out = new ByteArrayOutputStream(); + IFile descriptionFile = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME); try { - new ModelObjectWriter().write(description, out); + new ModelObjectWriter().write(description, out, FileUtil.getLineSeparator(descriptionFile)); } catch (IOException e) { String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath()); throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), msg, e); @@ -626,7 +627,6 @@ public class FileSystemResourceManager implements ICoreConstants, IManager, Pref byte[] newContents = out.toByteArray(); //write the contents to the IFile that represents the description - IFile descriptionFile = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME); if (!descriptionFile.exists()) workspace.createResource(descriptionFile, false); else { @@ -1158,7 +1158,8 @@ public class FileSystemResourceManager implements ICoreConstants, IManager, Pref OutputStream out = null; try { out = fileStore.openOutputStream(EFS.NONE, null); - new ModelObjectWriter().write(desc, out); + IFile file = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME); + new ModelObjectWriter().write(desc, out, FileUtil.getLineSeparator(file)); out.close(); } catch (IOException e) { String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath()); diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java index 531c65bff..6e6d94ccc 100644 --- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java +++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 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 @@ -27,7 +27,7 @@ import org.eclipse.osgi.util.NLS; public class LocalMetaArea implements ICoreConstants { /* package */static final String F_BACKUP_FILE_EXTENSION = ".bak"; //$NON-NLS-1$ /* package */static final String F_DESCRIPTION = ".workspace"; //$NON-NLS-1$ - + /* package */static final String F_HISTORY_STORE = ".history"; //$NON-NLS-1$ /* package */static final String F_MARKERS = ".markers"; //$NON-NLS-1$ /* package */static final String F_OLD_PROJECT = ".prj"; //$NON-NLS-1$ @@ -43,7 +43,6 @@ public class LocalMetaArea implements ICoreConstants { /* package */static final String F_TREE = ".tree"; //$NON-NLS-1$ /* package */static final String URI_PREFIX = "URI//"; //$NON-NLS-1$ /* package */static final String F_METADATA = ".metadata"; //$NON-NLS-1$ - protected final IPath metaAreaLocation; @@ -411,7 +410,7 @@ public class LocalMetaArea implements ICoreConstants { path.toFile().getParentFile().mkdirs(); IPath tempPath = getBackupLocationFor(path); try { - new ModelObjectWriter().write(description, path, tempPath); + new ModelObjectWriter().write(description, path, tempPath, System.getProperty("line.separator")); //$NON-NLS-1$ } catch (IOException e) { String message = NLS.bind(Messages.resources_writeWorkspaceMeta, path); throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, e); @@ -436,8 +435,7 @@ public class LocalMetaArea implements ICoreConstants { final IProject[] prjRefs = desc.getDynamicReferences(false); final String[] buildConfigs = desc.configNames; final Map<String, IBuildConfiguration[]> configRefs = desc.getBuildConfigReferences(false); - if (projectLocation == null && prjRefs.length == 0 && - buildConfigs.length == 0 && configRefs.isEmpty()) + if (projectLocation == null && prjRefs.length == 0 && buildConfigs.length == 0 && configRefs.isEmpty()) return; //write the private metadata file try { diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java index 7a7ac3617..b8afa9a3e 100644 --- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java +++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 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 @@ -93,7 +93,7 @@ public class ModelObjectWriter implements IModelObjectConstants { protected void write(IResourceFilterDescription description, XMLWriter writer) { writer.startTag(FILTER, null); if (description != null) { - writer.printSimpleTag(ID, new Long(((FilterDescription)description).getId())); + writer.printSimpleTag(ID, new Long(((FilterDescription) description).getId())); writer.printSimpleTag(NAME, description.getResource().getProjectRelativePath()); writer.printSimpleTag(TYPE, Integer.toString(description.getType())); if (description.getFileInfoMatcherDescription() != null) { @@ -102,7 +102,7 @@ public class ModelObjectWriter implements IModelObjectConstants { } writer.endTag(FILTER); } - + protected void write(FileInfoMatcherDescription description, XMLWriter writer) { writer.startTag(MATCHER, null); writer.printSimpleTag(ID, description.getId()); @@ -110,7 +110,7 @@ public class ModelObjectWriter implements IModelObjectConstants { if (description.getArguments() instanceof String) { writer.printSimpleTag(ARGUMENTS, description.getArguments()); } else if (description.getArguments() instanceof FileInfoMatcherDescription[]) { - writer.startTag(ARGUMENTS , null); + writer.startTag(ARGUMENTS, null); FileInfoMatcherDescription[] array = (FileInfoMatcherDescription[]) description.getArguments(); for (int i = 0; i < array.length; i++) { write(array[i], writer); @@ -150,12 +150,12 @@ public class ModelObjectWriter implements IModelObjectConstants { * The parameter tempLocation is a location to place our temp file (copy of the target one) * to be used in case we could not successfully write the new file. */ - public void write(Object object, IPath location, IPath tempLocation) throws IOException { + public void write(Object object, IPath location, IPath tempLocation, String lineSeparator) throws IOException { SafeFileOutputStream file = null; String tempPath = tempLocation == null ? null : tempLocation.toOSString(); try { file = new SafeFileOutputStream(location.toOSString(), tempPath); - write(object, file); + write(object, file, lineSeparator); file.close(); } finally { FileUtil.safeClose(file); @@ -165,9 +165,9 @@ public class ModelObjectWriter implements IModelObjectConstants { /** * The OutputStream is closed in this method. */ - public void write(Object object, OutputStream output) throws IOException { + public void write(Object object, OutputStream output, String lineSeparator) throws IOException { try { - XMLWriter writer = new XMLWriter(output); + XMLWriter writer = new XMLWriter(output, lineSeparator); write(object, writer); writer.flush(); writer.close(); @@ -213,7 +213,7 @@ public class ModelObjectWriter implements IModelObjectConstants { writer.printSimpleTag(NAME, description.getName()); String comment = description.getComment(); writer.printSimpleTag(COMMENT, comment == null ? "" : comment); //$NON-NLS-1$ - URI snapshotLocation= description.getSnapshotLocationURI(); + URI snapshotLocation = description.getSnapshotLocationURI(); if (snapshotLocation != null) { writer.printSimpleTag(SNAPSHOT_LOCATION, snapshotLocation.toString()); } diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/XMLWriter.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/XMLWriter.java index e591aabd0..d1f567a90 100644 --- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/XMLWriter.java +++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/XMLWriter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2012 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 @@ -20,13 +20,15 @@ import java.util.Map; */ public class XMLWriter extends PrintWriter { protected int tab; + protected String lineSeparator; /* constants */ protected static final String XML_VERSION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$ - public XMLWriter(OutputStream output) throws UnsupportedEncodingException { + public XMLWriter(OutputStream output, String separator) throws UnsupportedEncodingException { super(new OutputStreamWriter(output, "UTF8")); //$NON-NLS-1$ tab = 0; + lineSeparator = separator; println(XML_VERSION); } @@ -35,6 +37,11 @@ public class XMLWriter extends PrintWriter { printTag('/' + name, null); } + public void println(String x) { + super.print(x); + super.print(lineSeparator); + } + public void printSimpleTag(String name, Object value) { if (value != null) { printTag(name, null, true, false); diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ModelObjectReaderWriterTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ModelObjectReaderWriterTest.java index 6609b22f1..28da43781 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ModelObjectReaderWriterTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ModelObjectReaderWriterTest.java @@ -414,7 +414,7 @@ public class ModelObjectReaderWriterTest extends ResourceTest { description.setLinkDescriptions(linkDescriptions); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - new ModelObjectWriter().write(description, buffer); + new ModelObjectWriter().write(description, buffer, System.getProperty("line.separator")); String result = buffer.toString(); // order of keys in serialized file should be exactly the same as expected @@ -704,7 +704,7 @@ public class ModelObjectReaderWriterTest extends ResourceTest { description.setReferencedProjects(refProjects); OutputStream output = tempStore.openOutputStream(EFS.NONE, getMonitor()); - writer.write(description, output); + writer.write(description, output, System.getProperty("line.separator")); output.close(); /* test read */ @@ -800,7 +800,7 @@ public class ModelObjectReaderWriterTest extends ResourceTest { desc.setMaxFileStateSize(123456789l); SafeFileOutputStream output = new SafeFileOutputStream(location.toFile()); - writer.write(desc, output); + writer.write(desc, output, System.getProperty("line.separator")); output.close(); /* test read */ @@ -836,7 +836,7 @@ public class ModelObjectReaderWriterTest extends ResourceTest { OutputStream output = null; try { output = store.openOutputStream(EFS.NONE, getMonitor()); - new ModelObjectWriter().write(description, output); + new ModelObjectWriter().write(description, output, System.getProperty("line.separator")); } finally { assertClose(output); } diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectPreferencesTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectPreferencesTest.java index a728f633e..9dd1e8a47 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectPreferencesTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/resources/ProjectPreferencesTest.java @@ -1103,36 +1103,6 @@ public class ProjectPreferencesTest extends ResourceTest { return project.getLocation().append(DIR_NAME).append(qualifier).addFileExtension(FILE_EXTENSION).toFile(); } - private static String getLineSeparatorFromFile(IFile file) { - if (file.exists()) { - InputStream input = null; - try { - input = file.getContents(); - int c = input.read(); - while (c != -1 && c != '\r' && c != '\n') - c = input.read(); - if (c == '\n') - return "\n"; //$NON-NLS-1$ - if (c == '\r') { - if (input.read() == '\n') - return "\r\n"; //$NON-NLS-1$ - return "\r"; //$NON-NLS-1$ - } - } catch (CoreException e) { - // ignore - } catch (IOException e) { - // ignore - } finally { - try { - input.close(); - } catch (IOException e) { - // ignore - } - } - } - return null; - } - /* * Test to ensure that discovering a new pref file (e.g. loading from a repo) * is the same as doing an import. (ensure the modify listeners are called) diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IProjectTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IProjectTest.java index 3cfeeef97..041cd6cdc 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IProjectTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/IProjectTest.java @@ -19,6 +19,9 @@ import org.eclipse.core.internal.resources.Resource; import org.eclipse.core.internal.resources.Workspace; import org.eclipse.core.resources.*; import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.osgi.service.prefs.BackingStoreException; +import org.osgi.service.prefs.Preferences; public class IProjectTest extends ResourceTest { @@ -619,6 +622,93 @@ public class IProjectTest extends ResourceTest { } } + public void testProjectCreationLineSeparator() throws BackingStoreException, CoreException { + // make sure each line separator is different + String systemValue = System.getProperty("line.separator"); + String newInstanceValue; + String newProjectValue; + if (systemValue.equals("\n")) { + // for unix "\n" + newInstanceValue = "\r"; + newProjectValue = "\r\n"; + } else if (systemValue.equals("\r")) { + // for macos "\r" + newInstanceValue = "\n"; + newProjectValue = "\r\n"; + } else { + // for windows "\r\n" + newInstanceValue = "\r"; + newProjectValue = "\n"; + } + + Preferences rootNode = Platform.getPreferencesService().getRootNode(); + Preferences instanceNode = rootNode.node(InstanceScope.SCOPE).node(Platform.PI_RUNTIME); + String oldInstanceValue = instanceNode.get(Platform.PREF_LINE_SEPARATOR, null); + + IProject project = getWorkspace().getRoot().getProject(getUniqueString()); + IFile file = project.getFile(IProjectDescription.DESCRIPTION_FILE_NAME); + IProjectDescription description; + try { + ensureExistsInWorkspace(project, true); + // new .project should have OS default line separator + assertEquals("1.0", systemValue, getLineSeparatorFromFile(file)); + + // set instance-specific line separator + instanceNode.put(Platform.PREF_LINE_SEPARATOR, newInstanceValue); + instanceNode.flush(); + description = project.getDescription(); + description.setComment("some comment"); + project.setDescription(description, getMonitor()); + // existing .project should use existing line separator + assertEquals("2.0", systemValue, getLineSeparatorFromFile(file)); + project.delete(true, getMonitor()); + + ensureExistsInWorkspace(project, true); + // new .project should have instance-specific line separator + assertEquals("3.0", newInstanceValue, getLineSeparatorFromFile(file)); + + // remove preference for the next step + if (oldInstanceValue == null) + instanceNode.remove(Platform.PREF_LINE_SEPARATOR); + else + instanceNode.put(Platform.PREF_LINE_SEPARATOR, oldInstanceValue); + instanceNode.flush(); + description = project.getDescription(); + description.setComment("some comment"); + project.setDescription(description, getMonitor()); + // existing .project should use existing line separator + assertEquals("4.0", newInstanceValue, getLineSeparatorFromFile(file)); + project.delete(true, getMonitor()); + + ensureExistsInWorkspace(project, true); + // new .project should have OS default line separator + assertEquals("5.0", systemValue, getLineSeparatorFromFile(file)); + + // set project-specific line separator + Preferences projectNode = rootNode.node(ProjectScope.SCOPE).node(project.getName()).node(Platform.PI_RUNTIME); + projectNode.put(Platform.PREF_LINE_SEPARATOR, newProjectValue); + projectNode.flush(); + // remove .project file but leave the project + file.delete(true, getMonitor()); + assertFalse("6.0", file.exists()); + // workspace save should recreate .project file with project-specific line delimiter + getWorkspace().save(true, getMonitor()); + // refresh project to update the resource tree + project.refreshLocal(IResource.DEPTH_INFINITE, getMonitor()); + assertTrue("7.0", file.exists()); + // new .project should have project-specific line separator + assertEquals("8.0", newProjectValue, getLineSeparatorFromFile(file)); + } finally { + // revert instance preference to original value + if (oldInstanceValue == null) + instanceNode.remove(Platform.PREF_LINE_SEPARATOR); + else + instanceNode.put(Platform.PREF_LINE_SEPARATOR, oldInstanceValue); + instanceNode.flush(); + project.delete(true, getMonitor()); + } + } + /** * Tests creating a project whose location is invalid */ diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java index 202f3e9e8..43db1b616 100644 --- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java +++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java @@ -729,6 +729,36 @@ public abstract class ResourceTest extends CoreTest { }; } + protected String getLineSeparatorFromFile(IFile file) { + if (file.exists()) { + InputStream input = null; + try { + input = file.getContents(); + int c = input.read(); + while (c != -1 && c != '\r' && c != '\n') + c = input.read(); + if (c == '\n') + return "\n"; //$NON-NLS-1$ + if (c == '\r') { + if (input.read() == '\n') + return "\r\n"; //$NON-NLS-1$ + return "\r"; //$NON-NLS-1$ + } + } catch (CoreException e) { + // ignore + } catch (IOException e) { + // ignore + } finally { + try { + input.close(); + } catch (IOException e) { + // ignore + } + } + } + return null; + } + /** * Returns a FileStore instance backed by storage in a temporary location. * The returned store will not exist, but will belong to an existing parent. |