389940 - Metamodel generation should use appropriate text file line
delimiters
diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/PlatformTools.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/PlatformTools.java
index a283b1b..dbe9ea7 100644
--- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/PlatformTools.java
+++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/PlatformTools.java
@@ -21,10 +21,13 @@
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
+import org.eclipse.core.runtime.preferences.DefaultScope;
+import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.jpt.common.core.JptResourceType;
import org.eclipse.jpt.common.core.JptWorkspace;
import org.eclipse.jpt.common.core.internal.JptCommonCoreMessages;
import org.eclipse.jpt.common.core.internal.plugin.JptCommonCorePlugin;
+import org.eclipse.jpt.common.utility.internal.StringTools;
import org.osgi.framework.Bundle;
/**
@@ -237,6 +240,18 @@
}
+ // ********** workspace preferences **********
+
+ public static String getNewTextFileLineDelimiter() {
+ IScopeContext[] contexts = new IScopeContext[] { DefaultScope.INSTANCE };
+ return Platform.getPreferencesService().getString(
+ Platform.PI_RUNTIME,
+ Platform.PREF_LINE_SEPARATOR,
+ StringTools.CR,
+ contexts);
+ }
+
+
// ********** disabled constructor **********
private PlatformTools() {
diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/BodySourceWriter.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/BodySourceWriter.java
index 53a76d6..0ac5b50 100644
--- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/BodySourceWriter.java
+++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/utility/BodySourceWriter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2010 Oracle. All rights reserved.
+ * Copyright (c) 2009, 2012 Oracle. 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 http://www.eclipse.org/legal/epl-v10.html.
@@ -41,8 +41,8 @@
// key = short class name; value = import package
protected final HashMap<String, ImportPackage> imports = new HashMap<String, ImportPackage>();
- public BodySourceWriter(String packageName, String className) {
- super(new StringWriter(2000));
+ public BodySourceWriter(String packageName, String className, String lineSeparator) {
+ super(new StringWriter(2000), DEFAULT_INDENT, lineSeparator);
this.packageName = packageName;
this.className = className;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/IndentingPrintWriter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/IndentingPrintWriter.java
index 06cfbdf..d42d789 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/IndentingPrintWriter.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/IndentingPrintWriter.java
@@ -9,11 +9,11 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.io;
-import java.io.PrintWriter;
import java.io.Writer;
+import org.eclipse.jpt.common.utility.internal.StringTools;
/**
- * Extend {@link PrintWriter} to automatically indent new lines.
+ * Extend {@link JptPrintWriter} to automatically indent new lines.
* <p>
* Provisional API: This interface is part of an interim API that is still
* under development and expected to change significantly before reaching
@@ -22,7 +22,7 @@
* will almost certainly be broken (repeatedly) as the API evolves.
*/
public class IndentingPrintWriter
- extends PrintWriter
+ extends JptPrintWriter
{
private final String indent;
private int indentLevel;
@@ -35,14 +35,18 @@
* Construct a writer that indents with tabs.
*/
public IndentingPrintWriter(Writer out) {
- this(out, DEFAULT_INDENT);
+ this(out, DEFAULT_INDENT, StringTools.CR);
}
/**
* Construct a writer that indents with the specified string.
*/
public IndentingPrintWriter(Writer out, String indent) {
- this(out, indent, 0);
+ this(out, indent, 0, StringTools.CR);
+ }
+
+ public IndentingPrintWriter(Writer out, String indent, String lineSeparator) {
+ this(out, indent, 0, lineSeparator);
}
/**
@@ -50,7 +54,11 @@
* and begins with the specified indent level.
*/
public IndentingPrintWriter(Writer out, String indent, int initialIndentLevel) {
- super(out);
+ this(out, indent, initialIndentLevel, StringTools.CR);
+ }
+
+ public IndentingPrintWriter(Writer out, String indent, int initialIndentLevel, String lineSeparator) {
+ super(out, lineSeparator);
if (indent == null) {
throw new NullPointerException();
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/JptPrintWriter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/JptPrintWriter.java
new file mode 100644
index 0000000..9973fdb
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/io/JptPrintWriter.java
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Oracle. 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 http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.io;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.io.PrintWriter;
+import java.io.Writer;
+import org.eclipse.jpt.common.utility.internal.StringTools;
+
+/**
+ * Extend {@link PrintWriter} to give the option to select a line separator.
+ * <p>
+ * Provisional API: This interface is part of an interim API that is still
+ * under development and expected to change significantly before reaching
+ * stability. It is available at this early stage to solicit feedback from
+ * pioneering adopters on the understanding that any code that uses this API
+ * will almost certainly be broken (repeatedly) as the API evolves.
+ */
+public class JptPrintWriter
+ extends PrintWriter
+{
+ private String lineSeparator;
+
+ public JptPrintWriter(Writer out) {
+ this(out, StringTools.CR); // use system property by default
+ }
+
+ public JptPrintWriter(Writer out, String lineSeparator) {
+ super(out);
+ this.lineSeparator = lineSeparator;
+ }
+
+ @Override
+ public void println() {
+ try {
+ synchronized (this.lock) {
+ if (this.out == null) {
+ throw new IOException("Stream closed"); //$NON-NLS-1$
+ }
+ this.out.write(this.lineSeparator);
+ }
+ }
+ catch (InterruptedIOException e) {
+ Thread.currentThread().interrupt();
+ }
+ catch (IOException e) {
+ this.setError();
+ }
+ }
+}
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa2/GenericMetamodelSynchronizer.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa2/GenericMetamodelSynchronizer.java
index d5f95c8..3ab6226 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa2/GenericMetamodelSynchronizer.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa2/GenericMetamodelSynchronizer.java
@@ -9,7 +9,6 @@
******************************************************************************/
package org.eclipse.jpt.jpa.core.internal.jpa2;
-import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Date;
@@ -21,11 +20,13 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jpt.common.core.internal.utility.PlatformTools;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAbstractType;
import org.eclipse.jpt.common.core.utility.BodySourceWriter;
import org.eclipse.jpt.common.utility.internal.ClassNameTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.collection.LinkedStack;
+import org.eclipse.jpt.common.utility.io.JptPrintWriter;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.ReadOnlyPersistentAttribute;
@@ -221,14 +222,15 @@
BodySourceWriter bodySourceWriter = this.buildBodySourceWriter(memberTypeTree);
StringWriter sw = new StringWriter(bodySourceWriter.getLength() + 2000);
- PrintWriter pw = new PrintWriter(sw);
+ JptPrintWriter pw = new JptPrintWriter(sw, this.getLineSeparator());
this.printPackageAndImportsOn(pw, bodySourceWriter);
pw.print(bodySourceWriter.getSource());
return sw.toString();
}
protected BodySourceWriter buildBodySourceWriter(Map<String, Collection<MetamodelSourceType>> memberTypeTree) {
- BodySourceWriter pw = new BodySourceWriter(this.getPackageName(), this.getClassName());
+ BodySourceWriter pw = new BodySourceWriter(this.getPackageName(),
+ this.getClassName(), this.getLineSeparator());
this.printBodySourceOn(pw, memberTypeTree);
return pw;
}
@@ -405,7 +407,7 @@
// ********** package and imports **********
- protected void printPackageAndImportsOn(PrintWriter pw, BodySourceWriter bodySourceWriter) {
+ protected void printPackageAndImportsOn(JptPrintWriter pw, BodySourceWriter bodySourceWriter) {
if (this.getPackageName().length() != 0) {
pw.print("package ");
pw.print(this.getPackageName());
@@ -431,4 +433,8 @@
return ObjectTools.toString(this, this.sourceType.getName());
}
+ protected String getLineSeparator() {
+ return PlatformTools.getNewTextFileLineDelimiter();
+ }
+
}