use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.
the class WikiToDocTask method markupToDoc.
private void markupToDoc(MarkupLanguage markupLanguage, Path path, String markupContent, Map<String, SplitOutlineItem> pathNameToOutline, Set<String> imageFilenames) throws BuildException {
File htmlOutputFile = computeHtmlOutputFile(path);
File pathDir = htmlOutputFile.getParentFile();
if (!pathDir.exists()) {
if (!pathDir.mkdirs()) {
throw new BuildException(// $NON-NLS-1$
MessageFormat.format(// $NON-NLS-1$
Messages.getString("WikiToDocTask_cannot_create_dest_folder"), pathDir.getAbsolutePath()));
}
}
Writer writer;
try {
// $NON-NLS-1$
writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
} catch (Exception e) {
throw new BuildException(// $NON-NLS-1$
MessageFormat.format(// $NON-NLS-1$
Messages.getString("WikiToDocTask_cannot_create_output_file"), // $NON-NLS-1$
htmlOutputFile, e.getMessage()), e);
}
try {
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
for (Stylesheet stylesheet : stylesheets) {
HtmlDocumentBuilder.Stylesheet builderStylesheet = createBuilderStylesheet(pathDir, stylesheet);
builder.addCssStylesheet(builderStylesheet);
}
builder.setTitle(computeTitle(path));
builder.setEmitDtd(emitDoctype);
if (emitDoctype && htmlDoctype != null) {
builder.setHtmlDtd(htmlDoctype);
}
builder.setUseInlineStyles(useInlineCssStyles);
builder.setSuppressBuiltInStyles(suppressBuiltInCssStyles);
builder.setLinkRel(linkRel);
builder.setDefaultAbsoluteLinkTarget(defaultAbsoluteLinkTarget);
builder.setPrependImagePrefix(prependImagePrefix);
builder.setXhtmlStrict(xhtmlStrict);
MarkupLanguage markupLanguageClone = markupLanguage.clone();
if (markupLanguageClone instanceof MediaWikiLanguage) {
MediaWikiLanguage mediaWikiLanguage = (MediaWikiLanguage) markupLanguageClone;
mediaWikiLanguage.setPageMapping(new PathPageMapping(path, paths, pathNameToOutline));
if (imageFilenames != null) {
mediaWikiLanguage.setImageNames(imageFilenames);
}
}
SplitOutlineItem item = pathNameToOutline.get(path.name);
SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
splittingBuilder.setRootBuilder(builder);
splittingBuilder.setOutline(item);
splittingBuilder.setRootFile(htmlOutputFile);
splittingBuilder.setNavigationImages(navigationImages);
splittingBuilder.setFormatting(formatOutput);
splittingBuilder.setNavigationImagePath(computeNavigationImagePath(pathDir));
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguageClone);
parser.setBuilder(splittingBuilder);
parser.parse(markupContent);
} finally {
try {
writer.close();
} catch (Exception e) {
throw new BuildException(MessageFormat.format(// $NON-NLS-1$
Messages.getString("WikiToDocTask_cannot_write_output_file"), // $NON-NLS-1$
htmlOutputFile, e.getMessage()), e);
}
}
}
use of org.apache.tools.ant.BuildException in project mylyn.docs by eclipse.
the class MediaWikiApiImageFetchingStrategy method fetchImages.
@Override
public Set<String> fetchImages() {
if (pageName == null || pageName.length() == 0) {
// $NON-NLS-1$
throw new BuildException("please specify @pageName");
}
if (!pageName.equals(pageName.trim())) {
// $NON-NLS-1$
throw new BuildException("@pageName must not have leading or trailing whitespace");
}
String base;
try {
base = url.toURI().toString();
} catch (URISyntaxException e) {
throw new BuildException(e);
}
if (!base.endsWith("/")) {
// $NON-NLS-1$
// $NON-NLS-1$
base += "/";
}
ImageFetchingContentHandler contentHandler = new ImageFetchingContentHandler();
String gimcontinue = null;
Set<String> filenames = new HashSet<String>();
final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
parserFactory.setValidating(false);
int maxloop = 100;
do {
contentHandler.setGimcontinue(null);
URL apiUrl;
try {
String queryString = String.format(// $NON-NLS-1$
"action=query&titles=%s&generator=images&prop=imageinfo&iiprop=url&format=xml%s", // $NON-NLS-1$
URLEncoder.encode(pageName, "UTF-8"), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
(gimcontinue == null ? "" : "&gimcontinue=" + URLEncoder.encode(gimcontinue, "UTF-8")));
// $NON-NLS-1$
apiUrl = new URL(base + "api.php?" + queryString);
} catch (Exception e) {
// $NON-NLS-1$
throw new BuildException("Cannot compose API URL", e);
}
Reader input;
try {
// $NON-NLS-1$
log("Fetching " + apiUrl, Project.MSG_VERBOSE);
input = createInputReader(apiUrl);
} catch (IOException e) {
// $NON-NLS-1$
throw new BuildException(String.format("Cannot contact %s: %s", apiUrl, e.getMessage()), e);
}
try {
SAXParser saxParser = parserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setEntityResolver(IgnoreDtdEntityResolver.getInstance());
xmlReader.setContentHandler(contentHandler);
try {
xmlReader.parse(new InputSource(input));
gimcontinue = contentHandler.getGimcontinue();
} catch (IOException e) {
// $NON-NLS-1$
throw new BuildException(String.format("Unexpected exception retrieving data from %s", apiUrl), e);
} finally {
try {
input.close();
} catch (IOException e) {
// ignore
}
}
} catch (SAXException e) {
// $NON-NLS-1$
throw new BuildException("Unexpected error in XML content", e);
} catch (ParserConfigurationException e) {
// $NON-NLS-1$
throw new BuildException("Cannot configure SAX parser", e);
}
} while (gimcontinue != null && maxloop-- > 0);
int fileCount = 0;
for (Map.Entry<String, String> ent : contentHandler.imageTitleToUrl.entrySet()) {
String title = ent.getKey();
String imageUrl = ent.getValue();
Matcher titleMatcher = imageTitlePattern.matcher(title);
if (titleMatcher.matches()) {
String name = titleMatcher.group(1);
name = name.replace(' ', '_');
String qualifiedUrl = base;
if (imageUrl.matches("(file|https?)://.*")) {
// $NON-NLS-1$
qualifiedUrl = imageUrl;
} else {
if (imageUrl.startsWith("/")) {
// $NON-NLS-1$
qualifiedUrl += imageUrl.substring(1);
} else {
qualifiedUrl += imageUrl;
}
}
// $NON-NLS-1$
log("Fetching " + qualifiedUrl, Project.MSG_INFO);
Get get = new Get();
get.setProject(getProject());
get.setLocation(getLocation());
try {
get.setSrc(new URL(qualifiedUrl));
} catch (MalformedURLException e) {
// $NON-NLS-1$ //$NON-NLS-2$
log("Skipping " + url + ": " + e.getMessage(), Project.MSG_WARN);
continue;
}
get.setDest(new File(dest, name));
get.execute();
filenames.add(name);
++fileCount;
} else {
// $NON-NLS-1$
log(String.format("Unexpected title format: %s", title), Project.MSG_WARN);
}
}
// $NON-NLS-1$ //$NON-NLS-2$
log("Fetched " + fileCount + " image files for " + pageName, Project.MSG_INFO);
return filenames;
}
use of org.apache.tools.ant.BuildException in project gate-core by GateNLP.
the class PackageGappTask method getJars.
/**
* Extract the text values from any <JAR> elements contained in the
* referenced creole.xml file.
*
* @return a set with one element for each unique <JAR> entry in the
* given creole.xml.
*/
private Set<String> getJars(URL creoleXml) {
try {
Set<String> jars = new HashSet<String>();
// the XPath is a bit ugly, but needed to match the element name
// case-insensitively.
XPath jarXPath = XPath.newInstance("//*[translate(local-name(), 'jar', 'JAR') = 'JAR']");
SAXBuilder builder = new SAXBuilder();
Document creoleDoc = builder.build(creoleXml);
// technically unsafe, but we know that the above XPath expression
// can only match elements.
@SuppressWarnings("unchecked") List<Element> jarElts = jarXPath.selectNodes(creoleDoc);
for (Element e : jarElts) {
jars.add(e.getTextTrim());
}
return jars;
} catch (JDOMException e) {
throw new BuildException("Error extracting JAR elements from " + creoleXml, e, getLocation());
} catch (IOException e) {
throw new BuildException("Error loading " + creoleXml + " to extract JARs", e, getLocation());
}
}
use of org.apache.tools.ant.BuildException in project gate-core by GateNLP.
the class PackageGappTask method execute.
@Override
public void execute() throws BuildException {
// process the hints
for (MappingHint h : hintTasks) {
h.perform();
}
// map to store the necessary file copy operations
Map<URL, URL> fileCopyMap = new HashMap<URL, URL>();
Map<URL, URL> dirCopyMap = new HashMap<URL, URL>();
TreeMap<URL, URL> pluginCopyMap = new TreeMap<URL, URL>(PATH_COMPARATOR);
log("Packaging gapp file " + src);
// do the work
GappModel gappModel = null;
URL newFileURL = null;
try {
URL gateHomeURL = null;
// convert gateHome to a URL, if it was provided
if (gateHome != null) {
gateHomeURL = gateHome.toURI().toURL();
}
URL resourcesHomeURL = null;
// convert resourcesHome to a URL, if it was provided
if (resourcesHome != null) {
resourcesHomeURL = resourcesHome.toURI().toURL();
}
gappModel = new GappModel(src.toURI().toURL(), gateHomeURL, resourcesHomeURL);
newFileURL = destFile.toURI().toURL();
gappModel.setGappFileURL(newFileURL);
} catch (MalformedURLException e) {
throw new BuildException("Couldn't convert src or dest file to URL", e, getLocation());
}
// we use TreeSet for these sets so we will process the paths
// higher up the directory tree before paths pointing to their
// subdirectories.
Set<URL> plugins = new TreeSet<URL>(PATH_COMPARATOR);
plugins.addAll(gappModel.getPluginURLs());
Set<URL> resources = new TreeSet<URL>(PATH_COMPARATOR);
resources.addAll(gappModel.getResourceURLs());
// process the extraresourcespath elements (if any)
processExtraResourcesPaths(resources);
// first look at the explicit mapping hints
if (mappingHints != null && !mappingHints.isEmpty()) {
Iterator<URL> resourcesIt = resources.iterator();
while (resourcesIt.hasNext()) {
URL resource = resourcesIt.next();
for (URL hint : mappingHints.keySet()) {
String hintString = hint.toExternalForm();
if (resource.equals(hint) || (hintString.endsWith("/") && resource.toExternalForm().startsWith(hintString))) {
// found this resource under this hint
log("Found resource " + resource + " under mapping hint URL " + hint, Project.MSG_VERBOSE);
String hintTarget = mappingHints.get(hint);
URL newResourceURL = null;
if (hintTarget == null) {
// hint asks to map to an absolute URL
log(" Converting to absolute URL", Project.MSG_VERBOSE);
newResourceURL = resource;
} else {
// construct the new URL relative to the hint target
try {
URL mappedHint = new URL(newFileURL, hintTarget);
String resourceRelpath = PersistenceManager.getRelativePath(hint, resource);
newResourceURL = new URL(mappedHint, resourceRelpath);
fileCopyMap.put(resource, newResourceURL);
if (copyResourceDirs) {
dirCopyMap.put(new URL(resource, "."), new URL(newResourceURL, "."));
}
} catch (MalformedURLException e) {
throw new BuildException("Couldn't construct URL relative to " + hintTarget + " for " + resource, e, getLocation());
}
log(" Relocating to " + newResourceURL, Project.MSG_VERBOSE);
}
// do the relocation
gappModel.updatePathForURL(resource, newResourceURL, hintTarget != null);
// we've now dealt with this resource, so don't need to
// handle it later
resourcesIt.remove();
break;
}
}
}
}
// Any resources that aren't covered by the hints, try and
// resolve them relative to the plugins referenced by the
// application.
Iterator<URL> pluginsIt = plugins.iterator();
while (pluginsIt.hasNext()) {
URL pluginURL = pluginsIt.next();
pluginsIt.remove();
URL newPluginURL = null;
String pluginName = pluginURL.getFile();
log("Processing plugin " + pluginName, Project.MSG_VERBOSE);
// first check whether this plugin is a subdirectory of another plugin
// we have already processed
SortedMap<URL, URL> possibleAncestors = pluginCopyMap.headMap(pluginURL);
URL ancestorPlugin = null;
if (!possibleAncestors.isEmpty())
ancestorPlugin = possibleAncestors.lastKey();
if (ancestorPlugin != null && pluginURL.toExternalForm().startsWith(ancestorPlugin.toExternalForm())) {
// this plugin is under one we have already dealt with
log(" Plugin is located under another plugin " + ancestorPlugin, Project.MSG_VERBOSE);
String relPath = PersistenceManager.getRelativePath(ancestorPlugin, pluginURL);
try {
newPluginURL = new URL(pluginCopyMap.get(ancestorPlugin), relPath);
} catch (MalformedURLException e) {
throw new BuildException("Couldn't construct URL relative to plugins/" + " for " + pluginURL, e, getLocation());
}
} else {
// normal case, this plugin is not a subdir of another plugin
boolean addSlash = false;
// we will map the plugin whose directory name is X to plugins/X
if (pluginName.endsWith("/")) {
addSlash = true;
pluginName = pluginName.substring(pluginName.lastIndexOf('/', pluginName.length() - 2) + 1, pluginName.length() - 1);
} else {
pluginName = pluginName.substring(pluginName.lastIndexOf('/') + 1);
}
log(" Plugin name is " + pluginName, Project.MSG_VERBOSE);
try {
newPluginURL = new URL(newFileURL, "plugins/" + pluginName + (addSlash ? "/" : ""));
// until we find one that is available.
if (pluginCopyMap.containsValue(newPluginURL)) {
int index = 2;
do {
newPluginURL = new URL(newFileURL, "plugins/" + pluginName + "-" + (index++) + (addSlash ? "/" : ""));
} while (pluginCopyMap.containsValue(newPluginURL));
}
} catch (MalformedURLException e) {
throw new BuildException("Couldn't construct URL relative to plugins/" + " for " + pluginURL, e, getLocation());
}
}
log(" Relocating to " + newPluginURL, Project.MSG_VERBOSE);
// deal with the plugin URL itself (in the urlList)
gappModel.updatePathForURL(pluginURL, newPluginURL, true);
pluginCopyMap.put(pluginURL, newPluginURL);
// now look for resources located under that plugin
String pluginUri = pluginURL.toExternalForm();
if (!pluginUri.endsWith("/")) {
pluginUri += "/";
}
Iterator<URL> resourcesIt = resources.iterator();
while (resourcesIt.hasNext()) {
URL resourceURL = resourcesIt.next();
try {
if (resourceURL.toExternalForm().startsWith(pluginUri)) {
// found a resource under this plugin, so relocate it to be
// under the re-located plugin dir
resourcesIt.remove();
String resourceRelpath = PersistenceManager.getRelativePath(pluginURL, resourceURL);
log(" Found resource " + resourceURL, Project.MSG_VERBOSE);
URL newResourceURL = null;
newResourceURL = new URL(newPluginURL, resourceRelpath);
log(" Relocating to " + newResourceURL, Project.MSG_VERBOSE);
gappModel.updatePathForURL(resourceURL, newResourceURL, true);
fileCopyMap.put(resourceURL, newResourceURL);
if (copyResourceDirs) {
dirCopyMap.put(new URL(resourceURL, "."), new URL(newResourceURL, "."));
}
}
} catch (MalformedURLException e) {
throw new BuildException("Couldn't construct URL relative to " + newPluginURL + " for " + resourceURL, e, getLocation());
}
}
}
// anything left over, handle according to onUnresolved
if (!resources.isEmpty()) {
switch(onUnresolved) {
case fail:
// easy case - fail the build
log("There were unresolved resources:", Project.MSG_ERR);
for (URL res : resources) {
log(res.toExternalForm(), Project.MSG_ERR);
}
log("Either set onUnresolved=\"absolute|recover\" or add the " + "relevant mapping hints", Project.MSG_ERR);
throw new BuildException("There were unresolved resources", getLocation());
case absolute:
// convert all unresolved resources to absolute URLs
log("There were unresolved resources, which have been made absolute", Project.MSG_WARN);
for (URL res : resources) {
gappModel.updatePathForURL(res, res, false);
log(res.toExternalForm(), Project.MSG_VERBOSE);
}
break;
case recover:
// the clever case - recover by putting all the unresolved
// resources into subdirectories of an "application-resources"
// directory under the output dir
URL unresolvedResourcesDir = null;
try {
unresolvedResourcesDir = new URL(newFileURL, "application-resources/");
} catch (MalformedURLException e) {
throw new BuildException("Can't construct URL relative to " + newFileURL + " for application-resources", e, getLocation());
}
// map to track where under application-resources we should map
// each directory that contains unresolved resources
TreeMap<URL, URL> unresolvedResourcesSubDirs = new TreeMap<URL, URL>(PATH_COMPARATOR);
log("There were unresolved resources, which have been gathered into " + unresolvedResourcesDir, Project.MSG_INFO);
for (URL res : resources) {
URL resourceDir = null;
try {
resourceDir = new URL(res, ".");
} catch (MalformedURLException e) {
throw new BuildException("Can't construct URL to parent directory of " + res, e, getLocation());
}
URL targetDir = getUnresolvedResourcesTarget(unresolvedResourcesSubDirs, unresolvedResourcesDir, resourceDir);
String resName = res.getFile();
resName = resName.substring(resName.lastIndexOf('/') + 1);
URL newResourceURL = null;
try {
newResourceURL = new URL(targetDir, resName);
} catch (MalformedURLException e) {
throw new BuildException("Can't construct URL relative to " + unresolvedResourcesDir + " for " + resName, e, getLocation());
}
gappModel.updatePathForURL(res, newResourceURL, true);
fileCopyMap.put(res, newResourceURL);
if (copyResourceDirs) {
dirCopyMap.put(resourceDir, targetDir);
}
}
break;
default:
throw new BuildException("Unrecognised UnresolvedAction", getLocation());
}
}
// write out the fixed GAPP file
try {
log("Writing modified gapp to " + destFile);
gappModel.write();
} catch (IOException e) {
throw new BuildException("Error writing out modified GAPP file", e, getLocation());
}
// now copy the files that it references
if (fileCopyMap.size() > 0) {
log("Copying " + fileCopyMap.size() + " resources");
}
for (Map.Entry<URL, URL> resEntry : fileCopyMap.entrySet()) {
File source = Files.fileFromURL(resEntry.getKey());
File dest = Files.fileFromURL(resEntry.getValue());
if (source.isDirectory()) {
// source URL points to a directory, so create a corresponding
// directory dest
dest.mkdirs();
} else {
// source URL doesn't point to a directory, so
// ensure parent directory exists
dest.getParentFile().mkdirs();
if (source.isFile()) {
// source URL points to an existing file, copy it
try {
log("Copying " + source + " to " + dest, Project.MSG_VERBOSE);
FileUtils.getFileUtils().copyFile(source, dest);
} catch (IOException e) {
throw new BuildException("Error copying file " + source + " to " + dest, e, getLocation());
}
}
}
}
// handle the plugins
if (pluginCopyMap.size() > 0) {
log("Copying " + pluginCopyMap.size() + " plugins");
if (copyPlugins) {
log("Also copying complete plugin contents", Project.MSG_VERBOSE);
}
copyDirectories(pluginCopyMap, !copyPlugins);
}
// handle extra directories
if (dirCopyMap.size() > 0) {
log("Copying " + dirCopyMap.size() + " resource directories");
copyDirectories(dirCopyMap, false);
}
if (mavenCache != null) {
List<GappModel.MavenPlugin> mavenPlugins = gappModel.getMavenPlugins();
if (!mavenPlugins.isEmpty()) {
log("Creating Maven cache at " + mavenCache.getAbsolutePath());
SimpleMavenCache smc = new SimpleMavenCache(mavenCache);
for (GappModel.MavenPlugin plugin : mavenPlugins) {
log(" Cacheing " + plugin.group + ":" + plugin.artifact + ":" + plugin.version, Project.MSG_VERBOSE);
Artifact pluginArtifact = new DefaultArtifact(plugin.group, plugin.artifact, "jar", plugin.version);
try {
smc.cacheArtifact(pluginArtifact);
} catch (DependencyCollectionException | DependencyResolutionException | ArtifactResolutionException | IOException | SettingsBuildingException | ModelBuildingException e) {
throw new BuildException("Error cacheing plugin " + plugin.group + ":" + plugin.artifact + ":" + plugin.version, e);
}
// and the matching creole.jar if there is one
try {
smc.cacheArtifact(new SubArtifact(pluginArtifact, "creole", "jar"));
} catch (DependencyCollectionException | DependencyResolutionException | ArtifactResolutionException | IOException | SettingsBuildingException | ModelBuildingException e) {
log(" Could not cache " + plugin.group + ":" + plugin.artifact + "-" + plugin.version + "-creole.jar - cache will still work but will " + "print warnings at runtime");
}
}
}
}
}
use of org.apache.tools.ant.BuildException in project gate-core by GateNLP.
the class PackageGappTask method copyDirectories.
/**
* Copy directories as specified by the given map.
*
* @param copyMap map specifying the directories to copy and the
* target locations to which they should be copied.
* @param minimalPlugin if true, treat the directory as a GATE plugin
* and copy just the minimal files needed for the plugin to
* work (creole.xml and any referenced jars).
*/
private void copyDirectories(Map<URL, URL> copyMap, boolean minimalPlugin) {
for (Map.Entry<URL, URL> copyEntry : copyMap.entrySet()) {
File source = Files.fileFromURL(copyEntry.getKey());
if (!source.exists()) {
return;
}
File dest = Files.fileFromURL(copyEntry.getValue());
// set up a copy task to do the copying
Copy copyTask = new Copy();
copyTask.setProject(getProject());
copyTask.setLocation(getLocation());
copyTask.setTaskName(getTaskName());
copyTask.setTodir(dest);
// ensure the target directory exists
dest.mkdirs();
FileSet fileSet = new FileSet();
copyTask.addFileset(fileSet);
fileSet.setDir(source);
if (minimalPlugin) {
// just copy creole.xml and JARs
NameEntry include = fileSet.createInclude();
include.setName("creole.xml");
URL creoleXml;
try {
creoleXml = new URL(copyEntry.getKey().toExternalForm() + "/creole.xml");
} catch (MalformedURLException e) {
throw new BuildException("Error creating URL for creole.xml in plugin " + copyEntry.getKey());
}
for (String jarString : getJars(creoleXml)) {
NameEntry jarInclude = fileSet.createInclude();
jarInclude.setName(jarString);
}
}
// do the copying
copyTask.init();
copyTask.perform();
}
}
Aggregations