[34] | 1 | package de.ugoe.cs.swe.bnftools.ui.formatter;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedWriter;
|
---|
| 4 | import java.io.File;
|
---|
[41] | 5 | import java.io.FileInputStream;
|
---|
[34] | 6 | import java.io.FileWriter;
|
---|
[41] | 7 | import java.io.IOException;
|
---|
[34] | 8 |
|
---|
| 9 | import org.eclipse.core.commands.AbstractHandler;
|
---|
| 10 | import org.eclipse.core.commands.ExecutionEvent;
|
---|
| 11 | import org.eclipse.core.commands.ExecutionException;
|
---|
| 12 | import org.eclipse.core.resources.IContainer;
|
---|
| 13 | import org.eclipse.core.runtime.Platform;
|
---|
| 14 | import org.eclipse.emf.ecore.EObject;
|
---|
| 15 | import org.eclipse.ui.PlatformUI;
|
---|
| 16 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
| 17 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
| 18 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
| 19 | import org.eclipse.xtext.ui.editor.model.XtextDocument;
|
---|
[41] | 20 | import org.osgi.framework.Bundle;
|
---|
[34] | 21 |
|
---|
| 22 | import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
|
---|
| 23 |
|
---|
| 24 | public class HtmlFormatterHandler extends AbstractHandler {
|
---|
| 25 |
|
---|
| 26 | public Object execute(ExecutionEvent event) throws ExecutionException {
|
---|
| 27 | XtextEditor editor = (XtextEditor) PlatformUI.getWorkbench()
|
---|
| 28 | .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
---|
| 29 |
|
---|
| 30 | // TODO: better error handling
|
---|
| 31 | if (!(editor.getDocument() instanceof XtextDocument)) {
|
---|
| 32 | return null;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | XtextDocument doc = (XtextDocument) editor.getDocument();
|
---|
| 36 | IContainer parent = editor.getResource().getParent();
|
---|
| 37 |
|
---|
| 38 | EObject root = doc.readOnly(new RootEObjectFinder());
|
---|
| 39 | NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
|
---|
| 40 |
|
---|
| 41 | FormatterConfig config = new FormatterConfig();
|
---|
| 42 | EbnfHtmlFormatterVisitor visitor = new EbnfHtmlFormatterVisitor(
|
---|
| 43 | rootNode.getParserNode().getElement(), config);
|
---|
| 44 |
|
---|
| 45 | visitor.accept();
|
---|
| 46 |
|
---|
[41] | 47 | Bundle bundle = Platform.getBundle("de.ugoe.cs.swe.bnftools.ebnf.ui");
|
---|
| 48 | String bundlePath = bundle.getLocation().replaceAll("reference:file:","");
|
---|
| 49 | String resourcePath = bundle.getResource("resources/html-header.html").getPath();
|
---|
| 50 | String result = "";
|
---|
| 51 | try {
|
---|
| 52 | result = readFileAsString(bundlePath + resourcePath);
|
---|
| 53 | } catch (IOException e) {
|
---|
| 54 | }
|
---|
| 55 | result = result + visitor.getBuf();
|
---|
| 56 |
|
---|
[43] | 57 | // System.out.println(visitor.getBuf());
|
---|
[35] | 58 |
|
---|
[43] | 59 | String filename = editor.getResource().getLocation().toPortableString().toString() + ".html";
|
---|
| 60 | // System.out.println(filename);
|
---|
| 61 | writeFile(result, filename);
|
---|
[41] | 62 |
|
---|
[34] | 63 | return null;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[41] | 66 | private String readFileAsString(String filePath) throws java.io.IOException{
|
---|
| 67 | byte[] buffer = new byte[(int) new File(filePath).length()];
|
---|
| 68 | FileInputStream f = new FileInputStream(filePath);
|
---|
| 69 | f.read(buffer);
|
---|
| 70 | return new String(buffer);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[34] | 73 | private void writeFile(String str, String filename) {
|
---|
| 74 | try {
|
---|
| 75 | FileWriter fstream = new FileWriter(filename);
|
---|
| 76 | BufferedWriter out = new BufferedWriter(fstream);
|
---|
| 77 | out.write(str);
|
---|
| 78 | out.close();
|
---|
| 79 | } catch (Exception e) {// Catch exception if any
|
---|
| 80 | System.err.println("Error: " + e.getMessage());
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | }
|
---|