KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > xhtml > GetUnconvertedOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.xhtml;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.ArrayList JavaDoc;
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 JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43
44 public class GetUnconvertedOperation implements IRunnableWithProgress {
45
46     private static final String JavaDoc F_TOC_EXTENSION = "org.eclipse.help.toc"; //$NON-NLS-1$
47
private static final String JavaDoc F_HTML_CONTENT = "org.eclipse.help.html"; //$NON-NLS-1$
48

49     private IFile fBaseFile;
50     private TocReplaceTable fReplaceTable = new TocReplaceTable();
51     private IContentTypeManager fContentManager = Platform.getContentTypeManager();
52     
53     public GetUnconvertedOperation(ISelection selection) {
54         Object JavaDoc object = ((IStructuredSelection)selection).getFirstElement();
55         if (object instanceof IProject) {
56             fBaseFile = ((IProject)object).getFile("plugin.xml"); //$NON-NLS-1$
57
if (!fBaseFile.exists())
58                 fBaseFile = ((IProject)object).getFile("fragment.xml"); //$NON-NLS-1$
59
}
60     }
61
62     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
63             InterruptedException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc href = null;
108         String JavaDoc nodeName = element.getNodeName();
109         if (nodeName.equals("topic")) //$NON-NLS-1$
110
href = element.getAttribute("href"); //$NON-NLS-1$
111
else if (nodeName.equals("toc")) //$NON-NLS-1$
112
href = element.getAttribute("topic"); //$NON-NLS-1$
113
if (href == null || href.length() == 0)
114             return;
115         IFile htmlFile = project.getFile(href);
116         inspectLocationForHTML(htmlFile, element.getAttribute("label"), file); //$NON-NLS-1$
117
}
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 JavaDoc ext = file.getFileExtension();
130                     if (ext.equalsIgnoreCase("html") || ext.equalsIgnoreCase("htm")) //$NON-NLS-1$ //$NON-NLS-2$
131
inspectLocationForHTML(file, null, root);
132                 }
133             }
134         } catch (CoreException e) {
135         }
136     }
137     
138     private void inspectLocationForHTML(IFile htmlFile, String JavaDoc label, IResource tocFile) {
139         if (!htmlFile.exists())
140             return;
141         InputStream JavaDoc 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); //$NON-NLS-1$
147
} catch (CoreException e) {
148             PDEPlugin.log(e);
149         } catch (IOException JavaDoc e) {
150         } finally {
151             if (stream != null) {
152                 try {
153                     stream.close();
154                 } catch (IOException JavaDoc e) {
155                 }
156             }
157         }
158     }
159     
160     private ArrayList JavaDoc grabTocFiles(IProgressMonitor monitor, IPluginModelBase pluginModel) {
161         IPluginExtension[] extensions = pluginModel.getPluginBase().getExtensions();
162         ArrayList JavaDoc tocLocations = new ArrayList JavaDoc();
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") //$NON-NLS-1$
168
&& children[j] instanceof IPluginElement) {
169                         IPluginElement element = (IPluginElement) children[j];
170                         IPluginAttribute fileAttrib = element.getAttribute("file"); //$NON-NLS-1$
171
IProject project = fBaseFile.getProject();
172                         if (fileAttrib != null) {
173                             String JavaDoc 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"); //$NON-NLS-1$
178
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