KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > statement > EscapeSQLTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.dbtags.statement;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.servlet.jsp.JspTagException JavaDoc;
21 import javax.servlet.jsp.JspWriter JavaDoc;
22 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
23
24 /**
25  * <p>JSP tag escapesql, replaces each single quote in the tag body
26  * with a pair of single quotes.</p>
27  *
28  * <p>JSP Tag Lib Descriptor
29  * <pre>
30  * &lt;name>escapeSql&lt;/name>
31  * &lt;tagclass>org.apache.taglibs.dbtags.statement.EscapeSQLTag&lt;/tagclass>
32  * &lt;bodycontent>JSP&lt;/bodycontent>
33  * &lt;info>Replaces each single quote in the tag body
34  * with a pair of single quotes. Body content will not be trimmed.&lt;/info>
35  * </pre>
36  *
37  * @author Morgan Delagrange
38  */

39 public class EscapeSQLTag extends BodyTagSupport JavaDoc {
40     
41   public int doEndTag() throws JspTagException JavaDoc {
42     String JavaDoc unescaped = getBodyContent().getString();
43     char[] unescapedChars = new char[unescaped.length()];
44     unescaped.getChars(0,unescaped.length(),unescapedChars,0);
45     JspWriter JavaDoc output = pageContext.getOut();
46
47     try {
48       for (int i = 0; i < unescapedChars.length; ++i) {
49         switch (unescapedChars[i]) {
50         case ('\''):
51           output.print('\'');
52         default:
53           output.print(unescapedChars[i]);
54         }
55       }
56     } catch (IOException JavaDoc e) {
57       throw new JspTagException JavaDoc(e.toString());
58     }
59
60     return EVAL_PAGE;
61   }
62   
63 }
64
Popular Tags