[34] | 1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedWriter;
|
---|
| 4 | import java.io.File;
|
---|
| 5 | import java.io.FileWriter;
|
---|
| 6 |
|
---|
| 7 | import org.eclipse.core.commands.AbstractHandler;
|
---|
| 8 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
| 9 | import org.eclipse.core.commands.ExecutionException;
|
---|
| 10 | import org.eclipse.core.resources.IContainer;
|
---|
| 11 | import org.eclipse.core.resources.IWorkspace;
|
---|
| 12 | import org.eclipse.core.resources.IWorkspaceRoot;
|
---|
| 13 | import org.eclipse.core.resources.ResourcesPlugin;
|
---|
| 14 | import org.eclipse.core.runtime.IPath;
|
---|
| 15 | import org.eclipse.core.runtime.Platform;
|
---|
| 16 | import org.eclipse.emf.ecore.EObject;
|
---|
| 17 | import org.eclipse.ui.PlatformUI;
|
---|
| 18 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 19 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 20 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 21 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
| 22 |
|
---|
| 23 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 24 |
|
---|
| 25 | public class HtmlFormatterHandler extends AbstractHandler {
|
---|
| 26 |
|
---|
| 27 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 28 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 29 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 30 |
|
---|
| 31 | // TODO: better error handling
|
---|
| 32 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 33 | return null;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 37 | IContainer parent = editor.getResource().getParent();
|
---|
| 38 |
|
---|
| 39 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 40 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 41 |
|
---|
| 42 | FormatterConfig config = new FormatterConfig();
|
---|
| 43 | EbnfHtmlFormatterVisitor visitor = new EbnfHtmlFormatterVisitor(
|
---|
| 44 | rootNode.getParserNode().getElement(), config);
|
---|
| 45 |
|
---|
| 46 | visitor.accept();
|
---|
| 47 |
|
---|
| 48 | writeFile(visitor.getBuf().toString(), parent.getLocationURI().getPath() + "/" + editor.getResource().getName() + ".html");
|
---|
[35] | 49 | System.out.println(visitor.getBuf());
|
---|
| 50 |
|
---|
[34] | 51 | return null;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | private void writeFile(String str, String filename) {
|
---|
| 55 | try {
|
---|
| 56 | FileWriter fstream = new FileWriter(filename);
|
---|
| 57 | BufferedWriter out = new BufferedWriter(fstream);
|
---|
| 58 | out.write(str);
|
---|
| 59 | out.close();
|
---|
| 60 | } catch (Exception e) {// Catch exception if any
|
---|
| 61 | System.err.println("Error: " + e.getMessage());
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | }
|
---|