KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > common > EditorFactory


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.common;
24
25 import java.io.StringWriter JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28
29 import org.apache.velocity.Template;
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.Velocity;
32 import org.apache.velocity.context.Context;
33
34 import com.geinuke.servlet.GeiServlet;
35 import com.geinuke.util.NukeResource;
36 import com.geinuke.util.TextUtil;
37
38
39 public class EditorFactory {
40     protected boolean isCompatible;
41     protected Object JavaDoc object=null;
42     protected HttpServletRequest JavaDoc req=null;
43     public EditorFactory(HttpServletRequest JavaDoc req,Object JavaDoc object){
44         this.req=req;
45         this.isCompatible=isCompatible(req);
46         this.object=object;
47     }
48     
49
50     
51     protected String JavaDoc merge(String JavaDoc fileName,Context ctx){
52         String JavaDoc html=null;
53         String JavaDoc templateName=null;
54         Template t=null;
55         templateName="html/"+fileName;
56         StringWriter JavaDoc sw = new StringWriter JavaDoc(1024 * 4);
57         try {
58             t = Velocity.getTemplate(templateName);
59             t.merge(ctx, sw);
60         } catch (Exception JavaDoc e) {
61             e.printStackTrace();
62         }
63         html=sw.toString();
64         return html;
65     }
66     
67     public String JavaDoc getEditor(String JavaDoc instanceName, String JavaDoc value, String JavaDoc width, String JavaDoc height){
68         String JavaDoc tmp=null;
69         VelocityContext ctx=new VelocityContext();
70         value=TextUtil.normHTML(value);
71         ctx.put("instanceName",instanceName);
72         ctx.put("width",width);
73         ctx.put("value",value);
74         ctx.put("height",height);
75         ctx.put("object",object);
76     
77         if(isCompatible){
78             /*
79             String link="fckeditor/editor/fckeditor.html?InstanceName="+instanceName+"&Toolbar=JetNuke";
80             tmp.append("<input type=\"hidden\" id=\""+instanceName+"\" name=\""+instanceName+"\" value=\""+value+"\">");
81             tmp.append("<input type=\"hidden\" id=\""+instanceName+"___Config\" value=\"\">");
82             tmp.append("<iframe id=\""+instanceName+"___Frame\" SRC=\""+link+"\" width=\""+width+"\" height=\""+height+"\" frameborder=\"no\" scrolling=\"no\"></iframe>");
83             */

84             tmp=this.merge("fckeditor1.vm",ctx);
85         }
86         else{
87             String JavaDoc widthCSS=width;
88             String JavaDoc heightCSS=height;
89             if(widthCSS.indexOf("%")==-1){
90                 widthCSS=widthCSS+"px";
91             }
92             if(heightCSS.indexOf("%")==-1){
93                 heightCSS=heightCSS+"px";
94             }
95             ctx.put("widthCSS",widthCSS);
96             ctx.put("heightCSS",heightCSS);
97             tmp=this.merge("textarea.vm",ctx);
98             /*
99             tmp.append("<textarea name=\""+instanceName+"\" rows=\"4\" cols=\"40\" style=\"width: "+widthCSS+"; height: "+heightCSS+"\" wrap=\"virtual\">"+value+"</textarea>");
100             */

101         }
102         return tmp;
103     }
104     
105     public String JavaDoc getQuote(String JavaDoc aut,String JavaDoc text){
106         String JavaDoc res=null;
107         //System.out.println("getQuote1");
108
VelocityContext ctx=new VelocityContext();
109         //System.out.println("getQuote1.5"+this.req);
110
ctx.put("LOCALE",NukeResource.getLocale(this.req,null));
111         //System.out.println("getQuote2");
112
GeiServlet.fill(ctx);
113         //System.out.println("getQuote3");
114
ctx.put("w_writer",aut);
115         //System.out.println("getQuote4");
116
ctx.put("t_text",text);
117         //System.out.println("getQuote5");
118
res=this.merge("quote.vm",ctx);
119         //System.out.println("getQuote6");
120
return res;
121     }
122     
123     protected boolean isCompatible(HttpServletRequest JavaDoc req){
124     
125         String JavaDoc ua=req.getHeader("User-Agent");
126         int index=-1;
127         if((index=ua.indexOf("MSIE"))!=-1 && ua.indexOf("mac")==-1 && ua.indexOf("Opera")==-1){
128             
129             String JavaDoc tmp=ua.substring(index+5,index+8).replaceAll(",",".");
130             double d=Double.parseDouble(tmp);
131             if(d>=5.5){
132                 return true;
133             }
134             
135         }else if ((index=ua.indexOf("Gecko"))>-1){
136             index=ua.indexOf("Gecko/");
137             String JavaDoc tmp=ua.substring(index+6,index+14);
138             long lg=Long.parseLong(tmp);
139             if(lg>=20030210){
140                 return true;
141             }
142         }
143         return false;
144     }
145 }
146
Popular Tags