1 25 26 package org.objectweb.jonas.webapp.taglib; 27 28 import java.io.IOException ; 29 import java.util.StringTokenizer ; 30 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.JspWriter ; 33 import javax.servlet.jsp.tagext.BodyTagSupport ; 34 35 38 public class XmlFileTag extends BodyTagSupport { 39 40 private static final int NONE = 0; 42 private static final int HEADER = 1; 43 private static final int ELEMENT = 2; 44 private static final int ELEMENT_AUTO_CLOSE = 3; 45 private static final int ELEMENT_CLOSE = 4; 46 private static final int COMMENT = 5; 47 private static final int TEXT = 6; 48 50 protected String m_Body = null; 51 protected int m_LastPrint = NONE; 52 protected int m_Indent = 0; 53 54 protected boolean comment = false; 56 57 59 61 66 public int doEndTag() 67 throws JspException { 68 JspWriter out = pageContext.getOut(); 69 try { 70 String sXml = null; 71 if (m_Body != null) { 73 sXml = m_Body; 74 m_Body = null; 75 } 76 if (sXml != null) { 78 m_LastPrint = NONE; 79 m_Indent = 0; 80 render(out, sXml, 0, sXml.length()); 81 } 82 } 83 catch (IOException e) { 84 throw new JspException (e); 85 } 86 return (EVAL_PAGE); 87 } 88 89 public int doAfterBody() 90 throws JspException { 91 String sBody = bodyContent.getString(); 92 if (sBody != null) { 93 sBody = sBody.trim(); 94 if (sBody.length() > 0) { 95 this.m_Body = sBody; 96 } 97 } 98 return (SKIP_BODY); 99 } 100 101 104 public void release() { 105 m_Body = null; 106 } 107 108 110 protected void render(JspWriter p_Out, String p_Xml, int p_Begin, int p_End) 111 throws IOException , JspException { 112 113 int iCurrent = p_Begin; 114 int iBegin; 115 int iEnd; 116 String sElement; 117 String sElementWithAttr; 118 String sName; 119 char cType; 120 121 for (; ; ) { 122 iBegin = p_Xml.indexOf("<", iCurrent); 124 if ((iBegin == -1) || (iBegin >= p_End)) { 125 break; 126 } 127 iEnd = p_Xml.indexOf(">", iBegin); 129 if (iEnd == -1) { 130 break; 131 } 132 if (iBegin > iCurrent) { 134 String sText = p_Xml.substring(iCurrent, iBegin); 135 sText = sText.trim(); 136 if (sText.length() > 0) { 137 printText(p_Out, sText); 138 } 139 } 140 141 sElement = p_Xml.substring(iBegin, iEnd + 4); 143 cType = sElement.charAt(4); 145 if ((cType == '!') || (cType == '?')) { 146 if (cType == '!') { 148 cType = sElement.charAt(5); 150 if (cType == '-') { 151 iEnd = p_Xml.indexOf("-->", iBegin); 153 if (iEnd == -1) { 154 break; 155 } 156 iEnd = iEnd +2; 158 159 sElement = p_Xml.substring(iBegin, iEnd + 4); 161 162 printComment(p_Out, sElement); 163 } 164 else { 165 printHeader(p_Out, sElement); 166 } 167 } 168 else { 169 printHeader(p_Out, sElement); 170 } 171 iCurrent = iEnd + 4; 173 } 174 else { 175 sElementWithAttr = sElement.substring(4, sElement.length() - 4); 177 sName = getNameElement(sElementWithAttr); 178 if (sElementWithAttr.charAt(0) == '/') { 180 iCurrent = iEnd + 4; 182 break; 183 } 184 else if (sElementWithAttr.charAt(sElementWithAttr.length() - 1) == '/') { 186 printElementAutoClose(p_Out, sElement); 187 iCurrent = iEnd + 4; 189 } 190 else { 191 printElement(p_Out, sElement); 193 String sCloseElement = "</" + sName + ">"; 195 int iPosCloseElement = p_Xml.indexOf(sCloseElement, iEnd); 196 if (iPosCloseElement == -1) { 197 break; 199 } 200 render(p_Out, p_Xml, iEnd + 4, iPosCloseElement + sCloseElement.length()); 202 printElementClose(p_Out, sCloseElement); 204 iCurrent = iPosCloseElement + sCloseElement.length(); 206 } 207 } 208 } 209 } 210 211 protected void printElement(JspWriter p_Out, String p_Print) 212 throws IOException , JspException { 213 switch (m_LastPrint) { 214 case NONE: 215 case TEXT: 216 break; 217 case HEADER: 218 case ELEMENT: 219 case ELEMENT_AUTO_CLOSE: 220 case ELEMENT_CLOSE: 221 case COMMENT: 222 p_Out.print("<br>"); 223 break; 224 } 225 226 printIndent(p_Out, m_Indent); 227 228 if (!comment) 230 p_Out.print("<span class=\"xmlElement\">"); 231 232 p_Out.print(p_Print); 233 234 if (!comment) 236 p_Out.print("</span>"); 237 238 m_LastPrint = ELEMENT; 239 m_Indent++; 240 } 241 242 protected void printElementAutoClose(JspWriter p_Out, String p_Print) 243 throws IOException , JspException { 244 245 switch (m_LastPrint) { 246 case NONE: 247 case TEXT: 248 break; 249 case HEADER: 250 case ELEMENT: 251 case ELEMENT_AUTO_CLOSE: 252 case ELEMENT_CLOSE: 253 case COMMENT: 254 p_Out.print("<br>"); 255 break; 256 } 257 printIndent(p_Out, m_Indent); 258 if (!comment) 260 p_Out.print("<span class=\"xmlElement\">"); 261 262 p_Out.print(p_Print); 263 264 if (!comment) 266 p_Out.print("</span>"); 267 268 m_LastPrint = ELEMENT_AUTO_CLOSE; 269 } 270 271 protected void printElementClose(JspWriter p_Out, String p_Print) 272 throws IOException , JspException { 273 274 m_Indent--; 275 276 switch (m_LastPrint) { 277 case NONE: 278 case ELEMENT: 279 case TEXT: 280 break; 281 case HEADER: 282 case ELEMENT_AUTO_CLOSE: 283 case ELEMENT_CLOSE: 284 case COMMENT: 285 p_Out.print("<br>"); 286 printIndent(p_Out, m_Indent); 287 break; 288 } 289 if (!comment) 291 p_Out.print("<span class=\"xmlElement\">"); 292 293 p_Out.print(p_Print); 294 295 if (!comment) 297 p_Out.print("</span>"); 298 299 m_LastPrint = ELEMENT_CLOSE; 300 } 301 302 protected void printComment(JspWriter p_Out, String p_Print) 303 throws IOException , JspException { 304 switch (m_LastPrint) { 305 case NONE: 306 break; 307 case HEADER: 308 case ELEMENT: 309 case ELEMENT_AUTO_CLOSE: 310 case ELEMENT_CLOSE: 311 case COMMENT: 312 case TEXT: 313 p_Out.print("<br>"); 314 break; 315 } 316 printIndent(p_Out, m_Indent); 317 p_Out.print("<span class=\"xmlComment\">"); 318 319 p_Print = p_Print.substring(7,p_Print.length()-6).trim(); 321 p_Out.print("<!--"); 323 324 if (p_Print.indexOf("/>")==-1){ 326 p_Out.print(p_Print); 328 } else { 329 setComment(true); 331 render(p_Out, p_Print, 0, p_Print.length()-1); 333 p_Out.print("<br>"); 334 setComment(false); 336 } 337 338 printIndent(p_Out, m_Indent); 340 p_Out.print("-->"); 341 342 p_Out.print("</span>"); 343 344 m_LastPrint = COMMENT; 345 } 346 347 protected void printHeader(JspWriter p_Out, String p_Print) 348 throws IOException , JspException { 349 switch (m_LastPrint) { 350 case NONE: 351 break; 352 case HEADER: 353 case ELEMENT: 354 case ELEMENT_AUTO_CLOSE: 355 case ELEMENT_CLOSE: 356 case COMMENT: 357 case TEXT: 358 p_Out.print("<br>"); 359 break; 360 } 361 if (!comment) 363 p_Out.print("<span class=\"xmlHeader\">"); 364 365 p_Out.print(p_Print); 366 367 if (!comment) 369 p_Out.print("</span>"); 370 371 m_LastPrint = HEADER; 372 } 373 374 protected void printText(JspWriter p_Out, String p_Print) 375 throws IOException , JspException { 376 377 if (!comment) 379 p_Out.print("<span class=\"xmlText\">"); 380 381 p_Out.print(p_Print); 382 383 if (!comment) 385 p_Out.print("</span>"); 386 387 m_LastPrint = TEXT; 388 } 389 390 protected void printIndent(JspWriter p_Out, int p_Indent) 391 throws IOException , JspException { 392 for (int i = 0; i < p_Indent; i++) { 393 p_Out.print(" "); 394 } 395 } 396 397 protected String getNameElement(String p_ElementWithAttr) { 398 StringTokenizer st = new StringTokenizer (p_ElementWithAttr, " "); 399 return st.nextToken(); 400 } 401 402 protected void setComment(boolean value){ 403 comment = value; 405 } 406 407 } 408 | Popular Tags |