/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.renderer.bridge;
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xulfaces.annotation.faces.RENDERER;
import org.xulfaces.bridge.Bridge;
import org.xulfaces.bridge.Command;
import org.xulfaces.bridge.Parameter;
import org.xulfaces.component.bridge.BridgeComponent;
import org.xulfaces.renderer.XULRenderer;
import org.xulfaces.renderkit.KitConstants;
import org.xulfaces.utils.XulUtils;
/**
*
* @author kito31
* @version $Id: BridgeRenderer.java,v 1.12 2007/07/21 12:00:35 kito31 Exp $
*/
@RENDERER(kit = "XUL_RENDERKIT", family = "xul.component.family", type = "xul.renderer.Bridge")
public class BridgeRenderer extends XULRenderer {
private static final Log log = LogFactory.getLog(BridgeRenderer.class);
@Override
public void encodeEnd(FacesContext facesContext, UIComponent component)
throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
BridgeComponent bridgeComponent = (BridgeComponent) component;
renderXFC(facesContext, bridgeComponent);
renderJSInclude(facesContext, bridgeComponent,
KitConstants.XFBRIDGE_JS_FILE_PARENT_DIR,
"logger.js");
renderJSInclude(facesContext, bridgeComponent,
KitConstants.XFBRIDGE_JS_FILE_PARENT_DIR,
KitConstants.XFBRIDGE_JS_FILE_NAME);
Boolean debug = bridgeComponent.getDebug();
if (debug != null) {
if (debug.booleanValue()) {
responseWriter.startElement("script", bridgeComponent);
responseWriter.writeAttribute("type", "application/x-javascript", "type");
responseWriter.write("XUL_FACES_BRIDGE.logger.setLevelWriter(Logger.DEBUG_LEVEL, new AlertWriter());");
responseWriter.endElement("script");
}
}
}
private void renderJSInclude(FacesContext facesContext,
BridgeComponent bridgeComponent, String parentDir, String resource)
throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
HttpServletRequest servletRequest = (HttpServletRequest) facesContext
.getExternalContext().getRequest();
String protocol = servletRequest.getProtocol();
if (protocol.contains(new String("HTTPS"))) {
protocol = "https://";
} else {
protocol = "http://";
}
String js = bridgeComponent.getJs();
StringBuffer urlBuffer = new StringBuffer();
urlBuffer.append(servletRequest.getContextPath());
if (js != null) {
urlBuffer.append(js);
} else {
urlBuffer.append("/");
urlBuffer.append(parentDir);
urlBuffer.append("/");
urlBuffer.append(resource);
}
if (log.isDebugEnabled()) {
log.debug("Include Javascript " + urlBuffer.toString());
}
responseWriter.startElement("script", bridgeComponent);
responseWriter.writeAttribute("src", urlBuffer, null);
responseWriter.writeAttribute("type", "application/x-javascript", null);
responseWriter.endElement("script");
}
private void renderXFC(FacesContext facesContext,
BridgeComponent bridgeComponent) throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
Bridge bridge = XulUtils.getBridge();
responseWriter.startElement("xfc:bridge", bridgeComponent);
for (Command command : bridge.getCommands()) {
StringBuffer stringBuffer = new StringBuffer("xfc:");
stringBuffer.append(command.getName());
if (log.isDebugEnabled()) {
log.debug("Command " + stringBuffer.toString() + " on "
+ command.getTarget());
}
responseWriter.startElement(stringBuffer.toString(),
bridgeComponent);
for (Parameter parameter : command.getParameters()) {
if (log.isDebugEnabled()) {
log.debug("parameter " + parameter.getName() + " value "
+ parameter.getValue());
}
responseWriter.writeAttribute(parameter.getName(), parameter
.getValue().toString(), null);
}
responseWriter.endElement(stringBuffer.toString());
}
facesContext.getApplication().getViewHandler().writeState(facesContext);
responseWriter.endElement("xfc:bridge");
bridge.getCommands().clear();
}
}