Keep line separator while editing an existed layout and event xml file
Hi sutra,
Thank you for your contribution. I'm going to merge your patch to the next release of Maskat IDE.
Before doing so, let me confirm the copyright of your patch. It seems that LineSeparatorUtils.java is derived from SpringCoreUtils.java of the Spring IDE, isn't it? Even if it's true, there would be not so much problem -- both files are licensed under Eclipse Puglic License (EPL) 1.0. All I have to do is to list 'Spring IDE Developers' as the initial contributors of this file.
Thanks in advance,
kaz@
Copied From here and make some changes to look pretty: http://www.google.com/codesearch/p?hl=en#vCY0sFacVoc/gnu/sourceware/java/ecj-latest-source.tar.bz2|Pk_DDJWM4sY/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java&q=getLineSeparator%20ibm%20util
Continue the patch for new layout wizard:
diff -ruN /Users/sutra/workspace/maskat-ide-src-2.1.1.v20090731.1/jp.sf.maskat.ui/src/jp/sf/maskat/ui/wizards/layout/NewLayoutFileWizard.java /Users/sutra/workspace/maskat-ide-src-2.1.1.v20090731/jp.sf.maskat.ui/src/jp/sf/maskat/ui/wizards/layout/NewLayoutFileWizard.java --- /Users/sutra/workspace/maskat-ide-src-2.1.1.v20090731.1/jp.sf.maskat.ui/src/jp/sf/maskat/ui/wizards/layout/NewLayoutFileWizard.java 2010-01-25 23:46:10.000000000 +0900 +++ /Users/sutra/workspace/maskat-ide-src-2.1.1.v20090731/jp.sf.maskat.ui/src/jp/sf/maskat/ui/wizards/layout/NewLayoutFileWizard.java 2010-01-26 01:10:11.000000000 +0900 @@ -25,7 +25,9 @@ import jp.sf.maskat.ui.MaskatUIPlugin; import jp.sf.maskat.ui.Messages; import jp.sf.maskat.ui.editors.layout.LayoutGraphicalEditor; +import jp.sf.maskat.ui.editors.layout.LineSeparatorUtils; +import org.apache.commons.io.IOUtils; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; @@ -140,20 +142,25 @@ IContainer container = (IContainer) resource; final IFile file = container.getFile(new Path(fileName)); - try { - InputStream stream = openContentStream(layoutName); - if (file.exists()) { - String[] msgArgs = {fileName}; - throwCoreException(Messages.format( - "wizard.layout.msg.error.existfile", msgArgs)); - + if (file.exists()) { + String[] msgArgs = {fileName}; + throwCoreException(Messages.format( + "wizard.layout.msg.error.existfile", msgArgs)); + // throwCoreException( // Messages.getString("wizard.layout.msg.error.existfile")); //$NON-NLS-1$ - } else { + } else { + String lineSeparator = LineSeparatorUtils.getLineSeparator(null, + file.getProject()); + InputStream stream = null; + try { + stream = openContentStream(layoutName, lineSeparator); file.create(stream, true, monitor); + } catch (IOException e) { + throwCoreException("Read template failed: " + e.getMessage()); + } finally { + IOUtils.closeQuietly(stream); } - stream.close(); - } catch (IOException e) { } monitor.worked(1); @@ -173,7 +180,8 @@ monitor.worked(1); } - private String getTemplate(String templateName) throws IOException { + private String getTemplate(String templateName, String lineSeparator) + throws IOException { InputStream in = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { @@ -183,7 +191,8 @@ String line = null; while ((line = reader.readLine()) != null) { - pw.println(line); + pw.print(line); + pw.print(lineSeparator); } pw.close(); @@ -201,9 +210,10 @@ * @throws UnsupportedEncodingException */ - private InputStream openContentStream(String layoutName) + private InputStream openContentStream(String layoutName, + String lineSeparator) throws UnsupportedEncodingException, IOException { - String contents = getTemplate("layout.template"); //$NON-NLS-1$ + String contents = getTemplate("layout.template", lineSeparator); //$NON-NLS-1$ contents = contents.replaceAll("\\Q${layoutName}\\E", layoutName); //$NON-NLS-1$ return new ByteArrayInputStream(contents.getBytes("UTF-8")); //$NON-NLS-1$ }
The patch fixes two issues: