1 2 23 package com.geinuke.common; 24 25 import java.io.StringWriter ; 26 27 import javax.servlet.http.HttpServletRequest ; 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 object=null; 42 protected HttpServletRequest req=null; 43 public EditorFactory(HttpServletRequest req,Object object){ 44 this.req=req; 45 this.isCompatible=isCompatible(req); 46 this.object=object; 47 } 48 49 50 51 protected String merge(String fileName,Context ctx){ 52 String html=null; 53 String templateName=null; 54 Template t=null; 55 templateName="html/"+fileName; 56 StringWriter sw = new StringWriter (1024 * 4); 57 try { 58 t = Velocity.getTemplate(templateName); 59 t.merge(ctx, sw); 60 } catch (Exception e) { 61 e.printStackTrace(); 62 } 63 html=sw.toString(); 64 return html; 65 } 66 67 public String getEditor(String instanceName, String value, String width, String height){ 68 String 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 84 tmp=this.merge("fckeditor1.vm",ctx); 85 } 86 else{ 87 String widthCSS=width; 88 String 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 101 } 102 return tmp; 103 } 104 105 public String getQuote(String aut,String text){ 106 String res=null; 107 VelocityContext ctx=new VelocityContext(); 109 ctx.put("LOCALE",NukeResource.getLocale(this.req,null)); 111 GeiServlet.fill(ctx); 113 ctx.put("w_writer",aut); 115 ctx.put("t_text",text); 117 res=this.merge("quote.vm",ctx); 119 return res; 121 } 122 123 protected boolean isCompatible(HttpServletRequest req){ 124 125 String 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 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 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
|