1 11 package org.eclipse.pde.internal.ui.wizards.xhtml; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.util.ArrayList ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IFolder; 20 import org.eclipse.core.resources.IProject; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.core.runtime.content.IContentType; 26 import org.eclipse.core.runtime.content.IContentTypeManager; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.viewers.ISelection; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.pde.core.IBaseModel; 31 import org.eclipse.pde.core.plugin.IPluginAttribute; 32 import org.eclipse.pde.core.plugin.IPluginElement; 33 import org.eclipse.pde.core.plugin.IPluginExtension; 34 import org.eclipse.pde.core.plugin.IPluginModelBase; 35 import org.eclipse.pde.core.plugin.IPluginObject; 36 import org.eclipse.pde.internal.core.builders.ValidatingSAXParser; 37 import org.eclipse.pde.internal.core.builders.XMLErrorReporter; 38 import org.eclipse.pde.internal.ui.PDEPlugin; 39 import org.eclipse.pde.internal.ui.util.ModelModification; 40 import org.eclipse.pde.internal.ui.util.PDEModelUtility; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.NodeList ; 43 44 public class GetUnconvertedOperation implements IRunnableWithProgress { 45 46 private static final String F_TOC_EXTENSION = "org.eclipse.help.toc"; private static final String F_HTML_CONTENT = "org.eclipse.help.html"; 49 private IFile fBaseFile; 50 private TocReplaceTable fReplaceTable = new TocReplaceTable(); 51 private IContentTypeManager fContentManager = Platform.getContentTypeManager(); 52 53 public GetUnconvertedOperation(ISelection selection) { 54 Object object = ((IStructuredSelection)selection).getFirstElement(); 55 if (object instanceof IProject) { 56 fBaseFile = ((IProject)object).getFile("plugin.xml"); if (!fBaseFile.exists()) 58 fBaseFile = ((IProject)object).getFile("fragment.xml"); } 60 } 61 62 public void run(IProgressMonitor monitor) throws InvocationTargetException , 63 InterruptedException { 64 if (fBaseFile == null || !fBaseFile.exists()) 65 return; 66 ModelModification mod = new ModelModification(fBaseFile) { 67 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { 68 inspectModel(monitor, model); 69 } 70 }; 71 try { 72 PDEModelUtility.modifyModel(mod, monitor); 73 } catch (CoreException e) { 74 PDEPlugin.log(e); 75 } 76 } 77 78 private void inspectModel(IProgressMonitor monitor, IBaseModel baseModel) throws CoreException { 79 if (!(baseModel instanceof IPluginModelBase)) 80 return; 81 82 IPluginModelBase pluginModel = (IPluginModelBase)baseModel; 83 ArrayList tocs = grabTocFiles(monitor, pluginModel); 84 if (tocs.size() == 0) 85 return; 86 87 for (int i = 0; i < tocs.size(); i++) { 88 IFile file = (IFile)tocs.get(i); 89 XMLErrorReporter xml = new XMLErrorReporter(file); 90 ValidatingSAXParser.parse(file, xml); 91 Element root = xml.getDocumentRoot(); 92 if (root != null) 93 checkXML(root, file); 94 } 95 } 96 97 private void checkXML(Element root, IFile file) { 98 inspectElement(root, file); 99 NodeList children = root.getChildNodes(); 100 for (int i = 0; i < children.getLength(); i++) 101 if (children.item(i) instanceof Element) 102 checkXML((Element)children.item(i), file); 103 } 104 105 private void inspectElement(Element element, IFile file) { 106 IProject project = file.getProject(); 107 String href = null; 108 String nodeName = element.getNodeName(); 109 if (nodeName.equals("topic")) href = element.getAttribute("href"); else if (nodeName.equals("toc")) href = element.getAttribute("topic"); if (href == null || href.length() == 0) 114 return; 115 IFile htmlFile = project.getFile(href); 116 inspectLocationForHTML(htmlFile, element.getAttribute("label"), file); } 118 119 private void scanFolder(IFolder parent, IResource root) { 120 if (parent == null) 121 return; 122 try { 123 IResource[] members = parent.members(); 124 for (int i = 0; i < members.length; i++) { 125 if (members[i] instanceof IFolder) 126 scanFolder((IFolder)members[i], root); 127 else if (members[i] instanceof IFile) { 128 IFile file = (IFile)members[i]; 129 String ext = file.getFileExtension(); 130 if (ext.equalsIgnoreCase("html") || ext.equalsIgnoreCase("htm")) inspectLocationForHTML(file, null, root); 132 } 133 } 134 } catch (CoreException e) { 135 } 136 } 137 138 private void inspectLocationForHTML(IFile htmlFile, String label, IResource tocFile) { 139 if (!htmlFile.exists()) 140 return; 141 InputStream stream = null; 142 try { 143 stream = htmlFile.getContents(); 144 IContentType type = fContentManager.findContentTypeFor(stream, htmlFile.getName()); 145 if (type != null && type.getId().equals(F_HTML_CONTENT)) 146 fReplaceTable.addToTable(htmlFile.getProjectRelativePath().toString(), label, tocFile); } catch (CoreException e) { 148 PDEPlugin.log(e); 149 } catch (IOException e) { 150 } finally { 151 if (stream != null) { 152 try { 153 stream.close(); 154 } catch (IOException e) { 155 } 156 } 157 } 158 } 159 160 private ArrayList grabTocFiles(IProgressMonitor monitor, IPluginModelBase pluginModel) { 161 IPluginExtension[] extensions = pluginModel.getPluginBase().getExtensions(); 162 ArrayList tocLocations = new ArrayList (); 163 for (int i = 0; i < extensions.length && !monitor.isCanceled(); i++) { 164 if (extensions[i].getPoint().equals(F_TOC_EXTENSION)) { 165 IPluginObject[] children = extensions[i].getChildren(); 166 for (int j = 0; j < children.length && !monitor.isCanceled(); j++) { 167 if (children[j].getName().equals("toc") && children[j] instanceof IPluginElement) { 169 IPluginElement element = (IPluginElement) children[j]; 170 IPluginAttribute fileAttrib = element.getAttribute("file"); IProject project = fBaseFile.getProject(); 172 if (fileAttrib != null) { 173 String location = fileAttrib.getValue(); 174 IFile file = project.getFile(location); 175 if (file != null && file.exists()) { 176 tocLocations.add(file); 177 IPluginAttribute extraDir = element.getAttribute("extradir"); if (extraDir != null) { 179 IFolder folder = project.getFolder(extraDir.getValue()); 180 scanFolder(folder, folder); 181 } 182 } 183 } 184 } 185 } 186 } 187 } 188 return tocLocations; 189 } 190 191 public boolean needsWork() { 192 return fReplaceTable.numEntries() > 0; 193 } 194 195 public TocReplaceTable getChanges() { 196 return fReplaceTable; 197 } 198 } 199 | Popular Tags |