| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.ant; |
| 20 | |
|
| 21 | |
import java.io.File; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.util.Arrays; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
import net.sourceforge.jeuclid.MutableLayoutContext; |
| 27 | |
import net.sourceforge.jeuclid.context.LayoutContextImpl; |
| 28 | |
import net.sourceforge.jeuclid.context.Parameter; |
| 29 | |
import net.sourceforge.jeuclid.converter.Converter; |
| 30 | |
import net.sourceforge.jeuclid.converter.ConverterRegistry; |
| 31 | |
|
| 32 | |
import org.apache.tools.ant.BuildException; |
| 33 | |
import org.apache.tools.ant.DirectoryScanner; |
| 34 | |
import org.apache.tools.ant.Project; |
| 35 | |
import org.apache.tools.ant.taskdefs.MatchingTask; |
| 36 | |
import org.apache.tools.ant.util.FileUtils; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class MathMLConverter extends MatchingTask { |
| 44 | |
|
| 45 | |
private static final String CURRENT_DIR = "./"; |
| 46 | |
|
| 47 | |
private static final char EXTENSION_SEP = '.'; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
private File mdestDir; |
| 53 | |
|
| 54 | |
private File mbaseDir; |
| 55 | |
|
| 56 | |
private File minFile; |
| 57 | |
|
| 58 | |
private File moutFile; |
| 59 | |
|
| 60 | 0 | private String moutType = "image/png"; |
| 61 | |
|
| 62 | |
private boolean mforce; |
| 63 | |
|
| 64 | |
private final MutableLayoutContext context; |
| 65 | |
|
| 66 | |
private final FileUtils fileUtils; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 0 | public MathMLConverter() { |
| 72 | 0 | this.context = new LayoutContextImpl(LayoutContextImpl |
| 73 | |
.getDefaultLayoutContext()); |
| 74 | 0 | this.fileUtils = FileUtils.getFileUtils(); |
| 75 | 0 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
@Override |
| 82 | |
public void execute() { |
| 83 | |
DirectoryScanner scanner; |
| 84 | |
String[] list; |
| 85 | |
String[] dirs; |
| 86 | |
|
| 87 | 0 | if (this.mbaseDir == null) { |
| 88 | 0 | this.mbaseDir = this.getProject().resolveFile( |
| 89 | |
MathMLConverter.CURRENT_DIR); |
| 90 | 0 | this.log("Base is not sets, sets to " + this.mbaseDir, |
| 91 | |
Project.MSG_WARN); |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | 0 | if ((this.minFile != null) && (this.moutFile != null)) { |
| 96 | 0 | this.log("Transforming file: " + this.minFile + " --> " |
| 97 | |
+ this.moutFile, Project.MSG_VERBOSE); |
| 98 | |
try { |
| 99 | 0 | Converter.getInstance().convert(this.minFile, this.moutFile, |
| 100 | |
this.moutType, this.context); |
| 101 | 0 | } catch (final IOException io) { |
| 102 | 0 | throw new BuildException(io); |
| 103 | 0 | } |
| 104 | 0 | return; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | 0 | if (this.mdestDir == null) { |
| 114 | 0 | throw new BuildException("m_destDir attributes must be set!"); |
| 115 | |
} |
| 116 | 0 | scanner = this.getDirectoryScanner(this.mbaseDir); |
| 117 | 0 | this.log("Transforming into " + this.mdestDir, Project.MSG_INFO); |
| 118 | |
|
| 119 | |
|
| 120 | 0 | list = scanner.getIncludedFiles(); |
| 121 | 0 | this.log("Included files: " + Arrays.toString(list), |
| 122 | |
Project.MSG_VERBOSE); |
| 123 | 0 | this.process(this.mbaseDir, Arrays.asList(list), this.mdestDir); |
| 124 | |
|
| 125 | |
|
| 126 | 0 | dirs = scanner.getIncludedDirectories(); |
| 127 | 0 | this.log("Included directories: " + Arrays.toString(dirs), |
| 128 | |
Project.MSG_VERBOSE); |
| 129 | 0 | for (final String dir : dirs) { |
| 130 | 0 | list = this.fileUtils.resolveFile(this.mbaseDir, dir).list(); |
| 131 | 0 | this.process(this.mbaseDir, Arrays.asList(list), this.mdestDir); |
| 132 | |
} |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public void setAntiAlias(final boolean antiAlias) { |
| 142 | 0 | this.setOption(Parameter.ANTIALIAS, antiAlias); |
| 143 | 0 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
public void setAntiAliasMinSize(final float antiAliasMinSize) { |
| 152 | 0 | this.setOption(Parameter.ANTIALIAS_MINSIZE, antiAliasMinSize); |
| 153 | 0 | } |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public void setBackgroundColor(final String color) { |
| 162 | 0 | if (this.isNullOrEmpty(color)) { |
| 163 | 0 | this.log("Attribute \"backgroundcolor\" is empty, not used", |
| 164 | |
Project.MSG_WARN); |
| 165 | |
} else { |
| 166 | 0 | this.setOption(Parameter.MATHBACKGROUND, color); |
| 167 | |
} |
| 168 | 0 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public void setDebug(final boolean debug) { |
| 177 | 0 | this.setOption(Parameter.DEBUG, debug); |
| 178 | 0 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
public void setDisplay(final String display) { |
| 189 | 0 | this.setOption(Parameter.DISPLAY, display); |
| 190 | 0 | } |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public void setFontsDoublestruck(final String fonts) { |
| 199 | 0 | this.setOption(Parameter.FONTS_DOUBLESTRUCK, fonts); |
| 200 | 0 | } |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
public void setFontsFraktur(final String fonts) { |
| 209 | 0 | this.setOption(Parameter.FONTS_FRAKTUR, fonts); |
| 210 | 0 | } |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
public void setFontSize(final float fontSize) { |
| 219 | 0 | this.setOption(Parameter.MATHSIZE, fontSize); |
| 220 | 0 | } |
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
public void setFontsMonospaced(final String fonts) { |
| 229 | 0 | this.setOption(Parameter.FONTS_MONOSPACED, fonts); |
| 230 | 0 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public void setFontsSansSerif(final String fonts) { |
| 239 | 0 | this.setOption(Parameter.FONTS_SANSSERIF, fonts); |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
public void setFontsScript(final String fonts) { |
| 249 | 0 | this.setOption(Parameter.FONTS_SCRIPT, fonts); |
| 250 | 0 | } |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public void setFontsSerif(final String fonts) { |
| 259 | 0 | this.setOption(Parameter.FONTS_SERIF, fonts); |
| 260 | 0 | } |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
public void setForegroundColor(final String color) { |
| 269 | 0 | if (this.isNullOrEmpty(color)) { |
| 270 | 0 | this |
| 271 | |
.log( |
| 272 | |
"Attribute \"foregroundcolor\" is empty, use default color", |
| 273 | |
Project.MSG_WARN); |
| 274 | |
} else { |
| 275 | 0 | this.setOption(Parameter.MATHCOLOR, color); |
| 276 | |
} |
| 277 | 0 | } |
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public void setMfracKeepScriptLevel(final boolean keepScriptLevel) { |
| 287 | 0 | this.setOption(Parameter.MFRAC_KEEP_SCRIPTLEVEL, keepScriptLevel); |
| 288 | 0 | } |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
public void setScriptLevel(final int level) { |
| 297 | 0 | this.setOption(Parameter.SCRIPTLEVEL, level); |
| 298 | 0 | } |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
public void setScriptMinSize(final float minSize) { |
| 307 | 0 | this.setOption(Parameter.SCRIPTMINSIZE, minSize); |
| 308 | 0 | } |
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
public void setScriptSizeMult(final float multSize) { |
| 317 | 0 | this.setOption(Parameter.SCRIPTSIZEMULTIPLIER, multSize); |
| 318 | 0 | } |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
public void setForce(final boolean force) { |
| 327 | 0 | this.logProperty("force", force); |
| 328 | 0 | this.mforce = force; |
| 329 | 0 | } |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
public void setBasedir(final File dir) { |
| 338 | 0 | this.logProperty("basedir", dir); |
| 339 | 0 | this.mbaseDir = dir; |
| 340 | 0 | } |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public void setDestdir(final File dir) { |
| 350 | 0 | this.logProperty("destdir", dir); |
| 351 | 0 | this.mdestDir = dir; |
| 352 | 0 | } |
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
public void setOut(final File outFile) { |
| 361 | 0 | this.logProperty("out", outFile); |
| 362 | 0 | this.moutFile = outFile; |
| 363 | 0 | } |
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
public void setIn(final File inFile) { |
| 372 | 0 | this.logProperty("in", inFile); |
| 373 | 0 | this.minFile = inFile; |
| 374 | 0 | } |
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
public void setType(final String mimetype) { |
| 383 | 0 | this.logProperty("type", mimetype); |
| 384 | 0 | this.moutType = mimetype; |
| 385 | 0 | } |
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
private void process(final File baseDir, final List<String> xmlFiles, |
| 399 | |
final File destDir) { |
| 400 | 0 | for (final String xmlFile : xmlFiles) { |
| 401 | 0 | File outFile = null; |
| 402 | 0 | File inFile = null; |
| 403 | 0 | final String suffix = MathMLConverter.EXTENSION_SEP |
| 404 | |
+ ConverterRegistry.getInstance().getSuffixForMimeType( |
| 405 | |
this.moutType); |
| 406 | 0 | this.log("Found extension: " + suffix, Project.MSG_DEBUG); |
| 407 | |
try { |
| 408 | 0 | inFile = this.fileUtils.resolveFile(baseDir, xmlFile); |
| 409 | 0 | final int dotPos = xmlFile |
| 410 | |
.lastIndexOf(MathMLConverter.EXTENSION_SEP); |
| 411 | |
|
| 412 | 0 | if (dotPos > 0) { |
| 413 | 0 | outFile = this.fileUtils.resolveFile(destDir, xmlFile |
| 414 | |
.substring(0, dotPos) |
| 415 | |
+ suffix); |
| 416 | |
} else { |
| 417 | 0 | outFile = this.fileUtils.resolveFile(destDir, xmlFile |
| 418 | |
+ suffix); |
| 419 | |
} |
| 420 | 0 | this.log("Input file: " + inFile, Project.MSG_DEBUG); |
| 421 | 0 | this.log("Output file: " + outFile, Project.MSG_DEBUG); |
| 422 | 0 | if (this.mforce |
| 423 | |
|| !this.fileUtils.isUpToDate(inFile, outFile)) { |
| 424 | 0 | this.fileUtils.createNewFile(outFile, true); |
| 425 | 0 | Converter.getInstance().convert(inFile, outFile, |
| 426 | |
this.moutType, this.context); |
| 427 | |
} |
| 428 | 0 | } catch (final IOException ex) { |
| 429 | |
|
| 430 | |
|
| 431 | 0 | this.log("Failed to process " + inFile, Project.MSG_ERR); |
| 432 | 0 | FileUtils.delete(outFile); |
| 433 | |
|
| 434 | 0 | throw new BuildException(ex); |
| 435 | 0 | } |
| 436 | 0 | } |
| 437 | 0 | } |
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
private void setOption(final Parameter param, final String value) { |
| 448 | 0 | this.setOption(param, param.fromString(value)); |
| 449 | 0 | } |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
private void setOption(final Parameter param, final Object value) { |
| 460 | 0 | this.logProperty(param.getOptionName(), value); |
| 461 | 0 | this.context.setParameter(param, value); |
| 462 | 0 | } |
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
private boolean isNullOrEmpty(final String s) { |
| 474 | |
|
| 475 | 0 | return s == null || s.length() == 0; |
| 476 | |
} |
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
private void logProperty(final String param, final Object value) { |
| 487 | 0 | this.log("Sets property \"" + param + "\" with value: " + value, |
| 488 | |
Project.MSG_DEBUG); |
| 489 | 0 | } |
| 490 | |
} |