KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > taglib > XmlFileTag


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: XmlFileTag.java,v 1.4 2005/03/24 13:35:10 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.taglib;
27
28 import java.io.IOException JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.JspWriter JavaDoc;
33 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
34
35 /**
36  * @author Michel-Ange ANTON
37  */

38 public class XmlFileTag extends BodyTagSupport JavaDoc {
39
40 // ----------------------------------------------------- Constants
41
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 // ----------------------------------------------------- Instance Variables
49

50     protected String JavaDoc m_Body = null;
51     protected int m_LastPrint = NONE;
52     protected int m_Indent = 0;
53
54     //are we printing XML comment content
55
protected boolean comment = false;
56
57 // ------------------------------------------------------------- Properties
58

59 // --------------------------------------------------------- Public Methods
60

61     /**
62      * Render this instant actions control.
63      *
64      * @exception JspException if a processing error occurs
65      */

66     public int doEndTag()
67         throws JspException JavaDoc {
68         JspWriter JavaDoc out = pageContext.getOut();
69         try {
70             String JavaDoc sXml = null;
71             // Get body
72
if (m_Body != null) {
73                 sXml = m_Body;
74                 m_Body = null;
75             }
76             // Display
77
if (sXml != null) {
78                 m_LastPrint = NONE;
79                 m_Indent = 0;
80                 render(out, sXml, 0, sXml.length());
81             }
82         }
83         catch (IOException JavaDoc e) {
84             throw new JspException JavaDoc(e);
85         }
86         return (EVAL_PAGE);
87     }
88
89     public int doAfterBody()
90         throws JspException JavaDoc {
91         String JavaDoc 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     /**
102      * Release all state information set by this tag.
103      */

104     public void release() {
105         m_Body = null;
106     }
107
108 // -------------------------------------------------------- Protected Methods
109

110     protected void render(JspWriter JavaDoc p_Out, String JavaDoc p_Xml, int p_Begin, int p_End)
111         throws IOException JavaDoc, JspException JavaDoc {
112
113         int iCurrent = p_Begin;
114         int iBegin;
115         int iEnd;
116         String JavaDoc sElement;
117         String JavaDoc sElementWithAttr;
118         String JavaDoc sName;
119         char cType;
120
121         for (; ; ) {
122             // Find begin element
123
iBegin = p_Xml.indexOf("<", iCurrent);
124             if ((iBegin == -1) || (iBegin >= p_End)) {
125                 break;
126             }
127             // Find end element
128
iEnd = p_Xml.indexOf(">", iBegin);
129             if (iEnd == -1) {
130                 break;
131             }
132             // render normal text
133
if (iBegin > iCurrent) {
134                 String JavaDoc sText = p_Xml.substring(iCurrent, iBegin);
135                 sText = sText.trim();
136                 if (sText.length() > 0) {
137                     printText(p_Out, sText);
138                 }
139             }
140
141             // Get complete element
142
sElement = p_Xml.substring(iBegin, iEnd + 4);
143             // Detect special element (doctype, header, comment)
144
cType = sElement.charAt(4);
145             if ((cType == '!') || (cType == '?')) {
146                 // Special element (doctype, header, comment)
147
if (cType == '!') {
148                     // Detect comment
149
cType = sElement.charAt(5);
150                     if (cType == '-') {
151                         // Find actual end element - if we are dealing with comment
152
iEnd = p_Xml.indexOf("-->", iBegin);
153                         if (iEnd == -1) {
154                             break;
155                         }
156                         // set counter on proper value
157
iEnd = iEnd +2;
158
159                         // Get complete element
160
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                 // Next
172
iCurrent = iEnd + 4;
173             }
174             else {
175                 // get name of element
176
sElementWithAttr = sElement.substring(4, sElement.length() - 4);
177                 sName = getNameElement(sElementWithAttr);
178                 // Detect end element
179
if (sElementWithAttr.charAt(0) == '/') {
180                     // Next
181
iCurrent = iEnd + 4;
182                     break;
183                 }
184                 // Detect auto-end element
185
else if (sElementWithAttr.charAt(sElementWithAttr.length() - 1) == '/') {
186                     printElementAutoClose(p_Out, sElement);
187                     // Next
188
iCurrent = iEnd + 4;
189                 }
190                 else {
191                     // Render element
192
printElement(p_Out, sElement);
193                     // Search close element
194
String JavaDoc sCloseElement = "</" + sName + ">";
195                     int iPosCloseElement = p_Xml.indexOf(sCloseElement, iEnd);
196                     if (iPosCloseElement == -1) {
197                         // Error
198
break;
199                     }
200                     // render children element (or text)
201
render(p_Out, p_Xml, iEnd + 4, iPosCloseElement + sCloseElement.length());
202                     // render end element
203
printElementClose(p_Out, sCloseElement);
204                     // Next
205
iCurrent = iPosCloseElement + sCloseElement.length();
206                 }
207             }
208         }
209     }
210
211     protected void printElement(JspWriter JavaDoc p_Out, String JavaDoc p_Print)
212         throws IOException JavaDoc, JspException JavaDoc {
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         // are we printing comment
229
if (!comment)
230             p_Out.print("<span class=\"xmlElement\">");
231
232         p_Out.print(p_Print);
233
234         // are we printing comment
235
if (!comment)
236             p_Out.print("</span>");
237
238         m_LastPrint = ELEMENT;
239         m_Indent++;
240     }
241
242     protected void printElementAutoClose(JspWriter JavaDoc p_Out, String JavaDoc p_Print)
243         throws IOException JavaDoc, JspException JavaDoc {
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         //are we printing comment
259
if (!comment)
260             p_Out.print("<span class=\"xmlElement\">");
261
262         p_Out.print(p_Print);
263
264         // are we printing comment
265
if (!comment)
266             p_Out.print("</span>");
267
268         m_LastPrint = ELEMENT_AUTO_CLOSE;
269     }
270
271     protected void printElementClose(JspWriter JavaDoc p_Out, String JavaDoc p_Print)
272         throws IOException JavaDoc, JspException JavaDoc {
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         // are we printing comment
290
if (!comment)
291             p_Out.print("<span class=\"xmlElement\">");
292
293         p_Out.print(p_Print);
294
295         // are we printing comment
296
if (!comment)
297             p_Out.print("</span>");
298
299         m_LastPrint = ELEMENT_CLOSE;
300     }
301
302     protected void printComment(JspWriter JavaDoc p_Out, String JavaDoc p_Print)
303         throws IOException JavaDoc, JspException JavaDoc {
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         // take only comment content
320
p_Print = p_Print.substring(7,p_Print.length()-6).trim();
321         // start printing commented element
322
p_Out.print("&lt;!--");
323
324         // are we dealing with commented element
325
if (p_Print.indexOf("/&gt;")==-1){
326             // print simple comment text
327
p_Out.print(p_Print);
328         } else {
329             // set comment property value - disables <span/> elements
330
setComment(true);
331             // print commented element
332
render(p_Out, p_Print, 0, p_Print.length()-1);
333             p_Out.print("<br>");
334             // turn back comment property value - enable <span/> elements again
335
setComment(false);
336         }
337
338         // print comment ending
339
printIndent(p_Out, m_Indent);
340         p_Out.print("--&gt;");
341
342         p_Out.print("</span>");
343
344         m_LastPrint = COMMENT;
345     }
346
347     protected void printHeader(JspWriter JavaDoc p_Out, String JavaDoc p_Print)
348         throws IOException JavaDoc, JspException JavaDoc {
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         // are we printing comment
362
if (!comment)
363             p_Out.print("<span class=\"xmlHeader\">");
364
365         p_Out.print(p_Print);
366
367         // are we printing comment
368
if (!comment)
369             p_Out.print("</span>");
370
371         m_LastPrint = HEADER;
372     }
373
374     protected void printText(JspWriter JavaDoc p_Out, String JavaDoc p_Print)
375         throws IOException JavaDoc, JspException JavaDoc {
376
377         // are we printing comment
378
if (!comment)
379             p_Out.print("<span class=\"xmlText\">");
380
381         p_Out.print(p_Print);
382
383         // are we printing comment
384
if (!comment)
385             p_Out.print("</span>");
386
387         m_LastPrint = TEXT;
388     }
389
390     protected void printIndent(JspWriter JavaDoc p_Out, int p_Indent)
391         throws IOException JavaDoc, JspException JavaDoc {
392         for (int i = 0; i < p_Indent; i++) {
393             p_Out.print("&nbsp;&nbsp;");
394         }
395     }
396
397     protected String JavaDoc getNameElement(String JavaDoc p_ElementWithAttr) {
398         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(p_ElementWithAttr, " ");
399         return st.nextToken();
400     }
401
402     protected void setComment(boolean value){
403         // are we printing comment (enable/disable <span/> elements)
404
comment = value;
405     }
406
407 }
408
Popular Tags