1 11 package org.eclipse.pde.internal.ui.wizards.xhtml; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IWorkspaceRunnable; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.MultiStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.jface.dialogs.ErrorDialog; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.pde.core.IBaseModel; 26 import org.eclipse.pde.core.plugin.IExtensions; 27 import org.eclipse.pde.core.plugin.IExtensionsModelFactory; 28 import org.eclipse.pde.core.plugin.IPluginElement; 29 import org.eclipse.pde.core.plugin.IPluginExtension; 30 import org.eclipse.pde.core.plugin.IPluginModelBase; 31 import org.eclipse.pde.core.plugin.IPluginObject; 32 import org.eclipse.pde.internal.ui.PDEUIMessages; 33 import org.eclipse.pde.internal.ui.util.ModelModification; 34 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 35 import org.eclipse.pde.internal.ui.wizards.xhtml.TocReplaceTable.TocReplaceEntry; 36 import org.eclipse.swt.widgets.Shell; 37 38 39 public class XHTMLConversionOperation implements IWorkspaceRunnable { 40 41 private class AddDynamicHelpExtensions extends ModelModification { 42 43 private String F_CP_POINT = "org.eclipse.help.contentProducer"; private String F_LSP_POINT = "org.eclipse.help.base.luceneSearchParticipants"; private String F_BIND_ELEMENT = "binding"; private String F_PROD_ATT_NAME = "producerId"; private String F_PROD_ATT_VALUE = "org.eclipse.help.dynamic"; private String F_PART_ATT_NAME = "participantId"; private String F_PART_ATT_VALUE = "org.eclipse.help.base.xhtml"; 51 public AddDynamicHelpExtensions(IProject project) { 52 super(project); 53 } 54 55 protected void modifyModel(IBaseModel baseModel, IProgressMonitor monitor) throws CoreException { 56 if (!(baseModel instanceof IPluginModelBase)) 57 return; 58 IPluginModelBase model = (IPluginModelBase)baseModel; 59 IExtensions ext = model.getExtensions(); 60 IPluginExtension[] extensions = ext.getExtensions(); 61 62 boolean contentProducerFound = false; 63 boolean luceneSearchParticipantFound = false; 64 for (int i = 0; i < extensions.length; i++) { 65 String point = extensions[i].getPoint(); 66 if (point.equals(F_CP_POINT)) { 67 contentProducerFound = true; 68 ensureExists(extensions[i], F_BIND_ELEMENT, F_PROD_ATT_NAME, F_PROD_ATT_VALUE); 69 } else if (point.equals(F_LSP_POINT)) { 70 luceneSearchParticipantFound = true; 71 ensureExists(extensions[i], F_BIND_ELEMENT, F_PART_ATT_NAME, F_PART_ATT_VALUE); 72 } 73 if (luceneSearchParticipantFound && contentProducerFound) 74 break; 75 } 76 77 IExtensionsModelFactory factory = model.getFactory(); 78 if (!contentProducerFound) { 79 IPluginExtension extension = factory.createExtension(); 80 extension.setPoint(F_CP_POINT); 81 IPluginElement element = factory.createElement(extension); 82 element.setName(F_BIND_ELEMENT); 83 element.setAttribute(F_PROD_ATT_NAME, F_PROD_ATT_VALUE); 84 extension.add(element); 85 model.getPluginBase().add(extension); 86 } 87 if (!luceneSearchParticipantFound) { 88 IPluginExtension extension = factory.createExtension(); 89 extension.setPoint(F_LSP_POINT); 90 IPluginElement element = factory.createElement(extension); 91 element.setName(F_BIND_ELEMENT); 92 element.setAttribute(F_PART_ATT_NAME, F_PART_ATT_VALUE); 93 extension.add(element); 94 model.getPluginBase().add(extension); 95 } 96 } 97 98 private void ensureExists(IPluginExtension extension, String elementName, String attrName, String attrValue) throws CoreException { 99 IPluginObject[] children = extension.getChildren(); 100 boolean foundElement = false; 101 for (int i = 0; i < children.length; i++) { 102 if (children[i] instanceof IPluginElement) { 103 IPluginElement element = (IPluginElement)children[i]; 104 if (element.getName().equals(elementName)) { 105 foundElement = attrValue.equals(element.getAttribute(attrName)); 106 if (foundElement) 107 break; 108 } 109 } 110 } 111 if (!foundElement) { 112 IPluginElement element = extension.getPluginModel().getFactory().createElement(extension); 113 element.setName(elementName); 114 element.setAttribute(attrName, attrValue); 115 extension.add(element); 116 } 117 } 118 119 } 120 121 private TocReplaceEntry[] fEntries; 122 private Shell fShell; 123 private XHTMLConverter fConverter; 124 125 public XHTMLConversionOperation(TocReplaceEntry[] entries, Shell shell) { 126 fShell = shell; 127 fEntries = entries; 128 fConverter = new XHTMLConverter(XHTMLConverter.XHTML_TRANSITIONAL); 129 } 130 131 public void run(IProgressMonitor monitor) throws CoreException { 132 MultiStatus ms = new MultiStatus( 133 "org.eclipse.pde.ui", IStatus.OK, PDEUIMessages.XHTMLConversionOperation_failed, null); 135 136 monitor.beginTask(PDEUIMessages.XHTMLConversionOperation_taskName, fEntries.length * 3); 137 138 HashSet touchedProjects = new HashSet (); 139 for (int i = 0; i < fEntries.length; i++) 140 convert(fEntries[i], ms, monitor, touchedProjects); 141 142 Iterator it = touchedProjects.iterator(); 143 while (it.hasNext()) 144 PDEModelUtility.modifyModel(new AddDynamicHelpExtensions((IProject)it.next()), monitor); 145 monitor.worked(1); 146 147 checkFailed(ms); 148 } 149 150 private void checkFailed(final MultiStatus ms) { 151 if (ms.getChildren().length > 0) { 152 fShell.getDisplay().syncExec(new Runnable () { 153 public void run() { 154 String message; 155 if (ms.getChildren().length == 1) 156 message = PDEUIMessages.XHTMLConversionOperation_1prob; 157 else 158 message = NLS.bind(PDEUIMessages.XHTMLConversionOperation_multiProb, Integer.toString(ms.getChildren().length)); 159 ErrorDialog.openError(fShell, PDEUIMessages.XHTMLConversionOperation_title, message, ms); 160 } 161 }); 162 } 163 } 164 165 private void convert(TocReplaceEntry entry, MultiStatus ms, IProgressMonitor monitor, HashSet touchedProjects) { 166 if (monitor.isCanceled()) 167 return; 168 monitor.subTask(NLS.bind(PDEUIMessages.XHTMLConversionOperation_createXHTML, entry.getHref())); 169 try { 170 fConverter.convert(entry.getEntryFile(), monitor); 171 IProject project = entry.getEntryFile().getProject(); 172 if (!touchedProjects.contains(project)) 173 touchedProjects.add(project); 174 } catch (CoreException e) { 175 ms.add(new Status( 176 IStatus.WARNING, "org.eclipse.pde.ui", IStatus.OK, entry.getTocFile().getName(), e)); 178 } 179 } 180 181 182 } 183 | Popular Tags |