Source code: jtt/docbook/DocBookEquations.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: DocBookEquations.java,v $
3 // Purpose: Descriptor base class.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.2 $
8 // $Date: 2003/10/13 08:17:00 $
9 // $Author: wegner $
10 //
11 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation version 2 of the License.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 ///////////////////////////////////////////////////////////////////////////////
22 package jtt.docbook;
23
24 import jtt.util.Executable;
25
26 import wsi.ra.tool.PropertyHolder;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.io.LineNumberReader;
34 import java.io.PrintStream;
35
36 import java.util.StringTokenizer;
37
38 import org.apache.log4j.Category;
39 import org.apache.tools.ant.DirectoryScanner;
40
41
42 /*==========================================================================*
43 * IMPORTS
44 *========================================================================== */
45 /*==========================================================================*
46 * INTERFACE DECLARATION
47 *========================================================================== */
48
49 /**
50 * Interface for defining a decimal formatter.
51 *
52 * @author wegnerj
53 * @license GPL
54 * @cvsversion $Revision: 1.2 $, $Date: 2003/10/13 08:17:00 $
55 */
56 public class DocBookEquations
57 {
58 //~ Static fields/initializers /////////////////////////////////////////////
59
60 private static Category logger = Category.getInstance(
61 "jtt.docbook.DocBookEquations");
62
63 //~ Instance fields ////////////////////////////////////////////////////////
64
65 private PropertyHolder propertyHolder;
66 private String baseDir;
67 private String convert;
68 private String dvips;
69 private String fileExtension;
70 private String latex;
71 private boolean equationBold;
72 private boolean equationLarge;
73 private int fontSize;
74
75 //~ Constructors ///////////////////////////////////////////////////////////
76
77 public DocBookEquations() throws Exception
78 {
79 propertyHolder = PropertyHolder.instance();
80
81 if (!loadParameters())
82 {
83 throw new Exception("Could not get all parameters.");
84 }
85 }
86
87 //~ Methods ////////////////////////////////////////////////////////////////
88
89 public String[] getFileList()
90 {
91 DirectoryScanner ds = new DirectoryScanner();
92 String[] includes = {"**\\*." + fileExtension};
93 ds.setIncludes(includes);
94
95 //System.out.println("BASEDIR: " + baseDir);
96 ds.setBasedir(new File(baseDir));
97 ds.setCaseSensitive(true);
98 ds.scan();
99
100 return ds.getIncludedFiles();
101 }
102
103 public void apply(String[] args)
104 {
105 baseDir = args[0];
106
107 String[] files = getFileList();
108
109 //System.out.println("FILES:");
110 for (int i = 0; i < files.length; i++)
111 {
112 //System.out.println(files[i]);
113 readFile(baseDir + "/" + files[i]);
114 }
115 }
116
117 public void createGIFimage(String directory, String file, String equation)
118 {
119 // System.out.println("---");
120 // System.out.println(directory);
121 // System.out.println(file);
122 // System.out.println(equation);
123 // System.out.println("---");
124 File fileE = new File(file + ".gif");
125 System.out.println("file: " + file + ".gif exists: " + fileE.exists());
126
127 //if(fileE.exists())return;
128 boolean useAnt = true;
129
130 if (useAnt)
131 {
132 String[] args = new String[]{file + ".tex"};
133 Executable.execute(baseDir + "/" + directory, latex, args);
134
135 args = new String[]{"-E", file + ".dvi", "-o", file + ".eps"};
136 Executable.execute(baseDir + "/" + directory, dvips, args);
137 args = new String[]{"-antialias", file + ".eps", file + ".gif"};
138 Executable.execute(baseDir + "/" + directory, convert, args);
139 }
140 else
141 {
142 String[] args = new String[]{latex, file + ".tex"};
143 Executable.execute(args, true);
144 args = new String[]
145 {
146 dvips, "-E", baseDir + "/" + directory + "/" + file +
147 ".dvi", file + ".dvi", "-o",
148
149 baseDir + "/" + directory + "/" + file + file + ".eps"
150 };
151 Executable.execute(baseDir + "/" + directory, dvips, args);
152 args = new String[]
153 {
154 convert, "-antialias",
155
156 baseDir + "/" + directory + "/" + file + ".eps",
157
158 baseDir + "/" + directory + "/" + file + ".gif"
159 };
160 Executable.execute(args, true);
161 }
162 }
163
164 public void generateLatexFile(String directory, String file, String equation)
165 {
166 // System.out.println("---");
167 // System.out.println(directory);
168 // System.out.println(file);
169 // System.out.println(equation);
170 // System.out.println("---");
171 FileOutputStream os = null;
172 PrintStream ps = null;
173
174 try
175 {
176 os = new FileOutputStream(baseDir + "/" + directory + "/" + file +
177 ".tex");
178 ps = new PrintStream(os);
179 }
180 catch (Exception ex)
181 {
182 ex.printStackTrace();
183 }
184
185 ps.print("\\documentclass[");
186 ps.print(fontSize);
187 ps.println("]{article}");
188 ps.println("\\pagestyle{empty}\n\\begin{document}");
189
190 if (equationLarge)
191 {
192 ps.println("\\large");
193 }
194
195 ps.println("\\begin{eqnarray}");
196
197 if (equationBold)
198 {
199 ps.print("\\bf");
200 }
201
202 ps.println(equation);
203 ps.println("\\end{eqnarray}\n\\end{document}");
204
205 try
206 {
207 ps.close();
208 os.close();
209 }
210 catch (IOException e)
211 {
212 e.printStackTrace();
213 }
214 }
215
216 /**
217 * Description of the Method
218 *
219 * @return Description of the Return Value
220 */
221 public boolean loadParameters() throws Exception
222 {
223 String value;
224
225 if ((value = propertyHolder.getProperty(this, "latex")) == null)
226 {
227 logger.error("LaTeX executable not defined.");
228
229 return false;
230 }
231 else
232 {
233 latex = value;
234 }
235
236 if ((value = propertyHolder.getProperty(this, "dvips")) == null)
237 {
238 logger.error("DviPs executable not defined.");
239
240 return false;
241 }
242 else
243 {
244 dvips = value;
245 }
246
247 if ((value = propertyHolder.getProperty(this, "convert")) == null)
248 {
249 logger.error("ImageMagick's convert executable not defined.");
250
251 return false;
252 }
253 else
254 {
255 convert = value;
256 }
257
258 if ((value = propertyHolder.getProperty(this, "fileExtension")) == null)
259 {
260 logger.error("File extension not defined.");
261
262 return false;
263 }
264 else
265 {
266 fileExtension = value;
267 }
268
269 value = PropertyHolder.instance().getProperty(this, "bold");
270
271 if (((value != null) && value.equalsIgnoreCase("true")))
272 {
273 equationBold = true;
274 }
275 else
276 {
277 equationBold = false;
278 }
279
280 value = PropertyHolder.instance().getProperty(this, "large");
281
282 if (((value != null) && value.equalsIgnoreCase("true")))
283 {
284 equationLarge = true;
285 }
286 else
287 {
288 equationLarge = false;
289 }
290
291 fontSize = propertyHolder.getInt(this, "fontSize", 12);
292
293 return true;
294 }
295
296 /**
297 * The main program for the TestSmarts class
298 *
299 * @param args The command line arguments
300 */
301 public static void main(String[] args)
302 {
303 DocBookEquations createEquations;
304
305 try
306 {
307 createEquations = new DocBookEquations();
308 createEquations.apply(args);
309 }
310 catch (Exception e)
311 {
312 e.printStackTrace();
313 System.exit(1);
314 }
315
316 System.exit(0);
317 }
318
319 public boolean parseEquation(String equation)
320 {
321 if (equation.startsWith("<!--"))
322 {
323 if (equation.endsWith("-->"))
324 {
325 int index = equation.indexOf(":");
326
327 if (index == -1)
328 {
329 logger.error("LATEXEQUATION ':' delimiter not found.");
330 logger.error(
331 "<!-- LATEXEQUATION directory file: equation -->");
332
333 return false;
334 }
335
336 String equ = equation.substring(index + 1, equation.length() -
337 3);
338 StringTokenizer st = new StringTokenizer(equation.substring(4,
339 index), " \t");
340
341 if (st.hasMoreTokens())
342 {
343 if (!st.nextToken().equalsIgnoreCase("LATEXEQUATION"))
344 {
345 logger.error(
346 "LATEXEQUATION key word expected as first entry.");
347 logger.error(
348 "<!-- LATEXEQUATION directory file: equation -->");
349 }
350 }
351
352 String dir = null;
353
354 if (st.hasMoreTokens())
355 {
356 dir = st.nextToken();
357 }
358 else
359 {
360 logger.error("directory is missing.");
361 logger.error(
362 "<!-- LATEXEQUATION directory file: equation -->");
363 }
364
365 String file = null;
366
367 if (st.hasMoreTokens())
368 {
369 file = st.nextToken();
370 }
371 else
372 {
373 logger.error("file is missing.");
374 logger.error(
375 "<!-- LATEXEQUATION directory file: equation -->");
376 }
377
378 generateLatexFile(dir, file, equ);
379 createGIFimage(dir, file, equ);
380 }
381 else
382 {
383 logger.error("LATEXEQUATION should end with -->");
384
385 return false;
386 }
387 }
388 else
389 {
390 logger.error("LATEXEQUATION should start with <!--");
391
392 return false;
393 }
394
395 return true;
396 }
397
398 public void readFile(String file)
399 {
400 FileInputStream in = null;
401 LineNumberReader lnr = null;
402
403 try
404 {
405 in = new FileInputStream(file);
406 lnr = new LineNumberReader(new InputStreamReader(in));
407 }
408 catch (Exception ex)
409 {
410 ex.printStackTrace();
411 }
412
413 FileInputStream is = null;
414
415 try
416 {
417 String line;
418 int index;
419
420 while ((line = lnr.readLine()) != null)
421 {
422 if (line.indexOf("LATEXEQUATION") != -1)
423 {
424 //System.out.println(line);
425 if (!parseEquation(line))
426 {
427 logger.error("in file " + file);
428 }
429 }
430 }
431 }
432 catch (IOException ex)
433 {
434 ex.printStackTrace();
435 System.exit(1);
436 }
437 }
438
439 /**
440 * Description of the Method
441 */
442 public void usage()
443 {
444 StringBuffer sb = new StringBuffer();
445 String programName = this.getClass().getName();
446
447 sb.append("\nUsage is : ");
448 sb.append("java -cp . ");
449 sb.append(programName);
450 sb.append(" <args>");
451 sb.append(
452 "\n\nThis is version $Revision: 1.2 $ ($Date: 2003/10/13 08:17:00 $)\n");
453
454 System.out.println(sb.toString());
455
456 System.exit(0);
457 }
458 }
459 ///////////////////////////////////////////////////////////////////////////////
460 // END OF FILE.
461 ///////////////////////////////////////////////////////////////////////////////