1 11 package org.eclipse.pde.internal.core.builders; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.io.StringWriter ; 17 import java.io.UnsupportedEncodingException ; 18 import java.util.Map ; 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 IFile candidate = (IFile) resource; 63 if (isSchemaFile(candidate)) { 64 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 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 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 swriter = new StringWriter (); 105 PrintWriter writer = new PrintWriter (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 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 target = new ByteArrayInputStream (swriter.toString().getBytes("UTF8")); 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 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 e1) { 137 } 138 } 139 monitor.subTask(PDECoreMessages.Builders_updating); 140 monitor.done(); 141 } 142 143 private void ensureFoldersExist(IProject project, String 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 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 getDocLocation(IFile file) { 172 return CompilerFlags.getString(file.getProject(), CompilerFlags.S_DOC_FOLDER); 173 } 174 175 private String getOutputFileName(IFile file) { 176 String fileName = file.getName(); 177 int dot = fileName.lastIndexOf('.'); 178 String pageName = fileName.substring(0, dot) + ".html"; String mangledPluginId = getMangledPluginId(file); 180 if (mangledPluginId != null) 181 pageName = mangledPluginId + "_" + pageName; IPath path = file.getProject().getFullPath().append( 183 getDocLocation(file)).append(pageName); 184 return path.toString(); 185 } 186 187 private String 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()); } 202 203 private void removeOutputFile(IFile file, IProgressMonitor monitor) { 204 String 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 |