1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
181 if(versionextention != null){
182
183 stringWithoutExtention = stringWithoutExtention + versionextention;
184 }
185
186
187 String stringWithExtention = stringWithoutExtention + "." + extention;
188
189 String filename = stringWithExtention;
190 baseDir = filename;
191 try {
192 URI uri = new URI(stringWithExtention);
193
194
195 String uriString = uri.toString();
196
197
198 filename = uriString.substring(uriString.lastIndexOf("/") + 1 , uriString.length());
199
200
201 baseDir = uriString.substring(0, uriString.lastIndexOf("/") + 1);
202
203 } catch (URISyntaxException e) {
204
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
218 TransformerFactory tf = TransformerFactory.newInstance();
219
220
221 Templates template = tf.newTemplates(new StreamSource(xslfile));
222
223
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
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
282
283
284
285
286
287
288
289
290
291
292
293
294