Java Code Examples for org.eclipse.xtext.naming.QualifiedName#toLowerCase()

The following examples show how to use org.eclipse.xtext.naming.QualifiedName#toLowerCase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportScope.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected Iterable<IEObjectDescription> getAliasedElements(Iterable<IEObjectDescription> candidates) {
	Multimap<QualifiedName, IEObjectDescription> keyToDescription = LinkedHashMultimap.create();
	Multimap<QualifiedName, ImportNormalizer> keyToNormalizer = HashMultimap.create();

	for (IEObjectDescription imported : candidates) {
		QualifiedName fullyQualifiedName = imported.getName();
		for (ImportNormalizer normalizer : normalizers) {
			QualifiedName alias = normalizer.deresolve(fullyQualifiedName);
			if (alias != null) {
				QualifiedName key = alias;
				if (isIgnoreCase()) {
					key = key.toLowerCase();
				}
				keyToDescription.put(key, new AliasedEObjectDescription(alias, imported));
				keyToNormalizer.put(key, normalizer);
			}
		}
	}
	for (QualifiedName name : keyToNormalizer.keySet()) {
		if (keyToNormalizer.get(name).size() > 1)
			keyToDescription.removeAll(name);
	}
	return keyToDescription.values();
}
 
Example 2
Source File: PatternAwareEObjectDescriptionLookUp.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getExportedObjects(final EClass type, final QualifiedName name, final boolean ignoreCase) {
  QualifiedName lowerCase = name.toLowerCase(); // NOPMD UseLocaleWithCaseConversions not a String!
  QualifiedNameLookup<IEObjectDescription> lookup = getNameToObjectsLookup();
  Collection<IEObjectDescription> values;
  final boolean isPattern = lowerCase instanceof QualifiedNamePattern;
  if (isPattern) {
    values = lookup.get((QualifiedNamePattern) lowerCase, false);
  } else {
    values = lookup.get(lowerCase);
  }
  if (values == null) {
    return Collections.emptyList();
  }
  Predicate<IEObjectDescription> predicate = ignoreCase ? input -> EcoreUtil2.isAssignableFrom(type, input.getEClass())
      : input -> isPattern ? EcoreUtil2.isAssignableFrom(type, input.getEClass()) && ((QualifiedNamePattern) name).matches(name)
          : name.equals(input.getName()) && EcoreUtil2.isAssignableFrom(type, input.getEClass());
  return Collections2.filter(values, predicate);
}
 
Example 3
Source File: ContainerBasedScope.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
@Override
protected Iterable<IEObjectDescription> getLocalElementsByName(final QualifiedName name) {
  if (nameFunctions != null && nameFunctions.size() == 1 && nameFunctions.contains(NameFunctions.exportNameFunction())) {
    final boolean ignoreCase = isIgnoreCase();
    final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
    QualifiedName namePattern = criteria.getNamePattern();
    if (namePattern != null && ignoreCase) {
      namePattern = namePattern.toLowerCase();
    }
    if (namePattern == null || namePattern.equals(lookupName)
        || (namePattern instanceof QualifiedNamePattern && ((QualifiedNamePattern) namePattern).matches(lookupName))) {
      final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(lookupName).ignoreCase(ignoreCase);
      return copy.execute(container);
    }
    return Collections.<IEObjectDescription> emptyList();
  } else {
    return super.getLocalElementsByName(name);
  }
}
 
Example 4
Source File: PrefixedContainerBasedScope.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
  final boolean ignoreCase = isIgnoreCase();
  final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
  final IEObjectDescription result = contentByNameCache.get(lookupName);
  if (result != null && result != NULL_DESCRIPTION) {
    return result;
  } else if (result == null) {
    final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(prefix.append(lookupName)).ignoreCase(ignoreCase);
    final Iterable<IEObjectDescription> res = copy.execute(container);
    IEObjectDescription desc = Iterables.getFirst(res, null);
    if (desc != null) {
      IEObjectDescription aliased = new AliasingEObjectDescription(name, desc);
      contentByNameCache.put(lookupName, aliased);
      return aliased;
    }
    contentByNameCache.put(lookupName, NULL_DESCRIPTION);
  }

  // in case of aliasing revert to normal ContainerBasedScope behavior (using name pattern)
  if (nameFunctions.size() > 1) {
    return super.getSingleElement(name);
  }
  return getParent().getSingleElement(name);
}
 
