KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > module > ModuleWriter


1
2  /*
3  -- GeiNuke --
4 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
5  http://www.hostingjava.it/-geinuke/
6
7 This file is part of GeiNuke.
8
9     GeiNuke is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     GeiNuke is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with GeiNuke; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */

23 package com.geinuke.module;
24
25 import java.io.StringWriter JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.velocity.Template;
32 import org.apache.velocity.app.Velocity;
33 import org.apache.velocity.context.Context;
34
35 import com.geinuke.common.NukeModuleI;
36 import com.geinuke.servlet.GeiServlet;
37 import com.geinuke.util.NukeResource;
38 import com.geinuke.vo.ModuleDBVO;
39 import com.geinuke.vo.NukeModuleVO;
40 import com.magic.util.conf.MagicConfiguratorIfc;
41 import com.magic.util.props.MultiProps;
42
43 public class ModuleWriter {
44     
45     public static MultiProps loadResource(String JavaDoc moduleName,String JavaDoc locale)throws Exception JavaDoc{
46         MultiProps res=null;
47         String JavaDoc ePath="";
48         String JavaDoc ext="";
49         if(locale!=null && !locale.equals("")){
50             ePath="/i18n";
51             ext="_"+locale;
52         }
53         MagicConfiguratorIfc mc=(MagicConfiguratorIfc)Class.forName("com.magic.util.conf.PropsFileConfigurator").newInstance();
54         String JavaDoc fn=null;
55         fn=GeiServlet.getNukePath()+"/WEB-INF/templates/modules/"+moduleName+ePath+"/resources"+ext+".txt";
56         //GeiServlet.intLog("ModuleWriter.loadResource(...), A resource="+fn);
57
if(! NukeResource.existsFile(fn)){
58             fn=GeiServlet.getNukePath()+"/WEB-INF/templates/modules/"+moduleName+"/resources.txt";;
59             //GeiServlet.intLog("ModuleWriter.loadResource(...), B resource="+fn);
60
}
61         mc.setFileName(fn);
62         res=mc.getConfiguration();
63          
64         return res;
65     }
66     
67     
68     
69     /**
70      *
71      * @param ctx
72      * @param module module whose content has to be filled using setContent method
73      */

74     public static NukeModuleI fill(Context ctx,ModuleDBVO module,String JavaDoc templateName){
75         Template t=null;
76         HttpServletRequest JavaDoc req=(HttpServletRequest JavaDoc)ctx.get("req");
77         
78         MultiProps res=null;
79         NukeModuleVO modulePL=null;
80         modulePL=new NukeModuleVO();
81         Locale JavaDoc lo=(Locale JavaDoc)ctx.get("LOCALE");
82         if(! module.isDbStored()){
83             try{
84                 GeiServlet.fixStyle(req,ctx);
85                 res=loadResource(module.getName(),lo.getLanguage());
86                 String JavaDoc key=null;
87                 String JavaDoc value=null;
88                 Enumeration JavaDoc keys=res.keys();
89                 while(keys.hasMoreElements()){
90                     key=(String JavaDoc)keys.nextElement();
91                     value=(String JavaDoc)res.get(key);
92                     if(key.equals("TITLE"))
93                         modulePL.setTitle(value);
94                     ctx.put(key,value);
95                 }
96             }catch(Exception JavaDoc e){
97                 //GeiServlet.intLog("ModuleWriter.fill(...), exception\n"+e);
98
//e.printStackTrace();
99
}
100             templateName="modules/"+module.getName()+"/"+templateName;
101             StringWriter JavaDoc sw = new StringWriter JavaDoc(1024 * 4);
102             if(modulePL.getTitle()==null)
103                 modulePL.setTitle(module.getTitle());
104             modulePL.setSkin(module.getSkin());
105             ctx.put("module",modulePL);
106             try {
107                 t = Velocity.getTemplate(templateName);
108                 t.merge(ctx, sw);
109             } catch (Exception JavaDoc e) {
110                 
111                 e.printStackTrace();
112             }
113             modulePL.setContent( sw.toString());
114         }else{
115             
116             templateName="html/dbModule.vm";
117             ctx.put("module",module);
118             StringWriter JavaDoc sw = new StringWriter JavaDoc(1024 * 4);
119             
120             try {
121                 GeiServlet.fixStyle(req,ctx);
122                 t = Velocity.getTemplate(templateName);
123                 t.merge(ctx, sw);
124             } catch (Exception JavaDoc e) {
125                 
126                 e.printStackTrace();
127             }
128             
129             
130             modulePL.setContent( sw.toString());
131             modulePL.setSkin(module.getSkin());
132             modulePL.setTitle(module.getTitle());
133         }
134         
135         //modulePL.s( sw.toString());
136

137         modulePL.setName(module.getName());
138         
139         modulePL.setPos(module.getPos());
140         modulePL.setWidth(module.getWidth());
141         modulePL.setWeight(module.getWeight());
142         modulePL.setDbStored(module.isDbStored());
143             
144         return modulePL;
145     }
146 }
147
Popular Tags