KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javadocexport > JavadocWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.ui.javadocexport;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24 import javax.xml.transform.OutputKeys JavaDoc;
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.dom.DOMSource JavaDoc;
29 import javax.xml.transform.stream.StreamResult JavaDoc;
30
31 import org.w3c.dom.DOMException JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34
35 import org.eclipse.core.resources.IProject;
36 import org.eclipse.core.resources.ResourcesPlugin;
37
38 import org.eclipse.core.runtime.Assert;
39 import org.eclipse.core.runtime.IPath;
40 import org.eclipse.core.runtime.Path;
41
42
43 import org.eclipse.jdt.core.ICompilationUnit;
44 import org.eclipse.jdt.core.IJavaElement;
45 import org.eclipse.jdt.core.IJavaProject;
46 import org.eclipse.jdt.core.IPackageFragment;
47
48 public class JavadocWriter {
49     
50     private static final char PATH_SEPARATOR= '/'; // use forward slash for all platforms
51

52     private final OutputStream JavaDoc fOutputStream;
53     private final IJavaProject[] fJavaProjects;
54     private final IPath fBasePath;
55     private final String JavaDoc fEncoding;
56
57     /**
58      * Create a JavadocWriter on the given output stream.
59      * It is the client's responsibility to close the output stream.
60      * @param basePath The base path to which all path will be made relative (if
61      * possible). If <code>null</code>, paths are not made relative.
62      */

63     public JavadocWriter(OutputStream JavaDoc outputStream, String JavaDoc encoding, IPath basePath, IJavaProject[] projects) {
64         Assert.isNotNull(outputStream);
65         Assert.isNotNull(encoding);
66         fOutputStream= new BufferedOutputStream JavaDoc(outputStream);
67         fEncoding= encoding;
68         fBasePath= basePath;
69         fJavaProjects= projects;
70     }
71
72     public void writeXML(JavadocOptionsManager store) throws ParserConfigurationException JavaDoc, TransformerException JavaDoc {
73
74         DocumentBuilder JavaDoc docBuilder= null;
75         DocumentBuilderFactory JavaDoc factory= DocumentBuilderFactory.newInstance();
76         factory.setValidating(false);
77         docBuilder= factory.newDocumentBuilder();
78         Document JavaDoc document= docBuilder.newDocument();
79
80         // Create the document
81
Element JavaDoc project= document.createElement("project"); //$NON-NLS-1$
82
document.appendChild(project);
83
84         project.setAttribute("default", "javadoc"); //$NON-NLS-1$ //$NON-NLS-2$
85

86         Element JavaDoc javadocTarget= document.createElement("target"); //$NON-NLS-1$
87
project.appendChild(javadocTarget);
88         javadocTarget.setAttribute("name", "javadoc"); //$NON-NLS-1$ //$NON-NLS-2$
89

90         Element JavaDoc xmlJavadocDesc= document.createElement("javadoc"); //$NON-NLS-1$
91
javadocTarget.appendChild(xmlJavadocDesc);
92
93         if (!store.isFromStandard())
94             xmlWriteDoclet(store, document, xmlJavadocDesc);
95         else
96             xmlWriteJavadocStandardParams(store, document, xmlJavadocDesc);
97
98
99         // Write the document to the stream
100
Transformer JavaDoc transformer=TransformerFactory.newInstance().newTransformer();
101         transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
102
transformer.setOutputProperty(OutputKeys.ENCODING, fEncoding);
103         transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
104
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4"); //$NON-NLS-1$ //$NON-NLS-2$
105
DOMSource JavaDoc source = new DOMSource JavaDoc(document);
106         StreamResult JavaDoc result = new StreamResult JavaDoc(fOutputStream);
107         transformer.transform(source, result);
108
109     }
110
111     //writes ant file, for now only worry about one project
112
private void xmlWriteJavadocStandardParams(JavadocOptionsManager store, Document JavaDoc document, Element JavaDoc xmlJavadocDesc) throws DOMException JavaDoc {
113
114         String JavaDoc destination= getPathString(Path.fromOSString(store.getDestination()));
115
116         xmlJavadocDesc.setAttribute(store.DESTINATION, destination);
117         xmlJavadocDesc.setAttribute(store.VISIBILITY, store.getAccess());
118         String JavaDoc source= store.getSource();
119         if (source.length() > 0 && !source.equals("-")) { //$NON-NLS-1$
120
xmlJavadocDesc.setAttribute(store.SOURCE, store.getSource());
121         }
122         xmlJavadocDesc.setAttribute(store.USE, booleanToString(store.getBoolean("use"))); //$NON-NLS-1$
123
xmlJavadocDesc.setAttribute(store.NOTREE, booleanToString(store.getBoolean("notree"))); //$NON-NLS-1$
124
xmlJavadocDesc.setAttribute(store.NONAVBAR, booleanToString(store.getBoolean("nonavbar"))); //$NON-NLS-1$
125
xmlJavadocDesc.setAttribute(store.NOINDEX, booleanToString(store.getBoolean("noindex"))); //$NON-NLS-1$
126
xmlJavadocDesc.setAttribute(store.SPLITINDEX, booleanToString(store.getBoolean("splitindex"))); //$NON-NLS-1$
127
xmlJavadocDesc.setAttribute(store.AUTHOR, booleanToString(store.getBoolean("author"))); //$NON-NLS-1$
128
xmlJavadocDesc.setAttribute(store.VERSION, booleanToString(store.getBoolean("version"))); //$NON-NLS-1$
129
xmlJavadocDesc.setAttribute(store.NODEPRECATEDLIST, booleanToString(store.getBoolean("nodeprecatedlist"))); //$NON-NLS-1$
130
xmlJavadocDesc.setAttribute(store.NODEPRECATED, booleanToString(store.getBoolean("nodeprecated"))); //$NON-NLS-1$
131

132
133         //set the packages and source files
134
List JavaDoc packages= new ArrayList JavaDoc();
135         List JavaDoc sourcefiles= new ArrayList JavaDoc();
136         sortSourceElement(store.getSourceElements(), sourcefiles, packages);
137         if (!packages.isEmpty())
138             xmlJavadocDesc.setAttribute(store.PACKAGENAMES, toSeparatedList(packages));
139
140         if (!sourcefiles.isEmpty())
141             xmlJavadocDesc.setAttribute(store.SOURCEFILES, toSeparatedList(sourcefiles));
142
143         xmlJavadocDesc.setAttribute(store.SOURCEPATH, getPathString(store.getSourcepath()));
144         xmlJavadocDesc.setAttribute(store.CLASSPATH, getPathString(store.getClasspath()));
145
146         String JavaDoc overview= store.getOverview();
147         if (overview.length() > 0)
148             xmlJavadocDesc.setAttribute(store.OVERVIEW, overview);
149
150         String JavaDoc styleSheet= store.getStyleSheet();
151         if (styleSheet.length() > 0)
152             xmlJavadocDesc.setAttribute(store.STYLESHEETFILE, styleSheet);
153
154         String JavaDoc title= store.getTitle();
155         if (title.length() > 0)
156             xmlJavadocDesc.setAttribute(store.TITLE, title);
157
158         
159         String JavaDoc vmArgs= store.getVMParams();
160         String JavaDoc additionalArgs= store.getAdditionalParams();
161         if (vmArgs.length() + additionalArgs.length() > 0) {
162             String JavaDoc str= vmArgs + ' ' + additionalArgs;
163             xmlJavadocDesc.setAttribute(store.EXTRAOPTIONS, str);
164         }
165
166         String JavaDoc[] hrefs= store.getHRefs();
167         for (int i= 0; i < hrefs.length; i++) {
168             Element JavaDoc links= document.createElement("link"); //$NON-NLS-1$
169
xmlJavadocDesc.appendChild(links);
170             links.setAttribute(store.HREF, hrefs[i]);
171         }
172     }
173
174     private void sortSourceElement(IJavaElement[] iJavaElements, List JavaDoc sourcefiles, List JavaDoc packages) {
175         for (int i= 0; i < iJavaElements.length; i++) {
176             IJavaElement element= iJavaElements[i];
177             IPath p= element.getResource().getLocation();
178             if (p == null)
179                 continue;
180
181             if (element instanceof ICompilationUnit) {
182                 String JavaDoc relative= getPathString(p);
183                 sourcefiles.add(relative);
184             } else if (element instanceof IPackageFragment) {
185                 packages.add(element.getElementName());
186             }
187         }
188     }
189
190     private String JavaDoc getPathString(IPath[] paths) {
191         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
192         
193         for (int i= 0; i < paths.length; i++) {
194             if (buf.length() != 0) {
195                 buf.append(File.pathSeparatorChar);
196             }
197             buf.append(getPathString(paths[i]));
198         }
199
200         if (buf.length() == 0) {
201             buf.append('.');
202         }
203         return buf.toString();
204     }
205
206     private boolean hasSameDevice(IPath p1, IPath p2) {
207         String JavaDoc dev= p1.getDevice();
208         if (dev == null) {
209             return p2.getDevice() == null;
210         }
211         return dev.equals(p2.getDevice());
212     }
213
214     //make the path relative to the base path
215
private String JavaDoc getPathString(IPath fullPath) {
216         if (fBasePath == null || !hasSameDevice(fullPath, fBasePath)) {
217             return fullPath.toOSString();
218         }
219         int matchingSegments= fBasePath.matchingFirstSegments(fullPath);
220         if (fBasePath.segmentCount() == matchingSegments) {
221             return getRelativePath(fullPath, matchingSegments);
222         }
223         for (int i= 0; i < fJavaProjects.length; i++) {
224             IProject proj= fJavaProjects[i].getProject();
225             IPath projLoc= proj.getLocation();
226             if (projLoc != null && projLoc.segmentCount() <= matchingSegments && projLoc.isPrefixOf(fullPath)) {
227                 return getRelativePath(fullPath, matchingSegments);
228             }
229         }
230         IPath workspaceLoc= ResourcesPlugin.getWorkspace().getRoot().getLocation();
231         if (workspaceLoc.segmentCount() <= matchingSegments && workspaceLoc.isPrefixOf(fullPath)) {
232             return getRelativePath(fullPath, matchingSegments);
233         }
234         return fullPath.toOSString();
235     }
236
237     private String JavaDoc getRelativePath(IPath fullPath, int matchingSegments) {
238         StringBuffer JavaDoc res= new StringBuffer JavaDoc();
239         int backSegments= fBasePath.segmentCount() - matchingSegments;
240         while (backSegments > 0) {
241             res.append(".."); //$NON-NLS-1$
242
res.append(PATH_SEPARATOR);
243             backSegments--;
244         }
245         int segCount= fullPath.segmentCount();
246         for (int i= matchingSegments; i < segCount; i++) {
247             if (i > matchingSegments) {
248                 res.append(PATH_SEPARATOR);
249             }
250             res.append(fullPath.segment(i));
251         }
252         return res.toString();
253     }
254
255     private void xmlWriteDoclet(JavadocOptionsManager store, Document JavaDoc document, Element JavaDoc xmlJavadocDesc) throws DOMException JavaDoc {
256
257         //set the packages and source files
258
List JavaDoc packages= new ArrayList JavaDoc();
259         List JavaDoc sourcefiles= new ArrayList JavaDoc();
260         sortSourceElement(store.getSourceElements(), sourcefiles, packages);
261         if (!packages.isEmpty())
262             xmlJavadocDesc.setAttribute(store.PACKAGENAMES, toSeparatedList(packages));
263
264         if (!sourcefiles.isEmpty())
265             xmlJavadocDesc.setAttribute(store.SOURCEFILES, toSeparatedList(sourcefiles));
266
267         xmlJavadocDesc.setAttribute(store.SOURCEPATH, getPathString(store.getSourcepath()));
268         xmlJavadocDesc.setAttribute(store.CLASSPATH, getPathString(store.getClasspath()));
269         xmlJavadocDesc.setAttribute(store.VISIBILITY, store.getAccess());
270
271         Element JavaDoc doclet= document.createElement("doclet"); //$NON-NLS-1$
272
xmlJavadocDesc.appendChild(doclet);
273         doclet.setAttribute(store.NAME, store.getDocletName());
274         doclet.setAttribute(store.PATH, store.getDocletPath());
275
276         String JavaDoc str= store.getOverview();
277         if (str.length() > 0)
278             xmlJavadocDesc.setAttribute(store.OVERVIEW, str);
279
280         str= store.getAdditionalParams();
281         if (str.length() > 0)
282             xmlJavadocDesc.setAttribute(store.EXTRAOPTIONS, str);
283
284     }
285
286     private String JavaDoc toSeparatedList(List JavaDoc packages) {
287         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
288         Iterator JavaDoc iter= packages.iterator();
289         int nAdded= 0;
290         while (iter.hasNext()) {
291             if (nAdded > 0) {
292                 buf.append(',');
293             }
294             nAdded++;
295             String JavaDoc curr= (String JavaDoc) iter.next();
296             buf.append(curr);
297         }
298         return buf.toString();
299     }
300
301     private String JavaDoc booleanToString(boolean bool) {
302         if (bool)
303             return "true"; //$NON-NLS-1$
304
else
305             return "false"; //$NON-NLS-1$
306
}
307
308     public void close() throws IOException JavaDoc {
309         if (fOutputStream != null) {
310             fOutputStream.close();
311         }
312     }
313
314 }
315
Popular Tags