Search in sources :

Example 16 with SpecfileSource

use of org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource in project linuxtools by eclipse.

the class SpecfileCompletionProcessor method getPatches.

/**
 * Get patches as a String key->value pair for a given specfile and prefix.
 *
 * @param specfile
 *            to get defines from.
 * @param prefix
 *            used to find defines.
 * @return a <code>HashMap</code> of defines.
 */
private Map<String, String> getPatches(Specfile specfile, String prefix) {
    Collection<SpecfileSource> patches = specfile.getPatches();
    Map<String, String> ret = new HashMap<>();
    String patchName;
    for (SpecfileSource patch : patches) {
        // $NON-NLS-1$
        patchName = "%patch" + patch.getNumber();
        if (patchName.startsWith(prefix)) {
            ret.put(patchName.toLowerCase(), RPMUtils.getSourceOrPatchValue(specfile, // $NON-NLS-1$
            "patch" + patch.getNumber()));
        }
    }
    return ret;
}
Also used : SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) HashMap(java.util.HashMap)

Example 17 with SpecfileSource

use of org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource in project linuxtools by eclipse.

the class SpecfileEditorDownloadSourcesActionDelegate method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell shell = HandlerUtil.getActiveShellChecked(event);
    final SpecfileParser specparser = new SpecfileParser();
    final IResource resource = RPMHandlerUtils.getResource(event);
    final RPMProject rpj = RPMHandlerUtils.getRPMProject(resource);
    final IFile workFile = (IFile) rpj.getSpecFile();
    final Specfile specfile = workFile != null ? specparser.parse(workFile) : null;
    // retrieve source(s) from specfile
    final List<SpecfileSource> sourceURLList = specfile != null ? (List<SpecfileSource>) specfile.getSources() : null;
    // currently stops immediately once an invalid source URL is encountered
    for (final SpecfileSource sourceurls : sourceURLList) {
        try {
            String rawURL = sourceurls.getFileName();
            String resolvedURL = UiUtils.resolveDefines(specfile, rawURL);
            URL url = null;
            try {
                url = new URL(resolvedURL);
            } catch (MalformedURLException e) {
                SpecfileLog.logError(NLS.bind(Messages.DownloadSources_malformedURL, resolvedURL), e);
                // $NON-NLS-1$
                RPMUtils.showErrorDialog(// $NON-NLS-1$
                shell, // $NON-NLS-1$
                "Error", NLS.bind(Messages.DownloadSources_malformedURL, resolvedURL));
                return null;
            }
            URLConnection connection = url.openConnection();
            if (!(connection instanceof HttpURLConnection) || ((HttpURLConnection) connection).getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) {
                connection.connect();
                // grab the name of the file from the URL
                // $NON-NLS-1$
                int offset = url.toString().lastIndexOf("/");
                String filename = url.toString().substring(offset + 1);
                // create the path to the "to be downloaded" file
                IFile file = rpj.getConfiguration().getSourcesFolder().getFile(new Path(filename));
                Job downloadJob = new DownloadJob(file, connection);
                downloadJob.setUser(true);
                downloadJob.schedule();
            }
        } catch (IOException e) {
            SpecfileLog.logError(Messages.DownloadSources_cannotConnectToURL, e);
            // $NON-NLS-1$
            RPMUtils.showErrorDialog(// $NON-NLS-1$
            shell, // $NON-NLS-1$
            "Error", Messages.DownloadSources_cannotConnectToURL);
            return null;
        }
    }
    return null;
}
Also used : Specfile(org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile) Path(org.eclipse.core.runtime.Path) MalformedURLException(java.net.MalformedURLException) IFile(org.eclipse.core.resources.IFile) RPMProject(org.eclipse.linuxtools.rpm.core.RPMProject) IOException(java.io.IOException) SpecfileParser(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Shell(org.eclipse.swt.widgets.Shell) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) HttpURLConnection(java.net.HttpURLConnection) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) Job(org.eclipse.core.runtime.jobs.Job) IResource(org.eclipse.core.resources.IResource)

Example 18 with SpecfileSource

use of org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource in project linuxtools by eclipse.

the class SpecfileEditorPrepareSourcesActionDelegate method downloadFile.

public boolean downloadFile(Shell shell, RPMProject rpj, Specfile specfile) {
    // retrieve source(s) from specfile
    final List<SpecfileSource> sourceURLList = specfile != null ? specfile.getSources() : null;
    for (final SpecfileSource sourceurls : sourceURLList) {
        try {
            String resolvedURL = UiUtils.resolveDefines(specfile, sourceurls.getFileName());
            URL url = null;
            try {
                url = new URL(resolvedURL);
            } catch (MalformedURLException e) {
                SpecfileLog.logError(NLS.bind(Messages.PrepareSources_downloadSourcesMalformedURL, resolvedURL), e);
                RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, NLS.bind(Messages.PrepareSources_downloadSourcesMalformedURL, resolvedURL));
                return false;
            }
            URLConnection connection = url.openConnection();
            if (!(connection instanceof HttpURLConnection) || ((HttpURLConnection) connection).getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND) {
                connection.connect();
                // grab the name of the file from the URL
                // $NON-NLS-1$
                int offset = url.toString().lastIndexOf("/");
                String filename = url.toString().substring(offset + 1);
                // create the path to the "to be downloaded" file
                IFile file = rpj.getConfiguration().getSourcesFolder().getFile(new Path(filename));
                Job downloadJob = new DownloadJob(file, connection);
                downloadJob.setUser(true);
                downloadJob.schedule();
                try {
                    downloadJob.join();
                } catch (InterruptedException e1) {
                    return false;
                }
                if (!downloadJob.getResult().isOK()) {
                    return false;
                }
            }
        } catch (OperationCanceledException e) {
            SpecfileLog.logError(Messages.PrepareSources_downloadCancelled, e);
            RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, Messages.PrepareSources_downloadCancelled);
            return false;
        } catch (IOException e) {
            SpecfileLog.logError(Messages.PrepareSources_downloadConnectionFail, e);
            RPMUtils.showErrorDialog(shell, Messages.PrepareSources_error, Messages.PrepareSources_downloadConnectionFail);
            return false;
        }
    }
    return true;
}
Also used : Path(org.eclipse.core.runtime.Path) MalformedURLException(java.net.MalformedURLException) IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) HttpURLConnection(java.net.HttpURLConnection) DownloadJob(org.eclipse.linuxtools.rpm.core.utils.DownloadJob) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)18 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 IFile (org.eclipse.core.resources.IFile)2 Path (org.eclipse.core.runtime.Path)2 Job (org.eclipse.core.runtime.jobs.Job)2 IRegion (org.eclipse.jface.text.IRegion)2 SourceComparator (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SourceComparator)2 DownloadJob (org.eclipse.linuxtools.rpm.core.utils.DownloadJob)2 SpecfileDefine (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine)2 SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)2 StringTokenizer (com.ibm.icu.util.StringTokenizer)1