|
Added
Link Here
|
| 1 |
package org.eclipse.jpt.jpa.eclipselink.ui.internal.commands; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.HashMap; |
| 6 |
import javax.xml.parsers.DocumentBuilder; |
| 7 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 8 |
import javax.xml.parsers.ParserConfigurationException; |
| 9 |
import javax.xml.transform.OutputKeys; |
| 10 |
import javax.xml.transform.Transformer; |
| 11 |
import javax.xml.transform.TransformerConfigurationException; |
| 12 |
import javax.xml.transform.TransformerException; |
| 13 |
import javax.xml.transform.TransformerFactory; |
| 14 |
import javax.xml.transform.dom.DOMSource; |
| 15 |
import javax.xml.transform.stream.StreamResult; |
| 16 |
import org.eclipse.core.commands.AbstractHandler; |
| 17 |
import org.eclipse.core.commands.ExecutionEvent; |
| 18 |
import org.eclipse.core.commands.ExecutionException; |
| 19 |
import org.eclipse.core.resources.IProject; |
| 20 |
import org.eclipse.core.resources.IResource; |
| 21 |
import org.eclipse.core.runtime.CoreException; |
| 22 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 |
import org.eclipse.jpt.common.core.internal.utility.PlatformTools; |
| 24 |
import org.eclipse.jpt.jpa.core.JpaProject; |
| 25 |
import org.eclipse.jpt.jpa.core.context.XmlFile; |
| 26 |
import org.eclipse.jpt.jpa.core.resource.xml.JpaXmlResource; |
| 27 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.EclipseLink; |
| 28 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.XmlEntityMappings; |
| 29 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v1_1.EclipseLink1_1; |
| 30 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v1_2.EclipseLink1_2; |
| 31 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_0.EclipseLink2_0; |
| 32 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_1.EclipseLink2_1; |
| 33 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_2.EclipseLink2_2; |
| 34 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_3.EclipseLink2_3; |
| 35 |
import org.eclipse.jpt.jpa.eclipselink.core.resource.orm.v2_4.EclipseLink2_4; |
| 36 |
import org.eclipse.ui.handlers.HandlerUtil; |
| 37 |
import org.w3c.dom.Document; |
| 38 |
import org.w3c.dom.NamedNodeMap; |
| 39 |
import org.w3c.dom.Node; |
| 40 |
import org.w3c.dom.NodeList; |
| 41 |
import org.xml.sax.SAXException; |
| 42 |
|
| 43 |
public class UpgradeToEclipseLinkMappingFileHandler extends AbstractHandler { |
| 44 |
|
| 45 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
| 46 |
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event); |
| 47 |
|
| 48 |
for (Object selectedObject : selection.toArray()) { |
| 49 |
upgradeToEclipseLinkOrm(selectedObject); |
| 50 |
} |
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
protected void upgradeToEclipseLinkOrm(Object selectedObject) { |
| 55 |
JpaXmlResource xmlResource = PlatformTools.getAdapter(selectedObject, JpaXmlResource.class); |
| 56 |
if (xmlResource == null) { |
| 57 |
XmlFile xmlFile = PlatformTools.getAdapter(selectedObject, XmlFile.class); |
| 58 |
if (xmlFile != null) { |
| 59 |
xmlResource = xmlFile.getXmlResource(); |
| 60 |
} |
| 61 |
} |
| 62 |
if (xmlResource == null) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
try { |
| 67 |
JpaProject jpaProject = this.getJpaProject(xmlResource.getFile().getProject()); |
| 68 |
String fileLocation = xmlResource.getFile().getRawLocation().toOSString(); |
| 69 |
String newVersion = jpaProject.getJpaPlatform().getMostRecentSupportedResourceType(XmlEntityMappings.CONTENT_TYPE).getVersion(); |
| 70 |
|
| 71 |
|
| 72 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 73 |
DocumentBuilder builder = factory.newDocumentBuilder(); |
| 74 |
Document document = builder.parse(new File(fileLocation)); |
| 75 |
document.setXmlStandalone(true); |
| 76 |
NodeList nodes = document.getElementsByTagName("entity-mappings"); |
| 77 |
if (nodes.getLength() > 0) { |
| 78 |
// we know only one "entity-mappings" element exists in the mapping file |
| 79 |
Node node = nodes.item(0); |
| 80 |
NamedNodeMap attributes = node.getAttributes(); |
| 81 |
attributes.getNamedItem("version").setTextContent(newVersion); |
| 82 |
attributes.getNamedItem("xmlns").setTextContent(getNamespace()); |
| 83 |
attributes.getNamedItem("xsi:schemaLocation").setTextContent(buildSchemaLocationString(getNamespace(), getSchemaLocationForVersion(newVersion))); |
| 84 |
} |
| 85 |
|
| 86 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 87 |
Transformer transformer = transformerFactory.newTransformer(); |
| 88 |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 89 |
transformer.transform(new DOMSource(document), new StreamResult(new File(fileLocation))); |
| 90 |
|
| 91 |
// refresh the file to load the changes to the editor |
| 92 |
xmlResource.getFile().refreshLocal(IResource.DEPTH_ZERO, null); |
| 93 |
} catch (ParserConfigurationException pce) { |
| 94 |
pce.printStackTrace(); |
| 95 |
} catch (SAXException sxe) { |
| 96 |
sxe.printStackTrace(); |
| 97 |
} catch (IOException ioe) { |
| 98 |
ioe.printStackTrace(); |
| 99 |
} catch (TransformerConfigurationException tce) { |
| 100 |
tce.printStackTrace(); |
| 101 |
} catch (TransformerException te) { |
| 102 |
te.printStackTrace(); |
| 103 |
} catch (CoreException ce) { |
| 104 |
ce.printStackTrace(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private JpaProject getJpaProject(IProject project) { |
| 109 |
return (JpaProject) project.getAdapter(JpaProject.class); |
| 110 |
} |
| 111 |
|
| 112 |
protected static String buildSchemaLocationString(String namespace, String schemaLocation) { |
| 113 |
return namespace + ' ' + schemaLocation; |
| 114 |
} |
| 115 |
|
| 116 |
protected String getNamespace() { |
| 117 |
return EclipseLink.SCHEMA_NAMESPACE; |
| 118 |
} |
| 119 |
|
| 120 |
protected String getSchemaLocationForVersion(String schemaVersion) { |
| 121 |
return SCHEMA_LOCATIONS.get(schemaVersion); |
| 122 |
} |
| 123 |
|
| 124 |
private static HashMap<String, String> SCHEMA_LOCATIONS = buildSchemaLocations(); |
| 125 |
|
| 126 |
private static HashMap<String, String> buildSchemaLocations() { |
| 127 |
HashMap<String, String> map = new HashMap<String, String>(); |
| 128 |
map.put(EclipseLink.SCHEMA_VERSION, EclipseLink.SCHEMA_LOCATION); |
| 129 |
map.put(EclipseLink1_1.SCHEMA_VERSION, EclipseLink1_1.SCHEMA_LOCATION); |
| 130 |
map.put(EclipseLink1_2.SCHEMA_VERSION, EclipseLink1_2.SCHEMA_LOCATION); |
| 131 |
map.put(EclipseLink2_0.SCHEMA_VERSION, EclipseLink2_0.SCHEMA_LOCATION); |
| 132 |
map.put(EclipseLink2_1.SCHEMA_VERSION, EclipseLink2_1.SCHEMA_LOCATION); |
| 133 |
map.put(EclipseLink2_2.SCHEMA_VERSION, EclipseLink2_2.SCHEMA_LOCATION); |
| 134 |
map.put(EclipseLink2_3.SCHEMA_VERSION, EclipseLink2_3.SCHEMA_LOCATION); |
| 135 |
map.put(EclipseLink2_4.SCHEMA_VERSION, EclipseLink2_4.SCHEMA_LOCATION); |
| 136 |
return map; |
| 137 |
} |
| 138 |
|
| 139 |
} |