KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Sebastian Davids, sdavids@gmx.de, - bug 38692
11  * Matt Chapman, mpchapman@gmail.com - 89977 Make JDT .java agnostic
12  *******************************************************************************/

13
14 package org.eclipse.jdt.internal.ui.javadocexport;
15
16 import java.io.File JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IAdaptable;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.MultiStatus;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.core.runtime.content.IContentType;
34
35 import org.eclipse.core.resources.IContainer;
36 import org.eclipse.core.resources.IFile;
37 import org.eclipse.core.resources.IResource;
38 import org.eclipse.core.resources.IWorkspaceRoot;
39 import org.eclipse.core.resources.ResourcesPlugin;
40
41 import org.eclipse.jface.dialogs.IDialogSettings;
42 import org.eclipse.jface.preference.IPreferenceStore;
43
44 import org.eclipse.jdt.core.ICompilationUnit;
45 import org.eclipse.jdt.core.IJavaElement;
46 import org.eclipse.jdt.core.IJavaProject;
47 import org.eclipse.jdt.core.IPackageFragment;
48 import org.eclipse.jdt.core.IPackageFragmentRoot;
49 import org.eclipse.jdt.core.JavaCore;
50 import org.eclipse.jdt.core.JavaModelException;
51
52 import org.eclipse.jdt.internal.corext.util.Messages;
53
54 import org.eclipse.jdt.launching.ExecutionArguments;
55 import org.eclipse.jdt.launching.IVMInstall;
56 import org.eclipse.jdt.launching.IVMInstallType;
57 import org.eclipse.jdt.launching.JavaRuntime;
58
59 import org.eclipse.jdt.ui.JavaUI;
60 import org.eclipse.jdt.ui.PreferenceConstants;
61
62 import org.eclipse.jdt.internal.ui.JavaPlugin;
63 import org.eclipse.jdt.internal.ui.JavaUIStatus;
64 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
65
66 import org.w3c.dom.Element JavaDoc;
67 import org.w3c.dom.Node JavaDoc;
68 import org.w3c.dom.NodeList JavaDoc;
69 import org.xml.sax.SAXException JavaDoc;
70
71 public class JavadocOptionsManager {
72
73     private IFile fXmlfile;
74
75     private StatusInfo fWizardStatus;
76
77     private String JavaDoc[] fJavadocCommandHistory;
78     
79     
80     private IJavaElement[] fSelectedElements;
81     private IJavaElement[] fInitialElements;
82
83     private String JavaDoc fAccess;
84     private String JavaDoc fDocletpath;
85     private String JavaDoc fDocletname;
86     private boolean fFromStandard;
87     private String JavaDoc fStylesheet;
88     private String JavaDoc fAdditionalParams;
89     private String JavaDoc fVMParams;
90     private String JavaDoc fOverview;
91     private String JavaDoc fTitle;
92     
93     private String JavaDoc[] fHRefs;
94
95     private IPath[] fSourcepath;
96     private IPath[] fClasspath;
97
98     private boolean fNotree;
99     private boolean fNoindex;
100     private boolean fSplitindex;
101     private boolean fNonavbar;
102     private boolean fNodeprecated;
103     private boolean fNoDeprecatedlist;
104     private boolean fAuthor;
105     private boolean fVersion;
106     private boolean fUse;
107     
108     private String JavaDoc fSource;
109
110     private boolean fOpenInBrowser;
111     
112     private RecentSettingsStore fRecentSettings;
113
114     //add-on for multi-project version
115
private String JavaDoc fDestination;
116     private String JavaDoc fAntpath;
117
118     public final String JavaDoc PRIVATE= "private"; //$NON-NLS-1$
119
public final String JavaDoc PROTECTED= "protected"; //$NON-NLS-1$
120
public final String JavaDoc PACKAGE= "package"; //$NON-NLS-1$
121
public final String JavaDoc PUBLIC= "public"; //$NON-NLS-1$
122

123     public final String JavaDoc USE= "use"; //$NON-NLS-1$
124
public final String JavaDoc NOTREE= "notree"; //$NON-NLS-1$
125
public final String JavaDoc NOINDEX= "noindex"; //$NON-NLS-1$
126
public final String JavaDoc NONAVBAR= "nonavbar"; //$NON-NLS-1$
127
public final String JavaDoc NODEPRECATED= "nodeprecated"; //$NON-NLS-1$
128
public final String JavaDoc NODEPRECATEDLIST= "nodeprecatedlist"; //$NON-NLS-1$
129
public final String JavaDoc VERSION= "version"; //$NON-NLS-1$
130
public final String JavaDoc AUTHOR= "author"; //$NON-NLS-1$
131
public final String JavaDoc SPLITINDEX= "splitindex"; //$NON-NLS-1$
132
public final String JavaDoc STYLESHEETFILE= "stylesheetfile"; //$NON-NLS-1$
133
public final String JavaDoc OVERVIEW= "overview"; //$NON-NLS-1$
134
public final String JavaDoc DOCLETNAME= "docletname"; //$NON-NLS-1$
135
public final String JavaDoc DOCLETPATH= "docletpath"; //$NON-NLS-1$
136
public final String JavaDoc SOURCEPATH= "sourcepath"; //$NON-NLS-1$
137
public final String JavaDoc CLASSPATH= "classpath"; //$NON-NLS-1$
138
public final String JavaDoc DESTINATION= "destdir"; //$NON-NLS-1$
139
public final String JavaDoc OPENINBROWSER= "openinbrowser"; //$NON-NLS-1$
140

141     public final String JavaDoc VISIBILITY= "access"; //$NON-NLS-1$
142
public final String JavaDoc PACKAGENAMES= "packagenames"; //$NON-NLS-1$
143
public final String JavaDoc SOURCEFILES= "sourcefiles"; //$NON-NLS-1$
144
public final String JavaDoc EXTRAOPTIONS= "additionalparam"; //$NON-NLS-1$
145
public final String JavaDoc VMOPTIONS= "vmparam"; //$NON-NLS-1$
146
//public final String JAVADOCCOMMAND= "javadoccommand"; //$NON-NLS-1$
147
public final String JavaDoc TITLE= "doctitle"; //$NON-NLS-1$
148
public final String JavaDoc HREF= "href"; //$NON-NLS-1$
149

150     public final String JavaDoc NAME= "name"; //$NON-NLS-1$
151
public final String JavaDoc PATH= "path"; //$NON-NLS-1$
152
public final String JavaDoc FROMSTANDARD= "fromStandard"; //$NON-NLS-1$
153
public final String JavaDoc ANTPATH= "antpath"; //$NON-NLS-1$
154
public final String JavaDoc SOURCE= "source"; //$NON-NLS-1$
155

156     private final String JavaDoc SECTION_JAVADOC= "javadoc"; //$NON-NLS-1$
157

158     private static final String JavaDoc JAVADOC_COMMAND_HISTORY= "javadoc_command_history"; //$NON-NLS-1$
159

160     public JavadocOptionsManager(IFile xmlJavadocFile, IDialogSettings dialogSettings, List JavaDoc currSelection) {
161         fXmlfile= xmlJavadocFile;
162         fWizardStatus= new StatusInfo();
163
164         IDialogSettings javadocSection= dialogSettings.getSection(SECTION_JAVADOC);
165         
166         String JavaDoc commandHistory= null;
167         if (javadocSection != null) {
168             commandHistory= javadocSection.get(JAVADOC_COMMAND_HISTORY);
169         }
170         if (commandHistory == null || commandHistory.length() == 0) {
171             commandHistory= initJavadocCommandDefault();
172         }
173         fJavadocCommandHistory= arrayFromFlatString(commandHistory);
174         
175         fRecentSettings= new RecentSettingsStore(javadocSection);
176         
177         if (xmlJavadocFile != null) {
178             try {
179                 JavadocReader reader= new JavadocReader(xmlJavadocFile.getContents());
180                 Element element= reader.readXML();
181                 if (element != null) {
182                     loadFromXML(element);
183                     return;
184                 }
185                 fWizardStatus.setWarning(JavadocExportMessages.JavadocOptionsManager_antfileincorrectCE_warning);
186             } catch (CoreException e) {
187                 JavaPlugin.log(e);
188                 fWizardStatus.setWarning(JavadocExportMessages.JavadocOptionsManager_antfileincorrectCE_warning);
189             } catch (IOException JavaDoc e) {
190                 JavaPlugin.log(e);
191                 fWizardStatus.setWarning(JavadocExportMessages.JavadocOptionsManager_antfileincorrectIOE_warning);
192             } catch (SAXException JavaDoc e) {
193                 fWizardStatus.setWarning(JavadocExportMessages.JavadocOptionsManager_antfileincorrectSAXE_warning);
194             }
195         }
196         if (javadocSection != null) {
197             loadFromDialogStore(javadocSection, currSelection);
198         } else {
199             loadDefaults(currSelection);
200         }
201     }
202
203
204     /*
205      * Returns the Java project that is parent top all selected elements or null if
206      * the elements are from several projects.
207      */

208     private IJavaProject getSingleProjectFromInitialSelection() {
209         IJavaProject res= null;
210         for (int i= 0; i < fInitialElements.length; i++) {
211             IJavaProject curr= fInitialElements[i].getJavaProject();
212             if (res == null) {
213                 res= curr;
214             } else if (!res.equals(curr)) {
215                 return null;
216             }
217         }
218         if (res != null && res.isOpen()) {
219             return res;
220         }
221         return null;
222     }
223     
224     
225     private void loadFromDialogStore(IDialogSettings settings, List JavaDoc sel) {
226         fInitialElements= getInitialElementsFromSelection(sel);
227         
228         IJavaProject project= getSingleProjectFromInitialSelection();
229
230         fAccess= settings.get(VISIBILITY);
231         if (fAccess == null)
232             fAccess= PROTECTED;
233
234         //this is defaulted to false.
235
fFromStandard= settings.getBoolean(FROMSTANDARD);
236
237         //doclet is loaded even if the standard doclet is being used
238
fDocletpath= settings.get(DOCLETPATH);
239         fDocletname= settings.get(DOCLETNAME);
240         if (fDocletpath == null || fDocletname == null) {
241             fFromStandard= true;
242             fDocletpath= ""; //$NON-NLS-1$
243
fDocletname= ""; //$NON-NLS-1$
244
}
245
246         
247         if (project != null) {
248             fAntpath= getRecentSettings().getAntpath(project);
249         } else {
250             fAntpath= settings.get(ANTPATH);
251             if (fAntpath == null) {
252                 fAntpath= ""; //$NON-NLS-1$
253
}
254         }
255
256         
257         if (project != null) {
258             fDestination= getRecentSettings().getDestination(project);
259         } else {
260             fDestination= settings.get(DESTINATION);
261             if (fDestination == null) {
262                 fDestination= ""; //$NON-NLS-1$
263
}
264         }
265
266         fTitle= settings.get(TITLE);
267         if (fTitle == null)
268             fTitle= ""; //$NON-NLS-1$
269

270         fStylesheet= settings.get(STYLESHEETFILE);
271         if (fStylesheet == null)
272             fStylesheet= ""; //$NON-NLS-1$
273

274         fVMParams= settings.get(VMOPTIONS);
275         if (fVMParams == null)
276             fVMParams= ""; //$NON-NLS-1$
277

278         fAdditionalParams= settings.get(EXTRAOPTIONS);
279         if (fAdditionalParams == null)
280             fAdditionalParams= ""; //$NON-NLS-1$
281

282         fOverview= settings.get(OVERVIEW);
283         if (fOverview == null)
284             fOverview= ""; //$NON-NLS-1$
285

286         fUse= loadBoolean(settings.get(USE));
287         fAuthor= loadBoolean(settings.get(AUTHOR));
288         fVersion= loadBoolean(settings.get(VERSION));
289         fNodeprecated= loadBoolean(settings.get(NODEPRECATED));
290         fNoDeprecatedlist= loadBoolean(settings.get(NODEPRECATEDLIST));
291         fNonavbar= loadBoolean(settings.get(NONAVBAR));
292         fNoindex= loadBoolean(settings.get(NOINDEX));
293         fNotree= loadBoolean(settings.get(NOTREE));
294         fSplitindex= loadBoolean(settings.get(SPLITINDEX));
295         fOpenInBrowser= loadBoolean(settings.get(OPENINBROWSER));
296         
297         fSource= settings.get(SOURCE);
298         if (project != null) {
299             fSource= project.getOption(JavaCore.COMPILER_SOURCE, true);
300         }
301         
302         if (project != null) {
303             fHRefs= getRecentSettings().getHRefs(project);
304         } else {
305             fHRefs= new String JavaDoc[0];
306         }
307     }
308
309
310     //loads defaults for wizard (nothing is stored)
311
private void loadDefaults(List JavaDoc sel) {
312         fInitialElements= getInitialElementsFromSelection(sel);
313         
314         IJavaProject project= getSingleProjectFromInitialSelection();
315
316         if (project != null) {
317             fAntpath= getRecentSettings().getAntpath(project);
318             fDestination= getRecentSettings().getDestination(project);
319             fHRefs= getRecentSettings().getHRefs(project);
320         } else {
321             fAntpath= ""; //$NON-NLS-1$
322
fDestination= ""; //$NON-NLS-1$
323
fHRefs= new String JavaDoc[0];
324         }
325         
326         fAccess= PUBLIC;
327
328         fDocletname= ""; //$NON-NLS-1$
329
fDocletpath= ""; //$NON-NLS-1$
330
fTitle= ""; //$NON-NLS-1$
331
fStylesheet= ""; //$NON-NLS-1$
332
fVMParams= ""; //$NON-NLS-1$
333
fAdditionalParams= ""; //$NON-NLS-1$
334
fOverview= ""; //$NON-NLS-1$
335

336         fUse= true;
337         fAuthor= true;
338         fVersion= true;
339         fNodeprecated= false;
340         fNoDeprecatedlist= false;
341         fNonavbar= false;
342         fNoindex= false;
343         fNotree= false;
344         fSplitindex= true;
345         fOpenInBrowser= false;
346         fSource= "1.3"; //$NON-NLS-1$
347
if (project != null) {
348             fSource= project.getOption(JavaCore.COMPILER_SOURCE, true);
349         }
350
351         //by default it is empty all project map to the empty string
352
fFromStandard= true;
353     }
354
355     private void loadFromXML(Element element) {
356
357         fAccess= element.getAttribute(VISIBILITY);
358         if (fAccess.length() == 0)
359             fAccess= PROTECTED;
360
361         //Since the selected packages are stored we must locate the project
362
String JavaDoc destination= element.getAttribute(DESTINATION);
363         fDestination= makeAbsolutePathFromRelative(new Path(destination)).toOSString();
364         fFromStandard= true;
365         fDocletname= ""; //$NON-NLS-1$
366
fDocletpath= ""; //$NON-NLS-1$
367

368         if (destination.length() == 0) {
369             NodeList JavaDoc list= element.getChildNodes();
370             for (int i= 0; i < list.getLength(); i++) {
371                 Node JavaDoc child= list.item(i);
372                 if (child.getNodeName().equals("doclet")) { //$NON-NLS-1$
373
fDocletpath= ((Element) child).getAttribute(PATH);
374                     fDocletname= ((Element) child).getAttribute(NAME);
375                     if (fDocletpath.length() != 0 || fDocletname.length() != 0) {
376                         fFromStandard= false;
377                     } else {
378                         fDocletname= ""; //$NON-NLS-1$
379
fDocletpath= ""; //$NON-NLS-1$
380
}
381                     break;
382                 }
383             }
384         }
385         
386         fInitialElements= getSelectedElementsFromAnt(element);
387         
388
389         //find all the links stored in the ant script
390
NodeList JavaDoc children= element.getChildNodes();
391         fHRefs= new String JavaDoc[children.getLength()];
392         for (int i= 0; i < fHRefs.length; i++) {
393             Node JavaDoc child= children.item(i);
394             if (child.getNodeName().equals("link")) { //$NON-NLS-1$
395
fHRefs[i]= ((Element) child).getAttribute(HREF);
396             }
397         }
398
399         // The ant file for generating the Javadoc is always local.
400
IPath p= fXmlfile.getLocation();
401         if (p != null)
402             fAntpath= p.toOSString();
403         else
404             fAntpath= ""; //$NON-NLS-1$
405

406         fStylesheet= element.getAttribute(STYLESHEETFILE);
407         fTitle= element.getAttribute(TITLE);
408         
409
410         StringBuffer JavaDoc additionals= new StringBuffer JavaDoc();
411         StringBuffer JavaDoc vmargs= new StringBuffer JavaDoc();
412         String JavaDoc extraOptions= element.getAttribute(EXTRAOPTIONS);
413         if (extraOptions.length() > 0) {
414             ExecutionArguments tokens= new ExecutionArguments("", extraOptions); //$NON-NLS-1$
415
String JavaDoc[] args= tokens.getProgramArgumentsArray();
416
417             boolean vmarg= false;
418             for (int i= 0; i < args.length; i++) {
419                 String JavaDoc curr= args[i];
420                 if (curr.length() > 0 && curr.charAt(0) == '-') {
421                     // an command
422
vmarg=(curr.length() > 1 && curr.charAt(1) == 'J');
423                 }
424                 if (vmarg) {
425                     vmargs.append(curr).append(' ');
426                 } else {
427                     additionals.append(curr).append(' ');
428                 }
429             }
430         }
431         
432         fAdditionalParams= additionals.toString();
433         fVMParams= vmargs.toString();
434         fOverview= element.getAttribute(OVERVIEW);
435
436         fUse= loadBoolean(element.getAttribute(USE));
437         fAuthor= loadBoolean(element.getAttribute(AUTHOR));
438         fVersion= loadBoolean(element.getAttribute(VERSION));
439         fNodeprecated= loadBoolean(element.getAttribute(NODEPRECATED));
440         fNoDeprecatedlist= loadBoolean(element.getAttribute(NODEPRECATEDLIST));
441         fNonavbar= loadBoolean(element.getAttribute(NONAVBAR));
442         fNoindex= loadBoolean(element.getAttribute(NOINDEX));
443         fNotree= loadBoolean(element.getAttribute(NOTREE));
444         fSplitindex= loadBoolean(element.getAttribute(SPLITINDEX));
445         
446         fSource= element.getAttribute(SOURCE);
447     }
448
449     /*
450      * Method creates an absolute path to the project. If the path is already
451      * absolute it returns the path. If it encounters any difficulties in
452      * creating the absolute path, the method returns null.
453      *
454      * @param path
455      * @return IPath
456      */

457     private IPath makeAbsolutePathFromRelative(IPath path) {
458         if (!path.isAbsolute()) {
459             if (fXmlfile == null) {
460                 return null;
461             }
462             // The XML file is always local. So ok to use getLocation here
463
IPath basePath= fXmlfile.getParent().getLocation(); // relative to the ant file location
464
if (basePath == null) {
465                 return null;
466             }
467             return basePath.append(path);
468         }
469         return path;
470     }
471     
472     private IContainer[] getSourceContainers(Element element) {
473         String JavaDoc sourcePaths= element.getAttribute(SOURCEPATH);
474         
475         if (sourcePaths.endsWith(File.pathSeparator)) {
476             sourcePaths += '.';
477         }
478         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
479         
480         ArrayList JavaDoc res= new ArrayList JavaDoc();
481         
482         String JavaDoc[] strings= sourcePaths.split(File.pathSeparator);
483         for (int i= 0; i < strings.length; i++) {
484             IPath path= makeAbsolutePathFromRelative(new Path(strings[i].trim()));
485             if (path != null) {
486                 IContainer[] containers= root.findContainersForLocation(path);
487                 for (int k= 0; k < containers.length; k++) {
488                     res.add(containers[k]);
489                 }
490             }
491             
492         }
493         return (IContainer[]) res.toArray(new IContainer[res.size()]);
494     }
495
496     private IJavaElement[] getSelectedElementsFromAnt(Element element) {
497         List JavaDoc res= new ArrayList JavaDoc();
498
499         // get all the packages listed in the ANT file
500
String JavaDoc packagenames= element.getAttribute(PACKAGENAMES);
501         if (packagenames != null) {
502             IContainer[] containers= getSourceContainers(element);
503             
504             StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(packagenames, ","); //$NON-NLS-1$
505
while (tokenizer.hasMoreTokens()) {
506                 IPath relPackagePath= new Path(tokenizer.nextToken().trim().replace('.', '/'));
507                 for (int i= 0; i < containers.length; i++) {
508                     IContainer curr= containers[i];
509                     IResource resource= curr.findMember(relPackagePath);
510                     if (resource != null) {
511                         IJavaElement javaElem= JavaCore.create(resource);
512                         if (javaElem instanceof IPackageFragment) {
513                             res.add(javaElem);
514                         }
515                     }
516                 }
517             }
518         }
519
520         //get all CompilationUnites listed in the ANT file
521
String JavaDoc sourcefiles= element.getAttribute(SOURCEFILES);
522         if (sourcefiles != null) {
523             IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
524             
525             StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(sourcefiles, ","); //$NON-NLS-1$
526
while (tokenizer.hasMoreTokens()) {
527                 String JavaDoc name= tokenizer.nextToken().trim();
528                 if (JavaCore.isJavaLikeFileName(name)) {
529                     IPath path= makeAbsolutePathFromRelative(new Path(name));
530                     //if unable to create an absolute path to the resource skip it
531
if (path != null) {
532                         IFile[] files= root.findFilesForLocation(path);
533                         for (int i= 0; i < files.length; i++) {
534                             IJavaElement el= JavaCore.createCompilationUnitFrom(files[i]);
535                             if (el != null) {
536                                 res.add(el);
537                             }
538                         }
539                     }
540                 }
541             }
542         }
543         return (IJavaElement[]) res.toArray(new IJavaElement[res.size()]);
544     }
545
546     /**
547      * @return Returns the javadocCommandHistory.
548      */

549     public String JavaDoc[] getJavadocCommandHistory() {
550         return fJavadocCommandHistory;
551     }
552
553     
554     //it is possible that the package list is empty
555
public StatusInfo getWizardStatus() {
556         return fWizardStatus;
557     }
558
559     public IJavaElement[] getInitialElements() {
560         return fInitialElements;
561     }
562
563     public IJavaElement[] getSourceElements() {
564         return fSelectedElements;
565     }
566
567     public String JavaDoc getAccess() {
568         return fAccess;
569     }
570
571     public String JavaDoc getAntpath() {
572         return fAntpath;
573     }
574
575     public boolean isFromStandard() {
576         return fFromStandard;
577     }
578
579     public String JavaDoc getDestination() {
580         return fDestination;
581     }
582
583     public String JavaDoc getDocletPath() {
584         return fDocletpath;
585     }
586
587     public String JavaDoc getDocletName() {
588         return fDocletname;
589     }
590
591     public String JavaDoc getStyleSheet() {
592         return fStylesheet;
593     }
594
595     public String JavaDoc getOverview() {
596         return fOverview;
597     }
598
599     public String JavaDoc getAdditionalParams() {
600         return fAdditionalParams;
601     }
602     
603     public String JavaDoc getVMParams() {
604         return fVMParams;
605     }
606
607     public IPath[] getClasspath() {
608         return fClasspath;
609     }
610
611     public IPath[] getSourcepath() {
612         return fSourcepath;
613     }
614
615     public String JavaDoc getTitle() {
616         return fTitle;
617     }
618
619     public boolean doOpenInBrowser() {
620         return fOpenInBrowser;
621     }
622     
623     public String JavaDoc[] getHRefs() {
624         return fHRefs;
625     }
626
627     public boolean getBoolean(String JavaDoc flag) {
628
629         if (flag.equals(AUTHOR))
630             return fAuthor;
631         else if (flag.equals(VERSION))
632             return fVersion;
633         else if (flag.equals(USE))
634             return fUse;
635         else if (flag.equals(NODEPRECATED))
636             return fNodeprecated;
637         else if (flag.equals(NODEPRECATEDLIST))
638             return fNoDeprecatedlist;
639         else if (flag.equals(NOINDEX))
640             return fNoindex;
641         else if (flag.equals(NOTREE))
642             return fNotree;
643         else if (flag.equals(SPLITINDEX))
644             return fSplitindex;
645         else if (flag.equals(NONAVBAR))
646             return fNonavbar;
647         else
648             return false;
649     }
650
651     private boolean loadBoolean(String JavaDoc value) {
652
653         if (value == null || value.length() == 0)
654             return false;
655         else {
656             if (value.equals("true")) //$NON-NLS-1$
657
return true;
658             else
659                 return false;
660         }
661     }
662     
663     private String JavaDoc flatPathList(IPath[] paths) {
664         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
665         for (int i= 0; i < paths.length; i++) {
666             if (i > 0) {
667                 buf.append(File.pathSeparatorChar);
668             }
669             buf.append(paths[i].toOSString());
670         }
671         return buf.toString();
672     }
673     
674     private String JavaDoc flatStringList(String JavaDoc[] paths) {
675         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
676         for (int i= 0; i < paths.length; i++) {
677             if (i > 0) {
678                 buf.append(File.pathSeparatorChar);
679             }
680             buf.append(paths[i]);
681         }
682         return buf.toString();
683     }
684     
685     private String JavaDoc[] arrayFromFlatString(String JavaDoc str) {
686         StringTokenizer JavaDoc tok= new StringTokenizer JavaDoc(str, File.pathSeparator);
687         String JavaDoc[] res= new String JavaDoc[tok.countTokens()];
688         for (int i= 0; i < res.length; i++) {
689             res[i]= tok.nextToken();
690         }
691         return res;
692     }
693     
694
695     public IStatus getArgumentArray(List JavaDoc vmArgs, List JavaDoc toolArgs) {
696         MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, JavadocExportMessages.JavadocOptionsManager_status_title, null);
697         
698         //bug 38692
699
vmArgs.add(getJavadocCommandHistory()[0]);
700         
701         if (fFromStandard) {
702             toolArgs.add("-d"); //$NON-NLS-1$
703
toolArgs.add(fDestination);
704         } else {
705             toolArgs.add("-doclet"); //$NON-NLS-1$
706
toolArgs.add(fDocletname);
707             toolArgs.add("-docletpath"); //$NON-NLS-1$
708
toolArgs.add(fDocletpath);
709         }
710         
711         if (fSourcepath.length > 0) {
712             toolArgs.add("-sourcepath"); //$NON-NLS-1$
713
toolArgs.add(flatPathList(fSourcepath));
714         }
715         
716         if (fClasspath.length > 0) {
717             toolArgs.add("-classpath"); //$NON-NLS-1$
718
toolArgs.add(flatPathList(fClasspath));
719         }
720         toolArgs.add("-" + fAccess); //$NON-NLS-1$
721

722         if (fFromStandard) {
723             if (fSource.length() > 0 && !fSource.equals("-")) { //$NON-NLS-1$
724
toolArgs.add("-source"); //$NON-NLS-1$
725
toolArgs.add(fSource);
726             }
727             
728             if (fUse)
729                 toolArgs.add("-use"); //$NON-NLS-1$
730
if (fVersion)
731                 toolArgs.add("-version"); //$NON-NLS-1$
732
if (fAuthor)
733                 toolArgs.add("-author"); //$NON-NLS-1$
734
if (fNonavbar)
735                 toolArgs.add("-nonavbar"); //$NON-NLS-1$
736
if (fNoindex)
737                 toolArgs.add("-noindex"); //$NON-NLS-1$
738
if (fNotree)
739                 toolArgs.add("-notree"); //$NON-NLS-1$
740
if (fNodeprecated)
741                 toolArgs.add("-nodeprecated"); //$NON-NLS-1$
742
if (fNoDeprecatedlist)
743                 toolArgs.add("-nodeprecatedlist"); //$NON-NLS-1$
744
if (fSplitindex)
745                 toolArgs.add("-splitindex"); //$NON-NLS-1$
746

747             if (fTitle.length() != 0) {
748                 toolArgs.add("-doctitle"); //$NON-NLS-1$
749
toolArgs.add(fTitle);
750             }
751
752
753             if (fStylesheet.length() != 0) {
754                 toolArgs.add("-stylesheetfile"); //$NON-NLS-1$
755
toolArgs.add(fStylesheet);
756             }
757             
758             for (int i= 0; i < fHRefs.length; i++) {
759                 toolArgs.add("-link"); //$NON-NLS-1$
760
toolArgs.add(fHRefs[i]);
761             }
762             
763         } //end standard options
764

765         if (fAdditionalParams.length() + fVMParams.length() != 0) {
766             ExecutionArguments tokens= new ExecutionArguments(fVMParams, fAdditionalParams);
767             String JavaDoc[] vmArgsArray= tokens.getVMArgumentsArray();
768             for (int i= 0; i < vmArgsArray.length; i++) {
769                 vmArgs.add(vmArgsArray[i]);
770             }
771             String JavaDoc[] argsArray= tokens.getProgramArgumentsArray();
772             for (int i= 0; i < argsArray.length; i++) {
773                 toolArgs.add(argsArray[i]);
774             }
775         }
776         addProxyOptions(vmArgs);
777         
778         if (fOverview.length() != 0) {
779             toolArgs.add("-overview"); //$NON-NLS-1$
780
toolArgs.add(fOverview);
781         }
782
783         for (int i= 0; i < fSelectedElements.length; i++) {
784             IJavaElement curr= fSelectedElements[i];
785             if (curr instanceof IPackageFragment) {
786                 toolArgs.add(curr.getElementName());
787             } else if (curr instanceof ICompilationUnit) {
788                 // Since the Javadoc tool is running locally we can only create
789
// Javadoc for local resources. So using the location is fine here.
790
IPath p= curr.getResource().getLocation();
791                 if (p != null) {
792                     toolArgs.add(p.toOSString());
793                 } else {
794                     String JavaDoc message= JavadocExportMessages.JavadocOptionsManager_status_non_local;
795                     status.add(new StatusInfo(IStatus.WARNING, Messages.format(message, curr.getPath().makeRelative().toString())));
796                 }
797             }
798         }
799         return status;
800     }
801     
802     private void addProxyOptions(List JavaDoc vmOptions) {
803         // bug 74132
804
String JavaDoc hostPrefix= "-J-Dhttp.proxyHost="; //$NON-NLS-1$
805
String JavaDoc portPrefix= "-J-Dhttp.proxyPort="; //$NON-NLS-1$
806
for (int i= 0; i < vmOptions.size(); i++) {
807             String JavaDoc curr= (String JavaDoc) vmOptions.get(i);
808             if (curr.startsWith(hostPrefix) || curr.startsWith(portPrefix)) {
809                 return;
810             }
811         }
812         String JavaDoc proxyHost= System.getProperty("http.proxyHost"); //$NON-NLS-1$
813
if (proxyHost != null) {
814             vmOptions.add(hostPrefix + proxyHost);
815         }
816         
817         String JavaDoc proxyPort= System.getProperty("http.proxyPort"); //$NON-NLS-1$
818
if (proxyPort != null) {
819             vmOptions.add(portPrefix + proxyPort);
820         }
821     }
822     
823
824     public File JavaDoc createXML(IJavaProject[] projects) throws CoreException {
825         FileOutputStream JavaDoc objectStreamOutput= null;
826         //@change
827
//for now only writing ant files for single project selection
828
try {
829             if (fAntpath.length() > 0) {
830                 File JavaDoc file= new File JavaDoc(fAntpath);
831
832                 String JavaDoc encoding= "UTF-8"; //$NON-NLS-1$
833
IContentType type= Platform.getContentTypeManager().getContentType("org.eclipse.ant.core.antBuildFile"); //$NON-NLS-1$
834
if (type != null)
835                     encoding= type.getDefaultCharset();
836                 IPath filePath= Path.fromOSString(fAntpath);
837                 IPath directoryPath= filePath.removeLastSegments(1);
838                 
839                 IPath basePath= null;
840                 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
841                 if (root.findFilesForLocation(filePath).length > 0) {
842                     basePath= directoryPath; // only do relative path if ant file is stored in the workspace
843
}
844                 
845                 directoryPath.toFile().mkdirs();
846             
847                 objectStreamOutput= new FileOutputStream JavaDoc(file);
848                 JavadocWriter writer= new JavadocWriter(objectStreamOutput, encoding, basePath, projects);
849                 writer.writeXML(this);
850                 return file;
851             }
852         } catch (IOException JavaDoc e) {
853             String JavaDoc message= JavadocExportMessages.JavadocOptionsManager_createXM_error;
854             throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, message, e));
855         } catch (ParserConfigurationException JavaDoc e) {
856             String JavaDoc message= JavadocExportMessages.JavadocOptionsManager_createXM_error;
857             throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, message, e));
858         } catch (TransformerException JavaDoc e) {
859             String JavaDoc message= JavadocExportMessages.JavadocOptionsManager_createXM_error;
860             throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, message, e));
861         } finally {
862             if (objectStreamOutput != null) {
863                 try {
864                     objectStreamOutput.close();
865                 } catch (IOException JavaDoc e) {
866                 }
867             }
868         }
869         return null;
870     }
871
872     public void updateDialogSettings(IDialogSettings dialogSettings, IJavaProject[] checkedProjects) {
873         IDialogSettings settings= dialogSettings.addNewSection(SECTION_JAVADOC);
874
875         settings.put(JAVADOC_COMMAND_HISTORY, flatStringList(fJavadocCommandHistory));
876         if (fJavadocCommandHistory.length > 0) {
877             IPreferenceStore store= PreferenceConstants.getPreferenceStore();
878             store.setValue(PreferenceConstants.JAVADOC_COMMAND, fJavadocCommandHistory[0]);
879         }
880         
881         
882         settings.put(FROMSTANDARD, fFromStandard);
883
884         settings.put(DOCLETNAME, fDocletname);
885         settings.put(DOCLETPATH, fDocletpath);
886
887         settings.put(VISIBILITY, fAccess);
888
889         settings.put(USE, fUse);
890         settings.put(AUTHOR, fAuthor);
891         settings.put(VERSION, fVersion);
892         settings.put(NODEPRECATED, fNodeprecated);
893         settings.put(NODEPRECATEDLIST, fNoDeprecatedlist);
894         settings.put(SPLITINDEX, fSplitindex);
895         settings.put(NOINDEX, fNoindex);
896         settings.put(NOTREE, fNotree);
897         settings.put(NONAVBAR, fNonavbar);
898         settings.put(OPENINBROWSER, fOpenInBrowser);
899         settings.put(SOURCE, fSource);
900
901         if (fAntpath.length() != 0)
902             settings.put(ANTPATH, fAntpath);
903         if (fDestination.length() != 0)
904             settings.put(DESTINATION, fDestination);
905         if (fAdditionalParams.length() != 0)
906             settings.put(EXTRAOPTIONS, fAdditionalParams);
907         if (fVMParams.length() != 0)
908             settings.put(VMOPTIONS, fVMParams);
909         if (fOverview.length() != 0)
910             settings.put(OVERVIEW, fOverview);
911         if (fStylesheet.length() != 0)
912             settings.put(STYLESHEETFILE, fStylesheet);
913         if (fTitle.length() != 0)
914             settings.put(TITLE, fTitle);
915
916         if (checkedProjects.length == 1) {
917             updateRecentSettings(checkedProjects[0]);
918         }
919         getRecentSettings().store(settings);
920     }
921         
922     public void setJavadocCommandHistory(String JavaDoc[] javadocCommandHistory) {
923         fJavadocCommandHistory= javadocCommandHistory;
924     }
925
926     public void setAccess(String JavaDoc access) {
927         fAccess= access;
928     }
929     public void setDestination(String JavaDoc destination) {
930         fDestination= destination;
931     }
932
933     public void setDocletPath(String JavaDoc docletpath) {
934         fDocletpath= docletpath;
935     }
936
937     public void setDocletName(String JavaDoc docletname) {
938         fDocletname= docletname;
939     }
940
941     public void setStyleSheet(String JavaDoc stylesheet) {
942         fStylesheet= stylesheet;
943     }
944
945     public void setOverview(String JavaDoc overview) {
946         fOverview= overview;
947     }
948
949     public void setAdditionalParams(String JavaDoc params) {
950         fAdditionalParams= params;
951     }
952     
953     public void setVMParams(String JavaDoc params) {
954         fVMParams= params;
955     }
956
957     public void setGeneralAntpath(String JavaDoc antpath) {
958         fAntpath= antpath;
959     }
960     public void setClasspath(IPath[] classpath) {
961         fClasspath= classpath;
962     }
963
964     public void setSourcepath(IPath[] sourcepath) {
965         fSourcepath= sourcepath;
966     }
967
968     public void setSelectedElements(IJavaElement[] elements) {
969         fSelectedElements= elements;
970     }
971
972     public void setFromStandard(boolean fromStandard) {
973         fFromStandard= fromStandard;
974     }
975
976     public void setTitle(String JavaDoc title) {
977         fTitle= title;
978     }
979
980     public void setOpenInBrowser(boolean openInBrowser) {
981         fOpenInBrowser= openInBrowser;
982     }
983     
984     public void setHRefs(String JavaDoc[] hrefs) {
985         fHRefs= hrefs;
986     }
987
988     public void setBoolean(String JavaDoc flag, boolean value) {
989
990         if (flag.equals(AUTHOR))
991             fAuthor= value;
992         else if (flag.equals(USE))
993             fUse= value;
994         else if (flag.equals(VERSION))
995             fVersion= value;
996         else if (flag.equals(NODEPRECATED))
997             fNodeprecated= value;
998         else if (flag.equals(NODEPRECATEDLIST))
999             fNoDeprecatedlist= value;
1000        else if (flag.equals(NOINDEX))
1001            fNoindex= value;
1002        else if (flag.equals(NOTREE))
1003            fNotree= value;
1004        else if (flag.equals(SPLITINDEX))
1005            fSplitindex= value;
1006        else if (flag.equals(NONAVBAR))
1007            fNonavbar= value;
1008    }
1009    
1010    public void setSource(String JavaDoc source) {
1011        fSource= source;
1012    }
1013    
1014    public String JavaDoc getSource() {
1015        return fSource;
1016    }
1017
1018    private IJavaElement[] getInitialElementsFromSelection(List JavaDoc candidates) {
1019        ArrayList JavaDoc res= new ArrayList JavaDoc();
1020        for (int i= 0; i < candidates.size(); i++) {
1021            try {
1022                IJavaElement elem= getSelectableJavaElement(candidates.get(i));
1023                if (elem != null) {
1024                    res.add(elem);
1025                }
1026            } catch (JavaModelException ignore) {
1027                // ignore this
1028
}
1029        }
1030        return (IJavaElement[]) res.toArray(new IJavaElement[res.size()]);
1031    }
1032
1033    private IJavaElement getSelectableJavaElement(Object JavaDoc obj) throws JavaModelException {
1034        IJavaElement je= null;
1035        if (obj instanceof IAdaptable) {
1036            je= (IJavaElement) ((IAdaptable) obj).getAdapter(IJavaElement.class);
1037        }
1038
1039        if (je != null) {
1040            switch (je.getElementType()) {
1041                case IJavaElement.JAVA_MODEL :
1042                case IJavaElement.JAVA_PROJECT :
1043                case IJavaElement.CLASS_FILE :
1044                    break;
1045                case IJavaElement.PACKAGE_FRAGMENT_ROOT :
1046                    if (containsCompilationUnits((IPackageFragmentRoot) je)) {
1047                        return je;
1048                    }
1049                    break;
1050                case IJavaElement.PACKAGE_FRAGMENT :
1051                    if (containsCompilationUnits((IPackageFragment) je)) {
1052                        return je;
1053                    }
1054                    break;
1055                default :
1056                    ICompilationUnit cu= (ICompilationUnit) je.getAncestor(IJavaElement.COMPILATION_UNIT);
1057                    if (cu != null) {
1058                        return cu;
1059                    }
1060            }
1061            IJavaProject project= je.getJavaProject();
1062            if (isValidProject(project))
1063                return project;
1064        }
1065
1066        return null;
1067    }
1068
1069    private boolean isValidProject(IJavaProject project) throws JavaModelException {
1070        if (project != null && project.exists() && project.isOpen()) {
1071            return true;
1072        }
1073        return false;
1074    }
1075
1076    private boolean containsCompilationUnits(IPackageFragmentRoot root) throws JavaModelException {
1077        if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
1078            return false;
1079        }
1080
1081        IJavaElement[] elements= root.getChildren();
1082        for (int i= 0; i < elements.length; i++) {
1083            if (elements[i] instanceof IPackageFragment) {
1084                IPackageFragment fragment= (IPackageFragment) elements[i];
1085                if (containsCompilationUnits(fragment)) {
1086                    return true;
1087                }
1088            }
1089        }
1090        return false;
1091    }
1092
1093    private boolean containsCompilationUnits(IPackageFragment pack) throws JavaModelException {
1094        return pack.getCompilationUnits().length > 0;
1095    }
1096
1097    public RecentSettingsStore getRecentSettings() {
1098        return fRecentSettings;
1099    }
1100
1101    /**
1102     * @param project
1103     */

