KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspContainerNode


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.jsp.JspParseException;
32 import com.caucho.util.CharBuffer;
33 import com.caucho.util.L10N;
34 import com.caucho.util.LineCompileException;
35 import com.caucho.vfs.WriteStream;
36 import com.caucho.xml.QName;
37 import com.caucho.xml.XmlChar;
38
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * A node which can contain other nodes.
44  */

45 public abstract class JspContainerNode extends JspNode {
46   static final L10N L = new L10N(JspContainerNode.class);
47   
48   protected ArrayList JavaDoc<QName> _attributeNames = new ArrayList JavaDoc<QName>();
49   protected ArrayList JavaDoc<Object JavaDoc> _attributeValues = new ArrayList JavaDoc<Object JavaDoc>();
50   
51   protected ArrayList JavaDoc<JspNode> _children = new ArrayList JavaDoc<JspNode>();
52
53   protected boolean _hasJspAttribute;
54   
55   /**
56    * Adds an attribute.
57    *
58    * @param name the name of the attribute.
59    * @param value the value of the attribute.
60    */

61   public void addAttribute(QName name, String JavaDoc value)
62     throws JspParseException
63   {
64     if (name == null)
65       throw new NullPointerException JavaDoc();
66
67     if (_attributeNames.indexOf(name) >= 0)
68       throw error(L.l("'{0}' is a duplicate attribute name. Attributes must occur only once in a tag.", name.getName()));
69     
70     _attributeNames.add(name);
71     _attributeValues.add(value);
72   }
73
74   /**
75    * Adds a JspAttribute attribute.
76    *
77    * @param name the name of the attribute.
78    * @param value the value of the attribute.
79    */

80   public void addAttribute(QName name, JspAttribute value)
81     throws JspParseException
82   {
83     if (name == null)
84       throw new NullPointerException JavaDoc();
85     
86     if (_attributeNames.indexOf(name) >= 0)
87       throw error(L.l("'{0}' is a duplicate attribute name. Attributes must occur only once in a tag.", name.getName()));
88     
89     _attributeNames.add(name);
90     _attributeValues.add(value);
91
92     _gen.addFragment(value);
93   }
94
95   /**
96    * Returns the named attribute.
97    */

98   public Object JavaDoc getAttribute(String JavaDoc name)
99     throws JspParseException
100   {
101     for (int i = 0; i < _attributeNames.size(); i++) {
102       if (name.equals(_attributeNames.get(i).getName()))
103         return _attributeValues.get(i);
104     }
105
106     return null;
107   }
108   
109   /**
110    * Adds a child node.
111    */

112   public void addChild(JspNode node)
113     throws JspParseException
114   {
115     node.setParent(this);
116     
117     if (node instanceof JspAttribute) {
118       JspAttribute attr = (JspAttribute) node;
119
120       QName name = attr.getName();
121
122       addAttribute(name, attr);
123
124       _hasJspAttribute = true;
125
126       int i = 0;
127       while (_children != null && i < _children.size()) {
128         JspNode child = _children.get(i);
129
130         if (child instanceof StaticText) {
131           String JavaDoc text = ((StaticText) child).getText();
132
133           if (isWhitespace(text))
134             _children.remove(i);
135           else
136             throw child.error(L.l("tags using jsp:attribute must put body content in a jsp:body tag"));
137         }
138         else if (child instanceof JspBody)
139           i++;
140         else if (child instanceof JspAttribute)
141           i++;
142         else {
143           throw child.error(L.l("tags using jsp:attribute must put body content in a jsp:body tag"));
144     }
145       }
146     }
147     else if (_hasJspAttribute && ! (node instanceof JspBody)) {
148       throw node.error(L.l("tags using jsp:attribute must put body content in a jsp:body tag"));
149     }
150     else if (node instanceof StaticText) {
151       StaticText text = (StaticText) node;
152       String JavaDoc data = text.getText();
153
154       boolean isXml = _gen.isXml();
155       for (JspNode ptr = this; ptr != null; ptr = ptr.getParent()) {
156         if (ptr instanceof JspRoot)
157           isXml = true;
158       }
159
160       if (_children == null)
161         _children = new ArrayList JavaDoc<JspNode>();
162       
163       for (int i = 0;
164            i < data.length();
165            i++) {
166         if (! XmlChar.isWhitespace(data.charAt(i))) {
167           _children.add(node);
168           return;
169         }
170       }
171
172       if (! isXml)
173         _children.add(node);
174     }
175     else {
176       if (_children == null)
177         _children = new ArrayList JavaDoc<JspNode>();
178
179       _children.add(node);
180     }
181   }
182   
183   /**
184    * Adds a child node after its done initializing.
185    */

186   public void addChildEnd(JspNode node)
187     throws JspParseException
188   {
189   }
190
191   /**
192    * Returns true if the node is empty
193    */

194   public boolean isEmpty()
195   {
196     if (_children == null || _children.size() == 0)
197       return true;
198
199     for (int i = 0; i < _children.size(); i++) {
200       JspNode child = _children.get(i);
201
202       if (child instanceof JspBody) {
203     JspBody body = (JspBody) child;
204
205     return body.isEmpty();
206       }
207       else if (child instanceof StaticText) {
208     StaticText text = (StaticText) child;
209
210     if (! text.isWhitespace())
211       return false;
212       }
213       else
214     return false;
215     }
216
217     return false;
218   }
219
220   /**
221    * Returns the static text.
222    */

223   public void getStaticText(CharBuffer cb)
224   {
225     if (_children == null)
226       return;
227     
228     for (int i = 0; i < _children.size(); i++)
229       _children.get(i).getStaticText(cb);
230   }
231   
232   /**
233    * Set true if the node contains a child tag.
234    */

235   public boolean hasCustomTag()
236   {
237     for (int i = 0; _children != null && i < _children.size(); i++) {
238       JspNode child = _children.get(i);
239
240       if (child instanceof CustomTag)
241         return true;
242
243       if (child.hasCustomTag())
244         return true;
245     }
246     
247     for (int i = 0;
248          _attributeValues != null && i < _attributeValues.size();
249          i++) {
250       Object JavaDoc value = _attributeValues.get(i);
251
252       if (value instanceof CustomTag)
253         return true;
254       else if (value instanceof JspNode &&
255                ((JspNode) value).hasCustomTag())
256         return true;
257     }
258
259     return false;
260   }
261   
262   /**
263    * Set true if the node contains a child tag.
264    */

265   public boolean hasTag()
266   {
267     for (int i = 0; _children != null && i < _children.size(); i++) {
268       JspNode child = _children.get(i);
269
270       if (child instanceof CustomTag ||
271       child instanceof CustomSimpleTag)
272         return true;
273
274       if (child.hasTag())
275         return true;
276     }
277     
278     for (int i = 0;
279          _attributeValues != null && i < _attributeValues.size();
280          i++) {
281       Object JavaDoc value = _attributeValues.get(i);
282
283       if (value instanceof CustomTag || value instanceof CustomSimpleTag)
284         return true;
285
286       else if (value instanceof JspNode &&
287                ((JspNode) value).hasTag())
288         return true;
289     }
290
291     return false;
292   }
293   
294   /**
295    * Adds a text node.
296    */

297   public JspNode addText(String JavaDoc text)
298     throws JspParseException
299   {
300     if (! _hasJspAttribute) {
301       JspNode node = new StaticText(_gen, text, this);
302       
303       addChild(node);
304
305       return node;
306     }
307     else if (! isWhitespace(text)) {
308       throw error(L.l("tags using jsp:attribute must put body content in a jsp:body tag"));
309     }
310
311     return null;
312   }
313
314   protected boolean isWhitespace(String JavaDoc text)
315   {
316     for (int i = 0; i < text.length(); i++) {
317       if (! Character.isWhitespace(text.charAt(i)))
318         return false;
319     }
320
321     return true;
322   }
323
324   /**
325    * Returns the children.
326    */

327   public ArrayList JavaDoc<JspNode> getChildren()
328   {
329     return _children;
330   }
331
332   /**
333    * Has children.
334    */

335   public boolean hasChildren()
336   {
337     return ! isEmpty();
338   }
339   
340   /**
341    * True if the node has scripting
342    */

343   public boolean hasScripting()
344   {
345     for (int i = 0; _children != null && i < _children.size(); i++) {
346       if (_children.get(i).hasScripting())
347     return true;
348     }
349     
350     return false;
351   }
352
353   /**
354    * Returns true if the children are static.
355    */

356   public boolean isChildrenStatic()
357   {
358     if (_children == null)
359       return true;
360     
361     for (int i = 0; i < _children.size(); i++) {
362       if (! _children.get(i).isStatic())
363     return false;
364     }
365
366     return true;
367   }
368
369   /**
370    * Generates the XML text representation for the tag validation.
371    *
372    * @param os write stream to the generated XML.
373    */

374   public void printXmlChildren(WriteStream os)
375     throws IOException JavaDoc
376   {
377     if (_children == null)
378       return;
379
380     for (int i = 0; i < _children.size(); i++) {
381       JspNode child = _children.get(i);
382
383       child.printXml(os);
384     }
385   }
386
387   /**
388    * generates data for prologue children.
389    */

390   public void generatePrologueChildren(JspJavaWriter out)
391     throws Exception JavaDoc
392   {
393     if (_children == null)
394       return;
395
396     for (int i = 0; i < _children.size(); i++) {
397       JspNode child = _children.get(i);
398
399       child.generatePrologue(out);
400     }
401   }
402
403   /**
404    * generates data for declaration children.
405    */

406   public void generateDeclarationChildren(JspJavaWriter out)
407     throws IOException JavaDoc
408   {
409     if (_children == null)
410       return;
411
412     for (int i = 0; i < _children.size(); i++) {
413       JspNode child = _children.get(i);
414
415       child.generateDeclaration(out);
416     }
417   }
418
419   /**
420    * Generates the code for the children.
421    *
422    * @param out the output writer for the generated java.
423    */

424   public void generateChildren(JspJavaWriter out)
425     throws Exception JavaDoc
426   {
427     if (_children == null)
428       return;
429
430     for (int i = 0; i < _children.size(); i++) {
431       JspNode child = _children.get(i);
432
433       child.generateStartLocation(out);
434       try {
435         child.generate(out);
436       } catch (Exception JavaDoc e) {
437         if (e instanceof LineCompileException)
438           throw e;
439         else
440           throw child.error(e);
441       }
442       child.generateEndLocation(out);
443     }
444   }
445
446   /**
447    * Generates the code for the children.
448    *
449    * @param out the output writer for the generated java.
450    */

451   public void generateChildrenEmpty()
452     throws Exception JavaDoc
453   {
454     if (_children == null)
455       return;
456
457     for (int i = 0; i < _children.size(); i++) {
458       JspNode child = _children.get(i);
459
460       child.generateEmpty();
461     }
462   }
463
464   /**
465    * Generates static text.
466    *
467    * @param out the output writer for the generated java.
468    */

469   public void generateStatic(JspJavaWriter out)
470     throws Exception JavaDoc
471   {
472     if (_children == null)
473       return;
474
475     for (int i = 0; i < _children.size(); i++) {
476       JspNode child = _children.get(i);
477
478       out.setLocation(child.getFilename(), child.getStartLine());
479       child.generateStatic(out);
480       // out.setLocation(child.getFilename(), child.getEndLine());
481
}
482   }
483 }
484
485
Popular Tags