Applied patch for model serialization

see https://bugs.eclipse.org/bugs/attachment.cgi?id=114950
This commit is contained in:
jkohnlein 2008-10-22 09:45:55 +00:00
parent b1e55b2c28
commit 41f2a04118
7 changed files with 86 additions and 42 deletions

View file

@ -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);
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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();
}

View file

@ -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;
}
}

View file

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src-gen"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View file

@ -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 {