Skip to content

Package: ViewModelDeleteParticipant

ViewModelDeleteParticipant

nameinstructionbranchcomplexitylinemethod
ViewModelDeleteParticipant()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
checkConditions(IProgressMonitor, CheckConditionsContext)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
createChange(IProgressMonitor)
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%
getName()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
initialize(Object)
M: 2 C: 22
92%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (c) 2011-2019 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: * Eugen Neufeld - initial API and implementation
13: */
14: package org.eclipse.emf.ecp.view.model.refactoring;
15:
16: import java.io.FileWriter;
17: import java.io.IOException;
18: import java.util.regex.Matcher;
19: import java.util.regex.Pattern;
20:
21: import org.eclipse.core.resources.IFile;
22: import org.eclipse.core.resources.IResource;
23: import org.eclipse.core.runtime.Adapters;
24: import org.eclipse.core.runtime.CoreException;
25: import org.eclipse.core.runtime.IProgressMonitor;
26: import org.eclipse.core.runtime.IStatus;
27: import org.eclipse.core.runtime.OperationCanceledException;
28: import org.eclipse.core.runtime.Status;
29: import org.eclipse.emf.ecp.view.model.presentation.ContributionUtil;
30: import org.eclipse.ltk.core.refactoring.Change;
31: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
33: import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
34: import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
35:
36: /**
37: * Delete Participant for View Models.
38: * This class cleans up the plugin.xml.
39: *
40: * @author Eugen Neufeld
41: **/
42:
43: public class ViewModelDeleteParticipant extends DeleteParticipant {
44:         private static final Pattern EMPTY_PLUGIN_PATTERN = Pattern.compile("<plugin>\\s*</plugin>", //$NON-NLS-1$
45:                 Pattern.DOTALL | Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
46:
47:         private static final String EMPTY_STRING = ""; //$NON-NLS-1$
48:         private static final Pattern EMPTY_XMI_EXTENSIONPOINT = Pattern.compile(
49:                 "<extension.*point=\"org.eclipse.emf.ecp.view.model.provider.xmi.file\">\\s*</extension>", //$NON-NLS-1$
50:                 Pattern.DOTALL | Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
51:         private static final String ONE_LINE_FILE = "<file filePath=\"%s\"/>"; //$NON-NLS-1$
52:         private static final String MULTI_LINE_FILE = "<file\\s*filePath=\"%s\">\\s*</file>"; //$NON-NLS-1$
53:         private static final String REMOVE_PLUGIN_XML = "plugin.xml,?\\\\?\\s*"; //$NON-NLS-1$
54:         private static final String REMOVE_FOLDER = "%s/,?\\\\?\\s*"; //$NON-NLS-1$
55:         private static final String REMOVE_VIEW = "%s,?\\\\?\\s*"; //$NON-NLS-1$
56:         private static final String CLEAN_PLUGIN_XML = ",\\\\\\s*$"; //$NON-NLS-1$
57:
58:         /**
59:          * Custom Change that removes the registered view model from the plugin.xml .
60:          *
61:          * @author Eugen Neufeld
62:          *
63:          */
64:         private final class ChangeExtension extends Change {
65:                 @Override
66:                 public Change perform(IProgressMonitor pm) throws CoreException {
67:
68:                         try {
69:                                 final String pluginString = ContributionUtil.parseIFile(pluginXml).get();
70:                                 final String viewPath = viewModel.getProjectRelativePath().toString();
71:
72:                                 final String cleanExtensionString = pluginString
73:                                         .replaceAll(String.format(ONE_LINE_FILE, viewPath), EMPTY_STRING)
74:                                         .replaceAll(String.format(MULTI_LINE_FILE, viewPath), EMPTY_STRING);
75:
76:                                 final String cleanPlugin = EMPTY_XMI_EXTENSIONPOINT.matcher(cleanExtensionString)
77:                                         .replaceAll(EMPTY_STRING);
78:                                 final Matcher matcher = EMPTY_PLUGIN_PATTERN.matcher(cleanPlugin);
79:                                 boolean removePluginXml = false;
80:                                 if (!matcher.find()) {
81:                                         final FileWriter out = new FileWriter(pluginXml.getRawLocation().makeAbsolute().toFile());
82:                                         out.write(String.valueOf(cleanPlugin));
83:                                         out.close();
84:                                 } else {
85:                                         pluginXml.delete(true, pm);
86:                                         removePluginXml = true;
87:                                 }
88:                                 boolean removeFolder = false;
89:                                 if (viewModel.getParent().members().length == 0) {
90:                                         viewModel.getParent().delete(true, pm);
91:                                         removeFolder = true;
92:                                 }
93:                                 final IFile buildProperties = viewModel.getProject().getFile("build.properties"); //$NON-NLS-1$
94:                                 final String buildPropertiesString = ContributionUtil.parseIFile(buildProperties).get();
95:                                 String cleanedProperties = null;
96:                                 if (removePluginXml || removeFolder) {
97:                                         if (removePluginXml) {
98:                                                 cleanedProperties = buildPropertiesString
99:                                                         .replaceAll(REMOVE_PLUGIN_XML, EMPTY_STRING);
100:                                         }
101:                                         if (removeFolder) {
102:                                                 cleanedProperties = (cleanedProperties != null ? cleanedProperties : buildPropertiesString)
103:                                                         .replaceAll(
104:                                                                 String.format(REMOVE_FOLDER,
105:                                                                         viewModel.getParent().getProjectRelativePath().toString()),
106:                                                                 EMPTY_STRING);
107:                                         }
108:                                 }
109:                                 {
110:                                         cleanedProperties = (cleanedProperties != null ? cleanedProperties : buildPropertiesString)
111:                                                 .replaceAll(
112:                                                         String.format(REMOVE_VIEW, viewModel.getProjectRelativePath().toString()),
113:                                                         EMPTY_STRING);
114:                                 }
115:
116:                                 cleanedProperties = cleanedProperties.replaceAll(CLEAN_PLUGIN_XML, EMPTY_STRING);
117:                                 final FileWriter out = new FileWriter(buildProperties.getRawLocation().makeAbsolute().toFile());
118:                                 out.write(String.valueOf(cleanedProperties));
119:                                 out.flush();
120:                                 out.close();
121:
122:                                 viewModel.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
123:
124:                         } catch (final IOException ex) {
125:                                 throw new CoreException(
126:                                         new Status(IStatus.ERROR, "org.eclipse.emf.ecp.view.model.editor", ex.getMessage(), ex)); //$NON-NLS-1$
127:                         }
128:
129:                         return null;
130:                 }
131:
132:                 @Override
133:                 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
134:                         return RefactoringStatus.create(Status.OK_STATUS);
135:                 }
136:
137:                 @Override
138:                 public void initializeValidationData(IProgressMonitor pm) {
139:                 }
140:
141:                 @Override
142:                 public String getName() {
143:                         return "Remove view model from plugin.xml"; //$NON-NLS-1$
144:                 }
145:
146:                 @Override
147:                 public Object getModifiedElement() {
148:                         return null;
149:                 }
150:         }
151:
152:         private IFile viewModel;
153:         private IFile pluginXml;
154:
155:         /**
156:          * Default constructor.
157:          */
158:         public ViewModelDeleteParticipant() {
159:         }
160:
161:         @Override
162:         protected boolean initialize(Object element) {
163:                 viewModel = Adapters.adapt(element, IFile.class);
164:                 pluginXml = viewModel.getProject().getFile("plugin.xml"); //$NON-NLS-1$
165:
166:•                return viewModel != null && pluginXml.exists();
167:         }
168:
169:         @Override
170:         public String getName() {
171:                 return "Clean up after View Model Removal"; //$NON-NLS-1$
172:         }
173:
174:         @Override
175:         public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
176:                 throws OperationCanceledException {
177:                 final ResourceChangeChecker resourceChecker = context.getChecker(ResourceChangeChecker.class);
178:•                if (resourceChecker != null) {
179:                         try {
180:                                 final RefactoringStatus result = resourceChecker.check(pm);
181:                                 final String projectInfo = "Remove view model from plugin.xml"; //$NON-NLS-1$
182:                                 result.addInfo(projectInfo);
183:                                 return result;
184:                         } catch (final CoreException e) {
185:                                 return RefactoringStatus.create(e.getStatus());
186:                         }
187:                 }
188:
189:                 return new RefactoringStatus();
190:         }
191:
192:         @Override
193:         public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
194:                 return new ChangeExtension();
195:         }
196:
197: }