KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IFolder;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IResourceDelta;
26 import org.eclipse.core.resources.IResourceDeltaVisitor;
27 import org.eclipse.core.resources.IWorkspace;
28 import org.eclipse.core.resources.IncrementalProjectBuilder;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.osgi.util.NLS;
34 import org.eclipse.pde.core.IModel;
35 import org.eclipse.pde.core.plugin.IPluginBase;
36 import org.eclipse.pde.core.plugin.IPluginModelBase;
37 import org.eclipse.pde.core.plugin.PluginRegistry;
38 import org.eclipse.pde.internal.core.PDECore;
39 import org.eclipse.pde.internal.core.WorkspaceModelManager;
40 import org.eclipse.pde.internal.core.natures.PDE;
41 import org.eclipse.pde.internal.core.PDECoreMessages;
42 import org.eclipse.pde.internal.core.schema.Schema;
43 import org.eclipse.pde.internal.core.schema.SchemaDescriptor;
44
45 public class ExtensionPointSchemaBuilder extends IncrementalProjectBuilder {
46     class DeltaVisitor implements IResourceDeltaVisitor {
47         private IProgressMonitor monitor;
48         public DeltaVisitor(IProgressMonitor monitor) {
49             this.monitor = monitor;
50         }
51         public boolean visit(IResourceDelta delta) {
52             IResource resource = delta.getResource();
53
54             if (resource instanceof IProject)
55                 return isInterestingProject((IProject)resource);
56             
57             if (resource instanceof IFolder)
58                 return true;
59             
60             if (resource instanceof IFile) {
61                 // see if this is it
62
IFile candidate = (IFile) resource;
63                 if (isSchemaFile(candidate)) {
64                     // That's it, but only check it if it has been added or changed
65
if (delta.getKind() != IResourceDelta.REMOVED) {
66                         compileFile(candidate, monitor);
67                     } else {
68                         removeOutputFile(candidate, monitor);
69                     }
70                 }
71             }
72             return false;
73         }
74     }
75
76     protected IProject[] build(int kind, Map JavaDoc args, IProgressMonitor monitor)
77         throws CoreException {
78         IResourceDelta delta = null;
79         if (kind != FULL_BUILD)
80             delta = getDelta(getProject());
81
82         if (delta == null || kind == FULL_BUILD) {
83             if (isInterestingProject(getProject()))
84                 compileSchemasIn(getProject(), monitor);
85         } else {
86             delta.accept(new DeltaVisitor(monitor));
87         }
88         return new IProject[0];
89     }
90     
91     private boolean isInterestingProject(IProject project) {
92         return PDE.hasPluginNature(project) && !WorkspaceModelManager.isBinaryProject(project);
93     }
94
95     private void compileFile(IFile file, IProgressMonitor monitor) {
96         
97         String JavaDoc message = NLS.bind(PDECoreMessages.Builders_Schema_compiling, file.getFullPath().toString());
98         monitor.subTask(message);
99         
100         SchemaErrorReporter reporter = new SchemaErrorReporter(file);
101         DefaultSAXParser.parse(file, reporter);
102         reporter.validateContent(monitor);
103
104         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
105         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
106         try {
107             boolean generateDoc = CompilerFlags.getBoolean(file.getProject(), CompilerFlags.S_CREATE_DOCS);
108             if (reporter.getDocumentRoot() != null && reporter.getErrorCount() == 0 && generateDoc) {
109                 ensureFoldersExist(file.getProject(), getDocLocation(file));
110                 String JavaDoc outputFileName = getOutputFileName(file);
111                 IWorkspace workspace = file.getWorkspace();
112                 Path outputPath = new Path(outputFileName);
113
114                 SchemaDescriptor desc = new SchemaDescriptor(file, false);
115                 Schema schema = (Schema)desc.getSchema(false);
116                 
117                 SchemaTransformer transformer = new SchemaTransformer();
118                 transformer.transform(schema, writer);
119                 
120                 ByteArrayInputStream JavaDoc target = new ByteArrayInputStream JavaDoc(swriter.toString().getBytes("UTF8")); //$NON-NLS-1$
121
IFile outputFile = workspace.getRoot().getFile(outputPath);
122                 if (!workspace.getRoot().exists(outputPath)) {
123                     outputFile.create(target, true, monitor);
124                 } else {
125                     outputFile.setContents(target, true, false, monitor);
126                 }
127             }
128         } catch (UnsupportedEncodingException JavaDoc e) {
129             PDECore.logException(e);
130         } catch (CoreException e) {
131             PDECore.logException(e);
132         } finally {
133             writer.close();
134             try {
135                 swriter.close();
136             } catch (IOException JavaDoc e1) {
137             }
138         }
139         monitor.subTask(PDECoreMessages.Builders_updating);
140         monitor.done();
141     }
142     
143     private void ensureFoldersExist(IProject project, String JavaDoc pathName) throws CoreException {
144         IPath path = new Path(pathName);
145         IContainer parent=project;
146         
147         for (int i = 0; i < path.segmentCount(); i++) {
148             String JavaDoc segment = path.segment(i);
149             IFolder folder = parent.getFolder(new Path(segment));
150             if (!folder.exists()) {
151                 folder.create(true, true, null);
152             }
153             parent = folder;
154         }
155     }
156
157     private void compileSchemasIn(IContainer container, IProgressMonitor monitor) throws CoreException {
158         monitor.subTask(PDECoreMessages.Builders_Schema_compilingSchemas);
159         IResource[] members = container.members();
160         for (int i = 0; i < members.length; i++) {
161             IResource member = members[i];
162             if (member instanceof IContainer)
163                 compileSchemasIn((IContainer) member, monitor);
164             else if (member instanceof IFile && isSchemaFile((IFile) member)) {
165                 compileFile((IFile) member, monitor);
166             }
167         }
168         monitor.done();
169     }
170     
171     private String JavaDoc getDocLocation(IFile file) {
172         return CompilerFlags.getString(file.getProject(), CompilerFlags.S_DOC_FOLDER);
173     }
174     
175     private String JavaDoc getOutputFileName(IFile file) {
176         String JavaDoc fileName = file.getName();
177         int dot = fileName.lastIndexOf('.');
178         String JavaDoc pageName = fileName.substring(0, dot) + ".html"; //$NON-NLS-1$
179
String JavaDoc mangledPluginId = getMangledPluginId(file);
180         if (mangledPluginId != null)
181             pageName = mangledPluginId + "_" + pageName; //$NON-NLS-1$
182
IPath path = file.getProject().getFullPath().append(
183                 getDocLocation(file)).append(pageName);
184         return path.toString();
185     }
186     
187     private String JavaDoc getMangledPluginId(IFile file) {
188         IProject project = file.getProject();
189         IModel model = PluginRegistry.findModel(project);
190         if (model instanceof IPluginModelBase) {
191             IPluginBase plugin = ((IPluginModelBase) model).getPluginBase();
192             if (plugin != null) {
193                 return plugin.getId().replace('.', '_');
194             }
195         }
196         return null;
197     }
198     
199     private boolean isSchemaFile(IFile file) {
200         return "exsd".equals(file.getFileExtension()); //$NON-NLS-1$
201
}
202     
203     private void removeOutputFile(IFile file, IProgressMonitor monitor) {
204         String JavaDoc outputFileName = getOutputFileName(file);
205         monitor.subTask(NLS.bind(PDECoreMessages.Builders_Schema_removing, outputFileName));
206
207         IWorkspace workspace = file.getWorkspace();
208         IPath path = new Path(outputFileName);
209         if (workspace.getRoot().exists(path)) {
210             IFile outputFile = workspace.getRoot().getFile(path);
211             if (outputFile != null) {
212                 try {
213                     outputFile.delete(true, true, monitor);
214                 } catch (CoreException e) {
215                     PDECore.logException(e);
216                 }
217             }
218         }
219         monitor.done();
220     }
221 }
222
Popular Tags