View Javadoc

1   package DTDDoc;
2   
3   import java.io.*;
4   import java.util.ArrayList;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Set;
8   import java.util.Iterator;
9   
10  import org.apache.tools.ant.taskdefs.MatchingTask;
11  import org.apache.tools.ant.BuildException;
12  import org.apache.tools.ant.DirectoryScanner;
13  import org.apache.tools.ant.types.FileSet;
14  
15  /** This is the DTDDoc ant task. */
16  
17  public class DTDDocTask extends MatchingTask {
18  
19      private boolean showHiddenTags = false;
20      public void setShowHiddenTags( boolean f) {
21  	    showHiddenTags = f;
22  	}
23  
24  	private boolean showFixmeTags = true;
25      public void setShowFixmeTags( boolean f) {
26  	    showFixmeTags = f;
27  	}
28  
29      private boolean getAroundNetBeansComments = true;
30      public void setGetAroundNetBeansComments( boolean f) {
31  	    getAroundNetBeansComments = f;
32  	}
33  
34  
35      /** Track if the "sourceDir" task parameter was set or not in
36       *  the Ant script. */
37      private boolean sourceDirSet = false;
38  
39  	private File sourceDir = new File("."); // FIXME This is not portable
40      public void setSourceDir( File f) {
41  	    sourceDir = f;
42          sourceDirSet = true;
43      }
44  
45      /**
46       * Pseudo-parameter addition to mimic FileSet definition with the real
47       * task parameter : sourceDir.
48       * @param dir
49       */
50      public void setDir( File dir) {
51      	setSourceDir( dir);
52      }
53  
54      public void setFile( File file) {
55      	setSourceDir( file.getParentFile());
56      	setIncludes( file.getName());
57      }
58  
59  	private File destDir = new File("."); // FIXME This is not portable
60      public void setDestDir( File f) {
61  	    destDir = f;
62  	}
63  
64  	private String docTitle = "";
65      public void setDocTitle( String s) {
66  	    docTitle = s;
67  	}
68  
69  	private File styleSheet = null;
70  	public void setStyleSheet( File f) {
71  		if( f.canRead()) {
72  			styleSheet = f;
73  		} else {
74  			log.warn("Sorry, the style sheet '"+f+"' was not" +
75  				" found. I'll use the default one instead.");
76  		}
77  	}
78  
79  
80      /** Filesets as explained in Ant's documentation. */
81      private List filesets = new ArrayList();
82  
83      /** Filesets as explained in Ant's documentation. */
84      public void addFileset( FileSet fileset) {
85          filesets.add( fileset);
86      }
87  
88  	private Logger log;
89  
90  	public DTDDocTask() {
91  		log = new AntLogger( this);
92  	}
93  
94      /** Scan a directory with given scanner and inject the list of included
95       * Files (complete paths) in a given set.
96       *
97       * @param scanner DirectoryScanner to use.
98       * @param sDir Base directory where the scanner will go.
99       * @param hfiles Set where to put the path of the included files. */
100 
101     private void scanDirectory( DirectoryScanner scanner, File sDir, Set hfiles) {
102         String[] files = scanner.getIncludedFiles();
103 
104         for (int i = 0; i < files.length; i++) {
105 		    hfiles.add( new File( sDir, files[i]));
106 	    }
107     }
108 
109     public void execute() throws BuildException {
110 
111 	    log.info("Welcome to DTDDoc " + DTDCommenter.VERSION + ".");
112 	    log.info("Parts of this program are from DTDParser" +
113 	    	" and RegExp of the Apache Project.");
114 	    log.info("The settings are :");
115 
116 		log.info( "   " + (showHiddenTags ? "Show" : "Don't show") + " @hidden tags.");
117 	    log.info( "   " + (showFixmeTags ? "Show" : "Don't show") + " @fixme tags.");
118 
119 
120 		if( styleSheet != null) {
121 			log.info("   Style sheet source is '"+styleSheet.getPath()+"'.");
122 		} else {
123 			log.info("   No style sheet was specified.");
124 		}
125 
126 		try {
127 			log.info("   Source directory is '" +
128 				sourceDir.getCanonicalPath() + "'.");
129 			log.info("   Destination directory is '" +
130 				destDir.getCanonicalPath() + "'.");
131 		} catch ( IOException ex) {
132 			log.error("Something went wrong while figuring out" +
133 				" the source/dest directories paths.", ex);
134 			return;
135 		}
136 
137         // Now we locate the files according to the scanner...
138 
139 		DirectoryScanner scanner = getDirectoryScanner( sourceDir );
140 		String[] files = scanner.getIncludedFiles();
141 
142 		if( files.length == 0)
143 			log.error(" No files to process ! Please note that you" +
144 				" have to specify which files to include with the <include> tags. There's no" +
145 				" automatic selection of all DTD's anymore (as of 0.0.7).");
146 
147 		Set scan = new HashSet();
148 
149 		if( filesets.size() >  0) {
150             // Here we use a set of <filesets>
151 
152             if( sourceDirSet)
153                 log.warn("When using filesets, the \"dir\" attribute of " +
154                     "the file set has precedence over the \"sourceDir\" " +
155                     "of the task." );
156             for(Iterator itFSets = filesets.iterator(); itFSets.hasNext(); ) {
157                 FileSet fs = (FileSet)itFSets.next();
158                 scanDirectory( fs.getDirectoryScanner( getProject() ),
159                     fs.getDir( getProject()), scan);
160             }
161         } else {
162             // Here we use a combination of <includes> and <excludes>
163             scanDirectory( getDirectoryScanner( sourceDir ), sourceDir, scan);
164         }
165 
166 		try {
167             DTDCommenter commenter = new DTDCommenter( log);
168 		    commenter.commentDTDs( scan,
169 			                       sourceDir,
170 								   destDir,
171 								   showHiddenTags, showFixmeTags,
172                                    getAroundNetBeansComments, docTitle, styleSheet);
173 		} catch ( Exception ex) {
174 		    log.error("A problem occured while instanciating or " +
175                 "executing the DTDDoc task !", ex);
176 		    return;
177 		}
178 	}
179 }