Example: Generate HTML documentation from an Ecore metamodel with EGL

[%
  out.setContentType("HTML");
  
  var package : String = dataType.getEPackage().name;
%]
<h1>Package: [%=package%]</h1>
<h2>DataType [%=dataType.name%]</h2>
        
[% if (dataType.getEAnnotation("doc").isDefined()) { %]

  [% for (entry in dataType.getEAnnotation("doc").getDetails().
    select(e|e.key="description")) { %]
    <hr />
    <p id="description">
      [%=entry.value%]
    </p>
  [% } %]
  
  [% for (entry in dataType.getEAnnotation("doc").getDetails()) { %]
    [% if (entry.key = "version") { %]
      <h3>Version:</h3>
      <p id="version">[%=entry.value%]</p>
    [% } %]
    [% if (entry.key = "see") { %]
      <h3>See also: </h3>
      <ul>
        [% for (see in entry.value.split(",")) { %]
          <li>
          <a href="[%=see.trim().asClassifierFileName(package)%]">
          [%=see.trim()%]</a>
          </li>
        [% } %]
      </ul>
    [% } %]
  [% } %]
[% } %]

[% 
if (dataType.isKindOf(EEnum)) {
  var enumeration : EEnum = dataType;  
  
  if (enumeration.eLiterals.size() > 0) { %]
    <hr />
  
    <h4>Literals</h4>
    <table cellspacing="0">
      <tr>
        <th>Name</th>
        <th>Value</th>
        <th>Description</th>
      </tr>
      [% for (literal in enumeration.eLiterals.sortBy(l|l.name)) { %]
        <tr>
          <td>[%=literal.name%]</td>
          <td>[%=literal.value%]</td>
      
        [% if (literal.getEAnnotation("doc").isDefined()) { %]
          [% for (entry in literal.getEAnnotation("doc").
            getDetails()) { %]
            [% if (entry.key = "description") { %]
              <td>[%=enrry.value%]</td>
            [% } %]
          [% } %]
        [% } else { %]
          <td>&nbsp;</td>
        [% } %]  
        </tr>
      [% } %]
    </table>
  [% } %]
[% }

  operation String asClassifierFileName(package : String) : String {
    return package + "-" + self + ".html";
  }
%]
[%
%]
<h1>Packages</h1>
<ul>
[% for (p in EPackage.allInstances().sortBy(p|p.name)) { %]
  <li><a href="[%=p.filename()%]">[%=p.name%]</a></li>
[% } %]
</ul>

[% 
  operation EPackage filename() : String {
    return self.name + "-package-index.html";
  }
%]
[%
  TemplateFactory.setOutputRoot(outputDir + "/" + metaModelName);

  var t : Template = TemplateFactory.load("html/Page.egl");
  
  t.run("index.html", metaModelName, null, null);

  for (package in EPackage.allInstances()) {
    t.run(package.filename(), metaModelName, package, null);
    
    for (classifier in package.getEClassifiers()) { 
      t.run(classifier.filename(), metaModelName, package, classifier);
    }
  }
  
  operation Template run(filename : String, metaModelName : String, 
    package : EPackage, classifier : EClassifier) {
    var t : Template = TemplateFactory.load("html/Page.egl");
    
    t.populate("metaModelName", metaModelName);
    t.populate("package",       package);
    t.populate("classifier",    classifier);
    
    t.generate(filename);
  }
  
  operation EPackage filename() : String {
    return self.name + "-package-index.html";
  }

  operation EClassifier filename() : String {
    return self.ePackage.name + "-" + self.name + ".html";
  }

 %]

Check out the code from the SVN:

  • go to the SVN repository
  • navigate to trunk/examples
  • check out the org.eclipse.epsilon.examples.metamodels project
  • check out the org.eclipse.epsilon.examples.egldoc project

Once you have checked out/imported the code, to run the example you need to go through the following steps:

  1. register all .ecore metamodels in the org.eclipse.epsilon.examples.metamodels project (select all of them and then right click and select Register EPackages)
  2. register any .ecore metamodels in the org.eclipse.epsilon.examples.egldoc project
  3. right click the .launch file in the org.eclipse.epsilon.examples.egldoc project
  4. select Run as... and click the first item in the menu that pops up

What's this?

In this example, we demonstrate how EGL can be used to generate HTML documentation from an Ecore metamodel.

What are .emf files?

.emf files are Ecore metamodels expressed using the Emfatic textual syntax.

More examples...

Epsilon Object Language
Epsilon Transformation Language
Epsilon Generation Language
Epsilon Validation Language
Epsilon Merging Language
Epsilon Flock
Combining the Epsilon Languages
EuGENia
EUnit

Even more examples...

More examples are available in the examples folder of the SVN repository.