Skip to content

Commit

Permalink
Bug 517818 - Can not register exact file name with fileExtension
Browse files Browse the repository at this point in the history
- Searches IDE extensions by the longest matching subname of the file.
- Added searching by list of tags to IMarketplaceService

Change-Id: Ie6ef6f4f56a8890ccf625c10725d72eaae59b7b4
Signed-off-by: Lucas Bullen <lbullen@redhat.com>
  • Loading branch information
Lucas Bullen committed Oct 6, 2017
1 parent fce01a1 commit eda1f1d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
Expand Down Expand Up @@ -284,6 +285,11 @@ public ISearchResult doSearch(IProgressMonitor monitor) throws CoreException {
});
}

public ISearchResult tagged(List<String> tags, IProgressMonitor monitor) throws CoreException {
String combinedTags = tags.stream().collect(Collectors.joining(",")); //$NON-NLS-1$
return tagged(combinedTags, monitor);
}

private ISearchResult performSearch(IProgressMonitor monitor, String key, SearchOperation searchOperation)
throws CoreException {
ISearchResult result = null;
Expand Down
Expand Up @@ -27,6 +27,7 @@
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -364,7 +365,7 @@ public List<INode> getNodes(Collection<? extends INode> nodes, IProgressMonitor
Messages.DefaultMarketplaceService_nodeNotFound, query);
if (missingNodes == null) {
missingNodes = new MultiStatus(MarketplaceClientCore.BUNDLE_ID, 0,
"Some entries could not be found on the Marketplace", null);
"Some entries could not be found on the Marketplace", null); //$NON-NLS-1$
}
missingNodes.add(missingNodeDetailStatus);
}
Expand Down Expand Up @@ -535,6 +536,11 @@ public SearchResult tagged(String tag, IProgressMonitor monitor) throws CoreExce
return processSearchRequest(API_FREETAGGING_URI + URLUtil.urlEncode(tag) + '/' + API_URI_SUFFIX, tag, monitor);
}

public SearchResult tagged(List<String> tags, IProgressMonitor monitor) throws CoreException {
String combinedTags = tags.stream().collect(Collectors.joining(",")); //$NON-NLS-1$
return tagged(combinedTags, monitor);
}

