integrated velocity engine for URL templates
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / httpquery / AbstractHTTPQueryAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.httpquery;
2
3 import java.io.StringWriter;
4 import java.net.URL;
5
6 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
7 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
8 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationWorkingCopy;
9 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
10
11 import org.apache.velocity.VelocityContext;
12 import org.apache.velocity.app.Velocity;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.ui.IEditorActionDelegate;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IViewPart;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21
22 public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
23
24   private IEditorPart targetEditor;
25   private EditorText text;
26   public AbstractHTTPQueryAction() {
27     super();
28   }
29
30   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
31     this.targetEditor = targetEditor;
32     text = new EditorText(targetEditor);
33   }
34
35   abstract protected Configuration getConfiguration();
36
37   public void run(IAction action) {
38     URL url;
39     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
40     if (window != null) {
41       IWorkbenchPage page = window.getActivePage();
42       try {
43         IViewPart part = page.findView(BrowserView.ID_BROWSER);
44         if (part == null) {
45           part = page.showView(BrowserView.ID_BROWSER);
46         } else {
47           page.bringToTop(part);
48         }
49         Configuration config = getConfiguration();
50         String templateString = generateUrl(config, config.getURL());
51         if (templateString != null && !templateString.equals("")) {
52           ((BrowserView) part).setUrl(templateString);
53         }
54       } catch (Exception e) {
55       }
56     }
57   }
58
59   public void selectionChanged(IAction action, ISelection selection) {
60   }
61
62   public String generateUrl(Configuration config, String template) {
63
64     /* first, we init the runtime engine. Defaults are fine. */
65
66     try {
67       Velocity.init();
68
69       /* lets make a Context and put data into it */
70
71       VelocityContext context = new VelocityContext();
72       
73       context.put("config", config);
74       text.clear();
75       context.put("text", text);  
76
77       /* lets make our own string to render */
78       StringWriter w = new StringWriter();
79       w = new StringWriter();
80       Velocity.evaluate(context, w, "mystring", template);
81       return w.toString();
82
83     } catch (Exception e) {
84       // TODO Auto-generated catch block
85       e.printStackTrace();
86     }
87     return template;
88   }
89 }