use of org.apache.tools.ant.BuildException in project gate-core by GateNLP.
the class PackageGappTask method getUnresolvedResourcesTarget.
/**
* Get a URL for a directory to which the given (unresolved) resource
* directory should be mapped.
*
* @param unresolvedResourcesSubDirs a map from URLs of directories
* containing unresolved resources to the URLs under the
* target unresolved-resources directory that they will be
* mapped to. This map is updated by this method.
* @param unresolvedResourcesDir the top-level application-resources
* directory in the target location.
* @param resourceDir a directory containing an unresolved resource.
* @return the URL under application-resources to which this directory
* should be mapped. For a resourceDir of the form .../foo,
* the returned URL would typically be
* <applicationResourcesDir>/foo, but if a different
* directory with the same name has already been mapped then
* we will return the first ..../foo-2, foo-3, etc. that is
* not already in use. If one of the directory's ancestors
* has already been mapped then we return a URL pointing
* to the same relative path inside that ancestor's mapping,
* e.g. if .../foo has already been mapped to a-r/foo-2 then
* .../foo/bar/baz will map to a-r/foo-2/bar/baz.
*/
private URL getUnresolvedResourcesTarget(TreeMap<URL, URL> unresolvedResourcesSubDirs, URL unresolvedResourcesDir, URL resourceDir) throws BuildException {
URL targetDir = unresolvedResourcesSubDirs.get(resourceDir);
try {
if (targetDir == null) {
// no exact match, try an ancestor match
SortedMap<URL, URL> possibleAncestors = unresolvedResourcesSubDirs.headMap(resourceDir);
URL nearestAncestor = null;
if (!possibleAncestors.isEmpty())
nearestAncestor = possibleAncestors.lastKey();
if (nearestAncestor != null && resourceDir.toExternalForm().startsWith(nearestAncestor.toExternalForm())) {
// found an ancestor mapping, so take the relative path
// from the ancestor to this dir and map it to the same
// path under the ancestor's mapping.
String relPath = PersistenceManager.getRelativePath(nearestAncestor, resourceDir);
targetDir = new URL(unresolvedResourcesSubDirs.get(nearestAncestor), relPath);
} else {
// no ancestors currently mapped, so start a new sub-dir of
// unresolvedResourcesDir whose name is the last path
// component of the source URL
String resourcePath = resourceDir.getFile();
if (resourcePath.endsWith("/")) {
resourcePath = resourcePath.substring(0, resourcePath.length() - 1);
}
String targetDirName = resourcePath.substring(resourcePath.lastIndexOf('/') + 1);
if (targetDirName.length() == 0) {
// edge case, if the source URL points to the root directory "/"
targetDirName = "resources";
}
// try application-resources/{targetDirName} as the target
targetDir = new URL(unresolvedResourcesDir, targetDirName + "/");
// one that is available.
if (unresolvedResourcesSubDirs.containsValue(targetDir)) {
int index = 2;
do {
targetDir = new URL(unresolvedResourcesDir, targetDirName + "-" + (index++) + "/");
} while (unresolvedResourcesSubDirs.containsValue(targetDir));
}
}
// store the mapping for future use
unresolvedResourcesSubDirs.put(resourceDir, targetDir);
}
} catch (MalformedURLException e) {
throw new BuildException("Can't construct target URL for directory " + resourceDir, e, getLocation());
}
return targetDir;
}
use of org.apache.tools.ant.BuildException in project gate-core by GateNLP.
the class GazetteerLists method load.
/**
* Parse the definition and populate the array of list names.
*/
private void load() {
log("Listing gazetteer lists", Project.MSG_VERBOSE);
if (definition == null) {
throw new BuildException("\"definition\" attribute is required for gazetteerlists");
}
log("definition file: " + definition, Project.MSG_VERBOSE);
Set<String> lists = new HashSet<String>();
BufferedReader in = null;
try {
if (encoding == null) {
in = new BomStrippingInputStreamReader(new FileInputStream(definition));
} else {
in = new BomStrippingInputStreamReader(new FileInputStream(definition), encoding);
}
String line;
while ((line = in.readLine()) != null) {
int indexOfColon = line.indexOf(':');
// Ignore lines that don't include a colon.
if (indexOfColon > 0) {
String listFile = line.substring(0, indexOfColon);
lists.add(listFile);
log("Found list file " + listFile, Project.MSG_VERBOSE);
}
}
} catch (IOException ioe) {
throw new BuildException("Error reading gazetteer definition file " + definition, ioe);
} finally {
IOUtils.closeQuietly(in);
}
listNames = lists.toArray(new String[lists.size()]);
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class MacroDef method execute.
/**
* Create a new ant type based on the embedded tasks and types.
*/
@Override
public void execute() {
if (nestedSequential == null) {
throw new BuildException("Missing sequential element");
}
if (name == null) {
throw new BuildException("Name not specified");
}
name = ProjectHelper.genComponentName(getURI(), name);
MyAntTypeDefinition def = new MyAntTypeDefinition(this);
def.setName(name);
def.setClass(MacroInstance.class);
ComponentHelper helper = ComponentHelper.getComponentHelper(getProject());
helper.addDataTypeDefinition(def);
log("creating macro " + name, Project.MSG_VERBOSE);
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class MacroInstance method execute.
/**
* Execute the templates instance.
* Copies the unknown element, substitutes the attributes,
* and calls perform on the unknown element.
*/
@Override
public void execute() {
presentElements = new HashMap<>();
getNsElements();
processTasks();
localAttributes = new Hashtable<>();
Set<String> copyKeys = new HashSet<>(map.keySet());
for (Attribute attribute : macroDef.getAttributes()) {
String value = map.get(attribute.getName());
if (value == null && "description".equals(attribute.getName())) {
value = getDescription();
}
if (value == null) {
value = attribute.getDefault();
value = macroSubs(value, localAttributes);
}
if (value == null) {
throw new BuildException("required attribute %s not set", attribute.getName());
}
localAttributes.put(attribute.getName(), value);
copyKeys.remove(attribute.getName());
}
copyKeys.remove("id");
if (macroDef.getText() != null) {
if (text == null) {
String defaultText = macroDef.getText().getDefault();
if (!macroDef.getText().getOptional() && defaultText == null) {
throw new BuildException("required text missing");
}
text = defaultText == null ? "" : defaultText;
}
if (macroDef.getText().getTrim()) {
text = text.trim();
}
localAttributes.put(macroDef.getText().getName(), text);
} else if (text != null && !text.trim().isEmpty()) {
throw new BuildException("The \"%s\" macro does not support nested text data.", getTaskName());
}
if (!copyKeys.isEmpty()) {
throw new BuildException("Unknown attribute" + (copyKeys.size() > 1 ? "s " : " ") + copyKeys);
}
// need to set the project on unknown element
UnknownElement c = copy(macroDef.getNestedTask(), false);
c.init();
LocalProperties localProperties = LocalProperties.get(getProject());
localProperties.enterScope();
try {
c.perform();
} catch (BuildException ex) {
if (macroDef.getBackTrace()) {
throw ProjectHelper.addLocationToBuildException(ex, getLocation());
} else {
ex.setLocation(getLocation());
throw ex;
}
} finally {
presentElements = null;
localAttributes = null;
localProperties.exitScope();
}
}
use of org.apache.tools.ant.BuildException in project ant by apache.
the class ManifestClassPath method execute.
/**
* Sets a property, which must not already exist, with a space
* separated list of files and directories relative to the jar
* file's parent directory.
*/
@Override
public void execute() {
if (name == null) {
throw new BuildException("Missing 'property' attribute!");
}
if (dir == null) {
throw new BuildException("Missing 'jarfile' attribute!");
}
if (getProject().getProperty(name) != null) {
throw new BuildException("Property '%s' already set!", name);
}
if (path == null) {
throw new BuildException("Missing nested <classpath>!");
}
StringBuilder tooLongSb = new StringBuilder();
for (int i = 0; i < maxParentLevels + 1; i++) {
tooLongSb.append("../");
}
final String tooLongPrefix = tooLongSb.toString();
// Normalize the reference directory (containing the jar)
final FileUtils fileUtils = FileUtils.getFileUtils();
dir = fileUtils.normalize(dir.getAbsolutePath());
String[] elements = path.list();
StringBuilder buffer = new StringBuilder();
for (String element : elements) {
// Normalize the current file
File pathEntry = new File(element);
String fullPath = pathEntry.getAbsolutePath();
pathEntry = fileUtils.normalize(fullPath);
String relPath = null;
String canonicalPath = null;
try {
if (dir.equals(pathEntry)) {
relPath = ".";
} else {
relPath = FileUtils.getRelativePath(dir, pathEntry);
}
canonicalPath = pathEntry.getCanonicalPath();
// getRelativePath always uses '/' as separator, adapt
if (File.separatorChar != '/') {
canonicalPath = canonicalPath.replace(File.separatorChar, '/');
}
} catch (Exception e) {
throw new BuildException("error trying to get the relative path" + " from " + dir + " to " + fullPath, e);
}
// No match, so bail out!
if (relPath.equals(canonicalPath) || relPath.startsWith(tooLongPrefix)) {
throw new BuildException("No suitable relative path from %s to %s", dir, fullPath);
}
if (pathEntry.isDirectory() && !relPath.endsWith("/")) {
relPath = relPath + '/';
}
relPath = Locator.encodeURI(relPath);
// Manifest's ClassPath: attribute always uses forward
// slashes '/', and is space-separated. Ant will properly
// format it on 72 columns with proper line continuation
buffer.append(relPath);
buffer.append(' ');
}
// Finally assign the property with the manifest classpath
getProject().setNewProperty(name, buffer.toString().trim());
}
Aggregations