1 11 package org.eclipse.pde.internal.ui.wizards.product; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.InputStreamReader ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IFolder; 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.FileLocator; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.core.runtime.IStatus; 31 import org.eclipse.core.runtime.Path; 32 import org.eclipse.core.runtime.Status; 33 import org.eclipse.osgi.util.NLS; 34 import org.eclipse.pde.core.IBaseModel; 35 import org.eclipse.pde.core.plugin.IExtensionsModelFactory; 36 import org.eclipse.pde.core.plugin.IPluginBase; 37 import org.eclipse.pde.core.plugin.IPluginElement; 38 import org.eclipse.pde.core.plugin.IPluginExtension; 39 import org.eclipse.pde.core.plugin.IPluginModelBase; 40 import org.eclipse.pde.core.plugin.PluginRegistry; 41 import org.eclipse.pde.internal.core.TargetPlatformHelper; 42 import org.eclipse.pde.internal.core.iproduct.IProduct; 43 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase; 44 import org.eclipse.pde.internal.ui.PDEPlugin; 45 import org.eclipse.pde.internal.ui.PDEUIMessages; 46 import org.eclipse.pde.internal.ui.util.ModelModification; 47 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 48 import org.eclipse.pde.internal.ui.wizards.templates.ControlStack; 49 import org.eclipse.pde.ui.templates.IVariableProvider; 50 import org.eclipse.swt.widgets.Shell; 51 52 public class ProductIntroOperation extends BaseManifestOperation implements IVariableProvider { 53 54 protected String fIntroId; 55 private Shell fShell; 56 private IProduct fProduct; 57 private IProject fProject; 58 private static final String INTRO_POINT = "org.eclipse.ui.intro"; private static final String INTRO_CONFIG_POINT = "org.eclipse.ui.intro.config"; private static final String INTRO_CLASS = "org.eclipse.ui.intro.config.CustomizableIntroPart"; private static final String KEY_PRODUCT_NAME = "productName"; 63 public ProductIntroOperation(IProduct product, String pluginId, String introId, Shell shell) { 64 super(shell, pluginId); 65 fIntroId = introId; 66 fProduct = product; 67 fProject = PluginRegistry.findModel(pluginId).getUnderlyingResource().getProject(); 68 } 69 70 public void run(IProgressMonitor monitor) throws InvocationTargetException , 71 InterruptedException { 72 try { 73 IFile file = getFile(); 74 if (!file.exists()) { 75 createNewFile(file); 76 } else { 77 modifyExistingFile(file, monitor); 78 } 79 updateSingleton(monitor); 80 generateFiles(monitor); 81 } catch (CoreException e) { 82 throw new InvocationTargetException (e); 83 } 84 } 85 86 private void createNewFile(IFile file) throws CoreException { 87 WorkspacePluginModelBase model = (WorkspacePluginModelBase) getModel(file); 88 IPluginBase base = model.getPluginBase(); 89 base.setSchemaVersion(TargetPlatformHelper.getTargetVersion() < 3.2 ? "3.0" : "3.2"); base.add(createIntroExtension(model)); 91 base.add(createIntroConfigExtension(model)); 92 model.save(); 93 } 94 95 private IPluginExtension createIntroExtension(IPluginModelBase model) 96 throws CoreException { 97 IPluginExtension extension = model.getFactory().createExtension(); 98 extension.setPoint(INTRO_POINT); 99 extension.add(createIntroExtensionContent(extension)); 100 extension.add(createIntroBindingExtensionContent(extension)); 101 return extension; 102 } 103 104 private IPluginExtension createIntroConfigExtension(IPluginModelBase model) 105 throws CoreException { 106 IPluginExtension extension = model.getFactory().createExtension(); 107 extension.setPoint(INTRO_CONFIG_POINT); 108 extension.add(createIntroConfigExtensionContent(extension)); 109 return extension; 110 } 111 112 private IPluginElement createIntroExtensionContent(IPluginExtension extension) throws CoreException { 113 IPluginElement element = extension.getModel().getFactory().createElement(extension); 114 element.setName("intro"); element.setAttribute("id", fIntroId); element.setAttribute("class", INTRO_CLASS); return element; 118 } 119 120 private IPluginElement createIntroBindingExtensionContent(IPluginExtension extension) throws CoreException { 121 IPluginElement element = extension.getModel().getFactory().createElement(extension); 122 element.setName("introProductBinding"); element.setAttribute("productId", fProduct.getId()); element.setAttribute("introId", fIntroId); return element; 126 } 127 128 private IPluginElement createIntroConfigExtensionContent( 129 IPluginExtension extension) throws CoreException { 130 IPluginElement element = extension.getModel().getFactory() 131 .createElement(extension); 132 element.setName("config"); element.setAttribute("id", fPluginId + ".introConfigId"); element.setAttribute("introId", fIntroId); element.setAttribute("content", "introContent.xml"); element.add(createPresentationElement(element)); 137 138 return element; 139 } 140 141 private IPluginElement createPresentationElement(IPluginElement parent) 142 throws CoreException { 143 IPluginElement presentation = null; 144 IPluginElement implementation = null; 145 IExtensionsModelFactory factory = parent.getModel().getFactory(); 146 147 presentation = factory.createElement(parent); 148 presentation.setName("presentation"); presentation.setAttribute("home-page-id", "root"); 151 implementation = factory.createElement(presentation); 152 implementation.setName("implementation"); implementation.setAttribute("kind", "html"); implementation.setAttribute("style", "content/shared.css"); implementation.setAttribute("os", "win32,linux,macosx"); 157 presentation.add(implementation); 158 159 return presentation; 160 } 161 162 private void modifyExistingFile(IFile file, IProgressMonitor monitor) throws CoreException { 163 IStatus status = PDEPlugin.getWorkspace().validateEdit(new IFile[] { file }, fShell); 164 if (status.getSeverity() != IStatus.OK) 165 throw new CoreException(new Status(IStatus.ERROR, "org.eclipse.pde.ui", IStatus.ERROR, NLS.bind(PDEUIMessages.ProductDefinitionOperation_readOnly, fPluginId), null)); 167 ModelModification mod = new ModelModification(file) { 168 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { 169 if (!(model instanceof IPluginModelBase)) 170 return; 171 IPluginModelBase pluginModel = (IPluginModelBase)model; 172 IPluginExtension extension = getExtension(pluginModel, INTRO_POINT); 173 if (extension == null) { 174 extension = createIntroExtension(pluginModel); 175 pluginModel.getPluginBase().add(extension); 176 } else { 177 extension.add(createIntroExtensionContent(extension)); 178 extension.add(createIntroBindingExtensionContent(extension)); 179 } 180 181 extension = getExtension(pluginModel, INTRO_CONFIG_POINT); 182 if (extension == null) { 183 extension = createIntroConfigExtension(pluginModel); 184 pluginModel.getPluginBase().add(extension); 185 } else { 186 extension.add(createIntroConfigExtensionContent(extension)); 187 } 188 } 189 }; 190 PDEModelUtility.modifyModel(mod, monitor); 191 } 192 193 private IPluginExtension getExtension(IPluginModelBase model, 194 String tPoint) throws CoreException { 195 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 196 for (int i = 0; i < extensions.length; i++) { 197 String point = extensions[i].getPoint(); 198 if (tPoint.equals(point)) { 199 return extensions[i]; 200 } 201 } 202 return null; 203 } 204 205 protected void generateFiles(IProgressMonitor monitor) throws CoreException { 206 monitor.setTaskName(PDEUIMessages.AbstractTemplateSection_generating); 207 208 URL locationUrl = null; 209 try { 210 locationUrl = new URL (PDEPlugin.getDefault().getInstallURL(), "templates_3.1/intro/"); } catch (MalformedURLException e1) { return; } 212 if (locationUrl == null) { 213 return; 214 } 215 try { 216 locationUrl = FileLocator.resolve(locationUrl); 217 locationUrl = FileLocator.toFileURL(locationUrl); 218 } catch (IOException e) { 219 return; 220 } 221 if ("file".equals(locationUrl.getProtocol())) { File templateDirectory = new File (locationUrl.getFile()); 223 if (!templateDirectory.exists()) 224 return; 225 generateFiles(templateDirectory, fProject, true, false, monitor); 226 } 227 monitor.subTask(""); monitor.worked(1); 229 } 230 231 232 private void generateFiles(File src, IContainer dst, boolean firstLevel, 233 boolean binary, IProgressMonitor monitor) throws CoreException { 234 File [] members = src.listFiles(); 235 236 for (int i = 0; i < members.length; i++) { 237 File member = members[i]; 238 if (member.getName().equals("ext.xml") || member.getName().equals("java") || member.getName().equals("concept3.xhtml") || member.getName().equals("extContent.xhtml")) continue; 243 else if (member.isDirectory()) { 244 IContainer dstContainer = null; 245 if (firstLevel) { 246 binary = false; 247 if (member.getName().equals("bin")) { binary = true; 249 dstContainer = dst; 250 } 251 } 252 if (dstContainer == null) { 253 dstContainer = dst.getFolder(new Path(member.getName())); 254 } 255 if (dstContainer instanceof IFolder && !dstContainer.exists()) 256 ((IFolder) dstContainer).create(true, true, monitor); 257 generateFiles(member, dstContainer, false, binary, monitor); 258 } else { 259 if (firstLevel) 260 binary = false; 261 InputStream in = null; 262 try { 263 in = new FileInputStream (member); 264 copyFile(member.getName(), in, dst, binary, monitor); 265 } catch (IOException ioe) { 266 } finally { 267 if (in != null) 268 try { 269 in.close(); 270 } catch (IOException ioe2) { 271 } 272 } 273 } 274 } 275 } 276 277 private void copyFile(String fileName, InputStream input, IContainer dst, boolean binary, 278 IProgressMonitor monitor) throws CoreException { 279 280 monitor.subTask(fileName); 281 IFile dstFile = dst.getFile(new Path(fileName)); 282 283 try { 284 InputStream stream = getProcessedStream(fileName, input, binary); 285 if (dstFile.exists()) { 286 dstFile.setContents(stream, true, true, monitor); 287 } else { 288 dstFile.create(stream, true, monitor); 289 } 290 stream.close(); 291 292 } catch (IOException e) { 293 } 294 } 295 296 297 private InputStream getProcessedStream(String fileName, InputStream stream, 298 boolean binary) throws IOException , CoreException { 299 if (binary) 300 return stream; 301 302 InputStreamReader reader = new InputStreamReader (stream); 303 int bufsize = 1024; 304 char[] cbuffer = new char[bufsize]; 305 int read = 0; 306 StringBuffer keyBuffer = new StringBuffer (); 307 StringBuffer outBuffer = new StringBuffer (); 308 ControlStack preStack = new ControlStack(); 309 preStack.setValueProvider(this); 310 311 boolean replacementMode = false; 312 while (read != -1) { 313 read = reader.read(cbuffer); 314 for (int i = 0; i < read; i++) { 315 char c = cbuffer[i]; 316 317 if (preStack.getCurrentState() == false) { 318 continue; 319 } 320 321 if (c == '$') { 322 if (replacementMode) { 323 replacementMode = false; 324 String key = keyBuffer.toString(); 325 String value = key.length() == 0 ? "$" : getReplacementString(fileName, key); 327 outBuffer.append(value); 328 keyBuffer.delete(0, keyBuffer.length()); 329 } else { 330 replacementMode = true; 331 } 332 } else { 333 if (replacementMode) 334 keyBuffer.append(c); 335 else { 336 outBuffer.append(c); 337 } 338 } 339 } 340 } 341 return new ByteArrayInputStream (outBuffer.toString().getBytes( 342 fProject.getDefaultCharset())); 343 } 344 345 private String getReplacementString(String fileName, String key) { 346 if (key.equals(KEY_PRODUCT_NAME)) { 347 return fProduct.getName(); 348 } 349 return key; 350 } 351 352 public Object getValue(String variable) { 353 return null; 354 } 355 } 356 | Popular Tags |