View Javadoc
1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "AntGenerator.java".  Description: 
10  "This class creates an ANT build.xml file" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2001.  All Rights Reserved. 
14  
15  Contributor(s): James Agnew
16                  Paul Brohman
17                  Mitch Delachevrotiere
18                  Shawn Dyck
19    				Cory Metcalf
20    				
21  Alternatively, the contents of this file may be used under the terms of the 
22  GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
23  applicable instead of those above.  If you wish to allow use of your version of this 
24  file only under the terms of the GPL and not to allow others to use your version 
25  of this file under the MPL, indicate your decision by deleting  the provisions above 
26  and replace  them with the notice and other provisions required by the GPL License.  
27  If you do not delete the provisions above, a recipient may use your version of 
28  this file under either the MPL or the GPL. 
29  
30  */
31  package ca.uhn.hl7v2.conf.classes.generator.builders;
32  
33  import java.io.*;
34  
35  /** This class creates an ANT build.xml file
36   * @author <table><tr>James Agnew</tr>
37   *                <tr>Paul Brohman</tr>
38   *                <tr>Mitch Delachevrotiere</tr>
39   *                <tr>Shawn Dyck</tr>
40   * 				  <tr>Cory Metcalf</tr></table>
41   */
42  public class AntGenerator {
43     private String baseDir;
44     private String name;
45  		
46     /** Returns a String representation of the ANT build.xml file
47  	* @return a String representation of the ANT build.xml file
48  	*/
49     private String antString(){
50        String[] classPath = System.getProperty("java.class.path",".").split(";", -1);
51  		
52        StringBuilder ant =
53                new StringBuilder("<!-- Build file for the Conformance Classes -->\n" +
54                        "<project name=\"" + name + "\" default=\"dist\" basedir=\"" + baseDir + "\">\n" +
55                        "<!--  set global properties for this build -->\n" +
56                        "<property name=\"src\" value=\".\" />\n" +
57                        "<property name=\"build\" value=\"${basedir}/build\" />\n" +
58                        "<property name=\"classes\" value=\"${build}/classes\" />\n" +
59                        "<property name=\"docs\" value=\"${build}/docs\" />\n" +
60                        "<property name=\"dist\" value=\"${basedir}/dist\" />\n" +
61                        "<property name=\"build\" value=\"ca\" />\n" +
62  
63                        "<!-- The class path -->\n" +
64                        "<path id=\"class.path\">\n" +
65                        "<pathelement path=\"${src}\" />\n" +
66                        "<pathelement path=\"${classes}\" />\n");
67       				
68  	     			// add locations on the class path
69  	   for (String s : classPath) {
70  		   ant.append("<pathelement location=\"").append(s).append("\" />\n");
71  	   }
72      				
73      				ant.append("<pathelement location=\"${java.home}/jre/lib/rt.jar\" />\n" + "</path>\n" + "<!-- Compile the project -->\n" + "<target name=\"compile_core\">\n" + "<mkdir dir=\"${classes}\" />\n" + "<javac srcdir=\"${src}\" destdir=\"${classes}\" includes=\"**\">\n" + "<classpath refid=\"class.path\" />\n" + "</javac>\n" + "</target>\n" + "<!--  Creates JavaDoc documentation (core classes only)   -->\n" + "<target name=\"doc\">\n" + "<mkdir dir=\"${docs}\" />\n" + "<javadoc packagenames=\"*\" classpathref=\"class.path\" sourcepath=\"${src}\" destdir=\"${docs}\" windowtitle=\"HAPI API Documentation\" />\n" + "</target>\n" + "<!-- Add the files to a Jar Archive -->\n" + "<target name=\"jar\" depends=\"compile_core, doc\">\n" + "<mkdir dir=\"${dist}\" />\n" + "<jar jarfile=\"${dist}/").append(name).append(".jar\" basedir=\"${classes}\">\n").append("<fileset dir=\"${docs}\" />\n").append("</jar>\n").append("</target>\n").append("<target name=\"dist\" depends=\"jar,doc\" />\n").append("<target name=\"clean\">\n").append("<delete>\n").append("<fileset dir=\"${build}\" />\n").append("</delete>\n").append("</target>\n").append("</project>\n");
74  			  
75        return ant.toString();
76     }
77  	
78     /** Creates the ANT build.xml file
79  	* @param baseDir the directory where the ANT build.xml file will be saved
80  	* @param name the name of the project
81  	*/
82     public void createAnt(String baseDir, String name){
83        this.baseDir = baseDir;
84  	  this.name = name;
85        FileOutputStream fstream;
86  	  try{	
87           File f = new File( baseDir + File.separator + "build.xml");
88           fstream = new FileOutputStream( f ); /* open file stream */
89        } catch ( FileNotFoundException e ) {
90           System.out.println("Filenotfoundexception: " + e.toString() );
91           return;
92        }
93        
94        DataOutputStream ostream = null;
95        try {
96           ostream = new DataOutputStream( fstream );   /* open object stream */
97           ostream.writeBytes( this.antString() );
98  	  } catch ( IOException e ) {
99  	     System.out.println("IOexception:\n" + e.toString() + "\n" );
100 	  } finally {
101          try {
102             /* clean-up */
103             ostream.flush();
104             fstream.close();
105          } catch (Exception e1) {
106             e1.printStackTrace();
107          }
108       }
109    }
110 }