source: default/trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/HtmlFormatterHandler.java @ 34

Last change on this file since 34 was 34, checked in by zeiss, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 2.5 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.formatter;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6
7import org.eclipse.core.commands.AbstractHandler;
8import org.eclipse.core.commands.ExecutionEvent;
9import org.eclipse.core.commands.ExecutionException;
10import org.eclipse.core.resources.IContainer;
11import org.eclipse.core.resources.IWorkspace;
12import org.eclipse.core.resources.IWorkspaceRoot;
13import org.eclipse.core.resources.ResourcesPlugin;
14import org.eclipse.core.runtime.IPath;
15import org.eclipse.core.runtime.Platform;
16import org.eclipse.emf.ecore.EObject;
17import org.eclipse.ui.PlatformUI;
18import org.eclipse.xtext.parsetree.NodeAdapter;
19import org.eclipse.xtext.parsetree.NodeUtil;
20import org.eclipse.xtext.ui.editor.XtextEditor;
21import org.eclipse.xtext.ui.editor.model.XtextDocument;
22
23import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
24
25public 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
38                IContainer parent = editor.getResource().getParent();
39
40                EObject root = doc.readOnly(new RootEObjectFinder());
41                NodeAdapter rootNode = NodeUtil.getNodeAdapter(root);
42
43                FormatterConfig config = new FormatterConfig();
44                EbnfHtmlFormatterVisitor visitor = new EbnfHtmlFormatterVisitor(
45                                rootNode.getParserNode().getElement(), config);
46                // visitor.accept(rootNode.getElement());
47
48                visitor.accept();
49
50                // System.out.println(visitor.getBuf());
51                IWorkspace workspace = ResourcesPlugin.getWorkspace();
52                IWorkspaceRoot workspaceRoot = workspace.getRoot();
53                IPath location = workspaceRoot.getLocation();
54
55                System.out.println(parent.getLocationURI().getPath());
56//              File f = new File(".");
57                writeFile(visitor.getBuf().toString(), parent.getLocationURI().getPath() + "/" + editor.getResource().getName() + ".html");
58
59                return null;
60        }
61
62        private void writeFile(String str, String filename) {
63                try {
64                        FileWriter fstream = new FileWriter(filename);
65                        BufferedWriter out = new BufferedWriter(fstream);
66                        out.write(str);
67                        out.close();
68                } catch (Exception e) {// Catch exception if any
69                        System.err.println("Error: " + e.getMessage());
70                }
71        }
72
73}
Note: See TracBrowser for help on using the repository browser.