use of org.apache.tools.ant.taskdefs.Get 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.taskdefs.Get in project ci.ant by WASdev.
the class InstallLibertyTask method onlineDownload.
private void onlineDownload(URL source, File dest) throws IOException {
Get get = (Get) getProject().createTask("get");
DownloadProgress progress = null;
if (verbose) {
progress = new Get.VerboseProgress(System.out);
}
get.setUseTimestamp(true);
get.setUsername(username);
get.setPassword(password);
get.setMaxTime(maxDownloadTime);
get.doGet(source, dest, Project.MSG_INFO, progress);
}
use of org.apache.tools.ant.taskdefs.Get in project ant by apache.
the class URLResolver method resolve.
/**
* Returns the file resolved from URL and directory
* @param extension the extension
* @param project the project
* @return file the file resolved
* @throws BuildException if the URL is invalid
*/
@Override
public File resolve(final Extension extension, final Project project) throws BuildException {
validate();
final File file = getDest();
final Get get = new Get();
get.setProject(project);
get.setDest(file);
get.setSrc(url);
get.execute();
return file;
}
use of org.apache.tools.ant.taskdefs.Get in project mylyn.docs by eclipse.
the class HtmlSourceImageFetchingStrategy method fetchImages.
@Override
public Set<String> fetchImages() {
if (!src.exists()) {
// $NON-NLS-1$
throw new BuildException("@src does not exist: " + src);
}
if (!src.isFile()) {
// $NON-NLS-1$
throw new BuildException("@src is not a file: " + src);
}
if (base == null) {
// $NON-NLS-1$
throw new BuildException("Must specify @base");
}
if (base.endsWith("/")) {
// $NON-NLS-1$
base = base.substring(0, base.length() - 1);
}
Set<String> filenames = new HashSet<String>();
// $NON-NLS-1$
Pattern fragmentUrlPattern = Pattern.compile("src=\"([^\"]+)\"");
// $NON-NLS-1$
Pattern imagePattern = Pattern.compile("alt=\"Image:([^\"]*)\"([^>]+)", Pattern.MULTILINE);
String htmlSrc;
try {
htmlSrc = readSrc();
} catch (IOException e) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new BuildException("Cannot read src: " + src + ": " + e.getMessage(), e);
}
// $NON-NLS-1$
log("Parsing " + src, Project.MSG_INFO);
int fileCount = 0;
Matcher imagePatternMatcher = imagePattern.matcher(htmlSrc);
while (imagePatternMatcher.find()) {
String alt = imagePatternMatcher.group(1);
String imageFragment = imagePatternMatcher.group(2);
if (imageFragment != null) {
Matcher fragmentUrlMatcher = fragmentUrlPattern.matcher(imageFragment);
if (fragmentUrlMatcher.find()) {
String url = fragmentUrlMatcher.group(1);
String qualifiedUrl = base + url;
// $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;
}
// note: we use the alt text for the name since for some files there is a case-difference between
// the server URL and the text used in the image src of the markup
String name = alt == null ? url.substring(url.lastIndexOf('/')) : alt;
name = name.replace(' ', '_');
get.setDest(new File(dest, name));
get.execute();
filenames.add(name);
++fileCount;
}
}
}
// $NON-NLS-1$ //$NON-NLS-2$
log("Fetched " + fileCount + " image files for " + src, Project.MSG_INFO);
return filenames;
}
Aggregations