001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: XmlDisplayWindowRenderer.java,v 1.6 2006/12/12 09:58:39 drmlipp Exp $
021: *
022: * $Log: XmlDisplayWindowRenderer.java,v $
023: * Revision 1.6 2006/12/12 09:58:39 drmlipp
024: * Added resources and minor cleanup.
025: *
026: * Revision 1.5 2006/11/01 16:15:33 mlipp
027: * Make our JSF components depend on tomahawk only.
028: *
029: * Revision 1.4 2006/10/06 21:53:38 mlipp
030: * Handling null data.
031: *
032: * Revision 1.3 2006/10/06 21:02:52 mlipp
033: * Improved.
034: *
035: * Revision 1.2 2006/10/06 15:27:14 drmlipp
036: * Improved.
037: *
038: * Revision 1.1 2006/10/05 13:27:39 drmlipp
039: * New tag for XML display.
040: *
041: */
042: package de.danet.an.util.jsf.taglib;
043:
044: import java.io.IOException;
045: import java.util.Map;
046: import java.util.ResourceBundle;
047:
048: import javax.faces.component.UIComponent;
049: import javax.faces.context.FacesContext;
050: import javax.faces.context.ResponseWriter;
051: import javax.faces.render.Renderer;
052:
053: import org.apache.myfaces.renderkit.html.util.AddResource;
054: import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
055: import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
056: import org.apache.myfaces.shared_tomahawk.renderkit.html.util.JavascriptUtils;
057:
058: import de.danet.an.util.jsf.JSFUtil;
059:
060: /**
061: * This class provides a renderer for the HtmlLinkCssStylesheet component.
062: *
063: * @author Michael Lipp
064: */
065: public class XmlDisplayWindowRenderer extends Renderer {
066:
067: private static String SCRIPT_RENDERED = XmlDisplayWindowRenderer.class
068: .getName()
069: + "_SCRIPT_RENDERED";
070:
071: /* (non-Javadoc)
072: * @see javax.faces.render.Renderer#encodeBegin
073: */
074: public void encodeBegin(FacesContext context, UIComponent component)
075: throws IOException {
076: XmlDisplayWindow comp = (XmlDisplayWindow) component;
077: ResponseWriter writer = context.getResponseWriter();
078: JSLoading.ensureInclusion(context, JSLoading.XMLSAX);
079: JSLoading.ensureInclusion(context, JSLoading.XML2HTML);
080: AddResource addResource = AddResourceFactory
081: .getInstance(context);
082:
083: String xmlDataVar = JavascriptUtils.getValidJavascriptName(comp
084: .getClientId(context)
085: + "xmlDataVar", false);
086:
087: String urlPrefix = addResource.getResourceUri(context,
088: new MyFacesResourceHandler(XmlDisplayWindow.class, ""));
089: urlPrefix = context.getExternalContext().encodeResourceURL(
090: urlPrefix);
091: writer.startElement(HTML.SCRIPT_ELEM, null);
092: writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,
093: HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
094: String xmlData = (String) comp.getXmlData();
095: if (xmlData == null) {
096: xmlData = "";
097: }
098: xmlData = xmlData.replaceAll("\\r", "");
099: xmlData = xmlData.replaceAll("\\\\", "\\\\\\\\");
100: xmlData = xmlData.replaceAll("\"", "\\\\\"");
101: writer.write("var " + xmlDataVar + "=\"");
102: String[] lines = xmlData.split("\\n");
103: for (int i = 0; i < lines.length; i++) {
104: writer.write(lines[i]);
105: if (i == lines.length - 1) {
106: writer.write("\";");
107: } else {
108: writer.write("\\n\\\n");
109: }
110: }
111: writer.endElement(HTML.SCRIPT_ELEM);
112:
113: ResourceBundle bundle = ResourceBundle.getBundle(
114: "de.danet.an.util.jsf.taglib.L10n", JSFUtil
115: .activeLocale(), XmlDisplayWindowRenderer.class
116: .getClassLoader());
117: writer.startElement(HTML.ANCHOR_ELEM, null);
118: writer.writeAttribute(HTML.ID_ATTR, comp.getClientId(context),
119: null);
120: writer.writeAttribute(HTML.HREF_ATTR, "#", null);
121: writer.writeAttribute(HTML.ONCLICK_ATTR, "openXmlWindow('"
122: + urlPrefix + "', " + xmlDataVar + ", '"
123: + bundle.getString("closeButtonLabel") + "')", null);
124: }
125:
126: /* (non-Javadoc)
127: * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
128: */
129: public void encodeEnd(FacesContext context, UIComponent component)
130: throws IOException {
131: super .encodeEnd(context, component);
132: ResponseWriter writer = context.getResponseWriter();
133: // force separate end tag
134: writer.writeText("", null);
135: writer.endElement(HTML.ANCHOR_ELEM);
136: }
137:
138: }
|