001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.renderer;
042:
043: import com.sun.rave.web.ui.component.Markup;
044: import com.sun.rave.web.ui.util.RenderingUtilities;
045:
046: import java.io.IOException;
047: import java.util.Iterator;
048: import java.util.Map;
049: import java.util.Set;
050:
051: import javax.faces.component.UIComponent;
052:
053: import javax.faces.context.FacesContext;
054: import javax.faces.context.ResponseWriter;
055:
056: /**
057: * <p>This class is responsible for rendering any type of XML tag markup. </p>
058: */
059: public class MarkupRenderer extends AbstractRenderer {
060:
061: // -------------------------------------------------------- Static Variables
062:
063: /**
064: * <p>The set of String pass-through attributes to be rendered.</p>
065: */
066: // no pass throughs.
067: // -------------------------------------------------------- Renderer Methods
068: public boolean getRendersChildren() {
069: return true;
070: }
071:
072: /**
073: * <p>Render the start of an Link (Link) tag.</p>
074: * @param context <code>FacesContext</code> for the current request
075: * @param component <code>UIComponent</code> to be rendered
076: * @param writer <code>ResponseWriter</code> to which the element
077: * start should be rendered
078: * @exception IOException if an input/output error occurs
079: */
080: protected void renderStart(FacesContext context,
081: UIComponent component, ResponseWriter writer)
082: throws IOException {
083: //intentionally empty
084: Markup markup = (Markup) component;
085:
086: String tagName = markup.getTag();
087:
088: if (tagName == null)
089: return; //TODO: write out log message
090:
091: if (!markup.isSingleton()) {
092: // Markup is a singleton
093: writeInsides(markup, context, writer);
094: }
095: }
096:
097: /**
098: * <p>Render the attributes for the Markup. </p>
099: * @param context <code>FacesContext</code> for the current request
100: * @param component <code>UIComponent</code> to be rendered
101: * @param writer <code>ResponseWriter</code> to which the element
102: * attributes should be rendered
103: * @exception IOException if an input/output error occurs
104: */
105: protected void renderAttributes(FacesContext context,
106: UIComponent component, ResponseWriter writer)
107: throws IOException {
108: Markup markup = (Markup) component;
109:
110: }
111:
112: public void encodeChildren(FacesContext context,
113: UIComponent component) throws IOException {
114: Markup markup = (Markup) component;
115:
116: if (!markup.isSingleton()) {
117: super .encodeChildren(context, component);
118: }
119:
120: }
121:
122: /**
123: * <p>Write out the Markup.</p>
124: * @param context <code>FacesContext</code> for the current request
125: * @param component <code>UIComponent</code> to be rendered
126: * @param writer <code>ResponseWriter</code> to which the element
127: * end should be rendered
128: * @exception IOException if an input/output error occurs
129: */
130: protected void renderEnd(FacesContext context,
131: UIComponent component, ResponseWriter writer)
132: throws IOException {
133: // End the appropriate element
134:
135: Markup markup = (Markup) component;
136:
137: String tagName = markup.getTag();
138:
139: if (tagName == null)
140: return; //TODO: write out log message
141:
142: if (markup.isSingleton()) {
143: // Markup is a singleton
144: writeInsides(markup, context, writer);
145: }
146: writer.endElement(tagName);
147: }
148:
149: // --------------------------------------------------------- Private Methods
150:
151: private void writeInsides(Markup markup, FacesContext context,
152: ResponseWriter writer) throws IOException {
153: String tagName = markup.getTag();
154: writer.startElement(tagName, markup);
155: writeId(markup, context, writer);
156: writeStyles(markup, context, writer);
157: writeExtraAttributes(markup, context, writer);
158: }
159:
160: private void writeId(Markup markup, FacesContext context,
161: ResponseWriter writer) throws IOException {
162:
163: String id = markup.getClientId(context);
164: if (id != null) {
165: writer.writeAttribute("id", id, null); //NO18N
166: }
167: }
168:
169: private void writeStyles(Markup markup, FacesContext context,
170: ResponseWriter writer) throws IOException {
171:
172: String style = markup.getStyle();
173: String styleClass = markup.getStyleClass();
174: if (style != null) {
175: writer.writeAttribute("style", style, null); //NO18N
176: }
177:
178: if (styleClass != null) {
179: writer.writeAttribute("class", styleClass, "styleClass"); //NO18N
180: }
181: }
182:
183: private void writeExtraAttributes(Markup markup,
184: FacesContext context, ResponseWriter writer)
185: throws IOException {
186:
187: String extra = markup.getExtraAttributes();
188: if (extra != null) {
189: RenderingUtilities.renderExtraHtmlAttributes(writer, extra);
190: }
191:
192: Map attributes = markup.getAttributes();
193:
194: Set keys = attributes.keySet();
195:
196: Iterator listOfKeys = keys.iterator();
197:
198: while (listOfKeys.hasNext()) {
199: String key = (String) listOfKeys.next();
200: Object value = attributes.get(key);
201:
202: // Only take strings (their is a private arraylist that I need to
203: // avoid
204:
205: if (value != null && value instanceof String) {
206: writer.writeAttribute(key, value, null);
207: }
208: }
209: }
210: }
|