1104    public void updateRecentSettings(IJavaProject project) {
1105        fRecentSettings.setProjectSettings(project, fDestination, fAntpath, fHRefs);
1106    }
1107
1108    
1109    private static String JavaDoc initJavadocCommandDefault() {
1110        IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
1111        String JavaDoc cmd= store.getString(PreferenceConstants.JAVADOC_COMMAND); // old location
1112
if (cmd != null && cmd.length() > 0) {
1113            store.setToDefault(PreferenceConstants.JAVADOC_COMMAND);
1114            return cmd;
1115        }
1116        
1117        File JavaDoc file= findJavaDocCommand();
1118        if (file != null) {
1119            return file.getPath();
1120        }
1121        return ""; //$NON-NLS-1$
1122
}
1123    
1124
1125    private static File JavaDoc findJavaDocCommand() {
1126        IVMInstall install= JavaRuntime.getDefaultVMInstall();
1127        if (install != null) {
1128            File JavaDoc res= getCommand(install);
1129            if (res != null) {
1130                return res;
1131            }
1132        }
1133        
1134        IVMInstallType[] jreTypes= JavaRuntime.getVMInstallTypes();
1135        for (int i= 0; i < jreTypes.length; i++) {
1136            IVMInstallType jreType= jreTypes[i];
1137            IVMInstall[] installs= jreType.getVMInstalls();
1138            for (int k= 0; k < installs.length; k++) {
1139                File JavaDoc res= getCommand(installs[k]);
1140                if (res != null) {
1141                    return res;
1142                }
1143            }
1144        }
1145        return null;
1146    }
1147
1148    private static File JavaDoc getCommand(IVMInstall install) {
1149        File JavaDoc installLocation= install.getInstallLocation();
1150        if (installLocation != null) {
1151            File JavaDoc javaDocCommand= new File JavaDoc(installLocation, "bin/javadoc"); //$NON-NLS-1$
1152
if (javaDocCommand.isFile()) {
1153                return javaDocCommand;
1154            }
1155            javaDocCommand= new File JavaDoc(installLocation, "bin/javadoc.exe"); //$NON-NLS-1$
1156
if (javaDocCommand.isFile()) {
1157                return javaDocCommand;
1158            }
1159        }
1160        return null;
1161    }
1162
1163
1164
1165
1166}
1167
Popular Tags