KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > block > BlockWriter


1
2
3  /*
4  -- GeiNuke --
5 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
6  http://www.hostingjava.it/-geinuke/
7
8 This file is part of GeiNuke.
9
10     GeiNuke is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     GeiNuke 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 GeiNuke; if not, write to the Free Software
22     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */

24 package com.geinuke.block;
25 import java.io.StringWriter JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import org.apache.velocity.Template;
30 import org.apache.velocity.app.Velocity;
31 import org.apache.velocity.context.Context;
32
33 import com.geinuke.common.NukeBlockI;
34 import com.geinuke.servlet.GeiServlet;
35 import com.geinuke.util.NukeResource;
36 import com.geinuke.vo.BlockDBVO;
37 import com.geinuke.vo.NukeBlockVO;
38 import com.magic.util.conf.MagicConfiguratorIfc;
39 import com.magic.util.props.MultiProps;
40
41 public class BlockWriter {
42     
43     
44     protected static MultiProps loadResource(String JavaDoc blockname,String JavaDoc locale)throws Exception JavaDoc{
45         MultiProps res=null;
46         
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 fn=GeiServlet.getNukePath()+"/WEB-INF/templates/blocks/"+blockname+"/resources.txt";
55
String JavaDoc fn=null;
56         fn=GeiServlet.getNukePath()+"/WEB-INF/templates/blocks/"+blockname+ePath+"/resources"+ext+".txt";
57         //GeiServlet.intLog("BlockWriter.loadResource(...), A resource="+fn);
58
if(! NukeResource.existsFile(fn)){
59             fn=GeiServlet.getNukePath()+"/WEB-INF/templates/blocks/"+blockname+"/resources.txt";;
60             //GeiServlet.intLog("BlockWriter.loadResource(...), B resource="+fn);
61
}
62         
63         
64         
65         
66         mc.setFileName(fn);
67         
68         res=mc.getConfiguration();
69          
70         return res;
71     }
72     /**
73      *
74      * @param ctx
75      * @param module module whose content has to be filled using setContent method
76      */

77     public static NukeBlockI fill(Context ctx,BlockDBVO block){
78         NukeBlockVO result=null;
79         Template t=null;
80         String JavaDoc templateName=null;
81         MultiProps res=null;
82         result=new NukeBlockVO();
83         
84         if(! block.isDbStored()){
85         
86             try{
87                 Locale JavaDoc lo=(Locale JavaDoc)ctx.get("LOCALE");
88                 //GeiServlet.intLog("BlockWriter.fill(...), locale="+lo);
89
//GeiServlet.intLog("BlockWriter.fill(...), block="+block);
90
res=loadResource(block.getName(),lo.getLanguage());
91                 String JavaDoc key=null;
92                 String JavaDoc value=null;
93                 Enumeration JavaDoc keys=res.keys();
94                 while(keys.hasMoreElements()){
95                     key=(String JavaDoc)keys.nextElement();
96                     value=(String JavaDoc)res.get(key);
97                     if(key.equals("TITLE"))
98                         result.setTitle(value);
99                     ctx.put(key,value);
100                 }
101             }catch(Exception JavaDoc e){
102                 GeiServlet.intLog("BlockWriter.fill(...), exception\n"+e);
103                 e.printStackTrace();
104             }
105             templateName="blocks/"+block.getName()+"/block.vm";
106             StringWriter JavaDoc sw = new StringWriter JavaDoc(1024 * 4);
107             try {
108                 t = Velocity.getTemplate(templateName);
109                 t.merge(ctx, sw);
110             } catch (Exception JavaDoc e) {
111                 e.printStackTrace();
112             }
113             result.setContent( sw.toString());
114         }else{
115             result.setContent(block.getContent());
116         }
117         if(result.getTitle()==null)
118             result.setTitle(block.getTitle());
119         
120         
121         result.setPos(block.getPos());
122         result.setWeight(block.getWeight());
123         result.setDbStored(block.isDbStored());
124         result.setSkin(block.getSkin());
125         return result;
126         
127     }
128 }
129
Popular Tags