summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNan Li2012-09-18 16:35:07 (EDT)
committer Karen Butzke2012-09-18 16:35:07 (EDT)
commitd073d4fd0ca13613bac87c9381d54ecad78e9b1d (patch)
tree16503bf4b534f1488a457bf416a7903129391e52
parenta588d2c21918e52166fc5dfb193da389b082c0e5 (diff)
downloadwebtools.dali-d073d4fd0ca13613bac87c9381d54ecad78e9b1d.zip
webtools.dali-d073d4fd0ca13613bac87c9381d54ecad78e9b1d.tar.gz
webtools.dali-d073d4fd0ca13613bac87c9381d54ecad78e9b1d.tar.bz2
Bug 315464 - Ability to convert an orm.xml file into an eclipselink-orm.xml file
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/plugin.xml11
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/XmlFile.java15
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/JptResourceModelPropertyTester.java24
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/XmlFileAdapterFactory.java55
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/XmlFilePropertyTester.java6
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmXml.java3
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/persistence/GenericPersistenceXml.java3
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.properties4
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.xml84
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/commands/UpgradeToEclipseLinkMappingFileHandler.java148
10 files changed, 349 insertions, 4 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/plugin.xml b/jpa/plugins/org.eclipse.jpt.jpa.core/plugin.xml
index 8b465f4..a67ae34 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/plugin.xml
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/plugin.xml
@@ -81,6 +81,13 @@
<adapter type="org.eclipse.jpt.jpa.core.platform.JpaPlatformDescription"/>
</factory>
+ <!-- XmlFile -> JpaPlatformDescription -->
+ <factory
+ adaptableType="org.eclipse.jpt.jpa.core.context.XmlFile"
+ class="org.eclipse.jpt.jpa.core.internal.XmlFileAdapterFactory">
+ <adapter type="org.eclipse.jpt.jpa.core.platform.JpaPlatformDescription"/>
+ </factory>
+
</extension>
@@ -189,14 +196,14 @@
id="org.eclipse.jpt.jpa.core.propertyTester.XmlFile"
type="org.eclipse.jpt.jpa.core.context.XmlFile"
namespace="org.eclipse.jpt.jpa.core"
- properties="isLatestSupportedVersion, isNotLatestSupportedVersion"
+ properties="isLatestSupportedVersion, isNotLatestSupportedVersion, isGenericMappingFile"
class="org.eclipse.jpt.jpa.core.internal.context.XmlFilePropertyTester"/>
<propertyTester
id="org.eclipse.jpt.jpa.core.propertyTester.JptResourceModel"
type="org.eclipse.jpt.common.core.JptResourceModel"
namespace="org.eclipse.jpt.jpa.core"
- properties="isLatestSupportedVersion, isNotLatestSupportedVersion"
+ properties="isLatestSupportedVersion, isNotLatestSupportedVersion, isGenericMappingFile"
class="org.eclipse.jpt.jpa.core.internal.JptResourceModelPropertyTester"/>
</extension>
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/XmlFile.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/XmlFile.java
index 4069e6d..3f09091 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/XmlFile.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/XmlFile.java
@@ -9,9 +9,11 @@
******************************************************************************/
package org.eclipse.jpt.jpa.core.context;
+import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jpt.common.core.resource.xml.JptXmlResource;
import org.eclipse.jpt.common.utility.internal.Tools;
import org.eclipse.jpt.jpa.core.JpaStructureNode;
+import org.eclipse.jpt.jpa.core.resource.orm.XmlEntityMappings;
/**
* Context representation of any JPA XML file.
@@ -50,6 +52,11 @@ public interface XmlFile
*/
boolean isLatestSupportedVersion();
+ /**
+ * Return whether the XML file is a generic mapping file
+ */
+ boolean isGenericMappingFile();
+
// ********** XML file root element **********
@@ -77,6 +84,14 @@ public interface XmlFile
return Tools.valuesAreEqual(xmlFileVersion, latestVersion);
}
+ /**
+ * @see #isGenericMappingFile()
+ */
+ public static boolean isGenericMappingFile(XmlFile xmlFile) {
+ IContentType contentType = xmlFile.getXmlResource().getContentType();
+ return Tools.valuesAreEqual(contentType, XmlEntityMappings.CONTENT_TYPE);
+ }
+
private XmlFile_() {
super();
throw new UnsupportedOperationException();
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/JptResourceModelPropertyTester.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/JptResourceModelPropertyTester.java
index 5a136cc..f87642b 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/JptResourceModelPropertyTester.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/JptResourceModelPropertyTester.java
@@ -11,10 +11,12 @@ package org.eclipse.jpt.jpa.core.internal;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jpt.common.core.JptResourceModel;
import org.eclipse.jpt.common.core.JptResourceType;
import org.eclipse.jpt.common.utility.internal.Tools;
import org.eclipse.jpt.jpa.core.JpaProject;
+import org.eclipse.jpt.jpa.core.resource.orm.XmlEntityMappings;
/**
* Property tester for {@link JptResourceModel}.
@@ -25,7 +27,7 @@ public class JptResourceModelPropertyTester
{
public static final String IS_LATEST_SUPPORTED_VERSION = "isLatestSupportedVersion"; //$NON-NLS-1$
public static final String IS_NOT_LATEST_SUPPORTED_VERSION = "isNotLatestSupportedVersion"; //$NON-NLS-1$
-
+ public static final String IS_GENERIC_MAPPING_FILE = "isGenericMappingFile"; //$NON-NLS-1$
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (receiver instanceof JptResourceModel) {
@@ -43,6 +45,11 @@ public class JptResourceModelPropertyTester
boolean actual = this.isLatestSupportedVersion(resourceModel);
return actual == expected;
}
+ if (property.equals(IS_GENERIC_MAPPING_FILE)) {
+ boolean expected = (expectedValue == null) ? true : ((Boolean) expectedValue).booleanValue();
+ boolean actual = this.isGenericMappingFile(resourceModel);
+ return actual == expected;
+ }
return false;
}
@@ -61,6 +68,21 @@ public class JptResourceModelPropertyTester
return Tools.valuesAreEqual(resourceType.getVersion(), latestVersion);
}
+ private boolean isGenericMappingFile(JptResourceModel resourceModel) {
+ JpaProject jpaProject = this.getJpaProject(resourceModel.getFile().getProject());
+ if (jpaProject == null) {
+ // if we get to this tester, the JPA project should be there;
+ // so this will probably never happen
+ return true; // effectively disable "upgrade"
+ }
+ JptResourceType resourceType = resourceModel.getResourceType();
+ if (resourceType == null) {
+ return true; // effectively disable "upgrade"
+ }
+ IContentType contentType = resourceType.getContentType();
+ return Tools.valuesAreEqual(contentType, XmlEntityMappings.CONTENT_TYPE);
+ }
+
private JpaProject getJpaProject(IProject project) {
return (JpaProject) project.getAdapter(JpaProject.class);
}
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/XmlFileAdapterFactory.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/XmlFileAdapterFactory.java
new file mode 100644
index 0000000..3b0a966
--- /dev/null
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/XmlFileAdapterFactory.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * 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.jpa.core.internal;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IAdapterFactory;
+import org.eclipse.jpt.jpa.core.JpaPreferences;
+import org.eclipse.jpt.jpa.core.JpaWorkspace;
+import org.eclipse.jpt.jpa.core.context.XmlFile;
+import org.eclipse.jpt.jpa.core.platform.JpaPlatformDescription;
+import org.eclipse.jpt.jpa.core.platform.JpaPlatformManager;
+
+public class XmlFileAdapterFactory implements IAdapterFactory {
+ private static final Class<?>[] ADAPTER_LIST = new Class[] {
+ JpaPlatformDescription.class
+ };
+
+ public Class<?>[] getAdapterList() {
+ return ADAPTER_LIST;
+ }
+
+ public Object getAdapter(Object adaptableObject, @SuppressWarnings("rawtypes") Class adapterType) {
+ if (adaptableObject instanceof XmlFile) {
+ return this.getAdapter(((XmlFile) adaptableObject).getXmlResource().getFile(), adapterType);
+ }
+ return null;
+ }
+
+ private Object getAdapter(IResource resource, Class<?> adapterType) {
+ if (adapterType == JpaPlatformDescription.class) {
+ return this.getJpaPlatformDescription(resource);
+ }
+ return null;
+ }
+
+ private JpaPlatformDescription getJpaPlatformDescription(IResource resource) {
+ return this.getJpaPlatformManager().getJpaPlatformDescription(JpaPreferences.getJpaPlatformID(resource.getProject()));
+ }
+
+ private JpaPlatformManager getJpaPlatformManager() {
+ return this.getJpaWorkspace().getJpaPlatformManager();
+ }
+
+ private JpaWorkspace getJpaWorkspace() {
+ return (JpaWorkspace) ResourcesPlugin.getWorkspace().getAdapter(JpaWorkspace.class);
+ }
+}
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/XmlFilePropertyTester.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/XmlFilePropertyTester.java
index df04779..fb4a522 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/XmlFilePropertyTester.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/XmlFilePropertyTester.java
@@ -21,6 +21,7 @@ public class XmlFilePropertyTester
{
public static final String IS_LATEST_SUPPORTED_VERSION = "isLatestSupportedVersion"; //$NON-NLS-1$
public static final String IS_NOT_LATEST_SUPPORTED_VERSION = "isNotLatestSupportedVersion"; //$NON-NLS-1$
+ public static final String IS_GENERIC_MAPPING_FILE = "isGenericMappingFile"; //$NON-NLS-1$
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
@@ -39,6 +40,11 @@ public class XmlFilePropertyTester
boolean actual = xmlFile.isLatestSupportedVersion();
return actual == expected;
}
+ if (property.equals(IS_GENERIC_MAPPING_FILE)) {
+ boolean expected = (expectedValue == null) ? true : ((Boolean) expectedValue).booleanValue();
+ boolean actual = xmlFile.isGenericMappingFile();
+ return actual == expected;
+ }
return false;
}
}
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmXml.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmXml.java
index 5a866ec..cc19f5a 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmXml.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmXml.java
@@ -258,6 +258,9 @@ public class GenericOrmXml
return Tools.valuesAreEqual(member, file);
}
+ public boolean isGenericMappingFile() {
+ return XmlFile_.isGenericMappingFile(this);
+ }
// ********** JpaStructureNode implementation **********
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/persistence/GenericPersistenceXml.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/persistence/GenericPersistenceXml.java
index e8336db..5429817 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/persistence/GenericPersistenceXml.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/jpa1/context/persistence/GenericPersistenceXml.java
@@ -196,6 +196,9 @@ public class GenericPersistenceXml
return XmlFile_.isLatestSupportedVersion(this);
}
+ public boolean isGenericMappingFile() {
+ return false;
+ }
// ********** metamodel **********
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.properties b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.properties
index cfdfdfd..6ffe00e 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.properties
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.properties
@@ -37,4 +37,6 @@ convertJavaConverters=Java Converters...
jpaNode = JPA
eclipseLinkNode = EclipseLink
-addVirtualAttribute = Add Virtual Attribute... \ No newline at end of file
+addVirtualAttribute = Add Virtual Attribute...
+
+upgradeToEclipseLinkMappingFile = Upgrade to EclipseLink Mapping File \ No newline at end of file
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.xml b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.xml
index bcd9013..1158fe2 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.xml
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/plugin.xml
@@ -238,6 +238,11 @@
id="org.eclipse.jpt.jpa.eclipselink.ui.generateDynamicEntities"
name="%generateDynamicEntities"/>
+ <command
+ id="org.eclipse.jpt.jpa.eclipselink.ui.upgradeToEclipseLinkMappingFile"
+ name="%upgradeToEclipseLinkMappingFile">
+ </command>
+
</extension>
<!-- ***** handlers ***** -->
@@ -273,6 +278,29 @@
commandId="org.eclipse.jpt.jpa.eclipselink.ui.generateDynamicEntities"
class="org.eclipse.jpt.jpa.eclipselink.ui.internal.commands.GenerateDynamicEntitiesHandler">
</handler>
+
+ <handler
+ class="org.eclipse.jpt.jpa.eclipselink.ui.internal.commands.UpgradeToEclipseLinkMappingFileHandler"
+ commandId="org.eclipse.jpt.jpa.eclipselink.ui.upgradeToEclipseLinkMappingFile">
+ <enabledWhen>
+ <iterate>
+ <or>
+ <adapt
+ type="org.eclipse.jpt.jpa.core.context.XmlFile">
+ <test
+ property="org.eclipse.jpt.jpa.core.isGenericMappingFile">
+ </test>
+ </adapt>
+ <adapt
+ type="org.eclipse.jpt.common.core.resource.xml.JptXmlResource">
+ <test
+ property="org.eclipse.jpt.jpa.core.isGenericMappingFile">
+ </test>
+ </adapt>
+ </or>
+ </iterate>
+ </enabledWhen>
+ </handler>
</extension>
@@ -311,6 +339,32 @@
</with>
</visibleWhen>
</command>
+
+ <command
+ commandId="org.eclipse.jpt.jpa.eclipselink.ui.upgradeToEclipseLinkMappingFile"
+ style="push">
+ <visibleWhen>
+ <with
+ variable="selection">
+ <iterate
+ ifEmpty="false">
+ <adapt
+ type="org.eclipse.jpt.jpa.core.platform.JpaPlatformDescription">
+ <test
+ property="org.eclipse.jpt.jpa.core.jpaPlatformGroup"
+ value="eclipselink">
+ </test>
+ </adapt>
+ <adapt
+ type="org.eclipse.jpt.common.core.resource.xml.JptXmlResource">
+ <test
+ property="org.eclipse.jpt.jpa.core.isGenericMappingFile">
+ </test>
+ </adapt>
+ </iterate>
+ </with>
+ </visibleWhen>
+ </command>
</menuContribution>
@@ -362,6 +416,36 @@
</command>
</menuContribution>
+
+ <!-- contributions for any popup -->
+ <menuContribution
+ locationURI="popup:org.eclipse.ui.popup.any">
+ <command
+ commandId="org.eclipse.jpt.jpa.eclipselink.ui.upgradeToEclipseLinkMappingFile">
+ <visibleWhen>
+ <with
+ variable="selection">
+ <iterate
+ ifEmpty="false">
+ <adapt
+ type="org.eclipse.jpt.jpa.core.context.XmlFile">
+ <test
+ property="org.eclipse.jpt.jpa.core.isGenericMappingFile">
+ </test>
+ </adapt>
+ <adapt
+ type="org.eclipse.jpt.jpa.core.platform.JpaPlatformDescription">
+ <test
+ property="org.eclipse.jpt.jpa.core.jpaPlatformGroup"
+ value="eclipselink">
+ </test>
+ </adapt>
+ </iterate>
+ </with>
+ </visibleWhen>
+ </command>
+
+ </menuContribution>
</extension>
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/commands/UpgradeToEclipseLinkMappingFileHandler.java b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/commands/UpgradeToEclipseLinkMappingFileHandler.java
new file mode 100644
index 0000000..5132909
--- /dev/null
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/commands/UpgradeToEclipseLinkMappingFileHandler.java
@@ -0,0 +1,148 @@
+/*******************************************************************************
+ * 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.jpa.eclipselink.ui.internal.commands;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jpt.common.core.internal.utility.PlatformTools;
+import org.eclipse.jpt.common.core.resource.xml.JptXmlResource;
+import org.eclipse.jpt.jpa.core.JpaProject;
+import org.eclipse.jpt.jpa.core.context.XmlFile;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.EclipseLink;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.XmlEntityMappings;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v1_1.EclipseLink1_1;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v1_2.EclipseLink1_2;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_0.EclipseLink2_0;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_1.EclipseLink2_1;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_2.EclipseLink2_2;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_3.EclipseLink2_3;
+import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_4.EclipseLink2_4;
+import org.eclipse.jpt.jpa.eclipselink.ui.internal.plugin.JptJpaEclipseLinkUiPlugin;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+public class UpgradeToEclipseLinkMappingFileHandler extends AbstractHandler {
+
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
+
+ for (Object selectedObject : selection.toArray()) {
+ upgradeToEclipseLinkOrm(selectedObject);
+ }
+ return null;
+ }
+
+ protected void upgradeToEclipseLinkOrm(Object selectedObject) {
+ JptXmlResource xmlResource = PlatformTools.getAdapter(selectedObject, JptXmlResource.class);
+ if (xmlResource == null) {
+ XmlFile xmlFile = PlatformTools.getAdapter(selectedObject, XmlFile.class);
+ if (xmlFile != null) {
+ xmlResource = xmlFile.getXmlResource();
+ }
+ }
+ if (xmlResource == null) {
+ return;
+ }
+
+ JpaProject jpaProject = this.getJpaProject(xmlResource.getFile().getProject());
+ String fileLocation = xmlResource.getFile().getRawLocation().toOSString();
+ String newVersion = jpaProject.getJpaPlatform().getMostRecentSupportedResourceType(XmlEntityMappings.CONTENT_TYPE).getVersion();
+
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ try {
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ Document document = builder.parse(new File(fileLocation));
+ document.setXmlStandalone(true);
+ NodeList nodes = document.getElementsByTagName("entity-mappings"); //$NON-NLS-1$
+ if (nodes.getLength() > 0) {
+ // we know only one "entity-mappings" element exists in the mapping file
+ Node node = nodes.item(0);
+ NamedNodeMap attributes = node.getAttributes();
+ attributes.getNamedItem("version").setTextContent(newVersion); //$NON-NLS-1$
+ attributes.getNamedItem("xmlns").setTextContent(getNamespace()); //$NON-NLS-1$
+ attributes.getNamedItem("xsi:schemaLocation").setTextContent(buildSchemaLocationString(getNamespace(), getSchemaLocationForVersion(newVersion))); //$NON-NLS-1$
+ }
+
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ Transformer transformer = transformerFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+ transformer.transform(new DOMSource(document), new StreamResult(new File(fileLocation)));
+
+ // refresh the file to load the changes to the editor
+ xmlResource.getFile().refreshLocal(IResource.DEPTH_ZERO, null);
+ } catch (ParserConfigurationException pce) {
+ JptJpaEclipseLinkUiPlugin.instance().logError(pce);
+ } catch (SAXException sxe) {
+ JptJpaEclipseLinkUiPlugin.instance().logError(sxe);
+ } catch (IOException ioe) {
+ JptJpaEclipseLinkUiPlugin.instance().logError(ioe);
+ } catch (TransformerConfigurationException tce) {
+ JptJpaEclipseLinkUiPlugin.instance().logError(tce);
+ } catch (TransformerException te) {
+ JptJpaEclipseLinkUiPlugin.instance().logError(te);
+ } catch (CoreException ce) {
+ JptJpaEclipseLinkUiPlugin.instance().logError(ce);
+ }
+ }
+
+ private JpaProject getJpaProject(IProject project) {
+ return (JpaProject) project.getAdapter(JpaProject.class);
+ }
+
+ protected static String buildSchemaLocationString(String namespace, String schemaLocation) {
+ return namespace + ' ' + schemaLocation;
+ }
+
+ protected String getNamespace() {
+ return EclipseLink.SCHEMA_NAMESPACE;
+ }
+
+ protected String getSchemaLocationForVersion(String schemaVersion) {
+ return SCHEMA_LOCATIONS.get(schemaVersion);
+ }
+
+ private static HashMap<String, String> SCHEMA_LOCATIONS = buildSchemaLocations();
+
+ private static HashMap<String, String> buildSchemaLocations() {
+ HashMap<String, String> map = new HashMap<String, String>();
+ map.put(EclipseLink.SCHEMA_VERSION, EclipseLink.SCHEMA_LOCATION);
+ map.put(EclipseLink1_1.SCHEMA_VERSION, EclipseLink1_1.SCHEMA_LOCATION);
+ map.put(EclipseLink1_2.SCHEMA_VERSION, EclipseLink1_2.SCHEMA_LOCATION);
+ map.put(EclipseLink2_0.SCHEMA_VERSION, EclipseLink2_0.SCHEMA_LOCATION);
+ map.put(EclipseLink2_1.SCHEMA_VERSION, EclipseLink2_1.SCHEMA_LOCATION);
+ map.put(EclipseLink2_2.SCHEMA_VERSION, EclipseLink2_2.SCHEMA_LOCATION);
+ map.put(EclipseLink2_3.SCHEMA_VERSION, EclipseLink2_3.SCHEMA_LOCATION);
+ map.put(EclipseLink2_4.SCHEMA_VERSION, EclipseLink2_4.SCHEMA_LOCATION);
+ return map;
+ }
+
+} \ No newline at end of file