KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > pmti > v1 > autodoc > v1 > xml > XMLFileParserGenerator


1 /*
2  * @(#)XMLFileParserGenerator.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.pmti.v1.autodoc.v1.xml;
30
31
32 import java.io.File JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.io.StringWriter JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.FilenameFilter JavaDoc;
38
39 import java.util.Vector JavaDoc;
40 import java.util.Enumeration JavaDoc;
41
42 import net.sourceforge.groboutils.pmti.v1.itf.parser.IParserGenerator;
43 import net.sourceforge.groboutils.pmti.v1.itf.parser.IParser;
44 import net.sourceforge.groboutils.pmti.v1.itf.IIssueRecord;
45 import net.sourceforge.groboutils.pmti.v1.itf.ITestIssueRecord;
46
47 import org.xml.sax.Parser JavaDoc;
48
49
50 /**
51  * Parses the directory or directories containing the output of XMLFileServer
52  * into individual IParsers, one for each file.
53  *
54  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
55  * @since March 17, 2002
56  * @version $Date: 2003/02/10 22:51:57 $
57  */

58 public class XMLFileParserGenerator implements IParserGenerator
59 {
60     public static final String JavaDoc DEFAULT_PREFIX = "ISSUES-";
61     public static final String JavaDoc DEFAULT_POSTFIX = ".xml";
62     
63     private Vector JavaDoc sourceDirs = new Vector JavaDoc();
64     private String JavaDoc filePrefix;
65     private String JavaDoc filePostfix;
66     private Parser parser;
67     
68     
69     /**
70      * Case-insensitive file filter.
71      */

72     private static class PrefixPostfixFilter implements FilenameFilter JavaDoc
73     {
74         private String JavaDoc pre;
75         private String JavaDoc post;
76         
77         public PrefixPostfixFilter( String JavaDoc pre, String JavaDoc post )
78         {
79             this.pre = pre.toLowerCase();
80             this.post = post.toLowerCase();
81         }
82         
83         public boolean accept( File JavaDoc dir, String JavaDoc name )
84         {
85             String JavaDoc n = name.toLowerCase();
86             return ( n.startsWith( this.pre )
87                 && n.endsWith( this.post ) );
88         }
89     }
90     
91     
92     public XMLFileParserGenerator()
93     {
94         this( (File JavaDoc[])null, null, null );
95     }
96     
97     
98     public XMLFileParserGenerator( File JavaDoc dir )
99     {
100         this( new File JavaDoc[] { dir }, null, null );
101     }
102     
103     
104     public XMLFileParserGenerator( File JavaDoc dir, String JavaDoc prefix, String JavaDoc postfix )
105     {
106         this( new File JavaDoc[] { dir }, prefix, postfix );
107     }
108     
109     
110     public XMLFileParserGenerator( File JavaDoc dirs[] )
111     {
112         this( dirs, null, null );
113     }
114     
115     
116     public XMLFileParserGenerator( File JavaDoc dirs[], String JavaDoc prefix, String JavaDoc postfix )
117     {
118         addSourceDirs( dirs );
119         setFilePrefix( prefix );
120         setFilePostfix( postfix );
121     }
122     
123     
124     /**
125      * Sets the SAX 1.0 parser.
126      */

127     public void setSAXParser( Parser p )
128     {
129         if (p != null)
130         {
131             this.parser = p;
132         }
133     }
134     
135     
136     /**
137      * Set the text that all XML Issue file names must begin with. Unlike the
138      * <tt>XMLFileServer</tt>, the prefix must not contain path information
139      * due to the detection methods used. If a <tt>null</tt> is passed in,
140      * then the default value ("ISSUE-") is used instead. The string will
141      * be considered case-insensitive for searching purposes.
142      *
143      * @param prefix the new prefix value to use.
144      */

145     public void setFilePrefix( String JavaDoc prefix )
146     {
147         if (prefix == null)
148         {
149             prefix = DEFAULT_PREFIX;
150         }
151         this.filePrefix = prefix;
152     }
153     
154     
155     /**
156      * Set the text that all XML Issue file names must end with.
157      * If a <tt>null</tt> is passed in,
158      * then the default value (".xml") is used instead. The string will
159      * be considered case-insensitive for searching purposes.
160      *
161      * @param postfix the new postfix value to use.
162      */

163     public void setFilePostfix( String JavaDoc postfix )
164     {
165         if (postfix == null)
166         {
167             postfix = DEFAULT_POSTFIX;
168         }
169         this.filePostfix = postfix;
170     }
171     
172     
173     /**
174      * Adds <tt>dir</tt> to the internal list of directories to check
175      * for issue XML files. Subdirectories will not be inspected, unless they
176      * are explicitly added.
177      *
178      * @param dir additional directory to search for issue files.
179      * <tt>null</tt> values, non-directories, and non-existent files will
180      * be ignored.
181      * @see #addSourceDirs( File[] )
182      */

183     public void addSourceDir( File JavaDoc dir )
184     {
185         addSourceDirs( new File JavaDoc[] { dir } );
186     }
187     
188     
189     /**
190      * Adds the given directories to the internal list of directories to check
191      * for issue XML files. Subdirectories will not be inspected, unless they
192      * are explicitly added.
193      *
194      * @param dirs additional directories to search for issue files.
195      * <tt>null</tt> values, non-directories, and non-existent files will
196      * be ignored.
197      * @see #addSourceDir( File )
198      */

199     public void addSourceDirs( File JavaDoc dirs[] )
200     {
201         if (dirs == null)
202         {
203             return;
204         }
205         
206         // Get only the valid dirs
207
for ( int i = 0; i < dirs.length; ++i )
208         {
209             if ( dirs[i] != null
210                 && dirs[i].exists()
211                 && dirs[i].isDirectory() )
212             {
213                 this.sourceDirs.addElement( dirs[i] );
214             }
215         }
216     }
217     
218     
219     /**
220      * Generate a collection of parsers which know how to create test records.
221      */

222     public IParser[] createParsers()
223     {
224         Vector JavaDoc parsers = new Vector JavaDoc();
225         
226         Enumeration JavaDoc dirs = this.sourceDirs.elements();
227         while ( dirs.hasMoreElements() )
228         {
229             File JavaDoc f[] = findIssueFiles( (File JavaDoc)dirs.nextElement() );
230             for ( int i = 0; i < f.length; ++i )
231             {
232                 IParser p = createParser( f[i] );
233                 if ( p != null )
234                 {
235                     parsers.addElement( p );
236                 }
237             }
238         }
239         
240         IParser p[] = new IParser[ parsers.size() ];
241         parsers.copyInto( p );
242         
243         return p;
244     }
245     
246     
247     protected File JavaDoc[] findIssueFiles( File JavaDoc dir )
248     {
249         FilenameFilter JavaDoc filter = createFilter();
250         String JavaDoc found[] = dir.list( filter );
251         
252         File JavaDoc ret[] = new File JavaDoc[ found.length ];
253         for ( int i = 0; i < found.length; ++i )
254         {
255             ret[i] = new File JavaDoc( dir, found[i] );
256         }
257         return ret;
258     }
259     
260     
261     protected IParser createParser( File JavaDoc issueFile )
262     {
263         return new XMLFileParser( issueFile, this.parser );
264     }
265     
266     
267     protected FilenameFilter JavaDoc createFilter()
268     {
269         return new PrefixPostfixFilter( this.filePrefix, this.filePostfix );
270     }
271 }
272
273
Popular Tags