Search in sources :

Example 1 with Get

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;
}
Also used : InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) XMLReader(org.xml.sax.XMLReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SAXException(org.xml.sax.SAXException) Get(org.apache.tools.ant.taskdefs.Get) SAXParser(javax.xml.parsers.SAXParser) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) XMLReader(org.xml.sax.XMLReader) HashSet(java.util.HashSet) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with Get

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);
}
Also used : DownloadProgress(org.apache.tools.ant.taskdefs.Get.DownloadProgress) Get(org.apache.tools.ant.taskdefs.Get)

Example 3 with Get

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;
}
Also used : Get(org.apache.tools.ant.taskdefs.Get) File(java.io.File)

Example 4 with Get

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;
}
Also used : Pattern(java.util.regex.Pattern) MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) Get(org.apache.tools.ant.taskdefs.Get) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet)

Aggregations

Get (org.apache.tools.ant.taskdefs.Get)4 File (java.io.File)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2 BuildException (org.apache.tools.ant.BuildException)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 DownloadProgress (org.apache.tools.ant.taskdefs.Get.DownloadProgress)1 InputSource (org.xml.sax.InputSource)1