KickJava   Java API By Example, From Geeks To Geeks.

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


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.vfs.WriteStream;
33 import com.caucho.xml.QName;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashSet JavaDoc;
38
39 /**
40  * Represents a JSP implicit element
41  */

42 public class JspXmlElement extends JspContainerNode {
43   private ArrayList JavaDoc<QName> _attrNames = new ArrayList JavaDoc<QName>();
44   private ArrayList JavaDoc<String JavaDoc> _attrValues = new ArrayList JavaDoc<String JavaDoc>();
45
46   /**
47    * Adds an attribute.
48    */

49   public void addAttribute(QName name, String JavaDoc value)
50     throws JspParseException
51   {
52     _attrNames.add(name);
53     _attrValues.add(value);
54   }
55
56   /**
57    * Adds an attribute.
58    */

59   public void addAttribute(QName name, JspAttribute value)
60     throws JspParseException
61   {
62     throw error(L.l("jsp:attribute '{0}' is not allowed as a child of an XML element.",
63             name.getName()));
64   }
65
66   /**
67    * Returns true if the namespace decl has been printed.
68    */

69   public boolean hasNamespace(String JavaDoc prefix, String JavaDoc uri)
70   {
71     QName name = getQName();
72
73     if (prefix == null || uri == null)
74       return true;
75     else if (prefix.equals(name.getPrefix()) &&
76          uri.equals(name.getNamespaceURI()))
77       return true;
78     else
79       return _parent.hasNamespace(prefix, uri);
80   }
81
82   /**
83    * Generates the XML text representation for the tag validation.
84    *
85    * @param os write stream to the generated XML.
86    */

87   public void printXml(WriteStream os)
88     throws IOException JavaDoc
89   {
90     os.print("<" + getTagName());
91
92     for (int i = 0; i < _attrNames.size(); i++) {
93       QName name = _attrNames.get(i);
94       String JavaDoc value = _attrValues.get(i);
95       
96       os.print(" " + name.getName() + "=\"");
97       
98       printXmlText(os, value);
99       
100       os.print("\"");
101     }
102
103     os.print(">");
104
105     printXmlChildren(os);
106
107     os.print("</" + getTagName() + ">");
108   }
109
110   /**
111    * Generates the code for the element.
112    *
113    * @param out the output writer for the generated java.
114    */

115   public void generate(JspJavaWriter out)
116     throws Exception JavaDoc
117   {
118     out.addText("<");
119     out.addText(getTagName());
120
121     QName qName = getQName();
122
123     HashSet JavaDoc<String JavaDoc> prefixes = new HashSet JavaDoc<String JavaDoc>();
124     
125     if (qName.getNamespaceURI() != null &&
126     ! _parent.hasNamespace(qName)) {
127       prefixes.add(qName.getPrefix());
128       
129       out.addText(" ");
130       if (qName.getPrefix() == null || qName.getPrefix().equals(""))
131     out.addText("xmlns=\"");
132       else
133     out.addText("xmlns:" + qName.getPrefix() + "=\"");
134       out.addText(qName.getNamespaceURI());
135       out.addText("\"");
136     }
137
138     for (int i = 0; i < _attrNames.size(); i++) {
139       QName name = _attrNames.get(i);
140       String JavaDoc value = _attrValues.get(i);
141
142       if (name.getNamespaceURI() != null &&
143       ! prefixes.contains(name.getPrefix()) &&
144       ! _parent.hasNamespace(name)) {
145     prefixes.add(name.getPrefix());
146     out.addText(" ");
147     if (name.getPrefix() == null || name.getPrefix().equals(""))
148       out.addText("xmlns=\"");
149     else
150       out.addText("xmlns:" + name.getPrefix() + "=\"");
151     out.addText(name.getNamespaceURI());
152     out.addText("\"");
153       }
154       
155       out.addText(" ");
156       out.addText(name.getName());
157
158       if (value == null || value.equals("")) {
159     // XXX: possibly differ for html/text
160

161     out.addText("=\"\"");
162       }
163       else {
164     out.addText("=\"");
165     
166     if (value.indexOf("${") < 0 &&
167         ! value.startsWith("<%") &&
168         ! value.startsWith("%")) {
169       out.addText(value);
170     }
171     else {
172       String JavaDoc javaValue = generateParameterValue(String JavaDoc.class, value,
173                             true, null);
174       out.println("out.print(" + javaValue + ");");
175     }
176         out.addText("\"");
177       }
178     }
179
180     if (getChildren() != null && getChildren().size() > 0) {
181       out.addText(">");
182
183       generateChildren(out);
184
185       out.addText("</" + getTagName() + ">");
186     }
187     else
188       out.addText(" />");
189   }
190 }
191
Popular Tags