1 17 package org.eclipse.emf.mapping.ecore2xml.action; 18 19 20 import java.io.IOException ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.Iterator ; 23 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.emf.common.util.TreeIterator; 29 import org.eclipse.emf.common.util.URI; 30 import org.eclipse.emf.ecore.EClassifier; 31 import org.eclipse.emf.ecore.ENamedElement; 32 import org.eclipse.emf.ecore.EObject; 33 import org.eclipse.emf.ecore.EPackage; 34 import org.eclipse.emf.ecore.EStructuralFeature; 35 import org.eclipse.emf.ecore.resource.Resource; 36 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 37 import org.eclipse.emf.ecore.util.EcoreSwitch; 38 import org.eclipse.emf.ecore.util.EcoreUtil; 39 import org.eclipse.emf.ecore.util.ExtendedMetaData; 40 import org.eclipse.emf.ecore.xmi.XMLResource; 41 import org.eclipse.emf.mapping.Mapping; 42 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcoreMappingRoot; 43 import org.eclipse.emf.mapping.ecore2ecore.Ecore2EcorePackage; 44 import org.eclipse.emf.mapping.ecore2xml.Ecore2XMLFactory; 45 import org.eclipse.emf.mapping.ecore2xml.ui.Ecore2XMLUIPlugin; 46 import org.eclipse.emf.mapping.ecore2xml.util.Ecore2XMLResource; 47 import org.eclipse.jface.action.IAction; 48 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 49 import org.eclipse.jface.viewers.ISelection; 50 import org.eclipse.jface.viewers.IStructuredSelection; 51 import org.eclipse.jface.viewers.StructuredSelection; 52 import org.eclipse.ui.IWorkbenchPage; 53 import org.eclipse.ui.IWorkbenchPart; 54 import org.eclipse.ui.IWorkbenchWindow; 55 import org.eclipse.ui.PartInitException; 56 import org.eclipse.ui.PlatformUI; 57 import org.eclipse.ui.actions.ActionDelegate; 58 import org.eclipse.ui.actions.WorkspaceModifyOperation; 59 import org.eclipse.ui.part.FileEditorInput; 60 import org.eclipse.ui.part.ISetSelectionTarget; 61 62 63 66 public class GenerateEcore2XMLActionDelegate extends ActionDelegate 67 { 68 69 private static final String ECORE2ECORE_FILE_EXTENSION = "ecore2ecore"; 70 71 protected Ecore2EcoreMappingRoot getMappingRoot(IStructuredSelection structuredSelection) 72 { 73 74 if (IFile.class.isInstance(structuredSelection.getFirstElement())) 75 { 76 IFile file = (IFile)structuredSelection.getFirstElement(); 77 78 if (ECORE2ECORE_FILE_EXTENSION.equals(file.getFullPath().getFileExtension())) 79 { 80 return (Ecore2EcoreMappingRoot)EcoreUtil.getObjectByType(new ResourceSetImpl().getResource( 81 URI.createPlatformResourceURI(file.getFullPath().toString(), true), 82 true).getContents(), Ecore2EcorePackage.eINSTANCE.getEcore2EcoreMappingRoot()); 83 } 84 } 85 86 return null; 87 } 88 89 protected static XMLResource.XMLInfo createXMLInfo(EObject eObject) 90 { 91 return (XMLResource.XMLInfo)new EcoreSwitch() 92 { 93 public Object caseEPackage(EPackage ePackage) 94 { 95 XMLResource.XMLInfo xmlInfo = Ecore2XMLFactory.eINSTANCE.createXMLInfo(); 96 97 xmlInfo.setName(ePackage.getName()); 98 xmlInfo.setTargetNamespace(ExtendedMetaData.INSTANCE.getNamespace(ePackage)); 99 100 return xmlInfo; 101 } 102 103 public Object caseEClassifier(EClassifier eClassifier) 104 { 105 XMLResource.XMLInfo xmlInfo = Ecore2XMLFactory.eINSTANCE.createXMLInfo(); 106 107 xmlInfo.setName(ExtendedMetaData.INSTANCE.getName(eClassifier)); 108 xmlInfo.setTargetNamespace(ExtendedMetaData.INSTANCE.getNamespace(eClassifier)); 109 110 return xmlInfo; 111 } 112 113 public Object caseEStructuralFeature(EStructuralFeature eStructuralFeature) 114 { 115 XMLResource.XMLInfo xmlInfo = Ecore2XMLFactory.eINSTANCE.createXMLInfo(); 116 117 xmlInfo.setName(ExtendedMetaData.INSTANCE.getName(eStructuralFeature)); 118 xmlInfo.setTargetNamespace(ExtendedMetaData.INSTANCE.getNamespace(eStructuralFeature)); 119 120 switch (ExtendedMetaData.INSTANCE.getFeatureKind(eStructuralFeature)) 121 { 122 case ExtendedMetaData.ATTRIBUTE_FEATURE: 123 xmlInfo.setXMLRepresentation(XMLResource.XMLInfo.ATTRIBUTE); 124 break; 125 case ExtendedMetaData.ELEMENT_FEATURE: 126 xmlInfo.setXMLRepresentation(XMLResource.XMLInfo.ELEMENT); 127 break; 128 } 129 130 return xmlInfo; 131 } 132 }.doSwitch(eObject); 133 } 134 135 protected static XMLResource.XMLMap createXMLMap(Ecore2EcoreMappingRoot mappingRoot) 136 { 137 XMLResource.XMLMap xmlMap = Ecore2XMLFactory.eINSTANCE.createXMLMap(); 138 139 for (TreeIterator mappings = mappingRoot.treeIterator(); mappings.hasNext();) 140 { 141 Mapping mapping = (Mapping)mappings.next(); 142 143 if (!mapping.getInputs().isEmpty()) 144 { 145 EObject input = (EObject)mapping.getInputs().get(0); 146 147 for (Iterator outputs = mapping.getOutputs().iterator(); outputs.hasNext();) 148 { 149 ENamedElement output = (ENamedElement)outputs.next(); 150 XMLResource.XMLInfo xmlInfo = createXMLInfo(input); 151 if (xmlInfo != null) { 152 xmlMap.add(output, xmlInfo); 153 } 154 } 155 } 156 } 157 158 return xmlMap; 159 } 160 161 protected IFile getFile(Resource resource) 162 { 163 URI uri = resource.getURI(); 164 uri = resource.getResourceSet().getURIConverter().normalize(uri); 165 String scheme = uri.scheme(); 166 if ("platform".equals(scheme) && uri.segmentCount() > 1 && "resource".equals(uri.segment(0))) 167 { 168 StringBuffer platformResourcePath = new StringBuffer (); 169 for (int j = 1, size = uri.segmentCount(); j < size; ++j) 170 { 171 platformResourcePath.append('/'); 172 platformResourcePath.append(uri.segment(j)); 173 } 174 return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString())); 175 } 176 177 return null; 178 } 179 180 185 public void run(IAction action) 186 { 187 final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 188 189 try 190 { 191 new ProgressMonitorDialog(workbenchWindow.getShell()).run(false, true, new WorkspaceModifyOperation() 192 { 193 194 protected void execute(IProgressMonitor progressMonitor) 195 { 196 try 197 { 198 progressMonitor.beginTask("", IProgressMonitor.UNKNOWN); 199 200 Ecore2EcoreMappingRoot mappingRoot = getMappingRoot((IStructuredSelection)workbenchWindow.getSelectionService().getSelection()); 201 202 if (mappingRoot != null) 203 { 204 Resource ecore2ecoreResource = mappingRoot.eResource(); 205 206 URI ecore2xmlURI = ecore2ecoreResource.getURI().trimFileExtension().appendFileExtension(Ecore2XMLResource.FILE_EXTENSION); 207 Resource ecore2xmlResource = ecore2ecoreResource.getResourceSet().createResource(ecore2xmlURI); 208 209 ecore2xmlResource.getContents().add(createXMLMap(mappingRoot)); 210 211 try 212 { 213 ecore2xmlResource.save(null); 214 215 IFile file = getFile(ecore2xmlResource); 216 IWorkbenchPage workbenchPage = workbenchWindow.getActivePage(); 217 218 final IWorkbenchPart activePart = workbenchPage.getActivePart(); 219 if (activePart instanceof ISetSelectionTarget) 220 { 221 final ISelection targetSelection = new StructuredSelection(file); 222 workbenchWindow.getShell().getDisplay().asyncExec(new Runnable () 223 { 224 public void run() 225 { 226 ((ISetSelectionTarget)activePart).selectReveal(targetSelection); 227 } 228 }); 229 } 230 231 try { 232 workbenchPage.openEditor(new FileEditorInput(file), workbenchWindow.getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId()); 233 } catch (PartInitException pie) { 234 Ecore2XMLUIPlugin.INSTANCE.log(pie); 235 } 236 } 237 catch (IOException ioe) 238 { 239 Ecore2XMLUIPlugin.INSTANCE.log(ioe); 240 } 241 } 242 } 243 finally 244 { 245 progressMonitor.done(); 246 } 247 } 248 }); 249 } 250 catch (InterruptedException ie) 251 { 252 } 254 catch (InvocationTargetException ite) 255 { 256 Ecore2XMLUIPlugin.INSTANCE.log(ite); 257 } 258 } 259 260 266 public void selectionChanged(IAction action, ISelection selection) 267 { 268 action.setEnabled(IStructuredSelection.class.isInstance(selection) && getMappingRoot((IStructuredSelection)selection) != null); 269 } 270 271 } | Popular Tags |