Java Code Examples for org.eclipse.xtext.resource.IFragmentProvider#Fallback

The following examples show how to use org.eclipse.xtext.resource.IFragmentProvider#Fallback . 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: AbstractClassMirror.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getFragment(EObject obj, IFragmentProvider.Fallback fallback) {
	if (obj instanceof JvmTypeParameter)
		return getFragment(obj.eContainer(), fallback) + "/" + ((JvmTypeParameter) obj).getName();
	if (obj instanceof JvmTypeReference || obj instanceof JvmFormalParameter)
		return fallback.getFragment(obj);
	if (obj instanceof JvmIdentifiableElement) {
		if (obj instanceof JvmArrayType) {
			if (obj.eContainer() instanceof JvmGenericArrayTypeReference)
				return fallback.getFragment(obj);
			if (obj.eContainer() instanceof JvmIdentifiableElement) {
				return getFragment(obj.eContainer(), fallback) + "[]";
			}
		}
		return ((JvmIdentifiableElement) obj).getIdentifier();
	}
	return fallback.getFragment(obj);
}
 
Example 2
Source File: PrimitiveMirror.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EObject getEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
	if (fragment.endsWith("[]")) {
		return getArrayEObject(resource, fragment, fallback);
	}
	for (EObject obj: resource.getContents()) {
		String otherFragment = getFragment(obj, fallback);
		if (fragment.equals(otherFragment))
			return obj;
	}
	return fallback.getEObject(fragment);
}
 
Example 3
Source File: XtextFragmentProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EObject getEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
	if (!fragment.startsWith(PREFIX))
		return fallback.getEObject(fragment);
	String fragmentWithoutPrefix = fragment.substring(PREFIX.length());
	List<String> splitted = Strings.split(fragmentWithoutPrefix, '/');
	if (splitted.isEmpty()) {
		return fallback.getEObject(fragment);
	}
	String firstPart = splitted.get(0);
	Grammar grammar = null;
	for(EObject content: resource.getContents()) {
		if (content instanceof Grammar) {
			Grammar g = (Grammar) content;
			if (firstPart.equals(g.getName())) {
				grammar = g;
				if (splitted.size() == 1)
					return grammar;
				break;
			}
		}
	}
	if (splitted.size() == 2) {
		return GrammarUtil.findRuleForName(grammar, splitted.get(1));
	} else {
		return fallback.getEObject(fragment);
	}
}
 
Example 4
Source File: XtextFragmentProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String getFragment(EObject obj, IFragmentProvider.Fallback fallback) {
	if (obj instanceof Grammar) {
		return caseGrammar((Grammar)obj);
	} else if (obj instanceof AbstractRule && obj.eContainer() != null) {
		return caseAbstractRule((AbstractRule)obj, fallback);
	}
	return fallback.getFragment(obj);
}
 
Example 5
Source File: TypeResourceTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public EObject getEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
	assertEquals(expectedURI, resource.getURI());
	assertEquals(expectedFragment, fragment);
	return expectedObject;
}
 
Example 6
Source File: TypeResourceTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String getFragment(EObject obj, IFragmentProvider.Fallback fallback) {
	assertEquals(expectedObject, obj);
	return expectedFragment;
}
 
Example 7
Source File: AbstractClassMirror.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public EObject getEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
	if (fragment.endsWith("[]")) {
		return getArrayEObject(resource, fragment, fallback);
	}
	int slash = fragment.indexOf('/'); 
	if (slash != -1) {
		if (slash == 0)
			return fallback.getEObject(fragment);
		String containerFragment = fragment.substring(0, slash);
		EObject container = getEObject(resource, containerFragment, fallback);
		if (container != null) {
			String parameterName = fragment.substring(slash + 1);
			if (container instanceof JvmTypeParameterDeclarator) {
				JvmTypeParameterDeclarator executable = (JvmTypeParameterDeclarator) container;
				for(JvmTypeParameter parameter: executable.getTypeParameters()) {
					if (parameter.getName().equals(parameterName))
						return parameter;
				}
			} 
		}
	} else {
		if (resource.getContents().isEmpty()) {
			if (typeResourceServices!= null) 
				typeResourceServices.outdatedStateManager.checkCanceled(resource.getResourceSet());
			if (!isEmptynessLogged) {
				isEmptynessLogged = true;
				logger.error("resource is empty: " + resource.getURI(), new IllegalStateException());
				if (logger.isDebugEnabled()) {
					logger.debug(getClass().getName(), new Exception());
				}
			}
			return null;
		}
		final EObject type = resource.getContents().get(0);
		if (type instanceof JvmType) {
			String typeName = getTypeName((JvmType) type);
			if (typeName.equals(fragment)) {
				return type;
			}
			if(fragment.startsWith(typeName)) {
				EObject member = findMember(type, fragment);
				if(member != null)
					return member;
			}
		}
	}
	return fallback.getEObject(fragment);	
}
 
Example 8
Source File: AbstractClassMirror.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected EObject getArrayEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
	JvmComponentType component = (JvmComponentType) getEObject(resource, fragment.substring(0, fragment.length() - 2), fallback);
	if (component == null)
		return null;
	return component.getArrayType();
}
 
Example 9
Source File: XtextFragmentProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public String caseAbstractRule(AbstractRule obj, IFragmentProvider.Fallback fallback) {
	return getFragment(obj.eContainer(), fallback) + "/" + obj.getName();
}