Java Code Examples for org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic.Acceptor#accept()

The following examples show how to use org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic.Acceptor#accept() . 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: CrossReferenceSerializer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean isValid(EObject semanticObject, CrossReference crossref, EObject target, INode node, Acceptor errors) {
	if ((target == null || target.eIsProxy()) && node != null) {
		CrossReference crossrefFromNode = GrammarUtil.containingCrossReference(node.getGrammarElement());
		return crossref == crossrefFromNode;
	}
	
	final EReference ref = GrammarUtil.getReference(crossref, semanticObject.eClass());
	final IScope scope = scopeProvider.getScope(semanticObject, ref);
	if (scope == null) {
		if (errors != null)
			errors.accept(diagnostics.getNoScopeFoundDiagnostic(semanticObject, crossref, target));
		return false;
	}
	
	if (target != null && target.eIsProxy()) {
		target = handleProxy(target, semanticObject, ref);
	}
	
	return getCrossReferenceNameFromScope(semanticObject, crossref, target, scope, errors) != null;
}
 
Example 2
Source File: ValueSerializer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String serializeAssignedValue(EObject context, RuleCall ruleCall, Object value, INode node, Acceptor errors) {
	AbstractRule rule = ruleCall.getRule();
	String ruleName = ruleNames.getQualifiedName(rule);
	if (node != null) {
		AbstractRule nodeRule = linkingHelper.getRuleFrom(node.getGrammarElement());
		// TODO: analyze why grammar element identity is broken here during generation of Xtext languages using MWE2  
		if (nodeRule != null && ruleNames.getQualifiedName(nodeRule).equals(ruleName)) {
			Object converted = converter.toValue(NodeModelUtils.getTokenText(node), ruleName, node);
			if (converted != null && converted.equals(value))
				return tokenUtil.serializeNode(node);
		}
	}
	try {
		String str = converter.toString(value, ruleName);
		if (str != null)
			return str;
		if (errors != null)
			errors.accept(diagnostics.getNullNotAllowedDiagnostic(context, ruleCall));
		return null;
	} catch (Throwable e) {
		if (errors != null)
			errors.accept(diagnostics.getValueConversionExceptionDiagnostic(context, ruleCall, value, e));
		return null;
	}
}
 
Example 3
Source File: CrossReferenceSerializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String serializeCrossRef(EObject semanticObject, CrossReference crossref, EObject target, INode node,
		Acceptor errors) {
	if ((target == null || target.eIsProxy()) && node != null)
		return tokenUtil.serializeNode(node);

	final EReference ref = GrammarUtil.getReference(crossref, semanticObject.eClass());
	final IScope scope = scopeProvider.getScope(semanticObject, ref);
	if (scope == null) {
		if (errors != null)
			errors.accept(diagnostics.getNoScopeFoundDiagnostic(semanticObject, crossref, target));
		return null;
	}
	
	if (target != null && target.eIsProxy()) {
		target = handleProxy(target, semanticObject, ref);
	}

	if (target != null && node != null) {
		String text = linkingHelper.getCrossRefNodeAsString(node, true);
		QualifiedName qn = qualifiedNameConverter.toQualifiedName(text);
		URI targetURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(target);
		for (IEObjectDescription desc : scope.getElements(qn)) {
			if (targetURI.equals(desc.getEObjectURI()))
				return tokenUtil.serializeNode(node);
		}
	}

	return getCrossReferenceNameFromScope(semanticObject, crossref, target, scope, errors);
}
 
Example 4
Source File: CrossReferenceSerializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String getCrossReferenceNameFromScope(EObject semanticObject, CrossReference crossref, EObject target,
		final IScope scope, Acceptor errors) {
	String ruleName = linkingHelper.getRuleNameFrom(crossref);
	boolean foundOne = false;
	List<ISerializationDiagnostic> recordedErrors = null;
	for (IEObjectDescription desc : scope.getElements(target)) {
		foundOne = true;
		String unconverted = qualifiedNameConverter.toString(desc.getName());
		try {
			return valueConverter.toString(unconverted, ruleName);
		} catch (ValueConverterException e) {
			if (errors != null) {
				if (recordedErrors == null)
					recordedErrors = Lists.newArrayList();
				recordedErrors.add(diagnostics.getValueConversionExceptionDiagnostic(semanticObject, crossref,
						unconverted, e));
			}
		}
	}
	if (errors != null) {
		if (recordedErrors != null)
			for (ISerializationDiagnostic diag : recordedErrors)
				errors.accept(diag);
		if (!foundOne)
			errors.accept(diagnostics.getNoEObjectDescriptionFoundDiagnostic(semanticObject, crossref, target,
					scope));
	}
	return null;
}
 
Example 5
Source File: EnumLiteralSerializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String serializeAssignedEnumLiteral(EObject context, RuleCall ruleCall, Object value, INode node,
		Acceptor errorAcceptor) {
	Keyword nodeLit = getLiteral(node);
	Keyword modelLit = getLiteral(context, ruleCall, value);
	if (modelLit == null) {
		if (errorAcceptor != null)
			errorAcceptor.accept(diagnosticProvider.getInvalidEnumValueDiagnostic(context, ruleCall, value));
		return null;
	} else if (nodeLit != null && nodeLit.equals(modelLit))
		return tokenUtil.serializeNode(node);
	else
		return modelLit.getValue();
}
 
Example 6
Source File: ValueSerializer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isValid(EObject context, RuleCall ruleCall, Object value, Acceptor errors) {
	try {
		String str = converter.toString(value, ruleNames.getQualifiedName(ruleCall.getRule()));
		if (str != null)
			return true;
		if (errors != null)
			errors.accept(diagnostics.getNullNotAllowedDiagnostic(context, ruleCall));
		return false;
	} catch (Throwable e) {
		if (errors != null)
			errors.accept(diagnostics.getValueConversionExceptionDiagnostic(context, ruleCall, value, e));
		return false;
	}
}