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

Last change on this file since 43 was 43, checked in by zeiss, 13 years ago
  • Property svn:mime-type set to text/plain
File size: 2.8 KB
Line 
1package de.ugoe.cs.swe.bnftools.ui.formatter;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.FileWriter;
7import java.io.IOException;
8
9import org.eclipse.core.commands.AbstractHandler;
10import org.eclipse.core.commands.ExecutionEvent;
11import org.eclipse.core.commands.ExecutionException;
12import org.eclipse.core.resources.IContainer;
13import org.eclipse.core.runtime.Platform;
14import org.eclipse.emf.ecore.EObject;
15import org.eclipse.ui.PlatformUI;
16import org.eclipse.xtext.parsetree.NodeAdapter;
17import org.eclipse.xtext.parsetree.NodeUtil;
18import org.eclipse.xtext.ui.editor.XtextEditor;
19import org.eclipse.xtext.ui.editor.model.XtextDocument;
20import org.osgi.framework.Bundle;
21
22import de.ugoe.cs.swe.bnftools.utils.RootEObjectFinder;
23
24public 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
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               
57//              System.out.println(visitor.getBuf());
58               
59                String filename = editor.getResource().getLocation().toPortableString().toString() + ".html";
60//              System.out.println(filename);
61                writeFile(result, filename);
62               
63                return null;
64        }
65
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       
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}
Note: See TracBrowser for help on using the repository browser.