| /******************************************************************************* |
| * Copyright (c) 2012 SAP AG. |
| * 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: |
| * Georgi Konstantinov - initial API and implementation. |
| *******************************************************************************/ |
| package org.eclipse.wst.sse.sieditor.search.provider; |
| |
| import java.util.LinkedHashSet; |
| import java.util.Map; |
| import java.util.Set; |
| |
| import org.eclipse.core.resources.IContainer; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.resources.IWorkspaceRoot; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.platform.discovery.runtime.api.ISearchParameters; |
| import org.eclipse.platform.discovery.util.api.longop.ILongOperation; |
| import org.eclipse.platform.discovery.util.api.longop.LongOpCanceledException; |
| import org.eclipse.wst.sse.sieditor.model.api.IWsdlModelRoot; |
| import org.eclipse.wst.sse.sieditor.model.impl.WSDLFactory; |
| import org.eclipse.wst.sse.sieditor.search.ui.tree.FileNode; |
| import org.eclipse.wst.sse.sieditor.search.ui.tree.NamespaceNode; |
| import org.eclipse.wst.sse.sieditor.search.ui.tree.RootTreeNode; |
| import org.eclipse.wst.sse.sieditor.ui.v2.nodes.ITreeNode; |
| import org.eclipse.wst.wsdl.Definition; |
| import org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImpl; |
| import org.eclipse.wst.wsdl.util.WSDLResourceImpl; |
| |
| public abstract class AbstractSearchQueryOperation implements ILongOperation<ITreeNode> { |
| |
| protected static final String XSD_FILE_EXTENSION = "xsd"; //$NON-NLS-1$ |
| protected static final String WSDL_FILE_EXTENSION = "wsdl"; //$NON-NLS-1$ |
| protected static final String XML_FILE_EXTENSION = "xml"; //$NON-NLS-1$ |
| protected final ISearchParameters searchParams; |
| |
| public AbstractSearchQueryOperation(ISearchParameters searchParams) { |
| this.searchParams = searchParams; |
| } |
| |
| @Override |
| public ITreeNode run(IProgressMonitor monitor) throws LongOpCanceledException, CoreException { |
| Set<IFile> files = getFilesFromWorkspace(); |
| return getRootTreeNode(files); |
| } |
| |
| protected boolean isByNamespaceHierarchy() { |
| return searchParams.getGroupingHierarchy().equals(AbstractLocalDefinitionsSearchProvider.BY_NAMESPACES); |
| } |
| |
| protected boolean isByFilesHierarchy() { |
| return searchParams.getGroupingHierarchy().equals(AbstractLocalDefinitionsSearchProvider.BY_FILES); |
| } |
| |
| protected void chooseParentForNamespaceNode(RootTreeNode rootTreeNode, FileNode fileNode, NamespaceNode namespaceNode) { |
| if (namespaceNode.hasChildren()) { |
| if (isByFilesHierarchy()) { |
| fileNode.addChild(namespaceNode); |
| } else { |
| rootTreeNode.addChild(namespaceNode); |
| } |
| } |
| } |
| |
| private Set<IFile> getFilesFromWorkspace() throws CoreException { |
| Set<IFile> result = new LinkedHashSet<IFile>(); |
| |
| IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); |
| traversFiles(result, workspaceRoot); |
| |
| return result; |
| } |
| |
| private void traversFiles(Set<IFile> result, IResource resource) throws CoreException { |
| if (resource instanceof IFile) { |
| IFile file = (IFile) resource; |
| if (isFileThatMayHaveTheSearchedContenet(file)) { |
| result.add(file); |
| return; |
| } |
| } |
| |
| if (resource instanceof IContainer) { |
| if ("bin".equals(resource.getName()) && (resource.getParent() instanceof IProject)) { //$NON-NLS-1$ |
| return; |
| } |
| |
| IContainer container = (IContainer) resource; |
| IResource[] members = container.members(); |
| for (IResource member : members) { |
| traversFiles(result, member); |
| } |
| } |
| } |
| |
| protected abstract boolean isFileThatMayHaveTheSearchedContenet(IFile file); |
| |
| protected boolean isXmlFileExtension(String fileExtension) { |
| return checkForFileExtension(fileExtension, XML_FILE_EXTENSION); |
| } |
| |
| protected boolean isWsdlFileExtension(String fileExtension) { |
| return checkForFileExtension(fileExtension, WSDL_FILE_EXTENSION); |
| } |
| |
| protected boolean isXsdFileExtension(String fileExtension) { |
| return checkForFileExtension(fileExtension, XSD_FILE_EXTENSION); |
| } |
| |
| private boolean checkForFileExtension(String fileExtension, String searchedFileExtension) { |
| return fileExtension == null ? false : searchedFileExtension.equalsIgnoreCase(fileExtension); |
| } |
| |
| protected abstract ITreeNode getRootTreeNode(Set<IFile> files); |
| |
| protected IWsdlModelRoot getWsdlModelRootForFile(IFile file) { |
| Map<String, Object> extensionToFactoryMap = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(); |
| Object currentXmlResourceFactoryWithLowerCase = extensionToFactoryMap.get(XML_FILE_EXTENSION.toLowerCase()); |
| Object currentXmlResourceFactoryWithUpperCase = extensionToFactoryMap.get(XML_FILE_EXTENSION.toUpperCase()); |
| Object currentWsdlResourceFactoryWithUpperCase = extensionToFactoryMap.get(WSDL_FILE_EXTENSION.toUpperCase()); |
| try { |
| extensionToFactoryMap.put(XML_FILE_EXTENSION.toLowerCase(), new WSDLResourceFactoryImpl()); |
| extensionToFactoryMap.put(XML_FILE_EXTENSION.toUpperCase(), new WSDLResourceFactoryImpl()); |
| extensionToFactoryMap.put(WSDL_FILE_EXTENSION.toUpperCase(), new WSDLResourceFactoryImpl()); |
| |
| ResourceSet resourceSet = new ResourceSetImpl(); |
| |
| WSDLResourceImpl resource = (WSDLResourceImpl) resourceSet.getResource(createFileUri(file), true); |
| Definition definition = resource.getDefinition(); |
| |
| IWsdlModelRoot modelRoot = WSDLFactory.getInstance().createWSDLModelRoot(definition); |
| return modelRoot; |
| } finally { |
| extensionToFactoryMap.put(XML_FILE_EXTENSION.toLowerCase(), currentXmlResourceFactoryWithLowerCase); |
| extensionToFactoryMap.put(XML_FILE_EXTENSION.toUpperCase(), currentXmlResourceFactoryWithUpperCase); |
| extensionToFactoryMap.put(WSDL_FILE_EXTENSION.toUpperCase(), currentWsdlResourceFactoryWithUpperCase); |
| } |
| } |
| |
| protected URI createFileUri(IFile file) { |
| return URI.createFileURI(file.getLocation().toFile().getAbsolutePath()); |
| } |
| |
| } |