KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > builders > FeatureConsistencyChecker


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.core.builders;
12
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IResourceDelta;
20 import org.eclipse.core.resources.IResourceDeltaVisitor;
21 import org.eclipse.core.resources.IncrementalProjectBuilder;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.core.WorkspaceModelManager;
27 import org.eclipse.pde.internal.core.natures.PDE;
28 import org.eclipse.pde.internal.core.PDECoreMessages;
29 import org.osgi.framework.Bundle;
30
31 public class FeatureConsistencyChecker extends IncrementalProjectBuilder {
32     class DeltaVisitor implements IResourceDeltaVisitor {
33         private IProgressMonitor monitor;
34         public DeltaVisitor(IProgressMonitor monitor) {
35             this.monitor = monitor;
36         }
37         public boolean visit(IResourceDelta delta) {
38             IResource resource = delta.getResource();
39
40             if (resource instanceof IProject) {
41                 // Only check projects with feature nature
42
IProject project = (IProject) resource;
43                 try {
44                     return (project.hasNature(PDE.FEATURE_NATURE));
45                 } catch (CoreException e) {
46                     PDECore.logException(e);
47                     return false;
48                 }
49             }
50             if (resource instanceof IFile) {
51                 // see if this is it
52
IFile candidate = (IFile) resource;
53                 if (isManifestFile(candidate)) {
54                     // That's it, but only check it if it has been added or changed
55
if (delta.getKind() != IResourceDelta.REMOVED) {
56                         checkFile(candidate, monitor);
57                         return true;
58                     }
59                 }
60             }
61             return false;
62         }
63     }
64     
65     protected IProject[] build(int kind, Map JavaDoc args, IProgressMonitor monitor)
66         throws CoreException {
67         if (PDECore.getDefault().getBundle().getState() != Bundle.ACTIVE || monitor.isCanceled())
68             return new IProject[0];
69
70         if (WorkspaceModelManager.isBinaryProject(getProject()))
71             return new IProject[0];
72         
73         IResourceDelta delta = null;
74
75         if (kind != FULL_BUILD)
76             delta = getDelta(getProject());
77
78         if (delta == null || kind == FULL_BUILD) {
79             // Full build
80
checkProject(monitor);
81         } else {
82             delta.accept(new DeltaVisitor(monitor));
83         }
84         return new IProject[0];
85     }
86
87     private void checkProject(IProgressMonitor monitor) {
88         IFile file = getProject().getFile("feature.xml"); //$NON-NLS-1$
89
if (file.exists()) {
90             checkFile(file, monitor);
91         }
92     }
93     
94     private void checkFile(IFile file, IProgressMonitor monitor) {
95         String JavaDoc message =
96             NLS.bind(PDECoreMessages.Builders_verifying, file.getFullPath().toString());
97         monitor.subTask(message);
98         FeatureErrorReporter reporter = new FeatureErrorReporter(file);
99         DefaultSAXParser.parse(file, reporter);
100         if (reporter.getErrorCount() == 0) {
101             reporter.validateContent(monitor);
102         }
103         monitor.subTask(PDECoreMessages.Builders_updating);
104         monitor.done();
105     }
106     
107     private boolean isManifestFile(IFile file) {
108         return file.getParent().equals(file.getProject())
109                 && file.getName().toLowerCase(Locale.ENGLISH).equals("feature.xml"); //$NON-NLS-1$
110
}
111     
112
113     
114 }
115
Popular Tags