|
| 1 | +/******************************************************************************* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | + * |
| 4 | + * Copyright 2009 by sprylab technologies GmbH |
| 5 | + * |
| 6 | + * WebInLoop - a program for testing web applications |
| 7 | + * |
| 8 | + * This file is part of WebInLoop. |
| 9 | + * |
| 10 | + * WebInLoop is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Lesser General Public License version 3 |
| 12 | + * only, as published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * WebInLoop is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License version 3 for more details |
| 18 | + * (a copy is included in the LICENSE file that accompanied this code). |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser General Public License |
| 21 | + * version 3 along with WebInLoop. If not, see |
| 22 | + * <http://www.gnu.org/licenses/lgpl-3.0.html> |
| 23 | + * for a copy of the LGPLv3 License. |
| 24 | + ******************************************************************************/ |
| 25 | + |
| 26 | +package com.sprylab.webinloop.ant_webinloop_task; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +import org.apache.tools.ant.BuildException; |
| 35 | +import org.apache.tools.ant.Task; |
| 36 | +import org.apache.tools.ant.types.Path; |
| 37 | + |
| 38 | +import com.sprylab.webinloop.WebInLoop; |
| 39 | + |
| 40 | +/** |
| 41 | + * Ant task that executes WebInLoop. |
| 42 | + * |
| 43 | + * @author rzimmer |
| 44 | + * |
| 45 | + */ |
| 46 | +public class WebInLoopTask extends Task { |
| 47 | + |
| 48 | + private File configDir; |
| 49 | + |
| 50 | + private boolean debugMode; |
| 51 | + |
| 52 | + private List<Path> inputFiles = new ArrayList<Path>(); |
| 53 | + |
| 54 | + private File outputDir; |
| 55 | + |
| 56 | + private boolean translatorMode; |
| 57 | + |
| 58 | + /** |
| 59 | + * Adds a path to the inputFiles list. |
| 60 | + * |
| 61 | + * @param path |
| 62 | + * the path to add |
| 63 | + */ |
| 64 | + public void addPath(Path path) { |
| 65 | + inputFiles.add(path); |
| 66 | + } |
| 67 | + |
| 68 | + /* |
| 69 | + * (non-Javadoc) |
| 70 | + * |
| 71 | + * @see org.apache.tools.ant.Task#execute() |
| 72 | + */ |
| 73 | + public void execute() throws BuildException { |
| 74 | + try { |
| 75 | + WebInLoop webInLoop = new WebInLoop(); |
| 76 | + |
| 77 | + // configure WebInLoop |
| 78 | + webInLoop.setTranslatorMode(translatorMode); |
| 79 | + webInLoop.setDebugMode(debugMode); |
| 80 | + webInLoop.setOutputDir(outputDir); |
| 81 | + webInLoop.setConfigDir(configDir); |
| 82 | + webInLoop.setInputFiles(toFileList(inputFiles)); |
| 83 | + |
| 84 | + // execute WebInLoop |
| 85 | + try { |
| 86 | + webInLoop.start(); |
| 87 | + } catch (Exception e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + } catch (Exception e) { |
| 91 | + throw new BuildException(e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param configDir |
| 97 | + * the configDir to set |
| 98 | + */ |
| 99 | + public void setConfigDir(File configDir) { |
| 100 | + this.configDir = configDir; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param debugMode |
| 105 | + * the debugMode to set |
| 106 | + */ |
| 107 | + public void setDebugMode(boolean debugMode) { |
| 108 | + this.debugMode = debugMode; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param inputFiles |
| 113 | + * the inputFiles to set |
| 114 | + */ |
| 115 | + public void setInputFiles(List<Path> inputFiles) { |
| 116 | + this.inputFiles = inputFiles; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param outputDir |
| 121 | + * the outputDir to set |
| 122 | + */ |
| 123 | + public void setOutputDir(File outputDir) { |
| 124 | + this.outputDir = outputDir; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param translatorMode |
| 129 | + * the translatorMode to set |
| 130 | + */ |
| 131 | + public void setTranslatorMode(boolean translatorMode) { |
| 132 | + this.translatorMode = translatorMode; |
| 133 | + } |
| 134 | + |
| 135 | + public List<File> toFileList(List<Path> paths) { |
| 136 | + // using set to avoid duplicates |
| 137 | + Set<File> files = new HashSet<File>(); |
| 138 | + |
| 139 | + for (Path path : paths) { |
| 140 | + String[] fileNames = path.list(); |
| 141 | + for (int i = 0; i < fileNames.length; i++) { |
| 142 | + File file = new File(fileNames[i]); |
| 143 | + if (file.exists()) { |
| 144 | + files.add(file); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return new ArrayList<File>(files); |
| 150 | + } |
| 151 | +} |
0 commit comments