KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > resources > mapping > ResourceChangeValidator


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.resources.mapping;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import org.eclipse.core.internal.resources.mapping.ChangeDescription;
16 import org.eclipse.core.internal.resources.mapping.ResourceChangeDescriptionFactory;
17 import org.eclipse.core.internal.utils.Messages;
18 import org.eclipse.core.internal.utils.Policy;
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.osgi.util.NLS;
22
23 /**
24  * The resource change validator is used to validate that changes made to
25  * resources will not adversely affect the models stored in those resources.
26  * <p>
27  * The validator is used by first creating a resource delta describing the
28  * proposed changes. A delta can be generated using a {@link IResourceChangeDescriptionFactory}.
29  * The change is then validated by calling the {@link #validateChange(IResourceDelta, IProgressMonitor)}
30  * method. This example validates a change to a single file:
31  * <code>
32  * IFile file = ..;//some file that is going to be changed
33  * ResourceChangeValidator validator = ResourceChangeValidator.getValidator();
34  * IResourceChangeDescriptionFactory factory = validator.createDeltaFactory();
35  * factory.change(file);
36  * IResourceDelta delta = factory.getDelta();
37  * IStatus result = validator.validateChange(delta, null);
38  * </code>
39  * If the result status does not have severity {@link IStatus#OK}, then
40  * the changes may cause problems for models that are built on those
41  * resources. In this case the user should be presented with the status message
42  * to determine if they want to proceed with the modification.
43  * </p>
44  *
45  * @since 3.2
46  */

47 public final class ResourceChangeValidator {
48
49     private static ResourceChangeValidator instance;
50
51     /**
52      * Return the singleton change validator.
53      * @return the singleton change validator
54      */

55     public static ResourceChangeValidator getValidator() {
56         if (instance == null)
57             instance = new ResourceChangeValidator();
58         return instance;
59     }
60
61     /**
62      * Singleton accessor method should be used instead.
63      * @see #getValidator()
64      */

65     private ResourceChangeValidator() {
66         super();
67     }
68
69     private IStatus combineResults(IStatus[] result) {
70         List JavaDoc notOK = new ArrayList JavaDoc();
71         for (int i = 0; i < result.length; i++) {
72             IStatus status = result[i];
73             if (!status.isOK()) {
74                 notOK.add(status);
75             }
76         }
77         if (notOK.isEmpty()) {
78             return Status.OK_STATUS;
79         }
80         if (notOK.size() == 1) {
81             return (IStatus) notOK.get(0);
82         }
83         return new MultiStatus(ResourcesPlugin.PI_RESOURCES, 0, (IStatus[]) notOK.toArray(new IStatus[notOK.size()]), Messages.mapping_multiProblems, null);
84     }
85
86     /**
87      * Return an empty change description factory that can be used to build a
88      * proposed resource delta.
89      * @return an empty change description factory that can be used to build a
90      * proposed resource delta
91      */

92     public IResourceChangeDescriptionFactory createDeltaFactory() {
93         return new ResourceChangeDescriptionFactory();
94     }
95
96     private ModelProvider[] getProviders(IResource[] resources) {
97         IModelProviderDescriptor[] descriptors = ModelProvider.getModelProviderDescriptors();
98         List JavaDoc result = new ArrayList JavaDoc();
99         for (int i = 0; i < descriptors.length; i++) {
100             IModelProviderDescriptor descriptor = descriptors[i];
101             try {
102                 IResource[] matchingResources = descriptor.getMatchingResources(resources);
103                 if (matchingResources.length > 0) {
104                     result.add(descriptor.getModelProvider());
105                 }
106             } catch (CoreException e) {
107                 Policy.log(e.getStatus().getSeverity(), NLS.bind("Could not instantiate provider {0}", descriptor.getId()), e); //$NON-NLS-1$
108
}
109         }
110         return (ModelProvider[]) result.toArray(new ModelProvider[result.size()]);
111     }
112
113     /*
114      * Get the roots of any changes.
115      */

116     private IResource[] getRootResources(IResourceDelta root) {
117         final ChangeDescription changeDescription = new ChangeDescription();
118         try {
119             root.accept(new IResourceDeltaVisitor() {
120                 public boolean visit(IResourceDelta delta) {
121                     return changeDescription.recordChange(delta);
122                 }
123             });
124         } catch (CoreException e) {
125             // Shouldn't happen since the ProposedResourceDelta accept doesn't throw an
126
// exception and our visitor doesn't either
127
Policy.log(IStatus.ERROR, "Internal error", e); //$NON-NLS-1$
128
}
129         return changeDescription.getRootResources();
130     }
131
132     /**
133      * Validate the proposed changes contained in the given delta
134      * by consulting all model providers to determine if the changes
135      * have any adverse side effects.
136      * <p>
137      * This method returns either a {@link ModelStatus}, or a {@link MultiStatus}
138      * whose children are {@link ModelStatus}. In either case, the severity
139      * of the status indicates the severity of the possible side-effects of
140      * the operation. Any severity other than <code>OK</code> should be
141      * shown to the user. The message should be a human readable message that
142      * will allow the user to make a decision on whether to continue with the
143      * operation. The model provider id should indicate which model is flagging the
144      * the possible side effects.
145      * </p>
146      *
147      * @param delta a delta tree containing the proposed changes
148      * @return a status indicating any potential side effects
149      * on models stored in the affected resources.
150      */

151     public IStatus validateChange(IResourceDelta delta, IProgressMonitor monitor) {
152         monitor = Policy.monitorFor(monitor);
153         try {
154             IResource[] resources = getRootResources(delta);
155             ModelProvider[] providers = getProviders(resources);
156             if (providers.length == 0)
157                 return Status.OK_STATUS;
158             monitor.beginTask(Messages.mapping_validate, providers.length);
159             IStatus[] result = new IStatus[providers.length];
160             for (int i = 0; i < providers.length; i++)
161                 result[i] = providers[i].validateChange(delta, Policy.subMonitorFor(monitor, 1));
162             return combineResults(result);
163         } finally {
164             monitor.done();
165         }
166     }
167 }
168
Popular Tags