/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: XmlDisplayWindowRenderer.java 2048 2006-12-12 09:58:39Z drmlipp $
*
* $Log$
* Revision 1.5 2006/11/01 16:15:33 mlipp
* Make our JSF components depend on tomahawk only.
*
* Revision 1.4 2006/10/06 21:53:38 mlipp
* Handling null data.
*
* Revision 1.3 2006/10/06 21:02:52 mlipp
* Improved.
*
* Revision 1.2 2006/10/06 15:27:14 drmlipp
* Improved.
*
* Revision 1.1 2006/10/05 13:27:39 drmlipp
* New tag for XML display.
*
*/
package de.danet.an.util.jsf.taglib;
import java.io.IOException;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import org.apache.myfaces.renderkit.html.util.AddResource;
import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
import de.danet.an.util.jsf.JSFUtil;
/**
* This class provides a renderer for the HtmlLinkCssStylesheet component.
*
* @author Michael Lipp
*/
public class XmlDisplayWindowRenderer extends Renderer {
private static String SCRIPT_RENDERED
= XmlDisplayWindowRenderer.class.getName() + "_SCRIPT_RENDERED";
/* (non-Javadoc)
* @see javax.faces.render.Renderer#encodeBegin
*/
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
XmlDisplayWindow comp = (XmlDisplayWindow)component;
ResponseWriter writer = context.getResponseWriter();
JSLoading.ensureInclusion(context, JSLoading.XMLSAX);
JSLoading.ensureInclusion(context, JSLoading.XML2HTML);
AddResource addResource = AddResourceFactory.getInstance(context);
String xmlDataVar = JavascriptUtils.getValidJavascriptName(
comp.getClientId(context)+"xmlDataVar",false);
String urlPrefix = addResource.getResourceUri
(context, new MyFacesResourceHandler(XmlDisplayWindow.class, ""));
urlPrefix = context.getExternalContext().encodeResourceURL(urlPrefix);
writer.startElement(HTML.SCRIPT_ELEM, null);
writer.writeAttribute
(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
String xmlData = (String)comp.getXmlData();
if (xmlData == null) {
xmlData = "";
}
xmlData = xmlData.replaceAll("\\r", "");
xmlData = xmlData.replaceAll("\\\\", "\\\\\\\\");
xmlData = xmlData.replaceAll("\"", "\\\\\"");
writer.write("var " + xmlDataVar + "=\"");
String[] lines = xmlData.split("\\n");
for (int i = 0; i < lines.length; i++) {
writer.write(lines[i]);
if (i == lines.length - 1) {
writer.write("\";");
} else {
writer.write("\\n\\\n");
}
}
writer.endElement(HTML.SCRIPT_ELEM);
ResourceBundle bundle = ResourceBundle.getBundle
("de.danet.an.util.jsf.taglib.L10n", JSFUtil.activeLocale(),
XmlDisplayWindowRenderer.class.getClassLoader());
writer.startElement(HTML.ANCHOR_ELEM, null);
writer.writeAttribute(HTML.ID_ATTR, comp.getClientId(context), null);
writer.writeAttribute(HTML.HREF_ATTR, "#", null);
writer.writeAttribute
(HTML.ONCLICK_ATTR, "openXmlWindow('" + urlPrefix + "', "
+ xmlDataVar + ", '" + bundle.getString("closeButtonLabel") + "')",
null);
}
/* (non-Javadoc)
* @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
*/
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
super.encodeEnd(context, component);
ResponseWriter writer = context.getResponseWriter();
// force separate end tag
writer.writeText("", null);
writer.endElement(HTML.ANCHOR_ELEM);
}
}