Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68feefbb6fe1f97c904f9992a4935f55203ede78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2007, 2010 BMW Car IT, Technische Universitaet Muenchen, and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * BMW Car IT - Initial API and implementation
 * Technische Universitaet Muenchen - Major refactoring and extension
 *******************************************************************************/
package org.eclipse.emf.edapt.internal.migration.internal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edapt.internal.common.ResourceUtils;
import org.eclipse.emf.edapt.spi.migration.Metamodel;

/**
 * Helper methods to deal with model backup.
 *
 * @author herrmama
 * @author $Author$
 * @version $Rev$
 * @levd.rating YELLOW Hash: 79D1350584168827FC59AC2546F8221E
 */
public final class BackupUtils {

	/** File extension for backed up model. */
	public static final String BACKUP_FILE_EXTENSION = "backup"; //$NON-NLS-1$

	/** File extension for log file. */
	public static final String LOG_FILE_EXTENSION = "log"; //$NON-NLS-1$

	/** Constructor. */
	private BackupUtils() {
		// hidden, since this class only provides static helper methods
	}

	/**
	 * Get the URI of the backup model corresponding to the URI of the model.
	 *
	 * @param modelURI
	 *            URI of model
	 * @return URI of backup model
	 */
	public static URI getBackupURI(URI modelURI) {
		return modelURI.appendFileExtension(BACKUP_FILE_EXTENSION);
	}

	/**
	 * Get the URI of the model corresponding to the URI of the backup model.
	 *
	 * @param backupURI
	 *            URI of backup model
	 * @return URI of model
	 */
	public static URI getModelURI(URI backupURI) {
		if (BACKUP_FILE_EXTENSION.equals(backupURI.fileExtension())) {
			return backupURI.trimFileExtension();
		}
		return null;
	}

	/**
	 * Get the URI of the log file corresponding to the URI of the model.
	 *
	 * @param modelURI
	 *            URI of model
	 * @return URI of log file
	 */
	public static URI getLogURI(URI modelURI) {
		return modelURI.appendFileExtension(LOG_FILE_EXTENSION);
	}

	/** Backup a model based on a number of {@link URI}s. */
	public static List<URI> backup(List<URI> modelURIs, Metamodel metamodel)
		throws IOException {
		return copy(modelURIs, metamodel, new BackupMapper());
	}

	/** Backup a model based on a number of {@link URI}s. */
	public static List<URI> restore(List<URI> backupURIs, Metamodel metamodel)
		throws IOException {
		return copy(backupURIs, metamodel, new RestoreMapper());
	}

	/** Copy a model based on a number of {@link URI}s. */
	public static List<URI> copy(List<URI> sourceURIs, Metamodel metamodel,
		URIMapper mapper) throws IOException {
		final List<URI> targetURIs = new ArrayList<URI>();
		final ResourceSet model = ResourceUtils.loadResourceSet(sourceURIs, metamodel
			.getEPackages());
		for (final Resource resource : model.getResources()) {
			if (resource.getURI() == null
				|| resource.getURI().isPlatformPlugin()) {
				continue;
			}
			final URI targetURI = mapper.map(resource.getURI());
			if (targetURI != null) {
				resource.setURI(targetURI);
				targetURIs.add(targetURI);
			}
		}
		ResourceUtils.saveResourceSet(model, null);
		return targetURIs;
	}

	/** Mapper to perform a mapping on URIs. */
	public static interface URIMapper {
		/** Map a source URI to a target URI. */
		URI map(URI uri);
	}

	/** Mapper to backup a model based on a number of {@link URI}s. */
	private static class BackupMapper implements URIMapper {
		/** {@inheritDoc} */
		@Override
		public URI map(URI uri) {
			return getBackupURI(uri);
		}
	}

	/** Mapper to restore a model based on a number of {@link URI}s. */
	private static class RestoreMapper implements URIMapper {
		/** {@inheritDoc} */
		@Override
		public URI map(URI uri) {
			return getModelURI(uri);
		}
	}
}

Back to the top