001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.jsp.JspParseException;
032: import com.caucho.vfs.WriteStream;
033: import com.caucho.xml.QName;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037: import java.util.HashSet;
038:
039: /**
040: * Represents a JSP implicit element
041: */
042: public class JspXmlElement extends JspContainerNode {
043: private ArrayList<QName> _attrNames = new ArrayList<QName>();
044: private ArrayList<String> _attrValues = new ArrayList<String>();
045:
046: /**
047: * Adds an attribute.
048: */
049: public void addAttribute(QName name, String value)
050: throws JspParseException {
051: _attrNames.add(name);
052: _attrValues.add(value);
053: }
054:
055: /**
056: * Adds an attribute.
057: */
058: public void addAttribute(QName name, JspAttribute value)
059: throws JspParseException {
060: throw error(L
061: .l(
062: "jsp:attribute '{0}' is not allowed as a child of an XML element.",
063: name.getName()));
064: }
065:
066: /**
067: * Returns true if the namespace decl has been printed.
068: */
069: public boolean hasNamespace(String prefix, String uri) {
070: QName name = getQName();
071:
072: if (prefix == null || uri == null)
073: return true;
074: else if (prefix.equals(name.getPrefix())
075: && uri.equals(name.getNamespaceURI()))
076: return true;
077: else
078: return _parent.hasNamespace(prefix, uri);
079: }
080:
081: /**
082: * Generates the XML text representation for the tag validation.
083: *
084: * @param os write stream to the generated XML.
085: */
086: public void printXml(WriteStream os) throws IOException {
087: os.print("<" + getTagName());
088:
089: for (int i = 0; i < _attrNames.size(); i++) {
090: QName name = _attrNames.get(i);
091: String value = _attrValues.get(i);
092:
093: os.print(" " + name.getName() + "=\"");
094:
095: printXmlText(os, value);
096:
097: os.print("\"");
098: }
099:
100: os.print(">");
101:
102: printXmlChildren(os);
103:
104: os.print("</" + getTagName() + ">");
105: }
106:
107: /**
108: * Generates the code for the element.
109: *
110: * @param out the output writer for the generated java.
111: */
112: public void generate(JspJavaWriter out) throws Exception {
113: out.addText("<");
114: out.addText(getTagName());
115:
116: QName qName = getQName();
117:
118: HashSet<String> prefixes = new HashSet<String>();
119:
120: if (qName.getNamespaceURI() != null
121: && !_parent.hasNamespace(qName)) {
122: prefixes.add(qName.getPrefix());
123:
124: out.addText(" ");
125: if (qName.getPrefix() == null
126: || qName.getPrefix().equals(""))
127: out.addText("xmlns=\"");
128: else
129: out.addText("xmlns:" + qName.getPrefix() + "=\"");
130: out.addText(qName.getNamespaceURI());
131: out.addText("\"");
132: }
133:
134: for (int i = 0; i < _attrNames.size(); i++) {
135: QName name = _attrNames.get(i);
136: String value = _attrValues.get(i);
137:
138: if (name.getNamespaceURI() != null
139: && !prefixes.contains(name.getPrefix())
140: && !_parent.hasNamespace(name)) {
141: prefixes.add(name.getPrefix());
142: out.addText(" ");
143: if (name.getPrefix() == null
144: || name.getPrefix().equals(""))
145: out.addText("xmlns=\"");
146: else
147: out.addText("xmlns:" + name.getPrefix() + "=\"");
148: out.addText(name.getNamespaceURI());
149: out.addText("\"");
150: }
151:
152: out.addText(" ");
153: out.addText(name.getName());
154:
155: if (value == null || value.equals("")) {
156: // XXX: possibly differ for html/text
157:
158: out.addText("=\"\"");
159: } else {
160: out.addText("=\"");
161:
162: if (value.indexOf("${") < 0 && !value.startsWith("<%")
163: && !value.startsWith("%")) {
164: out.addText(value);
165: } else {
166: String javaValue = generateParameterValue(
167: String.class, value, true, null,
168: _parseState.isELIgnored());
169: out.println("out.print(" + javaValue + ");");
170: }
171: out.addText("\"");
172: }
173: }
174:
175: if (getChildren() != null && getChildren().size() > 0) {
176: out.addText(">");
177:
178: generateChildren(out);
179:
180: out.addText("</" + getTagName() + ">");
181: } else
182: out.addText(" />");
183: }
184: }
|