KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.L10N;
34 import com.caucho.vfs.WriteStream;
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 JspTop extends JspContainerNode implements JspSegmentNode {
45   private static final L10N L = new L10N(JspTop.class);
46
47   private boolean _hasRoot;
48   private int _maxFragmentIndex = -1;
49   private int _maxStaticFragmentIndex = -1;
50
51   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _namespaceMap = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
52
53   /**
54    * Adds a text node.
55    */

56   public JspNode addText(String JavaDoc text)
57     throws JspParseException
58   {
59     // jsp/0705
60
if (true || ! _hasRoot) {
61       JspNode node = new StaticText(_gen, text, this);
62
63       addChild(node);
64
65       return node;
66     }
67     else {
68       for (int i = 0; i < text.length(); i++)
69         if (! XmlChar.isWhitespace(text.charAt(i)))
70           throw error(L.l("JSP pages with <jsp:root> must not have text outside the <jsp:root>."));
71
72       return null;
73     }
74   }
75
76   /**
77    * Adds a child node.
78    */

79   public void addChild(JspNode child)
80     throws JspParseException
81   {
82     if (child instanceof JspRoot) {
83       _hasRoot = true;
84       _gen.setOmitXmlDeclaration(true);
85     }
86     
87     super.addChild(child);
88   }
89   
90   /**
91    * Set true if the node only has static text.
92    */

93   public boolean isStatic()
94   {
95     for (int i = 0; i < _children.size(); i++) {
96       JspNode node = _children.get(i);
97
98       if (! node.isStatic())
99         return false;
100     }
101
102     return true;
103   }
104
105   /**
106    * Returns the containing segment.
107    */

108   public JspSegmentNode getSegment()
109   {
110     return this;
111   }
112   
113   /**
114    * Returns the largest fragment index.
115    */

116   public int getMaxFragmentIndex()
117   {
118     return _maxFragmentIndex;
119   }
120   
121   /**
122    * Sets the largest fragment index.
123    */

124   public void setMaxFragmentIndex(int index)
125   {
126     if (_maxFragmentIndex < index)
127       _maxFragmentIndex = index;
128   }
129   
130   /**
131    * Returns the largest static fragment index.
132    */

133   public int getMaxStaticFragmentIndex()
134   {
135     return _maxStaticFragmentIndex;
136   }
137   
138   /**
139    * Sets the largest static fragment index.
140    */

141   public void setMaxStaticFragmentIndex(int index)
142   {
143     if (_maxStaticFragmentIndex < index)
144       _maxStaticFragmentIndex = index;
145   }
146
147   /**
148    * Adds a namespace, e.g. from a prefix declaration.
149    */

150   public void addNamespaceRec(String JavaDoc prefix, String JavaDoc value)
151   {
152     _namespaceMap.put(prefix, value);
153   }
154
155   /**
156    * Generates the XML text representation for the tag validation.
157    *
158    * @param os write stream to the generated XML.
159    */

160   public void printXml(WriteStream os)
161     throws IOException JavaDoc
162   {
163     if (_hasRoot) {
164       printXmlChildren(os);
165       return;
166     }
167     
168     /*
169     os.print("<jsp:root xmlns:jsp=\"http://java.sun.com/JSP/Page\">");
170     printXmlChildren(os);
171     os.print("</jsp:root>");
172     */

173     os.print("<jsp:root");
174     printJspId(os);
175     os.print(" version=\"2.0\"");
176     os.print(" xmlns:jsp=\"http://java.sun.com/JSP/Page\"");
177
178     for (Map.Entry JavaDoc entry : _namespaceMap.entrySet()) {
179       os.print(" xmlns:" + entry.getKey() + "=\"" + entry.getValue() + "\"");
180     }
181     os.print(">");
182     printXmlChildren(os);
183     os.print("</jsp:root>");
184   }
185
186   /**
187    * Returns true if the namespace decl has been printed.
188    */

189   public boolean hasNamespace(String JavaDoc prefix, String JavaDoc uri)
190   {
191     if ("".equals(uri) && ("".equals(prefix) || prefix == null))
192       return true;
193     else
194       return false;
195   }
196
197   /**
198    * Generates the code for the tag
199    *
200    * @param out the output writer for the generated java.
201    */

202   public void generate(JspJavaWriter out)
203     throws Exception JavaDoc
204   {
205     if (! _hasRoot) {
206       if (! _gen.isOmitXmlDeclaration()) {
207     String JavaDoc encoding = _gen.getCharacterEncoding();
208
209     if (encoding == null)
210       encoding = "UTF-8";
211
212     out.addText("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
213       }
214
215       if (_gen.getDoctypeSystem() != null) {
216     out.addText("<!DOCTYPE ");
217     out.addText(_gen.getDoctypeRootElement());
218
219     if (_gen.getDoctypePublic() != null) {
220       out.addText(" PUBLIC \"");
221       out.addText(_gen.getDoctypePublic());
222       out.addText("\" \"");
223       out.addText(_gen.getDoctypeSystem());
224       out.addText("\"");
225     }
226     else {
227       out.addText(" SYSTEM \"");
228       out.addText(_gen.getDoctypeSystem());
229       out.addText("\"");
230     }
231       
232     out.addText(">\n");
233       }
234     }
235     
236     generateChildren(out);
237   }
238 }
239
Popular Tags