From Skyway Wiki
Create a JET Transformation Project
- Add the following dependencies:
- org.eclipse.core.resources
- org.eclipse.ui
- org.eclipse.core.runtime
- org.skyway.core
- org.skyway.core.ui
- org.skyway.core.generate
- org.skyway.core.steps
Add Context Menu Option
- Add org.eclipse.ui.popupMenus extension:
- objectClass: org.eclipse.core.resources.IResource
- nameFilter: *.xml
- adaptable: true
- Add an action to the menu:
- class: org.demo.LaunchScaffolding
- menubarPath: popup:org.eclipse.jdt.ui.PackageExplorer
- style: push
- enablesFor: 1
Add Generator(s)
- For each XML node you want to process, add a generator using the org.skyway.core.generate.generators extension point.
- The id should be equal to the XML element node name.
- 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
- 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);
}