Skip to content

Package: EdaptViewModelMigrator$1

EdaptViewModelMigrator$1

nameinstructionbranchcomplexitylinemethod
compare(String, String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2014 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: * Lucas - initial API and implementation
13: * Johannes Faltermeier - initial API and implementation
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.edapt;
16:
17: import java.io.File;
18: import java.io.FileNotFoundException;
19: import java.io.FileReader;
20: import java.io.IOException;
21: import java.io.PrintWriter;
22: import java.util.ArrayList;
23: import java.util.Collections;
24: import java.util.Comparator;
25: import java.util.Date;
26: import java.util.Iterator;
27: import java.util.LinkedHashMap;
28: import java.util.LinkedHashSet;
29: import java.util.List;
30: import java.util.Map;
31: import java.util.Scanner;
32: import java.util.Set;
33: import java.util.TreeSet;
34:
35: import org.eclipse.core.runtime.IConfigurationElement;
36: import org.eclipse.core.runtime.IContributor;
37: import org.eclipse.core.runtime.IExtensionRegistry;
38: import org.eclipse.core.runtime.NullProgressMonitor;
39: import org.eclipse.core.runtime.Platform;
40: import org.eclipse.emf.common.util.URI;
41: import org.eclipse.emf.ecore.EPackage;
42: import org.eclipse.emf.ecore.EPackage.Registry;
43: import org.eclipse.emf.ecore.resource.Resource;
44: import org.eclipse.emf.ecore.resource.ResourceSet;
45: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
46: import org.eclipse.emf.ecore.xmi.XMLResource;
47: import org.eclipse.emf.ecp.spi.view.migrator.NameSpaceHandler;
48: import org.eclipse.emf.ecp.spi.view.migrator.SAXUtil;
49: import org.eclipse.emf.ecp.spi.view.migrator.string.StringViewModelMigrator;
50: import org.eclipse.emf.ecp.spi.view.migrator.string.StringViewModelMigratorUtil;
51: import org.eclipse.emf.ecp.view.migrator.ViewModelMigrationException;
52: import org.eclipse.emf.ecp.view.migrator.ViewModelMigrator;
53: import org.eclipse.emf.ecp.view.spi.model.util.VViewResourceFactoryImpl;
54: import org.eclipse.emf.edapt.common.IResourceSetFactory;
55: import org.eclipse.emf.edapt.migration.MigrationException;
56: import org.eclipse.emf.edapt.migration.execution.Migrator;
57: import org.eclipse.emf.edapt.migration.execution.MigratorRegistry;
58: import org.eclipse.emf.edapt.spi.history.Change;
59: import org.eclipse.emf.edapt.spi.history.History;
60: import org.eclipse.emf.edapt.spi.history.HistoryFactory;
61: import org.eclipse.emf.edapt.spi.history.MigrationChange;
62: import org.eclipse.emf.edapt.spi.history.Release;
63: import org.eclipse.emf.edapt.spi.migration.MigrationPlugin;
64: import org.osgi.framework.Bundle;
65: import org.xml.sax.Attributes;
66: import org.xml.sax.SAXException;
67: import org.xml.sax.helpers.DefaultHandler;
68:
69: /**
70: * A {@link ViewModelMigrator} using edapt.
71: *
72: * @author Lucas
73: * @author jfaltermeier
74: *
75: */
76: public class EdaptViewModelMigrator implements ViewModelMigrator, StringViewModelMigrator {
77:
78:         private static final String ECORE_NS_URI = "http://www.eclipse.org/emf/2002/Ecore"; //$NON-NLS-1$
79:
80:         private static final Comparator<String> RELEASE_COMPERATOR = new Comparator<String>() {
81:                 @Override
82:                 public int compare(String left, String right) {
83:                         return Integer.valueOf(left).compareTo(Integer.valueOf(right));
84:                 }
85:         };
86:
87:         /**
88:          *
89:          * {@inheritDoc}
90:          *
91:          * @see org.eclipse.emf.ecp.spi.view.migrator.string.StringViewModelMigrator#checkMigration(java.lang.String)
92:          * @since 1.8
93:          */
94:         @Override
95:         public boolean checkMigration(String serializedViewModel) {
96:                 return checkMigration(StringViewModelMigratorUtil.getNamespaceURIs(serializedViewModel));
97:         }
98:
99:         @Override
100:         public boolean checkMigration(final URI resourceURI) {
101:                 return checkMigration(getNamespaceURIs(resourceURI));
102:         }
103:
104:         private boolean checkMigration(final List<String> nsUris) {
105:                 boolean allReleasesAreLatest = true;
106:                 final List<Release> releases = new ArrayList<Release>();
107:                 for (final String nsUri : nsUris) {
108:                         final Migrator migrator = MigratorRegistry.getInstance()
109:                                 .getMigrator(nsUri);
110:                         if (migrator == null) {
111:                                 // no migrator registered. assume all is fine
112:                                 continue;
113:                         }
114:                         final Release nsRelease = getReleaseFromMigrator(nsUri, migrator);
115:                         releases.add(nsRelease);
116:                         if (!migrator.getLatestRelease().equals(nsRelease)) {
117:                                 allReleasesAreLatest = false;
118:                                 break;
119:                         }
120:                 }
121:                 return allReleasesAreLatest;
122:         }
123:
124:         /**
125:          *
126:          * {@inheritDoc}
127:          *
128:          * @see org.eclipse.emf.ecp.spi.view.migrator.string.StringViewModelMigrator#performMigration(java.lang.String)
129:          * @since 1.8
130:          */
131:         @Override
132:         public String performMigration(String serializedViewModel) throws ViewModelMigrationException {
133:                 PrintWriter printWriter = null;
134:                 File tempViewModelFile = null;
135:                 Scanner scanner = null;
136:                 try {
137:                         tempViewModelFile = File.createTempFile("view", ".view"); //$NON-NLS-1$//$NON-NLS-2$
138:                         tempViewModelFile.deleteOnExit();
139:                         printWriter = new PrintWriter(tempViewModelFile);
140:                         printWriter.print(serializedViewModel);
141:                         printWriter.flush();
142:                         printWriter.close();
143:                         performMigration(URI.createFileURI(tempViewModelFile.getAbsolutePath()));
144:                         scanner = new Scanner(tempViewModelFile, "UTF-8"); //$NON-NLS-1$
145:                         return scanner.useDelimiter("\\A").next(); //$NON-NLS-1$
146:                 } catch (final IOException ex) {
147:                         throw new ViewModelMigrationException(ex);
148:                 } finally {
149:                         if (printWriter != null) {
150:                                 printWriter.close();
151:                         }
152:                         if (scanner != null) {
153:                                 scanner.close();
154:                         }
155:                         if (tempViewModelFile != null) {
156:                                 tempViewModelFile.delete();
157:                         }
158:                 }
159:         }
160:
161:         @Override
162:         public void performMigration(final URI resourceURI) throws ViewModelMigrationException {
163:                 final History history = HistoryFactory.eINSTANCE.createHistory();
164:                 final Release sourceRelease = HistoryFactory.eINSTANCE.createRelease();
165:                 final List<Release> targetReleases = new ArrayList<Release>();
166:                 /*
167:                  * Analyse the domain model of the view. Generate a history for ecores which have no registered history and add
168:                  * them to source release. Mapping contains information about which uris from the domain have histories and
169:                  * which have no history.
170:                  */
171:                 final NSURIMapping domainMapping = addChangesForDomainModel(resourceURI, sourceRelease);
172:
173:                 /* Get NS URIs with a registered history (domain and view model) */
174:                 final List<String> nsUris = getNamespaceURIsWithHistory(resourceURI, domainMapping);
175:
176:                 /* Combine the changes from the histories in one big history */
177:                 orderHistoriesAndCombineChanges(sourceRelease, targetReleases, nsUris);
178:
179:                 /* add releases to history */
180:                 history.getReleases().add(sourceRelease);
181:                 history.getReleases().addAll(targetReleases);
182:                 history.getReleases().add(HistoryFactory.eINSTANCE.createRelease()); // expected by history editor
183:
184:                 try {
185:                         /* Save history file */
186:                         final URI uri = saveHistoryFile(history);
187:
188:                         /* Trigger the migration */
189:                         final Migrator migrator = new Migrator(uri, new CustomMigrationClassLoader());
190:                         migrator.setResourceSetFactory(new IResourceSetFactory() {
191:                                 @Override
192:                                 public ResourceSet createResourceSet() {
193:                                         final ResourceSetImpl resourceSet = new ResourceSetImpl();
194:                                         resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
195:                                                 Resource.Factory.Registry.DEFAULT_EXTENSION, new VViewResourceFactoryImpl());
196:                                         return resourceSet;
197:                                 }
198:                         });
199:                         final Release release = migrator.getRelease(0);
200:                         migrator.migrateAndSave(Collections.singletonList(resourceURI), release, null,
201:                                 new NullProgressMonitor());
202:
203:                 } catch (final IOException ex) {
204:                         throw new ViewModelMigrationException(ex);
205:                 } catch (final MigrationException ex) {
206:                         throw new ViewModelMigrationException(ex);
207:                 } finally {
208:                         reregisterExtensionMigrators();
209:                 }
210:         }
211:
212:         /**
213:          * @param history
214:          * @return
215:          * @throws IOException
216:          */
217:         private URI saveHistoryFile(final History history) throws IOException {
218:                 final File historyFile = File.createTempFile("EMFFormsMigration", ".history"); //$NON-NLS-1$//$NON-NLS-2$
219:                 historyFile.deleteOnExit();
220:                 final URI uri = URI.createFileURI(historyFile.getAbsolutePath());
221:                 final Resource resource = new ResourceSetImpl().createResource(uri);
222:                 resource.getContents().add(history);
223:                 resource.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
224:                 return uri;
225:         }
226:
227:         /**
228:          * @param history
229:          * @param sourceRelease
230:          * @param targetReleases
231:          * @param nsUris
232:          */
233:         private void orderHistoriesAndCombineChanges(final Release sourceRelease,
234:                 final List<Release> targetReleases, final List<String> nsUris) {
235:                 /* build a dependency tree for all view models with a history */
236:                 final PackageDependencyGraph viewModelPackageGraph = new PackageDependencyGraph();
237:                 final Map<String, Migrator> nsURIToMigratorMap = new LinkedHashMap<String, Migrator>();
238:                 final Map<String, String> latestToCurrentNSURIMap = new LinkedHashMap<String, String>();
239:                 for (final String nsUri : nsUris) {
240:                         final org.eclipse.emf.edapt.migration.execution.Migrator migrator = MigratorRegistry.getInstance()
241:                                 .getMigrator(nsUri);
242:                         nsURIToMigratorMap.put(nsUri, migrator);
243:                         /* get available uri from history */
244:                         for (final String migratorNSURI : migrator.getNsURIs()) {
245:                                 if (Registry.INSTANCE.containsKey(migratorNSURI)) {
246:                                         viewModelPackageGraph.addPackage(migratorNSURI);
247:                                         if (latestToCurrentNSURIMap.containsKey(migratorNSURI)) {
248:                                                 final String string = latestToCurrentNSURIMap.get(migratorNSURI);
249:                                                 final String olderNSURI = string.compareTo(nsUri) < 0 ? string : nsUri;
250:                                                 latestToCurrentNSURIMap.put(migratorNSURI, olderNSURI);
251:                                         } else {
252:                                                 latestToCurrentNSURIMap.put(migratorNSURI, nsUri);
253:                                         }
254:                                         break;
255:                                 }
256:                         }
257:                 }
258:
259:                 /* using the dependency tree, combine changes from all registered histories based on the release label. */
260:                 final Map<String, List<Change>> sourceReleaseNameToChangesMap = new LinkedHashMap<String, List<Change>>();
261:                 final Map<String, List<Change>> targetReleaseNameToChangesMap = new LinkedHashMap<String, List<Change>>();
262:                 final Iterator<Set<String>> viewModelPackageIterator = viewModelPackageGraph.getIerator();
263:                 while (viewModelPackageIterator.hasNext()) {
264:                         final Set<String> latestURIs = viewModelPackageIterator.next();
265:                         for (final String latestURI : latestURIs) {
266:                                 if (!latestToCurrentNSURIMap.keySet().contains(latestURI)) {
267:                                         continue;
268:                                 }
269:                                 final String nsUri = latestToCurrentNSURIMap.get(latestURI);
270:                                 final org.eclipse.emf.edapt.migration.execution.Migrator migrator = nsURIToMigratorMap
271:                                         .get(nsUri);
272:
273:                                 final Release nsRelease = getReleaseFromMigrator(nsUri, migrator);
274:
275:                                 // add changes to source release
276:                                 final int sourceIndex = nsRelease.getNumber();
277:                                 for (int i = 0; i <= sourceIndex; i++) {
278:                                         final Release currentRelease = migrator.getRelease(i);
279:                                         if (!sourceReleaseNameToChangesMap.containsKey(currentRelease.getLabel())) {
280:                                                 sourceReleaseNameToChangesMap.put(currentRelease.getLabel(), new ArrayList<Change>());
281:                                         }
282:                                         sourceReleaseNameToChangesMap.get(currentRelease.getLabel()).addAll(currentRelease.getChanges());
283:
284:                                 }
285:
286:                                 // add changes to target release
287:                                 final int targetIndex = migrator.getLatestRelease().getNumber();
288:                                 if (targetIndex > sourceIndex) {
289:                                         for (int i = sourceIndex + 1; i <= targetIndex; i++) {
290:                                                 final Release currentRelease = migrator.getRelease(i);
291:                                                 if (!targetReleaseNameToChangesMap.containsKey(currentRelease.getLabel())) {
292:                                                         targetReleaseNameToChangesMap.put(currentRelease.getLabel(), new ArrayList<Change>());
293:                                                 }
294:                                                 targetReleaseNameToChangesMap.get(currentRelease.getLabel())
295:                                                         .addAll(currentRelease.getChanges());
296:                                         }
297:                                 }
298:                         }
299:                 }
300:
301:                 /* Based on combined changes from all histories insert the changed to source release and target releases */
302:                 fillReleases(sourceRelease, targetReleases, sourceReleaseNameToChangesMap, targetReleaseNameToChangesMap);
303:         }
304:
305:         /**
306:          * @param sourceRelease
307:          * @param targetReleases
308:          * @param sourceReleaseNameToChangesMap
309:          * @param targetReleaseNameToChangesMap
310:          */
311:         private void fillReleases(final Release sourceRelease, final List<Release> targetReleases,
312:                 final Map<String, List<Change>> sourceReleaseNameToChangesMap,
313:                 final Map<String, List<Change>> targetReleaseNameToChangesMap) {
314:
315:                 final Set<String> sourceReleaseNames = new TreeSet<String>(RELEASE_COMPERATOR);
316:                 sourceReleaseNames.addAll(sourceReleaseNameToChangesMap.keySet());
317:                 final Set<String> targetReleaseNames = new TreeSet<String>(RELEASE_COMPERATOR);
318:                 targetReleaseNames.addAll(targetReleaseNameToChangesMap.keySet());
319:
320:                 for (final String release : sourceReleaseNames) {
321:                         final List<Change> list = sourceReleaseNameToChangesMap.get(release);
322:                         sourceRelease.getChanges().addAll(list);
323:                         sourceRelease.setDate(new Date());
324:                         sourceRelease.setLabel("source"); //$NON-NLS-1$
325:                 }
326:
327:                 for (final String release : targetReleaseNames) {
328:                         final Release targetRelease = HistoryFactory.eINSTANCE.createRelease();
329:                         targetRelease.setDate(new Date());
330:                         targetRelease.setLabel(release);
331:                         targetReleases.add(targetRelease);
332:                         final List<Change> list = targetReleaseNameToChangesMap.get(release);
333:                         targetRelease.getChanges().addAll(list);
334:
335:                         // move custom migrations to end of target release, because every custom migration issues a validation.
336:                         // TODO it would be great if we could config edapt to not validate after each custom migration.
337:                         final List<Integer> indexes = new ArrayList<Integer>();
338:                         for (int i = 0; i < targetRelease.getChanges().size(); i++) {
339:                                 final Change currentChange = targetRelease.getChanges().get(i);
340:                                 if (MigrationChange.class.isInstance(currentChange)) {
341:                                         indexes.add(i);
342:                                 }
343:                         }
344:                         final int lastIndex = targetRelease.getChanges().size() - 1;
345:                         for (int i = indexes.size() - 1; i > -1; i--) {
346:                                 final Change changeToMove = targetRelease.getChanges().get(indexes.get(i));
347:                                 targetRelease.getChanges().move(lastIndex, changeToMove);
348:                         }
349:                 }
350:         }
351:
352:         /**
353:          * @param resourceURI
354:          * @param domainMapping
355:          * @return
356:          */
357:         private List<String> getNamespaceURIsWithHistory(final URI resourceURI, final NSURIMapping domainMapping) {
358:                 final List<String> nsUris = getNamespaceURIs(resourceURI);
359:                 for (final String nsURI : domainMapping.getNsURIsWithGeneratedHistory()) {
360:                         nsUris.remove(nsURI);
361:                 }
362:                 nsUris.addAll(domainMapping.getNsURIsWithHistory());
363:                 return nsUris;
364:         }
365:
366:         /**
367:          * Returns the ns uris for which a history was created.
368:          *
369:          * @param resourceURI
370:          * @return
371:          */
372:         private NSURIMapping addChangesForDomainModel(URI resourceURI, Release release) {
373:                 // get rootpackage nsuri from view
374:                 // check if package is available in registry -> generate history
375:                 final Set<String> nsURIsWithHistory = new LinkedHashSet<String>();
376:                 final Set<String> nsURIsWithGeneratedHistory = new LinkedHashSet<String>();
377:
378:                 final PackageDependencyGraph domainModelPackageGraph = new PackageDependencyGraph();
379:                 domainModelPackageGraph.addPackage(ECORE_NS_URI);
380:                 final Set<String> rootPackageURIs = getRootPackageURI(resourceURI);
381:                 for (String rootPackageNsUri : rootPackageURIs) {
382:                         final Migrator rootMigrator = MigratorRegistry.getInstance().getMigrator(rootPackageNsUri);
383:                         if (rootMigrator != null) {
384:                                 for (final String migratorNSURI : rootMigrator.getNsURIs()) {
385:                                         if (Registry.INSTANCE.containsKey(migratorNSURI)) {
386:                                                 nsURIsWithHistory.add(rootPackageNsUri);
387:                                                 rootPackageNsUri = migratorNSURI;
388:                                                 break;
389:                                         }
390:                                 }
391:                         }
392:                         domainModelPackageGraph.addPackage(rootPackageNsUri);
393:                 }
394:
395:                 final List<EPackage> rootPackages = new ArrayList<EPackage>();
396:                 final Iterator<Set<String>> packageIterator = domainModelPackageGraph.getIerator();
397:
398:                 while (packageIterator.hasNext()) {
399:                         final Set<String> nsURIs = packageIterator.next();
400:                         for (final String nsURI : nsURIs) {
401:                                 /* there is no history available from the extensionpoint */
402:                                 if (MigratorRegistry.getInstance().getMigrator(nsURI) == null) {
403:                                         final EPackage ePackage = Registry.INSTANCE.getEPackage(nsURI);
404:                                         rootPackages.add(ePackage);
405:                                         nsURIsWithGeneratedHistory.add(nsURI);
406:                                 } else {
407:                                         nsURIsWithHistory.add(nsURI);
408:                                 }
409:                         }
410:                 }
411:
412:                 @SuppressWarnings("restriction")
413:                 final org.eclipse.emf.edapt.history.recorder.HistoryGenerator rootGenerator = new org.eclipse.emf.edapt.history.recorder.HistoryGenerator(
414:                         rootPackages);
415:                 @SuppressWarnings("restriction")
416:                 final List<Change> rootChanges = rootGenerator.generate().getFirstRelease().getChanges();
417:                 release.getChanges().addAll(rootChanges);
418:                 return new NSURIMapping(nsURIsWithHistory, nsURIsWithGeneratedHistory);
419:         }
420:
421:         /**
422:          * @param nsUri The name space URI of the package whose {@link Release} is wanted.
423:          * @param migrator The {@link Migrator} which should provide the {@link Release}.
424:          * @return
425:          */
426:         private Release getReleaseFromMigrator(final String nsUri, final Migrator migrator) {
427:                 final Map<String, Set<Release>> releaseMap = migrator.getReleaseMap();
428:                 final Set<Release> nsReleases = releaseMap.get(nsUri);
429:                 /* in case multiple are found pick the newest one */
430:                 return nsReleases.stream()
431:                         .sorted(this::compareReleases)
432:                         .findFirst()
433:                         .orElse(null);
434:         }
435:
436:         private int compareReleases(Release r1, Release r2) {
437:                 return r2.getNumber() - r1.getNumber();
438:         }
439:
440:         /**
441:          * @return the namespaces of all models used in the given resource.
442:          */
443:         private List<String> getNamespaceURIs(URI resourceURI) {
444:                 @SuppressWarnings("restriction")
445:                 final File file = org.eclipse.emf.edapt.internal.common.URIUtils.getJavaFile(resourceURI);
446:                 // read all namespaces from root element with SAX
447:                 final NameSpaceHandler handler = new NameSpaceHandler();
448:                 executeContentHandler(file, handler);
449:
450:                 return handler.getNsURIs();
451:         }
452:
453:         /**
454:          * @return the namespaces of all models used in the given resource.
455:          */
456:         private Set<String> getRootPackageURI(URI resourceURI) {
457:                 @SuppressWarnings("restriction")
458:                 final File file = org.eclipse.emf.edapt.internal.common.URIUtils.getJavaFile(resourceURI);
459:                 // read root package namespace uri with SAX
460:                 final RootPackageHandler handler = new RootPackageHandler();
461:                 executeContentHandler(file, handler);
462:                 if (handler.foundRootEClass()) {
463:                         return Collections.singleton(handler.getRootPackageURI());
464:                 }
465:                 final RootPackageCalculationHandler calcHandler = new RootPackageCalculationHandler();
466:                 executeContentHandler(file, calcHandler);
467:                 return calcHandler.getUsedPackages();
468:         }
469:
470:         private static void executeContentHandler(File file, final DefaultHandler contentHandler) {
471:                 try {
472:                         SAXUtil.executeContentHandler(new FileReader(file), contentHandler);
473:                 } catch (final FileNotFoundException ex) {
474:                 }
475:         }
476:
477:         /** Register all migrators from extensions. */
478:         private void reregisterExtensionMigrators() {
479:                 final IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
480:                 final IConfigurationElement[] configurationElements = extensionRegistry
481:                         .getConfigurationElementsFor("org.eclipse.emf.edapt.migrators"); //$NON-NLS-1$
482:
483:                 for (final IConfigurationElement configurationElement : configurationElements) {
484:                         registerExtensionMigrator(configurationElement);
485:                 }
486:         }
487:
488:         /** Register migrator for one extension. */
489:         @SuppressWarnings("restriction")
490:         private void registerExtensionMigrator(
491:                 IConfigurationElement configurationElement) {
492:
493:                 final String migrationPath = configurationElement.getAttribute("path"); //$NON-NLS-1$
494:
495:                 final IContributor contributor = configurationElement.getContributor();
496:                 final String bundleName = contributor.getName();
497:                 final Bundle bundle = Platform.getBundle(bundleName);
498:                 final URI migratorURI = URI.createPlatformPluginURI("/" + bundleName + "/" //$NON-NLS-1$ //$NON-NLS-2$
499:                         + migrationPath, true);
500:                 try {
501:                         MigratorRegistry.getInstance().registerMigrator(migratorURI,
502:                                 new org.eclipse.emf.edapt.internal.migration.execution.internal.BundleClassLoader(bundle));
503:                 } catch (final MigrationException e) {
504:                         org.eclipse.emf.edapt.internal.common.LoggingUtils.logError(MigrationPlugin.getPlugin(), e);
505:                 }
506:         }
507:
508:         /** Content handler for extraction of the root package namespace URI using SAX. */
509:         private static class RootPackageHandler extends DefaultHandler {
510:
511:                 /** The root package's namespace URI. */
512:                 private String rootPackageURI = ""; //$NON-NLS-1$
513:                 private boolean rootEClassFound;
514:
515:                 /**
516:                  * {@inheritDoc}
517:                  *
518:                  * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
519:                  * org.xml.sax.Attributes)
520:                  */
521:                 @Override
522:                 public void startElement(String uri, String localName, String qName, Attributes attributes)
523:                         throws SAXException {
524:                         super.startElement(uri, localName, qName, attributes);
525:                         if (localName.equals("rootEClass")) { //$NON-NLS-1$
526:                                 final String rawUri = attributes.getValue("href"); //$NON-NLS-1$
527:                                 rootPackageURI = rawUri.split("#")[0]; //$NON-NLS-1$
528:                                 rootEClassFound = true;
529:                                 throw new SAXException();
530:                         }
531:                 }
532:
533:                 /**
534:                  * Whether an element with the rootEClass attribute has been found.
535:                  *
536:                  * @return <code>true</code> if found, <code>false</code> otherwise
537:                  */
538:                 public boolean foundRootEClass() {
539:                         return rootEClassFound;
540:                 }
541:
542:                 /** Returns the root package's namespace URI. */
543:                 public String getRootPackageURI() {
544:                         return rootPackageURI;
545:                 }
546:         }
547:
548:         /**
549:          * Content handler for finding all used NS-URIs in a view model.
550:          *
551:          * @author jfaltermeier
552:          *
553:          */
554:         private static class RootPackageCalculationHandler extends DefaultHandler {
555:
556:                 /** The root package's namespace URI. */
557:                 private final Set<String> packages = new LinkedHashSet<String>();
558:
559:                 @Override
560:                 public void startElement(String uri, String localName, String qName, Attributes attributes)
561:                         throws SAXException {
562:                         super.startElement(uri, localName, qName, attributes);
563:                         final String href = attributes.getValue("href"); //$NON-NLS-1$
564:                         final String type = attributes.getValue("xsi:type"); //$NON-NLS-1$
565:                         if (href == null || type == null) {
566:                                 return;
567:                         }
568:                         if (!type.endsWith("EAttribute") && !type.endsWith("EReference")) { //$NON-NLS-1$ //$NON-NLS-2$
569:                                 return;
570:                         }
571:                         packages.add(href.split("#")[0]); //$NON-NLS-1$
572:                 }
573:
574:                 /** Returns the root package's namespace URI. */
575:                 public Set<String> getUsedPackages() {
576:                         return packages;
577:                 }
578:         }
579:
580:         /**
581:          * Mapping for domain nsURIs.
582:          *
583:          * @author jfaltermeier
584:          *
585:          */
586:         private static class NSURIMapping {
587:                 private final Set<String> nsURIsWithHistory;
588:                 private final Set<String> nsURIsWithGeneratedHistory;
589:
590:                 /**
591:                  * Default constructor.
592:                  *
593:                  * @param nsURIsWithHistory ns uris of models with a history available at the extension point
594:                  * @param nsURIsWithGeneratedHistory ns uris of models for which we generated a history
595:                  */
596:                 NSURIMapping(Set<String> nsURIsWithHistory, Set<String> nsURIsWithGeneratedHistory) {
597:                         this.nsURIsWithHistory = nsURIsWithHistory;
598:                         this.nsURIsWithGeneratedHistory = nsURIsWithGeneratedHistory;
599:                 }
600:
601:                 public Set<String> getNsURIsWithHistory() {
602:                         return nsURIsWithHistory;
603:                 }
604:
605:                 public Set<String> getNsURIsWithGeneratedHistory() {
606:                         return nsURIsWithGeneratedHistory;
607:                 }
608:         }
609:
610: }