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