Example 5
Source File: MultimapBasedScope.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Iterable<IEObjectDescription> getLocalElementsByName(QualifiedName name) {
	QualifiedName query = name;
	if (isIgnoreCase()) {
		query = name.toLowerCase();
	}
	if (elements.containsKey(query)) {
		Collection<IEObjectDescription> result = elements.get(query);
		return result;
	}
	return Collections.emptyList();
}
 
Example 6
Source File: MultimapBasedScope.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected boolean isShadowed(IEObjectDescription fromParent) {
	QualifiedName name = fromParent.getName();
	if (isIgnoreCase()) {
		name = name.toLowerCase();
	}
	boolean result = elements.containsKey(name);
	return result;
}
 
Example 7
Source File: ImportedNamesAdapter.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getElements(final QualifiedName name) {
	return new Iterable<IEObjectDescription>() {
		@Override
		public Iterator<IEObjectDescription> iterator() {
			final QualifiedName lowerCase = name.toLowerCase();
			importedNames.add(lowerCase);
			final Iterable<IEObjectDescription> elements = delegate.getElements(name);
			return elements.iterator();
		}
	};
}
 
Example 8
Source File: ContainerBasedScope.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings({"PMD.UseLocaleWithCaseConversions", "PMD.NPathComplexity"})
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
  if (nameFunctions != null && nameFunctions.contains(NameFunctions.exportNameFunction())) {
    final boolean ignoreCase = isIgnoreCase();
    final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
    final IEObjectDescription cachedResult = contentByNameCache.get(lookupName);
    if (cachedResult != null) {
      if (cachedResult != NULL_DESCRIPTION) {
        return cachedResult;
      }
      // Otherwise check for aliasing and if yes revert to normal behavior or delegate or parent otherwise
    } else {
      QualifiedName namePattern = criteria.getNamePattern();
      if (namePattern != null && ignoreCase) {
        namePattern = namePattern.toLowerCase();
      }
      if (namePattern == null || namePattern.equals(lookupName)
          || (namePattern instanceof QualifiedNamePattern && ((QualifiedNamePattern) namePattern).matches(lookupName))) {
        final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(lookupName).ignoreCase(ignoreCase);
        final Iterable<IEObjectDescription> queryResult = copy.execute(container);
        IEObjectDescription description = Iterables.getFirst(queryResult, null);
        if (description != null) {
          contentByNameCache.put(lookupName, description);
          return description;
        }
        contentByNameCache.put(lookupName, NULL_DESCRIPTION);
      }
    }
    // in case of aliasing revert to normal behavior
    return nameFunctions.size() > 1 ? super.getSingleElement(name) : getParent().getSingleElement(name);
  } else {
    return super.getSingleElement(name);
  }
}
 
Example 9
Source File: PrefixedContainerBasedScope.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
@Override
protected Iterable<IEObjectDescription> getLocalElementsByName(final QualifiedName name) {
  if (nameFunctions.size() == 1) {
    final boolean ignoreCase = isIgnoreCase();
    final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
    final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(prefix.append(lookupName)).ignoreCase(ignoreCase);
    return copy.execute(container);
  }
  return super.getLocalElementsByName(name);
}
 
Example 10
Source File: ImportedNamesTypesAdapter.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets an {@link IEObjectDescription} that best matches a given reference and qualified name.
 *
 * @param reference
 *          the reference for which the result element should be suitable, must not be {@code null}
 * @param name
 *          the name that the result element should match, must not be {@code null}
 * @return an eObjectDescription that best matches {@code name} in {@code scope} given the {@code reference},
 *         may by {@code null}
 */
public IEObjectDescription getSingleElement(final QualifiedName name, final EReference reference) {
  final QualifiedName lowerCase = name.toLowerCase(); // NOPMD UseLocaleWithCaseConversions not a String!
  getImportedNames().add(lowerCase);
  if (importedNamesTypes.containsKey(lowerCase)) {
    importedNamesTypes.get(lowerCase).add(reference.getEReferenceType());
  } else {
    importedNamesTypes.put(lowerCase, Sets.newHashSet(reference.getEReferenceType()));
  }

  return delegate.getSingleElement(name);
}
 
