01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * 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;
19: import java.io.Reader;
20: import java.io.UnsupportedEncodingException;
21: import java.net.URLEncoder;
22: import java.security.AccessController;
23:
24: import javax.servlet.jsp.JspException;
25: import javax.servlet.jsp.tagext.BodyTagSupport;
26:
27: import sun.security.action.GetPropertyAction;
28:
29: /**
30: * ÓÃÓÚ±àÂëURL×Ö´®µÄ±êÇ©¿â
31: * @author Liudong
32: */
33: public class URLEncoderTag extends BodyTagSupport {
34:
35: /* (non-Javadoc)
36: * @see javax.servlet.jsp.tagext.Tag#doStartTag()
37: */
38: public int doStartTag() throws JspException {
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 {
46: Reader reader = getBodyContent().getReader();
47: char[] buf = new char[32];
48: try {
49: StringBuffer content = new StringBuffer(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 e0) {
59: }
60: return EVAL_PAGE;
61: }
62:
63: /**
64: * URL×Ö·û±àÂë
65: * @param url
66: * @return
67: * @throws UnsupportedEncodingException
68: */
69: public String encodeURL(String url)
70: throws UnsupportedEncodingException {
71: String dfltEncName = (String) AccessController
72: .doPrivileged(new GetPropertyAction("file.encoding"));
73: return URLEncoder.encode(url, dfltEncName);
74: }
75: }
|