Customizing Scaffolding

From Skyway Wiki

Jump to: navigation, search

Contents

Create a JET Transformation Project

  1. Add the following dependencies:
    1. org.eclipse.core.resources
    2. org.eclipse.ui
    3. org.eclipse.core.runtime
    4. org.skyway.core
    5. org.skyway.core.ui
    6. org.skyway.core.generate
    7. org.skyway.core.steps

Add Context Menu Option

  1. Add org.eclipse.ui.popupMenus extension:
    1. objectClass: org.eclipse.core.resources.IResource
    2. nameFilter: *.xml
    3. adaptable: true
  2. Add an action to the menu:
    1. class: org.demo.LaunchScaffolding
    2. menubarPath: popup:org.eclipse.jdt.ui.PackageExplorer
    3. style: push
    4. enablesFor: 1

Add Generator(s)

  1. For each XML node you want to process, add a generator using the org.skyway.core.generate.generators extension point.
    1. The id should be equal to the XML element node name.
    2. The class should be a subclass of org.skyway.core.generate.generator.DescendantGenerator. It has access to your XML node through a descriptor. For example, this generator creates a Skyway data type and gives it a name:
public class DemoGenerator extends DescendantGenerator {
     
     public void generate(GenerationContext context) {
          DataTypeBuilder builder = DataTypeBuilder.instance();
          builder.name(getDescriptor().getAttribute("Name"));
          builder.create();
     }

Create Launcher

  1. Create the action you attached to your menu option:
public class LaunchDemo implements IActionDelegate {
    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
     public void run(IAction action) {
         final IResource resource = SelectionUtils.getFirstSelectedResource();
         final GenerationContext context = new GenerationContext(); 
         context.setCore((Project) resource.getProject().getAdapter(Core.class));
         // Read in the XML file
         Element element = null;
         try {
             DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
             String s = ResourceUtils.readFile((IFile) resource);
             Document doc = docBuilder.parse(new InputSource(new StringReader(s)));
             element = doc.getDocumentElement();
         } catch (ParserConfigurationException e) {
             throw new RuntimeException(e);
         } catch (SAXException e) {
             throw new RuntimeException(e);
         } catch (IOException e) {
             throw new RuntimeException(e);
         } catch (CoreException e) {
             throw new RuntimeException(e);
         }
         // Create our descriptor & configure the generation context.
         final IGenerationDescriptor descriptor = new XmlGenerationDescriptor(element);
         context.setDescriptor(descriptor);
         final DefaultGenerator generator = new DefaultGenerator();
         generator.setDescriptor(descriptor);
         GenerationResource r = new GenerationResource() {
             public IGenerator getGenerator() {
                 return generator;
             }
         };
         r.setDescriptor(descriptor);
         // Create a scaffolding (generation) job.
         GenerationApplication ga = new GenerationApplication();
         ga.setResource(r);
         ga.setContext(context);
         // Execute the scaffolding
         ga.execute(true);
     }
Personal tools