KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > util > tags > URLEncoderTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.util.tags;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Reader JavaDoc;
20 import java.io.UnsupportedEncodingException JavaDoc;
21 import java.net.URLEncoder JavaDoc;
22 import java.security.AccessController JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 import sun.security.action.GetPropertyAction;
28
29 /**
30  * ÓÃÓÚ±àÂëURL×Ö´®µÄ±êÇ©¿â
31  * @author Liudong
32  */

33 public class URLEncoderTag extends BodyTagSupport JavaDoc {
34
35     /* (non-Javadoc)
36      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
37      */

38     public int doStartTag() throws JspException JavaDoc {
39         return EVAL_BODY_BUFFERED;
40     }
41
42     /* (non-Javadoc)
43      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
44      */

45     public int doEndTag() throws JspException JavaDoc {
46         Reader JavaDoc reader = getBodyContent().getReader();
47         char[] buf = new char[32];
48         try{
49             StringBuffer JavaDoc content = new StringBuffer JavaDoc(32);
50             do{
51                 int rc = reader.read(buf);
52                 if(rc>0)
53                     content.append(buf,0,rc);
54                 if(rc<32)
55                     break;
56             }while(true);
57             pageContext.getOut().write(encodeURL(content.toString()));
58         }catch(IOException JavaDoc e0){
59         }
60         return EVAL_PAGE;
61     }
62     /**
63      * URL×Ö·û±àÂë
64      * @param url
65      * @return
66      * @throws UnsupportedEncodingException
67      */

68     public String JavaDoc encodeURL(String JavaDoc url) throws UnsupportedEncodingException JavaDoc {
69         String JavaDoc dfltEncName = (String JavaDoc)AccessController.doPrivileged(new GetPropertyAction("file.encoding"));
70         return URLEncoder.encode(url,dfltEncName);
71     }
72 }
73
Popular Tags