From 41f2a04118a0ef2370944f68b6624c37cfa5dd11 Mon Sep 17 00:00:00 2001 From: jkohnlein Date: Wed, 22 Oct 2008 09:45:55 +0000 Subject: [PATCH] Applied patch for model serialization see https://bugs.eclipse.org/bugs/attachment.cgi?id=114950 --- .../reconstr/ICrossReferenceSerializer.java | 35 +++++++++++++++++++ .../callbacks/SimpleSerializingCallback.java | 14 +++----- .../impl/DefaultCrossReferenceSerializer.java | 16 +++++++++ .../parsetree/reconstr/impl/Predicate.java | 27 -------------- .../impl/SimpleCrossReferenceSerializer.java | 25 +++++++++++++ .../.classpath | 2 +- .../reconstr/SimpleReconstrTest.java | 9 +++-- 7 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/ICrossReferenceSerializer.java create mode 100644 plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/DefaultCrossReferenceSerializer.java delete mode 100644 plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/Predicate.java create mode 100644 plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/SimpleCrossReferenceSerializer.java diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/ICrossReferenceSerializer.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/ICrossReferenceSerializer.java new file mode 100644 index 000000000..d38700330 --- /dev/null +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/ICrossReferenceSerializer.java @@ -0,0 +1,35 @@ +package org.eclipse.xtext.parsetree.reconstr; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.xtext.CrossReference; + +/** + * In the process of serializing a model to a DSL, references to model elements + * need to be turned into string representations which identify the targeted + * model element. Implementations of this interface compute this string + * representation. + * + * Implementations must be synchronous with the DSL's parser implementation. + * + * Implementations might introduce some kind of scoping. + * + * @author Moritz Eysholdt - Initial contribution and API + */ +public interface ICrossReferenceSerializer { + + /** + * Calculates a String which is a valid reference to the 'target' object + * within the DSL. + * + * @param container + * The object which contains the reference + * @param grammarElement + * The grammar element describing the cross reference + * @param target + * the referenced object + * @return A string representing a reference the target object. + */ + public String serializeCrossRef(IInstanceDescription container, + CrossReference grammarElement, EObject target); + +} diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/callbacks/SimpleSerializingCallback.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/callbacks/SimpleSerializingCallback.java index 17ef4c8e4..3a6a96184 100644 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/callbacks/SimpleSerializingCallback.java +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/callbacks/SimpleSerializingCallback.java @@ -10,6 +10,7 @@ import org.eclipse.xtext.CrossReference; import org.eclipse.xtext.Keyword; import org.eclipse.xtext.RuleCall; import org.eclipse.xtext.conversion.IValueConverterService; +import org.eclipse.xtext.parsetree.reconstr.ICrossReferenceSerializer; import org.eclipse.xtext.parsetree.reconstr.IInstanceDescription; import org.eclipse.xtext.service.Inject; @@ -22,6 +23,9 @@ public class SimpleSerializingCallback extends @Inject protected IValueConverterService converterService; + @Inject + protected ICrossReferenceSerializer crossRefSerializer; + protected OutputStream out; protected boolean outputHasStarted; @@ -52,11 +56,7 @@ public class SimpleSerializingCallback extends public void crossRefCall(IInstanceDescription current, CrossReference call, EObject value) { before(current, call); - append(value.eResource().getURIFragment(value)); - } - - public IValueConverterService getConverterService() { - return converterService; + append(crossRefSerializer.serializeCrossRef(current, call, value)); } public void keywordCall(IInstanceDescription current, Keyword call) { @@ -69,8 +69,4 @@ public class SimpleSerializingCallback extends before(current, call); append(converterService.toString(value, call.getName())); } - - public void setConverterService(IValueConverterService converterService) { - this.converterService = converterService; - } } diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/DefaultCrossReferenceSerializer.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/DefaultCrossReferenceSerializer.java new file mode 100644 index 000000000..f64fd2bbf --- /dev/null +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/DefaultCrossReferenceSerializer.java @@ -0,0 +1,16 @@ +package org.eclipse.xtext.parsetree.reconstr.impl; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.xtext.CrossReference; +import org.eclipse.xtext.parsetree.reconstr.ICrossReferenceSerializer; +import org.eclipse.xtext.parsetree.reconstr.IInstanceDescription; + +public class DefaultCrossReferenceSerializer implements + ICrossReferenceSerializer { + + public String serializeCrossRef(IInstanceDescription container, + CrossReference grammarElement, EObject target) { + return null; + } + +} diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/Predicate.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/Predicate.java deleted file mode 100644 index a89fd7931..000000000 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/Predicate.java +++ /dev/null @@ -1,27 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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 - * http://www.eclipse.org/legal/epl-v10.html - * - *******************************************************************************/ -package org.eclipse.xtext.parsetree.reconstr.impl; - - - -/** - * @author Sven Efftinge - Initial contribution and API - * - */ -public abstract class Predicate { - - protected InstanceDescription obj; - - public Predicate(InstanceDescription obj) { - this.obj = (InstanceDescription) obj.createClone(); - } - - public abstract boolean check(); - -} diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/SimpleCrossReferenceSerializer.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/SimpleCrossReferenceSerializer.java new file mode 100644 index 000000000..726ebd856 --- /dev/null +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/parsetree/reconstr/impl/SimpleCrossReferenceSerializer.java @@ -0,0 +1,25 @@ +package org.eclipse.xtext.parsetree.reconstr.impl; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.xtext.CrossReference; +import org.eclipse.xtext.crossref.IFragmentProvider; +import org.eclipse.xtext.parsetree.reconstr.IInstanceDescription; +import org.eclipse.xtext.parsetree.reconstr.XtextSerializationException; +import org.eclipse.xtext.service.Inject; + +public class SimpleCrossReferenceSerializer extends + DefaultCrossReferenceSerializer { + + @Inject + private IFragmentProvider fragmentProvider; + + public String serializeCrossRef(IInstanceDescription container, + CrossReference grammarElement, EObject target) { + String r = fragmentProvider.getFragment(target); + if (r == null) + throw new XtextSerializationException(container, + "Unable to create a string represenation for reference '" + + grammarElement.getType().getName() + "'."); + return r; + } +} diff --git a/tests/org.eclipse.xtext.generator.tests/.classpath b/tests/org.eclipse.xtext.generator.tests/.classpath index 04fe1774c..59cf3977c 100644 --- a/tests/org.eclipse.xtext.generator.tests/.classpath +++ b/tests/org.eclipse.xtext.generator.tests/.classpath @@ -1,8 +1,8 @@ - + diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SimpleReconstrTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SimpleReconstrTest.java index 7211f2000..98e9c2822 100644 --- a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SimpleReconstrTest.java +++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/parsetree/reconstr/SimpleReconstrTest.java @@ -102,11 +102,10 @@ public class SimpleReconstrTest extends AbstractGeneratorTest { // assertEquals(model, parseAndSerialize(model)); // } - // FIXME: make this work - // public void testCrossRef() throws Exception { - // String model = "type A extends B type B extends A"; - // assertEquals(model, parseAndSerialize(model)); - // } + public void testCrossRef() throws Exception { + String model = "type A extends B type B extends A"; + assertEquals(model, parseAndSerialize(model)); + } @Override protected void setUp() throws Exception {