1 25 26 package net.killingar.forum.tags; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.JspTagException ; 30 import javax.servlet.jsp.JspWriter ; 31 import javax.servlet.jsp.tagext.BodyContent ; 32 import javax.servlet.jsp.tagext.BodyTagSupport ; 33 34 public class PreTag extends BodyTagSupport 35 { 36 private String nohtml = "false"; 37 38 public int doStartTag() throws JspException 39 { 40 return EVAL_BODY_TAG; 41 } 42 43 public int doEndTag() 44 { 45 return EVAL_PAGE; 46 } 47 48 public int doAfterBody() throws JspTagException 49 { 50 BodyContent bc = getBodyContent(); 51 String query = bc.getString(); 53 bc.clearBody(); 55 56 try 57 { 58 printPre(bodyContent.getEnclosingWriter(), query, nohtml.equals("true")); 59 } 60 catch (Exception e) 61 { 62 throw new JspTagException ("PreTag exception: " + e); 63 } 64 65 return SKIP_BODY; 66 } 67 68 72 73 public void setNohtml(String nohtml) 74 { 75 this.nohtml = nohtml; 76 } 77 78 public static void printPre(JspWriter out, String s, boolean noHtml) throws java.io.IOException 79 { 80 if (s == null)return; 81 82 char[] sa = s.toCharArray(); 83 84 for (int i = 0; i < sa.length; i++) 85 { 86 if (sa[i] == '\n') 87 { 88 out.println("<br />"); 89 for (; i < sa.length-1 && (sa[i+1] == ' ' || sa[i+1] == '\t'); i++) 90 out.print(sa[i+1] == '\t'? " " : " "); 91 } 92 else if (i < sa.length-5 && sa[i] == '&' && sa[i+1] == 'q' && sa[i+2] == 'u' && sa[i+3] == 'o' && sa[i+4] == 't' && sa[i+5] == ';') 93 { 94 i+=5; 95 out.print("\""); 96 } 97 else if (sa[i] == '<') 98 { 99 if (noHtml) 100 out.print("<"); 101 else 102 out.print('<'); 103 } 104 else if (sa[i] == '>') 105 { 106 if (noHtml) 107 out.print(">"); 108 else 109 out.print('>'); 110 } 111 else if (sa[i] == '&') 112 { 113 if (noHtml) 114 out.print("&"); 115 else 116 out.print('&'); 117 } 118 else 119 out.print(sa[i]); 120 } 121 } 122 } 123 | Popular Tags |