Skip to content

Package: ViewNsMigrationUtil

ViewNsMigrationUtil

nameinstructionbranchcomplexitylinemethod
checkMigration(File)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
extractViewNsUri(Reader)
M: 0 C: 37
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
getRegisteredViewNsUri()
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
migrateViewEcoreNsUri(File)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
migrateViewEcoreNsUri(File, File)
M: 0 C: 43
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
needsMigration(String, String)
M: 0 C: 12
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
readFile(Reader)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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 Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.spi.view.migrator;
15:
16: import java.io.BufferedReader;
17: import java.io.File;
18: import java.io.FileReader;
19: import java.io.FileWriter;
20: import java.io.IOException;
21: import java.io.Reader;
22: import java.io.StringReader;
23: import java.util.List;
24: import java.util.regex.Matcher;
25: import java.util.regex.Pattern;
26:
27: import org.eclipse.emf.ecore.EPackage;
28:
29: /**
30: * Utility class to migrate the view name space uri in a file.
31: *
32: * @author Lucas Koehler
33: * @since 1.17
34: */
35: public final class ViewNsMigrationUtil {
36:
37:         // Utility class should not be instantiated
38:         private ViewNsMigrationUtil() {
39:         }
40:
41:         private static final Pattern VIEW_NS_URI_PATTERN = Pattern
42:                 .compile("http://org/eclipse/emf/ecp/view/model(/[0-9]+)?"); //$NON-NLS-1$
43:
44:         /**
45:          * If the given file references the view ecore, migrate the view ecore
46:          * namespace URI of the template model to the namespace URI of the registered version of the view ecore.
47:          * This is necessary for domain model reference selectors to be compatible to the view models of the current
48:          * version. Thereby, we assume that view models use the registered version of the view ecore because otherwise they
49:          * could not be used in the current runtime environment.
50:          * <p>
51:          * <strong>Note:</strong> If no migration is necessary, nothing is written to the output writer.
52:          *
53:          * @param file The {@link File} containing the template model
54:          * @return whether the namespace uri was migrated
55:          * @throws IOException if something goes wrong reading or writing the template model file
56:          */
57:         public static boolean migrateViewEcoreNsUri(File file) throws IOException {
58:                 return migrateViewEcoreNsUri(file, file);
59:         }
60:
61:         /**
62:          * Checks whether the view name space uri of the given file needs to be migrated.
63:          *
64:          * @param file The file to check
65:          * @return <code>true</code> if no migration is necessary and <code>false</code> if the name space uri needs
66:          * to be migrated.
67:          * @throws IOException if the file cannot be read
68:          */
69:         public static boolean checkMigration(File file) throws IOException {
70:                 final FileReader fileReader = new FileReader(file);
71:                 final String templateModelString = readFile(fileReader);
72:                 fileReader.close();
73:                 final String viewNs = extractViewNsUri(new StringReader(templateModelString));
74:                 final String registeredViewNsUri = getRegisteredViewNsUri();
75:
76:                 final boolean needsMigration = needsMigration(viewNs, registeredViewNsUri);
77:•                return !needsMigration;
78:         }
79:
80:         /**
81:          * @param viewNs The view package's name space uri of the file
82:          * @param registeredViewNsUri The name space uri of the registered view package.
83:          * @return Whether a migration is necessary
84:          */
85:         private static boolean needsMigration(final String viewNs, final String registeredViewNsUri) {
86:•                return viewNs != null && registeredViewNsUri != null && !viewNs.equals(registeredViewNsUri);
87:         }
88:
89:         /**
90:          * Inner method to migrate the view ecore namespace uri that allows using two different files for input and output.
91:          * Two different files are mainly useful for testing.
92:          *
93:          * @param inputFile The {@link File} containing the template model
94:          * @param outputFile The {@link File} containing the migrated template model if any migration as necessary
95:          * @return whether the namespace uri was migrated
96:          * @throws IOException if something goes wrong reading or writing the template model file
97:          * @see {@link #migrateViewEcoreNsUri(File)}
98:          */
99:         static boolean migrateViewEcoreNsUri(File inputFile, File outputFile) throws IOException {
100:                 final FileReader fileReader = new FileReader(inputFile);
101:                 final String templateModelString = readFile(fileReader);
102:                 fileReader.close();
103:                 final String viewNs = extractViewNsUri(new StringReader(templateModelString));
104:
105:                 final String registeredViewNsUri = getRegisteredViewNsUri();
106:
107:•                if (needsMigration(viewNs, registeredViewNsUri)) {
108:                         // NS URI migration needed
109:                         final String updatedModel = templateModelString.toString().replaceAll(viewNs, registeredViewNsUri);
110:
111:                         final FileWriter writer = new FileWriter(outputFile, false);
112:                         writer.write(updatedModel);
113:                         writer.close();
114:                         return true;
115:                 }
116:                 return false;
117:         }
118:
119:         /**
120:          * @return The whole file read into one String.
121:          */
122:         private static String readFile(Reader stringReader) throws IOException {
123:                 final StringBuilder builder = new StringBuilder();
124:                 final BufferedReader bufferedReader = new BufferedReader(stringReader);
125:                 String line;
126:•                while ((line = bufferedReader.readLine()) != null) {
127:                         builder.append(line);
128:                         builder.append('\n');
129:                 }
130:                 bufferedReader.close();
131:                 return builder.toString();
132:         }
133:
134:         /**
135:          * @return The namespace URI of the registered view ecore, or <code>null</code> if it is not registered
136:          */
137:         private static String getRegisteredViewNsUri() {
138:                 String registeredViewNsUri = null;
139:•                for (final String packageNsUri : EPackage.Registry.INSTANCE.keySet()) {
140:                         final Matcher matcher = VIEW_NS_URI_PATTERN.matcher(packageNsUri);
141:•                        if (matcher.matches()) {
142:                                 registeredViewNsUri = packageNsUri;
143:                                 break;
144:                         }
145:                 }
146:                 return registeredViewNsUri;
147:         }
148:
149:         /**
150:          * @return The view ecore's namespace uri used in the template model, or <code>null</code> if the template model
151:          * does not reference the view ecore.
152:          */
153:         private static String extractViewNsUri(final Reader reader) throws IOException {
154:                 final NameSpaceHandler handler = new NameSpaceHandler();
155:                 SAXUtil.executeContentHandler(reader, handler);
156:                 reader.close();
157:                 final List<String> nsURIs = handler.getNsURIs();
158:                 String viewNs = null;
159:•                for (final String nsUri : nsURIs) {
160:                         final Matcher matcher = VIEW_NS_URI_PATTERN.matcher(nsUri);
161:•                        if (matcher.matches()) {
162:                                 viewNs = nsUri;
163:                                 break;
164:                         }
165:                 }
166:                 return viewNs;
167:         }
168: }