View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.ant.tasks.docbook;
19  
20  import java.io.File;
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  
27  import javax.xml.transform.Templates;
28  import javax.xml.transform.Transformer;
29  import javax.xml.transform.TransformerConfigurationException;
30  import javax.xml.transform.TransformerFactory;
31  import javax.xml.transform.stream.StreamSource;
32  
33  import org.apache.tools.ant.BuildException;
34  import org.apache.tools.ant.DirectoryScanner;
35  import org.apache.tools.ant.Task;
36  import org.apache.tools.ant.types.FileSet;
37  
38  /***
39   * <code>AbstractDocBookTask.java</code>
40   *
41   *	This Abstract class represents a DocBook Task
42   *	other classes which implements this class must inplement the execute methode to
43   *	execute the task.
44   *
45   *	The class can only have a child which is a XSLTParameterTask and has the name of
46   *	"param" (This is a bad behaviour of ant)
47   *	
48   *	There are tree attributes that need to be set:
49   *	docbookfile representing the xml file which contains the docbook file to be transformed 
50   * xslfile the xsl file which will be used during transformation
51   * outputdir in this directory the output will be generated
52   * 
53   * @author Stefan Lenselink
54   * @version $Revision: 1.1 $
55   */
56  
57  public abstract class AbstractDocBookTask extends Task {
58  	private String docbookfile;
59  	private String versionextention;
60  	private String baseDir;
61  	private File xslfile;
62  	private File outputdir;
63  	private HashMap paramMap;
64  	private String[] files;
65  	private boolean useFileSet = false;
66  	private int currentFilesPos = 0;
67  	
68  	/***
69  	 * The setter method for the docbookfile attribute
70  	 * 
71  	 * @param docbookfile the path of the file to be used as the docbook source file
72  	 */
73  	public void setDocbookfile(String docbookfile){
74  		if(docbookfile == null){
75  			useFileSet = true;
76  		}else{
77  			if(useFileSet){
78  				System.out.println("There is a FileSet Specifyed, that will be used, " + docbookfile + " will be ignored");
79  			}else{
80  				this.docbookfile = docbookfile;
81  			}
82  		}
83  	}
84  	
85  	/***
86  	 * The setter method for the xslfile attribute
87  	 * 
88  	 * @param xslfile the xsl file to use to transform the docbookfile
89  	 */
90  	public void setXslfile(File xslfile){
91  		this.xslfile = xslfile;
92  	}
93  	
94  	/***
95  	 * The setter method for the outputdir attribute
96  	 * 
97  	 * @param outputdir the directory to put the output
98  	 */
99  	public void setOutputdir(File outputdir){
100 		this.outputdir = outputdir;
101 	}
102 	
103 	/***
104 	 * Add a nested XSLTParameterTask to this task
105 	 * 
106 	 * Watch it!! the name of the taskdef MUST be param and can't be changed!!!
107 	 * example: <taskdef name="param" classpathref="classpath" classname="com.finalist.ant.tasks.XSLTParameterTask"/>
108 	 * If you need to change the name of the taskdef change the method in the code as well
109 	 * This is because of the design of ant, can't help it.
110 	 * 
111 	 * implementing the TaskContainer interface and implement the addTask(Task task) method
112 	 * wouldn't help because a other classloader will be used to load XSLTParameterTask and so It can't be CAST
113 	 * 
114 	 * @param task the XSLTParameterTask containing the parameters used for transformation
115 	 */
116 	public void addConfiguredParam(XSLTParameterTask task){
117 		if(paramMap == null){
118 			paramMap = new HashMap();
119 		}
120 		paramMap.put(task.getName(), task.getValue());
121 	}
122 	/***
123 	 * This method addes a fileset to the current task, the files specifyed will be used
124 	 * for generation 
125 	 * 
126 	 * @param fileSet the FileSet to use
127 	 */
128 	public void addConfiguredFileset(FileSet fileSet){
129 		if(docbookfile != null){
130 			System.out.println("There is a docbookfile " + docbookfile + " specifyed but FileSet will be used");
131 		}
132 		useFileSet = true;
133 		DirectoryScanner scanner = fileSet.getDirectoryScanner(this.getProject());
134 		String[] arr = scanner.getIncludedFiles();
135 		files = new String[arr.length];
136 		File dir = scanner.getBasedir();
137 		for (int i = 0; i < arr.length; i++) {
138 			String string = arr[i];
139 			File f = new File(dir, string);
140 			files[i] = f.toURI().toString();
141 		}
142 	}
143 	/***
144 	 * This abstract method must be implemented by a implementing class to preform the execution of the task 
145 	 * 
146 	 * @see org.apache.tools.ant.Task#execute()
147 	 */
148 	public abstract void execute() throws BuildException;
149 	
150 	/***
151 	 * Getter method for the docbookfile attribute
152 	 * 
153 	 * @return the docbook source file specifyed with the docbookfile attribute
154 	 */
155 	protected String getDocbookfile() {
156 		if(useFileSet){
157 			String file = files[currentFilesPos];
158 			currentFilesPos++;
159 			return file;
160 		}else{
161 			return docbookfile;
162 		}
163 	}
164 
165 	/***
166 	 * This method returns a File to write the transformed document to
167 	 * 
168 	 * @param extention the extention which the generated file should have (normaly html or pdf)
169 	 * @return the full path to the file which need to be generated
170 	 */
171 	protected File getOutputFile(String extention){
172 		//remove the old extention
173 		String stringWithoutExtention;
174 		if(useFileSet){
175 			stringWithoutExtention = files[currentFilesPos].substring(0, files[currentFilesPos].lastIndexOf("."));
176 		}else{
177 			stringWithoutExtention= docbookfile.substring(0, docbookfile.lastIndexOf("."));
178 		}
179 		
180 		//is there a versionextention set?
181 		if(versionextention != null){
182 			//add the versionextention
183 			stringWithoutExtention = stringWithoutExtention + versionextention;
184 		}
185 		
186 		//add the new extention
187 		String stringWithExtention = stringWithoutExtention + "." + extention;
188 
189 		String filename = stringWithExtention;
190 		baseDir = filename;
191 		try {
192 			URI uri = new URI(stringWithExtention);
193 			
194 			//Get the URI as String 
195 			String uriString = uri.toString();
196 			
197 			//Get the new filename. because we're dealing with URI's (which contains '/') a File.seperator can't be used!!  
198 			filename = uriString.substring(uriString.lastIndexOf("/") + 1 , uriString.length());
199 			
200 			//Get the baseDir!! (needed for FOP to retrieve the images)
201 			baseDir = uriString.substring(0, uriString.lastIndexOf("/") + 1);
202 			
203 		} catch (URISyntaxException e) {
204 			//Ok for some reason this is no URI; printstackTrace to know what went wrong 
205 			e.printStackTrace();
206 		}
207 		return new File(outputdir, filename);
208 	}
209 	
210 	/***
211 	 * This method return the Transformer that need to be used
212 	 * 
213 	 * @return the Transformer used
214 	 * @throws TransformerConfigurationException when a TransformerConfigurationException occours a TransformerConfigurationException will be thrown
215 	 */
216 	protected Transformer getTransformer() throws TransformerConfigurationException {
217 		//Get a instance of the TransformerFactory
218 		TransformerFactory tf = TransformerFactory.newInstance();
219 		
220 		//Compile a template of the xslfile specifyed as attribute
221 		Templates template = tf.newTemplates(new StreamSource(xslfile));
222 			
223 		//Get the Transformer from the template and return it
224 		return template.newTransformer();
225 	}
226 	
227 	/***
228 	 * This method returns the total number of Cycles that will be done
229 	 * 
230 	 * @return the number of cycles
231 	 */
232 	protected int getNumberOfCycles(){
233 		if(useFileSet){
234 			return files.length;
235 		}else{
236 			return 1;
237 		}
238 	}
239 	/***
240 	 * This method returns the baseDir of the current docbook file
241 	 * Caution! the getOutputFile must be called first to prevent null returning
242 	 * @return the baseDir of the Docbook file
243 	 * @see AbstractDocBookTask#getOutputFile(String)
244 	 */
245 	protected String getBaseDir(){
246 		return baseDir;
247 	}
248 	
249 	/***
250 	 * This method sets the parameters on the given transfromer
251 	 * 
252 	 * @param transformer the transformer on which the parameters need te be set
253 	 */
254 	protected void doSetParameter(Transformer transformer){
255 		//Go fetch and set the parameters
256 		if(paramMap != null){
257 			Iterator it = paramMap.entrySet().iterator();
258 			while(it.hasNext()){
259 				Map.Entry entry = (Map.Entry) it.next();
260 				transformer.setParameter(String.valueOf(entry.getKey()), entry.getValue());
261 			}
262 		}
263 	}
264 
265 	/***
266 	 * Setter method for the versionExtention
267 	 * 
268 	 * @param string the versionExtention which needs to be added after the file name
269 	 * for example: if the versionExtention is "v1.0" and the docbookfile is manual.xml
270 	 * the html output will become manualv1.0.html 
271 	 */
272 	public void setVersionextention(String versionextention) {
273 		this.versionextention = versionextention;
274 	}
275 
276 }
277 
278 
279 
280 /*
281  * $Log: AbstractDocBookTask.java,v $
282  * Revision 1.1  2004/03/12 22:16:00  ekkelenkamp
283  * no message
284  *
285  * Revision 1.3  2004/01/14 14:40:13  stefan
286  * Added support for nested filesets, see manual for details
287  *
288  * Revision 1.2  2004/01/06 13:26:38  stefan
289  * Added local dtd support and local watermark image support, now it's independent from the Internet
290  *
291  * Revision 1.1  2003/12/29 15:46:41  stefan
292  * initial checkin of the DocBook Ant Task
293  *
294  */