KickJava   Java API By Example, From Geeks To Geeks.

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


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.L10N;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.QName;
35 import com.caucho.xml.XmlChar;
36
37 import java.io.IOException JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Represents the root node.
43  */

44 public class JspRoot extends JspContainerNode {
45   static final L10N L = new L10N(JspRoot.class);
46
47   static final private QName VERSION = new QName("version");
48
49   private String JavaDoc _version;
50
51   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _namespaceMap = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
52
53   /**
54    * Sets the versino.
55    */

56   public void setVersion(String JavaDoc version)
57   {
58     _version = version;
59   }
60   
61   /**
62    * Adds an attribute.
63    *
64    * @param name the attribute name
65    * @param value the attribute value
66    */

67   public void addAttribute(QName name, String JavaDoc value)
68     throws JspParseException
69   {
70     if (VERSION.equals(name)) {
71       if (! value.equals("2.1")
72       && ! value.equals("2.0")
73       && ! value.equals("1.2"))
74     throw error(L.l("'{0}' is an unsupported jsp:root version.",
75             value));
76
77       _version = value;
78     }
79     else {
80       throw error(L.l("'{0}' is an unknown jsp:root attribute. 'version' is the only allowed JSP root value.",
81                       name.getName()));
82     }
83   }
84
85   /**
86    * Adds a text node.
87    */

88   public JspNode addText(String JavaDoc text)
89     throws JspParseException
90   {
91     for (int i = 0; i < text.length(); i++) {
92       if (! XmlChar.isWhitespace(text.charAt(i))) {
93     JspNode node = new StaticText(_gen, text, this);
94     
95         addChild(node);
96     
97         return node;
98       }
99     }
100
101     return null;
102   }
103
104   /**
105    * Called after all the attributes from the tag.
106    */

107   public void endAttributes()
108     throws JspParseException
109   {
110     _gen.setOmitXmlDeclaration(true);
111
112     if (getParent() != null && ! (getParent() instanceof JspTop))
113       throw error(L.l("jsp:root must be the root JSP element."));
114
115     if (_version == null)
116       throw error(L.l("'version' is a required attribute of jsp:root"));
117   }
118   
119   /**
120    * Adds a child node.
121    */

122   public void addChild(JspNode node)
123     throws JspParseException
124   {
125     super.addChild(node);
126   }
127
128   /**
129    * Adds a namespace, e.g. from a prefix declaration.
130    */

131   public void addNamespaceRec(String JavaDoc prefix, String JavaDoc value)
132   {
133     _namespaceMap.put(prefix, value);
134   }
135   
136   /**
137    * Set true if the node only has static text.
138    */

139   public boolean isStatic()
140   {
141     for (int i = 0; i < _children.size(); i++) {
142       JspNode node = _children.get(i);
143
144       if (! node.isStatic())
145         return false;
146     }
147
148     return true;
149   }
150
151   /**
152    * Generates the XML text representation for the tag validation.
153    *
154    * @param os write stream to the generated XML.
155    */

156   public void printXml(WriteStream os)
157     throws IOException JavaDoc
158   {
159     os.print("<jsp:root xmlns:jsp=\"http://java.sun.com/JSP/Page\"");
160
161     for (Map.Entry JavaDoc entry : _namespaceMap.entrySet()) {
162       os.print(" xmlns:" + entry.getKey() + "=\"" + entry.getValue() + "\"");
163     }
164     
165     printJspId(os);
166     os.print(" version=\"2.0\"");
167     
168     os.print(">");
169     printXmlChildren(os);
170     os.print("</jsp:root>");
171   }
172
173   /**
174    * Generates the code for the tag
175    *
176    * @param out the output writer for the generated java.
177    */

178   public void generate(JspJavaWriter out)
179     throws Exception JavaDoc
180   {
181     if (! _gen.isOmitXmlDeclaration()) {
182       String JavaDoc encoding = _gen.getCharacterEncoding();
183
184       if (encoding == null)
185     encoding = "UTF-8";
186
187       out.addText("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
188     }
189
190     if (_gen.getDoctypeSystem() != null) {
191       out.addText("<!DOCTYPE ");
192       out.addText(_gen.getDoctypeRootElement());
193
194       if (_gen.getDoctypePublic() != null) {
195     out.addText(" PUBLIC \"");
196     out.addText(_gen.getDoctypePublic());
197     out.addText("\" \"");
198     out.addText(_gen.getDoctypeSystem());
199     out.addText("\"");
200       }
201       else {
202     out.addText(" SYSTEM \"");
203     out.addText(_gen.getDoctypeSystem());
204     out.addText("\"");
205       }
206       
207       out.addText(">\n");
208     }
209     
210     generateChildren(out);
211   }
212 }
213
Popular Tags