KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > loader > GroovyMacroLoader


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.render.macro.loader;
27
28 import groovy.lang.GroovyClassLoader;
29 import org.radeox.macro.Macro;
30 import org.radeox.macro.MacroLoader;
31 import org.radeox.macro.Repository;
32 import org.snipsnap.container.Components;
33 import org.snipsnap.notification.Consumer;
34 import org.snipsnap.notification.Message;
35 import org.snipsnap.notification.MessageService;
36 import org.snipsnap.snip.Snip;
37 import org.snipsnap.snip.SnipSpace;
38
39 import java.io.ByteArrayInputStream JavaDoc;
40 import java.io.InputStream JavaDoc;
41
42 /**
43  * Plugin loader for macros with Groovy source
44  * instead of Java
45  *
46  * @author Stephan J. Schmidt
47  * @version $Id: GroovyMacroLoader.java 1606 2004-05-17 10:56:18Z leo $
48  */

49
50 public class GroovyMacroLoader extends MacroLoader implements Consumer {
51   public GroovyMacroLoader() {
52     // We're interested in changed snips
53
MessageService service = (MessageService) Components.getComponent(MessageService.class);
54     service.register(this);
55   }
56
57   public void consume(Message messsage) {
58     if (Message.SNIP_MODIFIED.equals(messsage.getType())) {
59       Snip snip = (Snip) messsage.getValue();
60       if (snip.getName().startsWith("SnipSnap/config/macros/")) {
61         try {
62           Macro macro = compileMacro(snip.getContent());
63           if (null != macro) {
64             add(repository, macro);
65           }
66         } catch (Exception JavaDoc e) {
67           System.err.println("GroovyMacroLoader: unable to reload macros: " + e);
68           e.printStackTrace();
69         }
70       }
71     }
72   }
73
74   private Macro compileMacro(String JavaDoc macroSource) {
75     Macro macro = null;
76     try {
77       GroovyClassLoader gcl = new GroovyClassLoader();
78       InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(macroSource.getBytes());
79       Class JavaDoc clazz = gcl.parseClass(is, "");
80       Object JavaDoc aScript = clazz.newInstance();
81       macro = (Macro) aScript;
82       //System.out.println("Script="+macroSource);
83
} catch (Exception JavaDoc e) {
84       System.err.println("Cannot compile groovy macro: " + e.getMessage());
85       e.printStackTrace();
86     }
87     return macro;
88   }
89
90   /**
91    * Load all plugins of Class klass
92    *
93    * @param repository
94    * @param klass
95    * @return
96    */

97   public Repository loadPlugins(Repository repository, Class JavaDoc klass) {
98     if (null != repository) {
99       SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
100       Snip[] snips = space.match("SnipSnap/config/macros/");
101
102       for (int i = 0; i < snips.length; i++) {
103         Snip snip = snips[i];
104         Macro macro = compileMacro(snip.getContent());
105         add(repository, macro);
106       }
107
108 // String macroSource = "import java.io.Writer\n" +
109
// "import org.radeox.macro.parameter.MacroParameter\n" +
110
// "\n" +
111
// "class GroovyMacro extends org.radeox.macro.BaseMacro {\n" +
112
// " void execute(Writer writer, MacroParameter params) {\n" +
113
// " writer.write(\"Yipee ay ey, schweinebacke\")\n" +
114
// " }\n" +
115
// " String getName() {" +
116
// " return \"groovy\"\n" +
117
// " }\n " +
118
// "}";
119
// Macro macro = compileMacro(macroSource);
120
// if (null != macro) {
121
// add(repository, macro);
122
// }
123

124       //System.out.println(clazz.getName());
125
// System.out.println(aScriptSource);
126
}
127     return repository;
128   }
129 }
Popular Tags