KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.java;
31
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.jsp.TagInstance;
34 import com.caucho.vfs.WriteStream;
35 import com.caucho.xml.QName;
36 import com.caucho.xml.XmlChar;
37
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Represents a jsp:attribute node
42  */

43 public class JspAttribute extends JspFragmentNode {
44   private static final QName NAME = new QName("name");
45   private static final QName TRIM = new QName("trim");
46   
47   private QName _name;
48   private boolean _trim = true;
49   private TagInstance _tag;
50
51   private boolean _oldScriptingInvalid;
52
53   public JspAttribute()
54   {
55   }
56               
57   /**
58    * Returns the attribute name.
59    */

60   public QName getName()
61   {
62     return _name;
63   }
64
65   /**
66    * Returns true if trimming is enabled.
67    */

68   public boolean isTrim()
69   {
70     return _trim;
71   }
72   
73   /**
74    * Adds an attribute.
75    */

76   public void addAttribute(QName name, String JavaDoc value)
77     throws JspParseException
78   {
79     if (NAME.equals(name))
80       _name = _gen.getParseState().getQName(value);
81     else if (TRIM.equals(name))
82       _trim = value.equals("true");
83     else
84       throw error(L.l("`{0}' is an unknown attribute for jsp:attribute.",
85                       name));
86   }
87   
88   /**
89    * Called when the attributes end.
90    */

91   public void endAttributes()
92     throws JspParseException
93   {
94     _oldScriptingInvalid = _parseState.isScriptingInvalid();
95     // jsp/18di
96
// _parseState.setScriptingInvalid(true);
97

98     super.endAttributes();
99   }
100   
101   /**
102    * Adds a text node.
103    */

104   public JspNode addText(String JavaDoc text)
105     throws JspParseException
106   {
107     JspNode node = new StaticText(_gen, text, this);
108     
109     addChild(node);
110
111     return node;
112   }
113   
114   /**
115    * Adds an attribute.
116    */

117   public void endElement()
118     throws JspParseException
119   {
120     _parseState.setScriptingInvalid(_oldScriptingInvalid);
121     
122     if (_name == null)
123       throw error(L.l("jsp:attribute needs a `name' attribute."));
124
125     if (_trim) {
126       prefix_loop:
127       while (_children.size() > 0) {
128         JspNode node = (JspNode) _children.get(0);
129         
130         if (! (node instanceof StaticText))
131           break;
132
133         StaticText textNode = (StaticText) node;
134         
135         String JavaDoc text = textNode.getText();
136
137         for (int i = 0; i < text.length(); i++) {
138           if (! XmlChar.isWhitespace(text.charAt(i))) {
139             textNode.setText(text.substring(i));
140             break prefix_loop;
141           }
142         }
143
144         _children.remove(0);
145       }
146
147       suffix_loop:
148       while (_children.size() > 0) {
149         JspNode node = _children.get(_children.size() - 1);
150         
151         if (! (node instanceof StaticText))
152           break;
153
154         StaticText textNode = (StaticText) node;
155         
156         String JavaDoc text = textNode.getText();
157
158         for (int i = text.length() - 1; i >= 0; i--) {
159           if (! XmlChar.isWhitespace(text.charAt(i))) {
160             textNode.setText(text.substring(0, i + 1));
161             break suffix_loop;
162           }
163         }
164
165         _children.remove(_children.size() - 1);
166       }
167     }
168   }
169
170   /**
171    * Returns the root tag instance of the root.
172    */

173   public TagInstance getTag()
174   {
175     if (_tag == null)
176       _tag = new TagInstance(_gen.getTagManager());
177
178     return _tag;
179   }
180
181   /**
182    * Returns true if the children are static.
183    */

184   public boolean isStatic()
185   {
186     return isChildrenStatic();
187   }
188
189   /**
190    * Generates the XML text representation for the tag validation.
191    *
192    * @param os write stream to the generated XML.
193    */

194   public void printXml(WriteStream os)
195     throws IOException JavaDoc
196   {
197     os.print("<jsp:attribute name=\"" + _name + "\">");
198     printXmlChildren(os);
199     os.print("</jsp:attribute>");
200   }
201
202   /**
203    * Generates the prologue.
204    */

205   public void generatePrologue(JspJavaWriter out)
206     throws Exception JavaDoc
207   {
208     JspNode parent = getParent();
209
210     if (! isJspFragment()) {
211       generatePrologueChildren(out);
212       return;
213     }
214     
215     TagInstance parentTag = getParent().getTag();
216
217     if (parentTag == null || parentTag.getId() == null) {
218     }
219     else if (parentTag.isSimpleTag())
220       getTag().setId(TagInstance.FRAGMENT_WITH_SIMPLE_PARENT);
221     else
222       getTag().setId(TagInstance.FRAGMENT_WITH_TAG_PARENT);
223
224     super.generatePrologue(out);
225   }
226
227   /**
228    * Generates the fragment as a value.
229    */

230   String JavaDoc generateValue(Class JavaDoc type)
231     throws Exception JavaDoc
232   {
233     if (isStatic()) {
234       String JavaDoc text = getStaticText();
235
236       if (_trim)
237     text = text.trim();
238
239       return stringToValue(type, '"' + escapeJavaString(text) + '"');
240     }
241     else {
242       return stringToValue(type, generateValue());
243     }
244   }
245
246   /**
247    * Generates the code for a fragment.
248    */

249   protected String JavaDoc generateValue()
250     throws Exception JavaDoc
251   {
252     if (! isStatic())
253       return super.generateValue();
254     else if (_trim)
255       return '"' + escapeJavaString(getStaticText().trim()) + '"';
256     else
257       return '"' + escapeJavaString(getStaticText()) + '"';
258   }
259 }
260
Popular Tags