Commit dfcfd8a3 authored by EduardGlaas's avatar EduardGlaas Committed by Balazs Grill
Browse files

Bug 575391 BasicModelSearchService now supports the use of wildcards and

regular expressions

Change-Id: I2cb5ce4b05625d9ee5759bcd4a1f24103a4adcca
parent 16bb8fab
Loading
Loading
Loading
Loading
+54 −10
Original line number Diff line number Diff line
/**
 * <copyright>
 *
 * Copyright (c) 2015-2017 itemis and others.
 * Copyright (c) 2015-2021 itemis and others.
 * 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
@@ -10,6 +10,7 @@
 * Contributors:
 *     itemis - Initial API and implementation
 *     itemis - [501899] Use base index instead of IncQuery patterns
 *     Elektrobit - [575391] BasicModelSearchService now supports the use of wildcards and regular expressions
 *
 * </copyright>
 */
@@ -17,7 +18,10 @@ package org.eclipse.sphinx.emf.search.ui.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAttribute;
@@ -32,7 +36,8 @@ import org.eclipse.sphinx.emf.ui.util.RetrieveNameAttributeHelper;

public class BasicModelSearchService extends AbstractMetaModelService implements IModelSearchService {

	private RetrieveNameAttributeHelper helper = new RetrieveNameAttributeHelper();
	protected RetrieveNameAttributeHelper helper = new RetrieveNameAttributeHelper();
	private Map<Character, String> replacementMap = null;

	public BasicModelSearchService(Collection<IMetaModelDescriptor> mmDescriptors) {
		super(mmDescriptors);
@@ -46,20 +51,16 @@ public class BasicModelSearchService extends AbstractMetaModelService implements
	@Override
	public List<ModelSearchMatch> getMatches(Collection<Resource> resources, QuerySpecification spec) {
		List<ModelSearchMatch> result = new ArrayList<ModelSearchMatch>();
		initReplacementMap();
		for (Resource resource : resources) {
			TreeIterator<EObject> allContents = resource.getAllContents();
			while (allContents.hasNext()) {
				EObject eObject = allContents.next();
				EAttribute nameAttribute = helper.getNameAttribute(eObject);
				if (nameAttribute != null) {
					Object name = eObject.eGet(nameAttribute);
					if (name != null) {
						// TODO Add support for *, ?, \*, \?, \\
						if (spec.isCaseSensitive()) {
							if (name.toString().equals(spec.getPattern())) {
								result.add(createModelSearchMatch(eObject));
							}
						} else if (name.toString().equalsIgnoreCase(spec.getPattern())) {
					Object nameObj = eObject.eGet(nameAttribute);
					if (nameObj != null) {
						if (isMatchingPattern(spec, nameObj.toString())) {
							result.add(createModelSearchMatch(eObject));
						}
					}
@@ -69,6 +70,49 @@ public class BasicModelSearchService extends AbstractMetaModelService implements
		return result;
	}

	private boolean isMatchingPattern(QuerySpecification spec, String content) {
		if (spec == null) {
			return false;
		}
		String specificationPattern = spec.getPattern();
		if (specificationPattern == null) {
			return false;
		}
		String regularExpression = getRegularExpression(specificationPattern);
		int flag = spec.isCaseSensitive() ? 0 : Pattern.CASE_INSENSITIVE;
		return Pattern.compile(regularExpression, flag).matcher(content).find();
	}

	private String getRegularExpression(String globInput) {
		StringBuilder sb = new StringBuilder();
		for (char character : globInput.toCharArray()) {
			String replacement = replacementMap.get(character);
			if (replacement == null) {
				sb.append(character);
			} else {
				sb.append(replacement);
			}
		}
		return sb.toString();
	}

	private void initReplacementMap() {
		if (replacementMap != null) {
			return;
		}
		replacementMap = new HashMap<>();
		replacementMap.put('*', ".*"); //$NON-NLS-1$
		replacementMap.put('?', "."); //$NON-NLS-1$
		replacementMap.put('(', "\\("); //$NON-NLS-1$
		replacementMap.put(')', "\\)"); //$NON-NLS-1$
		replacementMap.put('[', "\\["); //$NON-NLS-1$
		replacementMap.put(']', "\\]"); //$NON-NLS-1$
		replacementMap.put('{', "\\}"); //$NON-NLS-1$
		replacementMap.put('.', "\\."); //$NON-NLS-1$
		replacementMap.put('$', "\\$"); //$NON-NLS-1$
		replacementMap.put('^', "\\^"); //$NON-NLS-1$
	}

	private ModelSearchMatch createModelSearchMatch(EObject eObject) {
		ModelSearchMatch match = new ModelSearchMatch(eObject);
		return match;
+1 −0
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@
		<module>../../tests/org.eclipse.sphinx.tests.emf.workspace</module>
		<module>../../tests/org.eclipse.sphinx.tests.emf.workspace.extension</module>
		<module>../../tests/org.eclipse.sphinx.tests.emf</module>
		<module>../../tests/org.eclipse.sphinx.tests.emf.search.ui</module>
		<module>../../tests/org.eclipse.sphinx.tests.emf.validation</module>
		<module>../../tests/org.eclipse.sphinx.tests.jdt.integration</module>
		<module>../../tests/org.eclipse.sphinx.tests.jdt</module>
+12 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
	<classpathentry kind="src" path="src/">
		<attributes>
			<attribute name="test" value="true"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="lib" path="/org.eclipse.sphinx.emf.search.ui/src/org/eclipse/sphinx/emf/search/ui/services"/>
	<classpathentry kind="output" path="target/classes"/>
</classpath>
+34 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>org.eclipse.sphinx.tests.emf.search.ui</name>
	<comment></comment>
	<projects>
	</projects>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
		<buildCommand>
			<name>org.eclipse.pde.ManifestBuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
		<buildCommand>
			<name>org.eclipse.pde.SchemaBuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
		<buildCommand>
			<name>org.eclipse.m2e.core.maven2Builder</name>
			<arguments>
			</arguments>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>org.eclipse.m2e.core.maven2Nature</nature>
		<nature>org.eclipse.pde.PluginNature</nature>
		<nature>org.eclipse.jdt.core.javanature</nature>
	</natures>
</projectDescription>
+4 −0
Original line number Diff line number Diff line
eclipse.preferences.version=1
encoding//design/default.ecore=UTF-8
encoding//design/resources.ecore_diagram=UTF-8
encoding/<project>=UTF-8
Loading