public SearchResult featured(IProgressMonitor monitor) throws CoreException {
return featured(null, null, monitor);
}
Expand Down
Expand Up @@ -118,6 +118,16 @@ ISearchResult search(IMarket market, ICategory category, String queryText, IProg
*/
ISearchResult tagged(String tag, IProgressMonitor monitor) throws CoreException;

/**
* Find nodes in the marketplace tagged with the given tags. Only nodes having an exact (case-insensitie) match for
* at least one of the given tags will be returned.
*
* @param tags
* free-form tags
* @return the search result
*/
ISearchResult tagged(List<String> tags, IProgressMonitor monitor) throws CoreException;

/**
* Find featured nodes in the marketplace
*
Expand Down
5 changes: 4 additions & 1 deletion org.eclipse.epp.mpc.ui/.settings/org.eclipse.jdt.core.prefs
Expand Up @@ -180,9 +180,12 @@ org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=false
org.eclipse.jdt.core.formatter.indentation.size=4
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_member=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert
Expand Down
@@ -1,16 +1,22 @@
/*******************************************************************************
* Copyright (c) 2016 Red Hat Inc.
* Copyright (c) 2017 Red Hat Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Mickael Istria (Red Hat Inc.) - initial implementation
* Lucas Bullen (Red Hat Inc.) - Bug 517818: search IDE extensions by all subname's of the file
*******************************************************************************/
package org.eclipse.epp.internal.mpc.ui.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
Expand All @@ -22,6 +28,7 @@
import org.eclipse.epp.internal.mpc.ui.Messages;
import org.eclipse.epp.mpc.core.model.INode;
import org.eclipse.epp.mpc.core.model.ISearchResult;
import org.eclipse.epp.mpc.core.model.ITag;
import org.eclipse.epp.mpc.core.service.IMarketplaceService;
import org.eclipse.epp.mpc.core.service.IMarketplaceServiceLocator;
import org.eclipse.osgi.util.NLS;
Expand Down Expand Up @@ -56,7 +63,8 @@ protected IStatus run(IProgressMonitor monitor) {
ServiceReference<IMarketplaceServiceLocator> locatorReference = bundleContext
.getServiceReference(IMarketplaceServiceLocator.class);
IMarketplaceServiceLocator locator = bundleContext.getService(locatorReference);
IMarketplaceService marketplaceService = locator.getDefaultMarketplaceService();
IMarketplaceService marketplaceService = locator
.getDefaultMarketplaceService();
try {
return run(marketplaceService, monitor);
} finally {
Expand All @@ -65,12 +73,17 @@ protected IStatus run(IProgressMonitor monitor) {
}

private IStatus run(IMarketplaceService marketplaceService, IProgressMonitor monitor) {
final String fileExtension = getFileExtension(fileName);
String fileExtensionTag = getFileExtensionTag(fileExtension);
final List<String> fileExtensions = getFileExtensions(fileName);
final List<String> fileExtensionTags = new ArrayList<String>();

for (String string : fileExtensions) {
fileExtensionTags.add(getFileExtensionTag(string));
}

final List<? extends INode> nodes;
try {
ISearchResult searchResult = marketplaceService.tagged(fileExtensionTag, monitor);
nodes = searchResult.getNodes();
ISearchResult searchResult = marketplaceService.tagged(fileExtensionTags, monitor);
nodes = orderNodesByTagSubExtensionCount(searchResult.getNodes(), fileExtensionTags);
} catch (CoreException ex) {
IStatus status = new Status(IStatus.ERROR, MarketplaceClientUi.BUNDLE_ID,
NLS.bind(Messages.DiscoverFileSupportJob_discoveryFailed, getFileExtensionLabel(fileName)), ex);
Expand All @@ -93,14 +106,79 @@ private static String getFileExtensionTag(final String fileExtension) {
return "fileExtension_" + fileExtension;//$NON-NLS-1$
}

/**
* Sorts the given list of tags by number of sub extension dividers(.) and uses the sorted list to return an ordered
* list of the nodes by first occurrence of highest sub extension tag in fileExtensionTags.
*
* @param nodes
* @param fileExtensionTags
* @return nodes ordered by first occurrence of highest sub extension count found in fileExtensionTags
*/
private static List<? extends INode> orderNodesByTagSubExtensionCount(List<? extends INode> nodes,
List<String> fileExtensionTags) {

Collections.sort(fileExtensionTags, new Comparator<String>() {
public int compare(String s1, String s2) {
return (s2.length() - s2.replace(".", "").length()) - (s1.length() - s1.replace(".", "").length()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
});

Map<String, List<INode>> nodesByTags = new HashMap<String, List<INode>>();
for (INode iNode : nodes) {
for (ITag nodeTag : iNode.getTags().getTags()) {
boolean foundTag = false;
for (String tag : fileExtensionTags) {
if (nodeTag.getName().equals(tag)) {
if (nodesByTags.containsKey(tag)) {
nodesByTags.get(tag).add(iNode);
} else {
List<INode> newNodeList = new ArrayList<INode>();
newNodeList.add(iNode);
nodesByTags.put(tag, newNodeList);
}
foundTag = true;
break;
}
}
if (foundTag) {
break;
}
}
}
List<INode> ordered = new ArrayList<INode>();
for (String tag : fileExtensionTags) {
if (nodesByTags.containsKey(tag)) {
ordered.addAll(nodesByTags.get(tag));
}
}
return nodes;
}

/**
* @param fileName
* full name of the file including all extensions
* @return The last segment of the file's extension. eg "file.tar.gz" returns "gz"
*/
static String getFileExtensionLabel(String fileName) {
String fileExtension = getFileExtension(fileName);
return fileExtension.length() == fileName.length() ? fileName
: "*." + fileExtension; //$NON-NLS-1$
return fileName.indexOf('.') == -1 ? fileName
: '*' + fileName.substring(fileName.lastIndexOf('.'), fileName.length());
}

static String getFileExtension(String fileName) {
String[] split = fileName.split("\\."); //$NON-NLS-1$
return split[split.length - 1];
/**
* @param fileName
* full name of the file including all extensions
* @return All sub-strings of the fileName in longest to shortest order split by periods moving from left to right.
* eg "file.tar.gz" returns ["file.tar.gz", "tar.gz", "gz"]
*/
static List<String> getFileExtensions(String fileName) {
List<String> extensions = new ArrayList<String>();
while (fileName.length() > 0) {
extensions.add(fileName);
if (fileName.indexOf('.') == -1) {
break;
}
fileName = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
}
return extensions;
}
}
@@ -1,12 +1,13 @@
/*******************************************************************************
* Copyright (c) 2016 Red Hat Inc.
* Copyright (c) 2017 Red Hat Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Mickael Istria (Red Hat Inc.) - initial implementation
* Lucas Bullen (Red Hat Inc.) - Bug 517818: retain same functionality post fix
*******************************************************************************/
package org.eclipse.epp.internal.mpc.ui.discovery;

Expand Down Expand Up @@ -67,8 +68,9 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
IMarketplaceClientConfiguration config = marketplaceClientService.newConfiguration();
marketplaceClientService.open(config, new LinkedHashSet<INode>(nodes));
} else if (dialog.isAssociateToExtension()) {
String fileExtension = DiscoverFileSupportJob.getFileExtension(fileName);
IFileEditorMapping newMapping = createDefaultDescriptorMapping(fileExtension);
List<String> fileExtensions = DiscoverFileSupportJob.getFileExtensions(fileName);
IFileEditorMapping newMapping = createDefaultDescriptorMapping(
fileExtensions.get(fileExtensions.size() - 1));
addEditorMapping(newMapping);
}
return Status.OK_STATUS;
Expand Down

0 comments on commit eda1f1d

Please sign in to comment.