use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class CrossReferenceSerializer method serializeCrossRef.
@Override
public String serializeCrossRef(EObject context, CrossReference grammarElement, final EObject target, INode node) {
final EReference ref = GrammarUtil.getReference(grammarElement, context.eClass());
String text = null;
if (node != null) {
List<EObject> objects = linkingService.getLinkedObjects(context, ref, node);
if (Iterables.any(objects, new Predicate<EObject>() {
private final URI targetURI = EcoreUtil.getURI(target);
@Override
public boolean apply(EObject input) {
return input == target || EcoreUtil.getURI(input).equals(targetURI);
}
}))
return ITokenSerializer.KEEP_VALUE_FROM_NODE_MODEL;
}
text = getUnconvertedLinkText(target, ref, context);
if (text != null)
return getConvertedValue(text, grammarElement);
if (node != null) {
return linkingHelper.getCrossRefNodeAsString(node, false);
}
return null;
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class Linker method ensureIsLinked.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void ensureIsLinked(EObject obj, INode node, CrossReference ref, Set<EReference> handledReferences, IDiagnosticProducer producer) {
final EReference eRef = GrammarUtil.getReference(ref, obj.eClass());
if (eRef == null) {
ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
DiagnosticMessage message = diagnosticMessageProvider.getIllegalCrossReferenceMessage(context, ref);
producer.addDiagnostic(message);
return;
}
handledReferences.add(eRef);
beforeEnsureIsLinked(obj, eRef, producer);
producer.setTarget(obj, eRef);
try {
final List<EObject> links = getLinkedObject(obj, eRef, node);
if (links == null || links.isEmpty()) {
if (!isNullValidResult(obj, eRef, node)) {
ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
DiagnosticMessage message = diagnosticMessageProvider.getUnresolvedProxyMessage(context);
producer.addDiagnostic(message);
}
return;
}
if (eRef.getUpperBound() >= 0 && links.size() > eRef.getUpperBound()) {
ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
DiagnosticMessage message = diagnosticMessageProvider.getViolatedBoundsConstraintMessage(context, links.size());
producer.addDiagnostic(message);
return;
}
if (eRef.getUpperBound() == 1) {
obj.eSet(eRef, links.get(0));
} else {
// eRef.getUpperBound() == -1 ||
// eRef.getUpperBound() < links.size
// TODO extract and check weather equals or identity is used by list
final List<EObject> list = (List<EObject>) obj.eGet(eRef);
if (links.size() > 1 && eRef.isUnique() && (list instanceof InternalEList)) {
final Set<EObject> addUs = new LinkedHashSet<EObject>(links);
// addUs.removeAll(list); // removeAll calls most likely list.contains() which is rather slow
for (int i = 0; i < list.size(); i++) addUs.remove(list.get(i));
if (!((InternalEList) list).addAllUnique(addUs)) {
ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
DiagnosticMessage message = diagnosticMessageProvider.getViolatedBoundsConstraintMessage(context, links.size());
producer.addDiagnostic(message);
}
} else if (!list.addAll(links)) {
ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
DiagnosticMessage message = diagnosticMessageProvider.getViolatedBoundsConstraintMessage(context, links.size());
producer.addDiagnostic(message);
}
}
} catch (IllegalNodeException e) {
ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
DiagnosticMessage message = diagnosticMessageProvider.getIllegalNodeMessage(context, e);
producer.addDiagnostic(message);
if (log.isDebugEnabled()) {
log.debug(e.getMessage(), e);
}
}
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class Linker method ensureLinked.
public void ensureLinked(EObject obj, IDiagnosticProducer producer) {
ICompositeNode node = NodeModelUtils.getNode(obj);
if (node == null)
return;
Set<EReference> handledReferences = new HashSet<EReference>();
ensureLinked(obj, producer, node, handledReferences, false);
producer.setNode(node);
setDefaultValues(obj, handledReferences, producer);
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class LazyURIEncoder method resolveShortFragment.
public EObject resolveShortFragment(Resource res, String shortFragment) {
List<String> split = Strings.split(shortFragment, '.');
int contentsIdx = Integer.parseInt(split.get(0));
EObject result = res.getContents().get(contentsIdx);
int splitIdx = 1;
while (splitIdx < split.size()) {
int featureId = Integer.parseInt(split.get(splitIdx++));
EReference reference = (EReference) result.eClass().getEStructuralFeature(featureId);
if (reference.isMany()) {
List<?> list = (List<?>) result.eGet(reference);
int listIdx = Integer.parseInt(split.get(splitIdx++));
result = (EObject) list.get(listIdx);
} else {
result = (EObject) result.eGet(reference);
}
}
return result;
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class LazyURIEncoder method appendShortFragment.
public void appendShortFragment(EObject obj, StringBuilder target) {
EReference containmentFeature = obj.eContainmentFeature();
if (containmentFeature == null) {
target.append(obj.eResource().getContents().indexOf(obj));
} else {
EObject container = obj.eContainer();
appendShortFragment(container, target);
target.append('.').append(container.eClass().getFeatureID(containmentFeature));
if (containmentFeature.isMany()) {
List<?> list = (List<?>) container.eGet(containmentFeature);
target.append('.').append(list.indexOf(obj));
}
}
}
Aggregations