Example 11
Source File: ImportedNamesAdapter.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public IEObjectDescription getSingleElement(QualifiedName name) {
	final QualifiedName lowerCase = name.toLowerCase();
	importedNames.add(lowerCase);
	return delegate.getSingleElement(name);
}
 
Example 12
Source File: AbstractRecursiveScope.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) { // NOPMD NPathComplexity
  if (name == null) {
    throw new IllegalArgumentException("Null name in getContentByName"); //$NON-NLS-1$
  }
  try {
    if (DEBUG && !invocationTrace.add(name)) {
      throw new IllegalStateException(Messages.bind(CYCLIC_DEPENDENCY_MESSAGE, String.valueOf(name)));
    }
    final QualifiedName lookupName = isIgnoreCase() ? name.toLowerCase() : name; // NOPMD UseLocaleWithCaseConversions
    IEObjectDescription result = null;
    result = nameCache.get(lookupName);
    if (result != null) {
      return result == NULL_DESCRIPTION ? null : result; // NOPMD CompareObjectsWithEquals
    }

    if (contentsIterator == null) {
      contentsIterator = getAllLocalElements().iterator();
    }

    while (result == null && contentsIterator.hasNext()) {
      result = contentsIterator.next();
      QualifiedName thisName = result.getName();
      if (isIgnoreCase()) {
        thisName = thisName.toLowerCase(); // NOPMD UseLocaleWithCaseConversions
      }
      if (!isIgnoreCase() || nameCache.get(thisName) == null) {
        nameCache.put(thisName, result);
      }
      if (!lookupName.equals(thisName)) {
        result = null; // NOPMD NullAssignment
      }
    }

    if (result == null) {
      result = getParent().getSingleElement(name);
      // Possibly cache this result, too. For the time being, let the outer scope use its own cache, otherwise we'll duplicate
      // a lot.
    }
    if (result == null) {
      nameCache.put(lookupName, NULL_DESCRIPTION);
      // } else {
      // ScopeTrace.addTrace(result, getId());
    }
    return result;
  } catch (ConcurrentModificationException e) {
    // cache invalidated: retry
    reset();
    // Remove name from invocation trace, otherwise the intended retry recursive call will fail
    if (DEBUG) {
      invocationTrace.remove(name);
    }
    return getSingleElement(name);
  } finally {
    if (DEBUG) {
      invocationTrace.remove(name);
    }
  }
}
 
Example 13
Source File: PrefixedContainerBasedScope.java    From dsl-devkit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Create a new scope with id, parent, container, query, prefix, and case-sensitivity.
 *
 * @param id
 *          The id of the scope.
 * @param parent
 *          The parent scope.
 * @param container
 *          The container to run the query in.
 * @param query
 *          The query.
 * @param nameFunctions
 *          The name functions to apply.
 * @param prefix
 *          The prefix to apply for single element lookups.
 * @param recursive
 *          whether the qualified name pattern used to search the index should be {@link QualifiedNamePattern#RECURSIVE_WILDCARD_SEGMENT recursive}
 * @param caseInsensitive
 *          The scope's case-sensitivity.
 */
// Using QualifiedName#toLowerCase() not String#toLowerCase()
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
public PrefixedContainerBasedScope(final String id, final IScope parent, final IContainer container, final ContainerQuery.Builder query, final Iterable<INameFunction> nameFunctions, final QualifiedName prefix, final boolean recursive, final boolean caseInsensitive) {
  super(id, parent, container, query.name(QualifiedNamePattern.create(prefix.append(recursive ? QualifiedNamePattern.RECURSIVE_WILDCARD_SEGMENT
      : "*"))), nameFunctions, caseInsensitive); //$NON-NLS-1$
  this.container = container;
  this.criteria = query;
  this.nameFunctions = nameFunctions instanceof Collection ? (Collection<INameFunction>) nameFunctions : ImmutableList.copyOf(nameFunctions);
  this.prefix = caseInsensitive ? prefix.toLowerCase() : prefix;
}