Skip to content

Package: EcoreGenModelLinkerImpl

EcoreGenModelLinkerImpl

nameinstructionbranchcomplexitylinemethod
EcoreGenModelLinkerImpl()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
adjustEPackages()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
adjustModelImporter()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
adjustModelImporterAfterPrepare()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
computeEPackages()
M: 7 C: 8
53%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 3
60%
M: 0 C: 1
100%
createGenModel()
M: 7 C: 14
67%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 5
83%
M: 0 C: 1
100%
generateGenModel(String, String, String)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getPathFromPathString(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
handleGenModelPath(IPath)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
handleQualifiedEPackageName(EPackage)
M: 16 C: 11
41%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 4
57%
M: 0 C: 1
100%
traverseEPackages(List)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * David Soto Setzke - initial API and implementation, implementation based on
13: * org.eclipse.emf.importer.ecore.EcoreImporterApplication
14: ******************************************************************************/
15: /**
16: *
17: * Copyright (c) 2005-2007 IBM Corporation and others.
18: * All rights reserved. This program and the accompanying materials
19: * are made available under the terms of the Eclipse Public License 2.0
20: * which accompanies this distribution, and is available at
21: * https://www.eclipse.org/legal/epl-2.0/
22: *
23: * SPDX-License-Identifier: EPL-2.0
24: *
25: * Contributors:
26: * IBM - Initial API and implementation
27: *
28: */
29: package org.eclipse.emf.ecp.ecore.editor.internal;
30:
31: import java.io.File;
32: import java.util.List;
33:
34: import org.eclipse.core.runtime.IPath;
35: import org.eclipse.core.runtime.NullProgressMonitor;
36: import org.eclipse.core.runtime.Path;
37: import org.eclipse.emf.codegen.ecore.genmodel.GenJDKLevel;
38: import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
39: import org.eclipse.emf.codegen.util.CodeGenUtil.EclipseUtil;
40: import org.eclipse.emf.common.util.Monitor;
41: import org.eclipse.emf.common.util.URI;
42: import org.eclipse.emf.ecore.EPackage;
43: import org.eclipse.emf.ecp.ecore.editor.IEcoreGenModelLinker;
44: import org.eclipse.emf.ecp.ecore.editor.util.EcoreGenException;
45: import org.eclipse.emf.importer.ModelImporter;
46: import org.eclipse.emf.importer.ecore.EcoreImporter;
47:
48: /**
49: * A class which creates a new ecore file and a new genmodel file and
50: * links the two of them.
51: */
52: public class EcoreGenModelLinkerImpl implements IEcoreGenModelLinker {
53:
54:         private final Monitor monitor = EclipseUtil.createMonitor(
55:                 new NullProgressMonitor(), 1);
56:         private IPath modelProjectLocationPath;
57:         private IPath modelFragmentPath;
58:         private IPath genModelPath;
59:         private String modelLocation;
60:         private final ModelImporter modelImporter = new EcoreImporter();
61:         private final IPath defaultSrcPath = new Path("src");
62:
63:         /**
64:          * Creates a new ecore file and a new genmodel file and links
65:          * the two of them.
66:          *
67:          * @param ecorePath The path (absolute) of the new ecore file
68:          * @param genModelPathParam The path (relative) of the new genmodel file
69:          * @param modelProjectPath The path (relative) of the model project
70:          * @throws EcoreGenException in case generating the genmodel fails
71:          */
72:         @Override
73:         public void generateGenModel(String ecorePath, String genModelPathParam,
74:                 String modelProjectPath) throws EcoreGenException {
75:
76:                 modelProjectLocationPath = getPathFromPathString(modelProjectPath);
77:                 modelFragmentPath = defaultSrcPath;
78:                 genModelPath = getPathFromPathString(genModelPathParam);
79:                 modelLocation = ecorePath;
80:
81:                 adjustModelImporter();
82:                 computeEPackages();
83:                 adjustEPackages();
84:                 createGenModel();
85:         }
86:
87:         private IPath getPathFromPathString(String pathString) {
88:                 return new Path(new File(pathString).getAbsolutePath());
89:         }
90:
91:         private void adjustModelImporter() {
92:                 modelImporter.setUsePlatformURI(false);
93:                 modelImporter.setGenModelProjectLocation(modelProjectLocationPath);
94:                 modelImporter.setModelPluginDirectory(modelProjectLocationPath + "/./"
95:                         + modelFragmentPath + "/.");
96:                 handleGenModelPath(genModelPath);
97:                 final File modelFile = new File(modelLocation);
98:                 final URI modelFileURI = URI.createFileURI(modelFile.getPath());
99:                 modelImporter.setModelLocation(modelFileURI.toString());
100:         }
101:
102:         private void handleGenModelPath(IPath genModelPath) {
103:                 modelImporter.setGenModelContainerPath(genModelPath
104:                         .removeLastSegments(1));
105:                 modelImporter.setGenModelFileName(genModelPath.lastSegment());
106:         }
107:
108:         private void computeEPackages() throws EcoreGenException {
109:                 try {
110:                         modelImporter.computeEPackages(monitor);
111:                         // BEGIN SUPRESS CATCH EXCEPTION
112:                 } catch (final Exception e) {
113:                         // END SUPRESS CATCH EXCEPTION
114:                         throw new EcoreGenException("The packages could not be computed by the model importer. "
115:                                 + "This might lead to unexpected results.", e);
116:                 }
117:         }
118:
119:         private void adjustEPackages() {
120:                 final List<EPackage> ePackages = modelImporter.getEPackages();
121:                 traverseEPackages(ePackages);
122:                 modelImporter.adjustEPackages(monitor);
123:         }
124:
125:         private void createGenModel() throws EcoreGenException {
126:                 modelImporter.prepareGenModelAndEPackages(monitor);
127:                 adjustModelImporterAfterPrepare();
128:                 try {
129:                         modelImporter.saveGenModelAndEPackages(monitor);
130:                         // BEGIN SUPRESS CATCH EXCEPTION
131:                 } catch (final Exception e) {
132:                         // END SUPRESS CATCH EXCEPTION
133:                         throw new EcoreGenException("Gen model and packages could not be saved!", e);
134:                 }
135:         }
136:
137:         private void adjustModelImporterAfterPrepare() {
138:                 final GenModel genModel = modelImporter.getGenModel();
139:                 genModel.setComplianceLevel(GenJDKLevel.JDK50_LITERAL);
140:                 genModel.setImportOrganizing(true);
141:                 genModel.setOperationReflection(true);
142:         }
143:
144:         private void traverseEPackages(List<EPackage> ePackages) {
145:•                for (final EPackage ePackage : ePackages) {
146:                         handleQualifiedEPackageName(ePackage);
147:                         traverseEPackages(ePackage.getESubpackages());
148:                 }
149:         }
150:
151:         private void handleQualifiedEPackageName(EPackage ePackage) {
152:                 final String packageName = ePackage.getName();
153:                 final int index = packageName.lastIndexOf(".");
154:•                if (index != -1) {
155:                         modelImporter.getEPackageImportInfo(ePackage).setBasePackage(
156:                                 packageName.substring(0, index));
157:                         ePackage.setName(packageName.substring(index + 1));
158:                 }
159:         }
160:
161: }