View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: JTidyDoclet.java,v 1.1.1.1 2004/05/19 12:07:30 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.utils.javadoc;
21  import java.io.*;
22  import java.util.HashMap;
23  import java.util.Properties;
24  import org.w3c.tidy.Tidy;
25  import org.w3c.tidy.Configuration;
26  import com.sun.javadoc.*;
27  
28  import org.backsource.utils.io.ReaderInputStream;
29  /***
30   * <p>A simple doclet that runs the javadoc in the specifyed
31   * classes/packages through jtidy.</p>
32   * 
33   * <p>This might be handy to have when you get tired on fixing the
34   * HTML in your comments. Running this with javadoc as a doclet will
35   * produce text output with the cleaned comments either on stdout or
36   * in the filesystem.</p>
37   * 
38   * <p>The doclet takes one extra parameter <b>-d</b>. If specifyed the
39   * output will be written out in the dir pointed to by the argument to
40   * -d. The dir must exist.</p>
41   * 
42   * <p>Here is an example of how to set this up in ant:</p>
43   * 
44   * <pre>
45   *   
46   * &lt;target name="jtidy" depends="prepare"&gt;
47   * &lt;mkdir dir="${basedir}/tidy"/&gt;
48   * 
49   * &lt;javadoc packagenames="${packages}"
50   * destdir="${basedir}/tidy"
51   * sourcepath="${src.dir}"
52   * classpath="${classpath}"
53   * extdirs="${lib.dir}"
54   * doclet="org.backsource.utils.javadoc.JTidyDoclet"
55   * docletpath="${classes.dir}:${lib.dir}/Tidy.jar"
56   * /&gt;
57   * &lt;/target&gt;
58   * </pre>
59   *
60   * @author <a href="mailto:pra@tim.se"></a>
61   * @version $Revision: 1.1.1.1 $
62   */
63  
64  public class JTidyDoclet{
65     File destDir;
66     RootDoc root;
67     HashMap packageDone = new HashMap();
68     public JTidyDoclet (RootDoc root) throws Exception {
69        this.root = root; 
70        readOptions(root.options());
71     }
72     
73     public static boolean start(RootDoc root)throws Exception {
74        System.out.println("Starting JTidy");
75        JTidyDoclet doc = new JTidyDoclet(root);
76        doc.doClasses();
77        return true;
78        
79     }
80     
81     private void readOptions(String[][] options) throws IOException{
82        for (int i = 0; i < options.length; i++) {
83           String[] opt = options[i];
84           if (opt[0].equals("-d")) {
85              destDir = new File(opt[1]);
86              if ( !destDir.exists()|| !destDir.isDirectory() ) {
87                 throw new IOException("File given by -d must exist: " + destDir);
88              } // end of if ()
89              
90           }
91        }
92     }
93     
94     public static int optionLength(String option) {
95        if(option.equals("-d")) {
96           return 2;
97        }
98        return 0;
99     }
100    
101    public static boolean validOptions(String options[][], 
102                                       DocErrorReporter reporter) {
103       boolean foundDirOption = false;
104       for (int i = 0; i < options.length; i++) {
105          String[] opt = options[i];
106          if (opt[0].equals("-d")) {
107             if (foundDirOption) {
108                reporter.printError("Only one -tag option allowed.");
109                return false;
110             } else { 
111                foundDirOption = true;
112             }
113          } 
114       }
115       
116       return true;
117    }
118    
119    public void doClasses() throws Exception{
120       
121       ClassDoc[] classes = root.classes();
122       for (int i = 0; i < classes.length; ++i) {
123          doClass( classes[i] );
124       }
125       
126    }
127    
128    
129    public void doClass(ClassDoc classDoc) throws Exception {
130       // We start with some debugging
131       /*
132       System.out.println("Classname =" + classDoc.name());
133       System.out.println("Package =" + classDoc.containingPackage().name());
134       System.out.println("Raw text =" + classDoc.getRawCommentText());
135       System.out.println("Comment text =" + classDoc.commentText());
136       */
137 
138       doPackage(classDoc.containingPackage());
139      
140       String jt = tidy(classDoc.commentText());
141       jt = dotit(jt);
142       // Dotify
143 
144       MethodDoc[] meth = classDoc.methods();
145       for ( int i =0; i < meth.length;i++) {
146          jt = jt + "\n" + doMethod( meth[i]);
147       } // end of for ()
148       
149 
150       doWrite( classDoc.containingPackage().name(),classDoc.name(),jt);
151       
152    }
153    
154    public String doMethod(MethodDoc meth) throws Exception {
155       String jt = tidy(meth.commentText());
156       jt = dotit(jt);
157       return meth.name() + ":\n" + jt;
158    }
159 
160    public void doPackage(PackageDoc doc)throws Exception {
161       if ( doc == null) {
162          return;
163       } // end of if ()
164       
165       iftrong> ( !packageDone.containsKey( doc.name()) ) {
166          // Process
167          //System.err.println("PACKET DOCK" + doc.commentText());
168          String jt = tidy(doc.commentText());
169          doWrite( doc.name(),"package",jt);
170          
171          packageDone.put(doc.name(),"done");
172       } // end of if ()
173       
174    }
175 
176    public String tidy(String comment) throws Exception {
177       	Tidy tidy = new Tidy();
178 	setUpTidy(tidy);
179         //ReaderInputStream is = new ReaderInputStream(new StringReader(comment));
180         ByteArrayInputStream is = new ByteArrayInputStream( comment.getBytes());
181         ByteArrayOutputStream os = new ByteArrayOutputStream();
182         tidy.parse(is, os);
183         return os.toString();
184    }
185    private void setUpTidy(Tidy tidy) throws Exception {
186       
187       Properties prop = new Properties();
188       prop.put("markup", "yes");
189       prop.put("clean", "no");
190       prop.put("output-xml", "yes");
191       prop.put("input-xml", "no");
192       prop.put("numeric-entities", "yes");
193       prop.put("quote-marks", "no");
194       prop.put("quote-nbsp", "yes");
195       prop.put("quote-ampersand", "no");
196       prop.put("break-before-br", "no");
197       prop.put("uppercase-tags", "no");
198       prop.put("uppercase-attributes", "no");
199       prop.put("output-xhtml", "no");
200       prop.put("char-encoding", "latin1");
201       prop.put("drop-font-tags", "no");
202       prop.put("enclose-text", "yes");
203       
204       
205       Configuration conf = tidy.getConfiguration();
206       conf.addProps(prop);
207       
208       
209    }
210    public String dotit(String in) throws Exception {
211       BufferedReader r = new BufferedReader( new StringReader(in));
212       StringWriter target = new StringWriter();
213       PrintWriter wr = new PrintWriter(target);
214       String line = null;
215       do {
216          line = r.readLine();
217          if ( line != null) {
218             wr.println(" * " + line);
219          } // end of if ()
220          
221       }while ( line != null); // end of while ()
222       
223       wr.flush();
224       return target.toString();
225    }
226 
227    public void doWrite(String pack, String clazz, String content) throws Exception{
228       if ( destDir!= null) {
229          PrintWriter wr = new PrintWriter( new FileWriter( getFile(pack,clazz,".tidy")));
230          wr.print(content);
231          wr.flush();
232       } else {
233          System.out.println("Dock for " + pack + "." +clazz);
234          System.out.println(content);
235       } // end of else
236 
237 
238    }
239 
240    public File getFile(String pack, String clazz, String sufix) throws Exception  {
241       String subdir = pack.replace('.',File.separatorChar);
242       File sub = new File(destDir,subdir);
243       if ( !sub.exists()) {
244          if ( !sub.mkdirs()) {
245             throw new IOException("Could  no create all subdirs for " + sub);
246          } // end of if ()
247          
248       } // end of if ()
249 
250       return new File(sub,clazz + sufix);
251       
252 
253    }
254    
255    public static void main(String[] args){
256       
257       
258    }
259 } // JTidyDoclet