View Javadoc

1   package net.sf.jlayercheck.ant;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.net.URL;
8   import java.util.Map;
9   import java.util.TreeMap;
10  
11  import javax.xml.parsers.ParserConfigurationException;
12  
13  import org.xml.sax.SAXException;
14  
15  import net.sf.jlayercheck.util.DependencyVisitor;
16  import net.sf.jlayercheck.util.HTMLOutput;
17  import net.sf.jlayercheck.util.XMLConfiguration;
18  import net.sf.jlayercheck.util.XMLConfigurationParser;
19  import net.sf.jlayercheck.util.model.ClassSource;
20  
21  /**
22   * Configure and start HTML-output generation as ant task.
23   * 
24   * @author timo
25   * @author $Author: dgunia $
26   * @version $Id: JLCTask.java 46 2007-05-29 20:50:17Z dgunia $
27   */
28  public class JLCTask {
29  
30  	/** Default name for JLayerCheck configuration file */
31  	public static final String CONFIG = "jlayercheck_test.xml"; //$NON-NLS-1$
32  
33  	/** Default name for JLayerCheck output dir */
34  	public static final String OUTDIR = "jlayercheck-out"; //$NON-NLS-1$
35  
36  	/** The configuration file */
37  	protected File config = new File(CONFIG);
38  
39  	/** the output directory */
40  	protected File outDir = new File(OUTDIR);
41  
42  	// methods
43  
44  	// getter and setter
45  	public File getConfig() {
46  		return config;
47  	}
48  
49  	public void setConfig(File config) {
50  		this.config = config;
51  	}
52  
53  	public File getOutDir() {
54  		return outDir;
55  	}
56  
57  	public void setOutDir(File outDir) {
58  		this.outDir = outDir;
59  	}
60  
61  	// ant entry point
62  
63  	/**
64  	 * Start execution. {@link #config} and {@link #outDir} must be set prior.
65  	 */
66  	public void execute() {
67  		// check params
68  		if (getConfig() == null || getOutDir() == null) {
69  			throw new IllegalArgumentException("Config and outDir must be set!");
70  		}
71  		File config = getConfig();
72  		if (!config.exists()) {
73  			throw new RuntimeException("Cannot read " + config);
74  		}
75  
76  		try {
77  			// read config
78  			InputStream confIn = new FileInputStream(config);
79  			XMLConfiguration xcp = new XMLConfigurationParser().parse(confIn);
80  
81  			// prepare outdir
82  			File outDir = getOutDir();
83  			outDir.mkdirs();
84  
85  			// start processing
86  			DependencyVisitor dv = new DependencyVisitor();
87  			Map<String, URL> javaSources = new TreeMap<String, URL>();
88  
89  			// visit all classes
90  			for (ClassSource cs : xcp.getClassSources()) {
91  				cs.call(dv);
92  				javaSources.putAll(cs.getSourceFiles());
93  			}
94  
95  			// create output
96  			HTMLOutput html = new HTMLOutput(outDir.getAbsolutePath());
97  			html.write(dv, xcp);
98  
99  		} catch (IOException ioe) {
100 			throw new RuntimeException("Error reading configuration " + config
101 					+ ": " + ioe, ioe);
102 		} catch (SAXException e) {
103 			throw new RuntimeException("Error reading configuration " + config
104 					+ ": " + e, e);
105 		} catch (ParserConfigurationException e) {
106 			throw new RuntimeException("Error reading configuration " + config
107 					+ ": " + e, e);
108 		} catch (Exception e) {
109 			throw new RuntimeException("unexpected Exception caught: " + e, e);
110 		}
111 
112 	}
113 
114 }