XmlFileTag.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Michel-Ange ANTON
00022  * --------------------------------------------------------------------------
00023  * $Id: XmlFileTag.java,v 1.3 2003/06/20 17:15:42 antonma Exp $
00024  * --------------------------------------------------------------------------
00025  */
00026 
00027 package org.objectweb.jonas.webapp.taglib;
00028 
00029 import java.io.IOException;
00030 import java.util.StringTokenizer;
00031 
00032 import javax.servlet.jsp.JspException;
00033 import javax.servlet.jsp.JspWriter;
00034 import javax.servlet.jsp.tagext.BodyTagSupport;
00035 
00036 public class XmlFileTag extends BodyTagSupport {
00037 
00038 // ----------------------------------------------------- Constants
00039     private static final int NONE = 0;
00040     private static final int HEADER = 1;
00041     private static final int ELEMENT = 2;
00042     private static final int ELEMENT_AUTO_CLOSE = 3;
00043     private static final int ELEMENT_CLOSE = 4;
00044     private static final int COMMENT = 5;
00045     private static final int TEXT = 6;
00046 // ----------------------------------------------------- Instance Variables
00047 
00048     protected String m_Body = null;
00049     protected int m_LastPrint = NONE;
00050     protected int m_Indent = 0;
00051 
00052 // ------------------------------------------------------------- Properties
00053 
00054 // --------------------------------------------------------- Public Methods
00055 
00061     public int doEndTag()
00062         throws JspException {
00063         JspWriter out = pageContext.getOut();
00064         try {
00065             String sXml = null;
00066             // Get body
00067             if (m_Body != null) {
00068                 sXml = m_Body;
00069                 m_Body = null;
00070             }
00071             // Display
00072             if (sXml != null) {
00073                 m_LastPrint = NONE;
00074                 m_Indent = 0;
00075                 render(out, sXml, 0, sXml.length());
00076             }
00077         }
00078         catch (IOException e) {
00079             throw new JspException(e);
00080         }
00081         return (EVAL_PAGE);
00082     }
00083 
00084     public int doAfterBody()
00085         throws JspException {
00086         String sBody = bodyContent.getString();
00087         if (sBody != null) {
00088             sBody = sBody.trim();
00089             if (sBody.length() > 0) {
00090                 this.m_Body = sBody;
00091             }
00092         }
00093         return (SKIP_BODY);
00094     }
00095 
00099     public void release() {
00100         m_Body = null;
00101     }
00102 
00103 // -------------------------------------------------------- Protected Methods
00104 
00105     protected void render(JspWriter p_Out, String p_Xml, int p_Begin, int p_End)
00106         throws IOException, JspException {
00107 
00108         int iCurrent = p_Begin;
00109         int iBegin;
00110         int iEnd;
00111         String sElement;
00112         String sElementWithAttr;
00113         String sName;
00114         char cType;
00115 
00116         for (; ; ) {
00117             // Find begin element
00118             iBegin = p_Xml.indexOf("<", iCurrent);
00119             if ((iBegin == -1) || (iBegin >= p_End)) {
00120                 break;
00121             }
00122             // Find end element
00123             iEnd = p_Xml.indexOf(">", iBegin);
00124             if (iEnd == -1) {
00125                 break;
00126             }
00127             // render normal text
00128             if (iBegin > iCurrent) {
00129                 String sText = p_Xml.substring(iCurrent, iBegin);
00130                 sText = sText.trim();
00131                 if (sText.length() > 0) {
00132                     printText(p_Out, sText);
00133                 }
00134             }
00135 
00136             // Get complete element
00137             sElement = p_Xml.substring(iBegin, iEnd + 4);
00138             // Detect special element (doctype, header, comment)
00139             cType = sElement.charAt(4);
00140             if ((cType == '!') || (cType == '?')) {
00141                 // Special element (doctype, header, comment)
00142                 if (cType == '!') {
00143                     // Detect comment
00144                     cType = sElement.charAt(5);
00145                     if (cType == '-') {
00146                         printComment(p_Out, sElement);
00147                     }
00148                     else {
00149                         printHeader(p_Out, sElement);
00150                     }
00151                 }
00152                 else {
00153                     printHeader(p_Out, sElement);
00154                 }
00155                 // Next
00156                 iCurrent = iEnd + 4;
00157             }
00158             else {
00159                 // get name of element
00160                 sElementWithAttr = sElement.substring(4, sElement.length() - 4);
00161                 sName = getNameElement(sElementWithAttr);
00162                 // Detect end element
00163                 if (sElementWithAttr.charAt(0) == '/') {
00164                     // Next
00165                     iCurrent = iEnd + 4;
00166                     break;
00167                 }
00168                 // Detect auto-end element
00169                 else if (sElementWithAttr.charAt(sElementWithAttr.length() - 1) == '/') {
00170                     printElementAutoClose(p_Out, sElement);
00171                     // Next
00172                     iCurrent = iEnd + 4;
00173                 }
00174                 else {
00175                     // Render element
00176                     printElement(p_Out, sElement);
00177                     // Search close element
00178                     String sCloseElement = "</" + sName + ">";
00179                     int iPosCloseElement = p_Xml.indexOf(sCloseElement, iEnd);
00180                     if (iPosCloseElement == -1) {
00181                         // Error
00182                         break;
00183                     }
00184                     // render children element (or text)
00185                     render(p_Out, p_Xml, iEnd + 4, iPosCloseElement + sCloseElement.length());
00186                     // render end element
00187                     printElementClose(p_Out, sCloseElement);
00188                     // Next
00189                     iCurrent = iPosCloseElement + sCloseElement.length();
00190                 }
00191             }
00192         }
00193     }
00194 
00195     protected void printElement(JspWriter p_Out, String p_Print)
00196         throws IOException, JspException {
00197         switch (m_LastPrint) {
00198             case NONE:
00199             case TEXT:
00200                 break;
00201             case HEADER:
00202             case ELEMENT:
00203             case ELEMENT_AUTO_CLOSE:
00204             case ELEMENT_CLOSE:
00205             case COMMENT:
00206                 p_Out.print("<br>");
00207                 break;
00208         }
00209 
00210         printIndent(p_Out, m_Indent);
00211 
00212         p_Out.print("<span class=\"xmlElement\">");
00213         p_Out.print(p_Print);
00214         p_Out.print("</span>");
00215 
00216         m_LastPrint = ELEMENT;
00217         m_Indent++;
00218     }
00219 
00220     protected void printElementAutoClose(JspWriter p_Out, String p_Print)
00221         throws IOException, JspException {
00222 
00223         switch (m_LastPrint) {
00224             case NONE:
00225             case TEXT:
00226                 break;
00227             case HEADER:
00228             case ELEMENT:
00229             case ELEMENT_AUTO_CLOSE:
00230             case ELEMENT_CLOSE:
00231             case COMMENT:
00232                 p_Out.print("<br>");
00233                 break;
00234         }
00235         printIndent(p_Out, m_Indent);
00236         p_Out.print("<span class=\"xmlElement\">");
00237         p_Out.print(p_Print);
00238         p_Out.print("</span>");
00239 
00240         m_LastPrint = ELEMENT_AUTO_CLOSE;
00241     }
00242 
00243     protected void printElementClose(JspWriter p_Out, String p_Print)
00244         throws IOException, JspException {
00245 
00246         m_Indent--;
00247 
00248         switch (m_LastPrint) {
00249             case NONE:
00250             case ELEMENT:
00251             case TEXT:
00252                 break;
00253             case HEADER:
00254             case ELEMENT_AUTO_CLOSE:
00255             case ELEMENT_CLOSE:
00256             case COMMENT:
00257                 p_Out.print("<br>");
00258                 printIndent(p_Out, m_Indent);
00259                 break;
00260         }
00261         p_Out.print("<span class=\"xmlElement\">");
00262         p_Out.print(p_Print);
00263         p_Out.print("</span>");
00264 
00265         m_LastPrint = ELEMENT_CLOSE;
00266     }
00267 
00268     protected void printComment(JspWriter p_Out, String p_Print)
00269         throws IOException, JspException {
00270         switch (m_LastPrint) {
00271             case NONE:
00272                 break;
00273             case HEADER:
00274             case ELEMENT:
00275             case ELEMENT_AUTO_CLOSE:
00276             case ELEMENT_CLOSE:
00277             case COMMENT:
00278             case TEXT:
00279                 p_Out.print("<br>");
00280                 break;
00281         }
00282         printIndent(p_Out, m_Indent);
00283         p_Out.print("<span class=\"xmlComment\">");
00284         p_Out.print(p_Print);
00285         p_Out.print("</span>");
00286 
00287         m_LastPrint = COMMENT;
00288     }
00289 
00290     protected void printHeader(JspWriter p_Out, String p_Print)
00291         throws IOException, JspException {
00292         switch (m_LastPrint) {
00293             case NONE:
00294                 break;
00295             case HEADER:
00296             case ELEMENT:
00297             case ELEMENT_AUTO_CLOSE:
00298             case ELEMENT_CLOSE:
00299             case COMMENT:
00300             case TEXT:
00301                 p_Out.print("<br>");
00302                 break;
00303         }
00304 
00305         p_Out.print("<span class=\"xmlHeader\">");
00306         p_Out.print(p_Print);
00307         p_Out.print("</span>");
00308 
00309         m_LastPrint = HEADER;
00310     }
00311 
00312     protected void printText(JspWriter p_Out, String p_Print)
00313         throws IOException, JspException {
00314 
00315         p_Out.print("<span class=\"xmlText\">");
00316         p_Out.print(p_Print);
00317         p_Out.print("</span>");
00318 
00319         m_LastPrint = TEXT;
00320     }
00321 
00322     protected void printIndent(JspWriter p_Out, int p_Indent)
00323         throws IOException, JspException {
00324         for (int i = 0; i < p_Indent; i++) {
00325             p_Out.print("&nbsp;&nbsp;");
00326         }
00327     }
00328 
00329     protected String getNameElement(String p_ElementWithAttr) {
00330         StringTokenizer st = new StringTokenizer(p_ElementWithAttr, " ");
00331         return st.nextToken();
00332     }
00333 
00334 }

Generated on Tue Feb 15 15:05:34 2005 for JOnAS by  doxygen 1.3.9.1