Author: nbelaevski
Date: 2007-10-25 19:21:35 -0400 (Thu, 25 Oct 2007)
New Revision: 3555
Added:
trunk/framework/test/src/test/java/org/richfaces/renderkit/TemplateEncoderRendererBaseTest.java
Modified:
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
Log:
http://jira.jboss.com/jira/browse/RF-1130 : code fix & test case
Modified:
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
===================================================================
---
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java 2007-10-25
21:47:54 UTC (rev 3554)
+++
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java 2007-10-25
23:21:35 UTC (rev 3555)
@@ -20,12 +20,15 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
+import org.ajax4jsf.javascript.JSReference;
+import org.ajax4jsf.javascript.ScriptUtils;
+import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
import org.ajax4jsf.webapp.tidy.TidyParser;
import org.ajax4jsf.webapp.tidy.TidyXMLFilter;
-import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
import org.richfaces.component.TemplateComponent;
import org.richfaces.json.JSContentHandler;
import org.w3c.dom.Document;
+import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -65,39 +68,51 @@
TidyParser tidyParser = new TidyParser(tidyProperties);
Document parsedHtml = tidyParser.parseHtmlByTidy(new StringReader(string), null);
- NodeList nodeList = parsedHtml.getDocumentElement().getChildNodes();
- Node bodyNode = nodeList.item(nodeList.getLength() - 1);
- NodeList bodyChildren = bodyNode.getChildNodes();
- int bodyChildrenLength = bodyChildren.getLength();
- writer.write("[");
+ Element documentElement = null;
+
+ if (parsedHtml != null) {
+ documentElement = parsedHtml.getDocumentElement();
+ }
+
+ if (documentElement != null) {
+ writer.write("[");
+
+ NodeList nodeList = documentElement.getChildNodes();
+ Node bodyNode = nodeList.item(nodeList.getLength() - 1);
+ NodeList bodyChildren = bodyNode.getChildNodes();
+ int bodyChildrenLength = bodyChildren.getLength();
- try {
- Transformer transformer;
+ try {
+ Transformer transformer;
- TransformerFactory factory = getTransformerFactory();
-
- synchronized (factory) {
- transformer = factory.newTransformer();
- }
+ TransformerFactory factory = getTransformerFactory();
+
+ synchronized (factory) {
+ transformer = factory.newTransformer();
+ }
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
- JSContentHandler contentHandler = new MacroDefinitionJSContentHandler(writer,
"Richfaces.evalMacro(\"", "\", context)");
- Result result = new SAXResult(contentHandler);
+ JSContentHandler contentHandler = new MacroDefinitionJSContentHandler(writer,
"Richfaces.evalMacro(\"", "\", context)");
+ Result result = new SAXResult(contentHandler);
- for (int i = 0; i < bodyChildrenLength; i++) {
- if (i != 0) {
- writer.write(", ");
+ for (int i = 0; i < bodyChildrenLength; i++) {
+ if (i != 0) {
+ writer.write(", ");
+ }
+ transformer.transform(new DOMSource(bodyChildren.item(i)), result);
}
- transformer.transform(new DOMSource(bodyChildren.item(i)), result);
+ } catch (TransformerException e) {
+ throw new IOException(e.getMessage());
}
- } catch (TransformerException e) {
- throw new IOException(e.getMessage());
+
+ writer.write("]");
+
+ } else {
+ writer.write(ScriptUtils.toScript(JSReference.NULL));
}
-
- writer.write("]");
}
protected void writeScriptBody(FacesContext context, UIComponent component, boolean
children)
Added:
trunk/framework/test/src/test/java/org/richfaces/renderkit/TemplateEncoderRendererBaseTest.java
===================================================================
---
trunk/framework/test/src/test/java/org/richfaces/renderkit/TemplateEncoderRendererBaseTest.java
(rev 0)
+++
trunk/framework/test/src/test/java/org/richfaces/renderkit/TemplateEncoderRendererBaseTest.java 2007-10-25
23:21:35 UTC (rev 3555)
@@ -0,0 +1,64 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.renderkit;
+
+import javax.faces.component.UIOutput;
+
+import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
+
+/**
+ * Created 26.10.2007
+ * @author Nick Belaevski - mailto:nbelaevski@exadel.com
+ * @since 3.2
+ */
+
+public class TemplateEncoderRendererBaseTest extends AbstractAjax4JsfTestCase {
+
+ public TemplateEncoderRendererBaseTest(String name) {
+ super(name);
+ }
+
+ public void testEncodeNonRendered() throws Exception {
+ setupResponseWriter();
+
+ TemplateEncoderRendererBase rendererBase = new TemplateEncoderRendererBase() {
+
+ @Override
+ protected Class getComponentClass() {
+ return UIOutput.class;
+ }
+
+ };
+
+ UIOutput output = new UIOutput();
+
+ UIOutput c = new UIOutput();
+ c.setRendered(false);
+ c.setValue("");
+ output.getChildren().add(c);
+
+ //that should not fail
+ rendererBase.writeScriptBody(facesContext, c, true);
+
+ processResponseWriter();
+ }
+}