Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f38f9d6817f3a89d3403e9ebed700024f622608 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
 * 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 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.eclipselink.core.resource.orm.EclipseLink;
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.XmlEntityMappings;
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;

/**
 * Handler used to upgrade an orm.xml file to an eclipselink orm.xml file
 * when the selected object adapts to a <code>JptXmlResource</code>.
 * See org.eclipse.jpt.jpa.eclipselink.ui/plugin.xml
 */
public class UpgradeToEclipseLinkMappingFileXmlResourceHandler 
	extends AbstractHandler
{

	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection selection 	= (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);

		for (Object selectedObject : selection.toArray()) {
			this.upgradeToEclipseLinkMappingFile(selectedObject);
		}
		return null;
	}

	protected void upgradeToEclipseLinkMappingFile(Object selectedObject) {
		upgradeToEclipseLinkMappingFile(this.adaptSelection(selectedObject));
	}

	protected JptXmlResource adaptSelection(Object selectedObject) {
		return PlatformTools.getAdapter(selectedObject, JptXmlResource.class);
	}

	protected static void upgradeToEclipseLinkMappingFile(JptXmlResource xmlResource) {
		JpaProject jpaProject = 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 static JpaProject getJpaProject(IProject project) {
		return (JpaProject) project.getAdapter(JpaProject.class);
	}

	protected static String buildSchemaLocationString(String namespace, String schemaLocation) {
		return namespace + ' ' + schemaLocation;
	}

	protected static String getNamespace() {
		return EclipseLink.SCHEMA_NAMESPACE;
	}

	protected static String getSchemaLocationForVersion(String schemaVersion) {
		String schemaLocation =  XmlEntityMappings.SCHEMA_LOCATIONS.get(schemaVersion);
		if (schemaLocation == null) {
			JptJpaEclipseLinkUiPlugin.instance().logError(new Throwable("No schema location defined for version: " + schemaVersion)); //$NON-NLS-1$
		}
		return schemaLocation;
	}
}

Back to the top