PageRenderTime 32ms CodeModel.GetById 23ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/org.eclipse.xtext.example.fj/src/org/eclipse/xtext/example/linker/FJLinkingResource.java

https://bitbucket.org/gcubar/fj-eclipse
Java | 102 lines | 55 code | 15 blank | 32 comment | 7 complexity | 61aa2b661345f8fdcc71f192792ec299 MD5 | raw file
  1/**
  2 * 
  3 */
  4package org.eclipse.xtext.example.linker;
  5
  6import java.io.IOException;
  7import java.io.OutputStream;
  8import java.util.Map;
  9
 10import org.eclipse.xtext.example.fj.Class;
 11import org.eclipse.xtext.example.fj.FjFactory;
 12import org.eclipse.xtext.example.fj.Program;
 13import org.eclipse.xtext.example.lookup.AuxiliaryFunctions;
 14
 15import org.eclipse.emf.common.util.URI;
 16import org.eclipse.emf.ecore.EObject;
 17import org.eclipse.emf.ecore.resource.Resource;
 18import org.eclipse.emf.ecore.resource.ResourceSet;
 19import org.eclipse.xtext.linking.lazy.LazyLinkingResource;
 20
 21/**
 22 * Customized resource which automatically adds in the resource set
 23 * the resource with the implicit class Object
 24 * 
 25 * @author bettini
 26 * 
 27 */
 28public class FJLinkingResource extends LazyLinkingResource {
 29	/**
 30	 * The implicit object element
 31	 */
 32	private Class Object = null;
 33
 34	AuxiliaryFunctions auxiliaryFunctions = new AuxiliaryFunctions();
 35	
 36	/**
 37	 * The uri of the implicit resource containing the implicit object 
 38	 */
 39	public static final URI implicitObjectUri = URI.createURI("http:///Object.fj");
 40
 41	@Override
 42	protected void doLinking() {
 43		ensureObjectIsPresent();
 44		super.doLinking();
 45	}
 46
 47	/**
 48	 * Ensures that in the resource set there is the implicit Object resource
 49	 */
 50	private void ensureObjectIsPresent() {
 51		if (Object != null) {
 52			return;
 53		}
 54		
 55		// retrieve the implicit Object resource
 56		ResourceSet resourceSet = getResourceSet();
 57		Resource res = resourceSet.getResource(implicitObjectUri, true);
 58		if (res != null) {
 59			// store the implicit object locally for later use
 60			setObjectClass(((Program) res.getContents().get(0)).getClasses().get(0));
 61			System.out.println("Using implicit resource Object class");
 62			return;
 63		}
 64	}
 65	
 66	/**
 67	 * Utility method returning the implicit Object in the resource
 68	 * set of the specified element
 69	 * 
 70	 * @param eObject
 71	 * @return
 72	 */
 73	public static Class getObjectClass(EObject eObject) {
 74		return ((FJLinkingResource)eObject.eResource()).getObjectClass();
 75	}
 76
 77	/**
 78	 * @return the implicit Object (creates it on the first invocation)
 79	 */
 80	public Class getObjectClass() {
 81		if (Object == null) {
 82			Object = FjFactory.eINSTANCE.createClass();
 83			Object.setName("Object");
 84		}
 85
 86		return Object;
 87	}
 88	
 89	public void setObjectClass(Class objectClass) {
 90		Object = objectClass;
 91	}
 92
 93	@Override
 94	public void doSave(OutputStream outputStream, Map<?, ?> options)
 95			throws IOException {
 96		// don't save the implicit resource
 97		if (getURI().equals(implicitObjectUri))
 98			return;
 99		
100		super.doSave(outputStream, options);
101	}
102}