1 package DTDDoc;
2
3 import java.io.*;
4
5 import java.util.HashSet;
6 import java.util.Properties;
7 import java.util.Set;
8
9 import org.apache.regexp.RE;
10 import org.apache.regexp.RESyntaxException;
11
12
13 /** This is the command-line version of DTDDoc. Please try to use the
14 * Ant task instead (it's easier, more powerful and not doomed to be
15 * deprecated :))
16 *
17 * @deprecated The command-line version of DTDDoc is deprecated in favour
18 * of an Ant Task version. Please try to migrate. */
19
20 public class DTDDoc {
21
22 private Logger log;
23
24 /** Interpret a property of a configuration into an array of RegExp.
25 * The property must be a coma separated list of RegExp.
26 * @param configuration Where to look for the property.
27 * @param propertyName The name of the property.*
28 * @return An array of RegExp. */
29
30 private RE[] propertyToREArray( Properties configuration, String propertyName)
31 throws RESyntaxException {
32
33 String s[] = Tools.listToArray( configuration.getProperty( propertyName), ",");
34
35 RE l[] = new RE[s.length];
36 for( int i=0; i<s.length; i++)
37 l[i] = new RE( s[i]);
38
39 return l;
40 }
41
42 /** Checks if a given string matches one out of several regular expressions.
43 *
44 * @param s the string to match.
45 * @param regexps the regular expressions to look for.
46 * @return true if s matches one of the reg. exp. given in regexps. False
47 * in any other case. */
48
49 private boolean satisfyOneRE( String s, RE[] regexps) {
50 for( int i=0; i<regexps.length; i++)
51 if( regexps[i].match(s))
52 return true;
53 return false;
54 }
55
56 /** Find all the files located under a directory.
57 * Will take the acceptList and rejectList into account !
58 *
59 * @param source the directory to look for. If it happens to be a file,
60 * then the file will be the only one scanned.
61 * @return a set of all the found files. Each file being represented
62 * as a File. */
63
64 private Set fileScanner( File source, RE[] acceptList, RE[] rejectList) throws IOException {
65
66 Set files = new HashSet();
67
68
69
70 if( source.isDirectory()) {
71
72 File[] allFiles = source.listFiles();
73
74 if( allFiles != null)
75
76 for( int i=0; i<allFiles.length; i++)
77 files.addAll( fileScanner( allFiles[i], acceptList, rejectList));
78
79 } else
80
81
82
83 if( source.isFile() &&
84 satisfyOneRE( source.getCanonicalPath(), acceptList) &&
85 !satisfyOneRE( source.getCanonicalPath(), rejectList) ) {
86
87 log.info("Scanned "+source.getCanonicalPath());
88 files.add( source);
89
90 }
91
92 return files;
93
94 }
95
96 /** Configuration for the session, loaded from a .properties file. */
97 private Properties configuration = null;
98
99 /** Get a configuration value.
100 *
101 * @param key Configuration value to look for.
102 * @return the value. Returns null if the configuration value is not
103 * found. */
104
105 public String getConfig( String key) {
106 if( configuration == null)
107 return null;
108 else
109 return configuration.getProperty( key);
110 }
111
112 public boolean isPropertySet( String key) {
113 return configuration != null && configuration.containsKey(key);
114 }
115
116 /** Test if a value of a property of the configuration is true.
117 * @param key The name of the property to look for.
118 * @return true if the property is set to "true", false in any other
119 * case. */
120
121 public boolean isConfigTrueFor( String key) {
122 return "true".equals( getConfig( key));
123 }
124
125 /** Set up the configuration from a file. If we can't open the file, then
126 * we revert to "default.properties". If this still doesn't work, then
127 * we're into troubles.
128 *
129 * @param source where to get the configuration from. */
130
131 private void setConfiguration( File source)
132 throws FileNotFoundException, IOException, RESyntaxException {
133
134 if( !source.canRead()) {
135 log.warn("Unable to read the configuration file : "+source.getCanonicalPath());
136 source = new File("default.properties");
137 log.info("Trying to read it at "+source.getCanonicalPath());
138 if( !source.canRead()) {
139 log.warn("Can't find "+source.getCanonicalPath()+" neither, we'll work blindly !");
140 return;
141 }
142 }
143
144 log.info("Configuration used is in :"+source.getCanonicalPath());
145 configuration = new Properties();
146 configuration.load( new FileInputStream( source));
147 }
148
149 public DTDDoc( Logger log) {
150 this.log = log;
151 }
152
153 public static void main( String[] args ) throws Exception {
154
155 Logger log = new SystemLogger();
156
157 log.info("Welcome to DTDDoc !");
158
159 DTDDoc doc = new DTDDoc( log);
160
161 if( args != null && args[0] != null)
162 doc.setConfiguration( new File(args[0]));
163 else
164 doc.setConfiguration( new File(""));
165
166
167 String docTitle = doc.getConfig("docTitle");
168
169
170
171 File sourceDir = new File( doc.getConfig("DTDsourceDir"));
172
173
174
175 File destDir = new File( doc.getConfig("destinationDir"));
176 boolean showHiddenTags = doc.isConfigTrueFor("showHiddenTags");
177 boolean showFixmeTags = doc.isConfigTrueFor("showFixmeTags");
178
179 boolean getAroundNetBeanComments = doc.isPropertySet("getAroundNetBeanComments") ?
180 doc.isConfigTrueFor("getAroundNetBeanComments") : true;
181
182 log.info("Source dir : "+sourceDir.getCanonicalPath());
183 log.info("Destination dir : "+destDir.getCanonicalPath());
184
185 Set scan = doc.fileScanner( sourceDir,
186 doc.propertyToREArray( doc.configuration, "acceptList"),
187 doc.propertyToREArray( doc.configuration, "rejectList"));
188
189 DTDCommenter commenter = new DTDCommenter( log);
190 commenter.commentDTDs( scan, sourceDir, destDir, showHiddenTags, showFixmeTags,
191 getAroundNetBeanComments, docTitle, null);
192 }
193 }