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