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