1 17 package org.eclipse.emf.importer.rose; 18 19 import java.io.File ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.core.runtime.Status; 30 31 import org.eclipse.emf.codegen.ecore.genmodel.GenModel; 32 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage; 33 import org.eclipse.emf.common.util.URI; 34 import org.eclipse.emf.ecore.EPackage; 35 import org.eclipse.emf.importer.ImporterPlugin; 36 import org.eclipse.emf.importer.ModelImporter; 37 import org.eclipse.emf.importer.rose.builder.RoseUtil; 38 import org.eclipse.emf.importer.rose.builder.UnitTreeNode; 39 import org.eclipse.emf.importer.util.ImporterUtil; 40 41 42 45 public class RoseImporter extends ModelImporter 46 { 47 protected Map pathMap; 48 protected boolean noQualify = false; 49 protected boolean unsettablePrimitive = false; 50 51 protected RoseUtil roseUtil; 52 protected UnitTreeNode unitTreeNode; 53 54 protected Map roseEPackageInformationMap; 55 56 public void dispose() 57 { 58 if (pathMap != null) 59 { 60 pathMap.clear(); 61 pathMap = null; 62 } 63 if (roseEPackageInformationMap != null) 64 { 65 roseEPackageInformationMap.clear(); 66 roseEPackageInformationMap = null; 67 } 68 69 super.dispose(); 70 } 71 72 public String getID() 73 { 74 return "org.eclipse.emf.importer.rose"; 75 } 76 77 public Map getPathMap() 78 { 79 if (pathMap == null) 80 { 81 pathMap = new HashMap (); 82 } 83 return pathMap; 84 } 85 86 public void setModelLocation(String location) 87 { 88 boolean isEqual = location == null ? 89 getModelLocation() == null 90 : location.equals(getModelLocation()); 91 if (!isEqual) 92 { 93 getPathMap().clear(); 94 } 95 super.setModelLocation(location); 96 } 97 98 public boolean isNoQualify() 99 { 100 return noQualify; 101 } 102 103 public void setNoQualify(boolean noQualify) 104 { 105 this.noQualify = noQualify; 106 } 107 108 public boolean isUnsettablePrimitive() 109 { 110 return unsettablePrimitive; 111 } 112 113 public void setUnsettablePrimitive(boolean unsettablePrimitive) 114 { 115 this.unsettablePrimitive = unsettablePrimitive; 116 } 117 118 public File computeRoseModelFile() 119 { 120 URI modelURI = getFirstModelLocationURI(true); 121 if (modelURI != null) 122 { 123 File modelFile = new File (modelURI.isFile() ? modelURI.toFileString() : modelURI.toString()); 124 if (modelFile.isFile()) 125 { 126 return modelFile; 127 } 128 } 129 return null; 130 } 131 132 public void clearEPackagesCollections() 133 { 134 super.clearEPackagesCollections(); 135 roseEPackageInformationMap = null; 136 } 137 138 public IStatus loadPathMap(IProgressMonitor progressMonitor) throws Exception 139 { 140 clearEPackagesCollections(); 141 142 IStatus status = null; 143 144 File roseModelFile = computeRoseModelFile(); 145 if (roseModelFile == null) 146 { 147 status = new Status(IStatus.ERROR, 148 ImporterPlugin.ID, ImporterUtil.ACTION_DIALOG_NONE | ImporterUtil.ACTION_MESSAGE_SET_ERROR, 149 RoseImporterPlugin.INSTANCE.getString("_UI_SpecifyAValidRoseModel_message"), null); 150 } 151 else 152 { 153 String roseModelAbsolutePath = roseModelFile.getAbsolutePath(); 154 155 progressMonitor.beginTask("", 2); 156 progressMonitor.subTask(RoseImporterPlugin.INSTANCE.getString("_UI_Loading_message", new Object []{ roseModelAbsolutePath })); 157 158 Map pathMap = getPathMap(); 159 for (;;) 160 { 161 roseUtil = createRoseUtil(); 162 roseUtil.getRoseEcoreBuilder().noQualify = noQualify; 163 roseUtil.getRoseEcoreBuilder().unsettablePrimitive = unsettablePrimitive; 164 165 roseUtil.getVariableToDirectoryMap().putAll(pathMap); 166 unitTreeNode = roseUtil.createRoseUnitTreeAndTable(roseModelAbsolutePath, null); 167 if (unitTreeNode == null) 168 { 169 status = new Status(IStatus.ERROR, 170 ImporterPlugin.ID, ImporterUtil.ACTION_DIALOG_NONE | ImporterUtil.ACTION_MESSAGE_SET_ERROR, 171 RoseImporterPlugin.INSTANCE.getString("_UI_SpecifyAValidRoseModel_message"), null); 172 } 173 else 174 { 175 boolean hasSymbolWithoutValue = adjustPathMap(roseUtil); 176 if (adjustKnownPathMapSymbols(roseModelAbsolutePath)) 177 { 178 continue; 179 } 180 if (!hasSymbolWithoutValue) 181 { 182 status = ImporterUtil.mergeStatus(status, roseUtil.getStatus()); 183 } 184 185 if (getGenModelFileName() == null) 186 { 187 String fileName = unitTreeNode.getNodes().size() != 1 188 ? roseModelFile.getName() : ((UnitTreeNode)unitTreeNode.getNodes().get(0)).getRoseFileName(); 189 190 int index = fileName.lastIndexOf(File.separatorChar); 191 if (index >= 0) 192 { 193 fileName = fileName.substring(index + 1); 194 } 195 index = fileName.lastIndexOf("."); 196 if (index >= 0) 197 { 198 fileName = fileName.substring(0, index); 199 } 200 setGenModelFileName(fileName + ".genmodel"); 201 } 202 } 203 break; 204 } 205 } 206 207 return status != null ? status : Status.OK_STATUS; 208 } 209 210 protected boolean adjustPathMap(RoseUtil roseUtil) 211 { 212 boolean hasSymbolWithoutValue = false; 213 Map currentPathMap = getPathMap(); 214 Map pathMap = new HashMap (); 215 for (Iterator i = roseUtil.getVariableToDirectoryMap().entrySet().iterator(); i.hasNext();) 216 { 217 Map.Entry entry = (Map.Entry )i.next(); 218 String symbol = (String )entry.getKey(); 219 if (symbol != null) 220 { 221 String value = (String )entry.getValue(); 222 if (value == null) 223 { 224 value = (String )currentPathMap.get(symbol); 225 } 226 pathMap.put(symbol, value); 227 hasSymbolWithoutValue = hasSymbolWithoutValue || (value == null); 228 } 229 } 230 currentPathMap.clear(); 231 currentPathMap.putAll(pathMap); 232 return hasSymbolWithoutValue; 233 } 234 235 protected boolean adjustKnownPathMapSymbols(String roseModelAbsolutePath) 236 { 237 return false; 238 } 239 240 247 public boolean hasRoseGenPackageProperties(EPackage ePackage) 248 { 249 return roseEPackageInformationMap != null && roseEPackageInformationMap.get(ePackage) != null; 250 } 251 252 protected IStatus doComputeEPackages(IProgressMonitor progressMonitor) throws Exception 253 { 254 progressMonitor.beginTask("", 2); 255 IStatus status = loadPathMap(progressMonitor); 256 if (status.isOK()) 257 { 258 if (getPathMap().values().contains(null) && !roseUtil.getStatus().isOK()) 259 { 260 status = new Status(IStatus.ERROR, 261 ImporterPlugin.ID, ImporterUtil.ACTION_DIALOG_NONE | ImporterUtil.ACTION_MESSAGE_SET_ERROR, 262 RoseImporterPlugin.INSTANCE.getString("_UI_SpecifyTheSymbolLocations_message"), null); 263 } 264 else 265 { 266 roseUtil.createExtent4RoseUnitTree(unitTreeNode); 267 roseUtil.processUnitTree(unitTreeNode); 268 roseEPackageInformationMap = roseUtil.getEPackageToInformationMap(); 269 status = ImporterUtil.createStatus(roseUtil.getStatus(), ImporterPlugin.ID, ImporterUtil.ACTION_MESSAGE_NONE); 270 271 for (Iterator i = roseUtil.getEPackageToInformationMap().entrySet().iterator(); i.hasNext();) 272 { 273 Map.Entry entry = (Map.Entry )i.next(); 274 List information = (List )entry.getValue(); 275 if (information != null) 276 { 277 EPackageInfo ePackageInfo = getEPackageInfo((EPackage)entry.getKey()); 278 ePackageInfo.setBasePackage((String )information.get(0)); 279 ePackageInfo.setPrefix((String )information.get(1)); 280 } 281 } 282 283 traverseEPackages(unitTreeNode); 284 } 285 } 286 progressMonitor.done(); 287 return status; 288 } 289 290 protected void traverseEPackages(UnitTreeNode subNode) 291 { 292 getEPackages().addAll(subNode.getExtent()); 293 294 for (Iterator i = subNode.getExtent().iterator(); i.hasNext();) 295 { 296 EPackage ePackage = (EPackage)i.next(); 297 EPackageInfo ePackageInfo = getEPackageInfo(ePackage); 298 299 String ecoreFileName = null; 300 String roseFileName = subNode.getRoseFileName(); 301 int indexOfSlash = roseFileName.lastIndexOf(File.separator); 302 303 GenPackage genPackage = getGenPackage(ePackage); 304 if (genPackage != null) 305 { 306 String ePackagePath = genPackage.getEcorePackage().eResource().getURI().lastSegment(); 307 ecoreFileName = URI.decode(ePackagePath); 308 } 309 else if (roseFileName.endsWith(".cat") && indexOfSlash != -1) 310 { 311 String baseName = roseFileName.substring(indexOfSlash + 1, roseFileName.length() - 4); 312 int indexOfDot = baseName.lastIndexOf("."); 313 if (indexOfDot != -1) 314 { 315 baseName = baseName.substring(indexOfDot + 1); 316 } 317 ecoreFileName = baseName + ".ecore"; 318 } 319 else if (subNode.getExtent().size() == 1) 320 { 321 ecoreFileName = subNode.getName() + ".ecore"; 322 } 323 else 324 { 325 ecoreFileName = ePackage.getName() + ".ecore"; 326 } 327 ePackageInfo.setEcoreFileName(ecoreFileName); 328 } 329 330 for (Iterator i = subNode.getNodes().iterator(); i.hasNext();) 331 { 332 UnitTreeNode childTree = (UnitTreeNode)i.next(); 333 traverseEPackages(childTree); 334 } 335 } 336 337 protected void adjustGenModel(IProgressMonitor progressMonitor) 338 { 339 super.adjustGenModel(progressMonitor); 340 341 GenModel genModel = getGenModel(); 342 String projectName = getModelProjectName(); 343 IPath genModelPath = getGenModelPath(); 344 URI modelURI = (URI)getModelLocationURIs().get(0); 345 URI genModelURI = getGenModel().eResource().getURI(); 346 347 genModel.getForeignModel().add(makeRelative(modelURI, genModelURI).toFileString()); 348 IPath projectLocation = getWorkspaceRoot().getProject(projectName).getLocation(); 349 if (projectLocation != null) 350 { 351 projectLocation = projectLocation.removeLastSegments(1); 352 } 353 else 354 { 355 projectLocation = getGenModelProjectLocation(); 356 } 357 358 for (Iterator i = getPathMap().entrySet().iterator(); i.hasNext();) 359 { 360 Map.Entry entry = (Map.Entry )i.next(); 361 String value = (String )entry.getValue(); 362 if (value != null) 363 { 364 genModel.getForeignModel().add(entry.getKey()); 365 if (new Path(value).equals(projectLocation)) 366 { 367 value = ".."; 368 for (int depth = genModelPath.segmentCount(); depth > 2; --depth) 369 { 370 value += "/.."; 371 } 372 genModel.getForeignModel().add(value); 373 } 374 else 375 { 376 genModel.getForeignModel().add(value); 377 } 378 } 379 } 380 } 381 382 protected void loadOriginalGenModel(URI genModelURI) 383 { 384 super.loadOriginalGenModel(genModelURI); 385 386 try 387 { 388 Iterator values = getOriginalGenModel().getForeignModel().iterator(); 389 if (values.hasNext()) 390 { 391 URI modelURI = null; 392 String modelFile = (String )values.next(); 393 modelFile = modelFile.replace('\\', '/'); 394 if (modelFile.endsWith(".mdl")) 395 { 396 URI genModelFileURI = createFileURI(getOriginalGenModelPath().toString()); 397 modelURI = makeAbsolute(URI.createFileURI(modelFile), genModelFileURI); 398 setModelLocation(modelURI.toString()); 399 400 while (values.hasNext()) 401 { 402 String variable = (String )values.next(); 403 String value = (String )values.next(); 404 getPathMap().put(variable, value); 405 } 406 } 407 } 408 } 409 catch (Exception exception) 410 { 411 RoseImporterPlugin.INSTANCE.log(exception); 412 } 413 } 414 415 protected RoseUtil createRoseUtil() 416 { 417 RoseUtil roseUtil = new RoseUtil(createResourceSet().getURIConverter()); 418 roseUtil.getRoseEcoreBuilder().noQualify = noQualify; 419 roseUtil.getRoseEcoreBuilder().unsettablePrimitive = unsettablePrimitive; 420 return roseUtil; 421 } 422 } | Popular Tags |