Author: nbelaevski
Date: 2007-06-25 12:48:09 -0400 (Mon, 25 Jun 2007)
New Revision: 1303
Added:
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/Expression.java
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
Removed:
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/ExpressionHolder.java
Modified:
trunk/richfaces/common/pom.xml
trunk/richfaces/common/src/main/antlr/RichMacroDefinition.g
trunk/richfaces/common/src/main/java/org/richfaces/json/JSContentHandler.java
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/json/json-dom.js
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/utils.js
trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
trunk/richfaces/drag-drop/src/main/java/org/richfaces/renderkit/DragIndicatorRendererBase.java
trunk/richfaces/drag-drop/src/main/resources/org/richfaces/renderkit/html/scripts/drag-indicator.js
trunk/richfaces/drag-drop/src/main/templates/org/richfaces/htmlDragIndicator.jspx
trunk/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
trunk/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx
Log:
New-style template engine support implementation
Modified: trunk/richfaces/common/pom.xml
===================================================================
--- trunk/richfaces/common/pom.xml 2007-06-25 16:31:51 UTC (rev 1302)
+++ trunk/richfaces/common/pom.xml 2007-06-25 16:48:09 UTC (rev 1303)
@@ -8,13 +8,6 @@
<groupId>org.richfaces</groupId>
<artifactId>common</artifactId>
<name>common classes</name>
- <dependencies>
- <dependency>
- <groupId>org.antlr</groupId>
- <artifactId>antlr</artifactId>
- <version>3.0</version>
- </dependency>
- </dependencies>
<build>
<plugins>
<plugin>
Modified: trunk/richfaces/common/src/main/antlr/RichMacroDefinition.g
===================================================================
--- trunk/richfaces/common/src/main/antlr/RichMacroDefinition.g 2007-06-25 16:31:51 UTC
(rev 1302)
+++ trunk/richfaces/common/src/main/antlr/RichMacroDefinition.g 2007-06-25 16:48:09 UTC
(rev 1303)
@@ -39,7 +39,7 @@
expression returns [List result] @init { $result = new ArrayList(); }:
(m = macrodef
- {$result.add(new org.richfaces.renderkit.ExpressionHolder(
+ {$result.add(new org.richfaces.renderkit.Expression(
unescape(stripBraces($m.text))));}
| t = text
Modified: trunk/richfaces/common/src/main/java/org/richfaces/json/JSContentHandler.java
===================================================================
---
trunk/richfaces/common/src/main/java/org/richfaces/json/JSContentHandler.java 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/common/src/main/java/org/richfaces/json/JSContentHandler.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -606,4 +606,8 @@
protected boolean isProcessingCdata() {
return processing_cdata;
}
+
+ protected boolean isBeforeDocumentStart() {
+ return beforeDocumentStart;
+ }
}
Copied: trunk/richfaces/common/src/main/java/org/richfaces/renderkit/Expression.java (from
rev 1246,
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/ExpressionHolder.java)
===================================================================
--- trunk/richfaces/common/src/main/java/org/richfaces/renderkit/Expression.java
(rev 0)
+++
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/Expression.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -0,0 +1,50 @@
+/**
+ *
+ */
+package org.richfaces.renderkit;
+
+/**
+ * @author Nick Belaevski - mailto:nbelaevski@exadel.com
+ * created 20.06.2007
+ *
+ */
+public class Expression {
+ private Object expression;
+
+ public Expression(Object expression) {
+ super();
+ this.expression = expression;
+ }
+
+ public Object getExpression() {
+ return expression;
+ }
+
+ public String toString() {
+ return super.toString() + "[" + expression + "]";
+ }
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((expression == null) ? 0 : expression.hashCode());
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final Expression other = (Expression) obj;
+ if (expression == null) {
+ if (other.expression != null)
+ return false;
+ } else if (!expression.equals(other.expression))
+ return false;
+ return true;
+ }
+}
Deleted:
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/ExpressionHolder.java
===================================================================
---
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/ExpressionHolder.java 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/ExpressionHolder.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -1,26 +0,0 @@
-/**
- *
- */
-package org.richfaces.renderkit;
-
-/**
- * @author Nick Belaevski - mailto:nbelaevski@exadel.com
- * created 20.06.2007
- *
- */
-public class ExpressionHolder {
- private Object expression;
-
- public ExpressionHolder(Object expression) {
- super();
- this.expression = expression;
- }
-
- public Object getExpression() {
- return expression;
- }
-
- public String toString() {
- return super.toString() + "[" + expression + "]";
- }
-}
Added:
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
===================================================================
---
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
(rev 0)
+++
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -0,0 +1,174 @@
+/**
+ *
+ */
+package org.richfaces.renderkit;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.List;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.richfaces.RichMacroDefinitionLexer;
+import org.richfaces.RichMacroDefinitionParser;
+import org.richfaces.RichMacroDefinitionParser.expression_return;
+import org.richfaces.json.JSContentHandler;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+/**
+ * @author Nick Belaevski - mailto:nbelaevski@exadel.com created 21.06.2007
+ *
+ */
+public class MacroDefinitionJSContentHandler extends JSContentHandler {
+ private String prolog;
+ private String epilog;
+
+ public MacroDefinitionJSContentHandler(Writer writer, String prolog,
+ String epilog) {
+ super(writer);
+ this.prolog = prolog;
+ this.epilog = epilog;
+ }
+
+ private List parseExpressiion(String expressionString) throws SAXException {
+ ANTLRStringStream stream = new ANTLRStringStream(expressionString);
+ RichMacroDefinitionLexer lexer = new RichMacroDefinitionLexer(stream);
+ RichMacroDefinitionParser macroParser = new RichMacroDefinitionParser(
+ new CommonTokenStream(lexer));
+ try {
+ expression_return expression = macroParser.expression();
+ List result = expression.result;
+
+ return result;
+ } catch (RecognitionException e) {
+ throw new SAXException(e.getMessage(), e);
+ }
+ }
+
+ private void encodeExpressionString(String string) throws IOException,
+ SAXException {
+ List parsedExpressiion = parseExpressiion(string);
+
+ boolean isExpression = false;
+ for (Iterator iterator = parsedExpressiion.iterator(); iterator
+ .hasNext();) {
+ Object next = (Object) iterator.next();
+ if (next instanceof Expression) {
+ isExpression = true;
+ break;
+ }
+ }
+
+ if (isExpression) {
+ this.outputWriter.write("function (context) { return ");
+ }
+
+ boolean first = true;
+ for (Iterator iterator = parsedExpressiion.iterator(); iterator
+ .hasNext();) {
+ Object next = (Object) iterator.next();
+
+ if (next == null) {
+ continue;
+ }
+
+ if (!first) {
+ this.outputWriter.write('+');
+ }
+
+ if (next instanceof Expression) {
+ Expression macroExpression = (Expression) next;
+
+ this.outputWriter.write(prolog);
+ this.encode(macroExpression.getExpression().toString());
+ this.outputWriter.write(epilog);
+ } else {
+ this.outputWriter.write('\'');
+ this.encode(next.toString());
+ this.outputWriter.write('\'');
+ }
+
+ first = false;
+ }
+
+ if (isExpression) {
+ this.outputWriter.write(";}");
+ }
+ }
+
+ protected void encodeAttributeValue(Attributes attributes, int idx)
+ throws SAXException, IOException {
+
+ String value = attributes.getValue(idx);
+ encodeExpressionString(value);
+ }
+
+ public void characters(char[] ch, int start, int length)
+ throws SAXException {
+ if (isProcessingCdata()) {
+ super.characters(ch, start, length);
+ } else {
+ List parsedExpression = parseExpressiion(new String(ch, start, length));
+ for (Iterator iterator = parsedExpression.iterator(); iterator
+ .hasNext();) {
+
+ Object next = iterator.next();
+
+ if (next instanceof Expression) {
+ Expression expression = (Expression) next;
+
+ if (this.isBeforeDocumentStart() || level < 0)
+ return;
+ try {
+ if (level != 0 && !this.closeElement(false) &&
this.isProcessingCdata() == false) {
+ this.outputWriter.write(',');
+ }
+
+ if (this.isProcessingCdata() == false) {
+ this.outputWriter.write("new ET(");
+ }
+
+ this.outputWriter.write("function (context) { return ");
+ this.outputWriter.write(prolog);
+ this.encode(expression.getExpression().toString());
+ this.outputWriter.write(epilog);
+ this.outputWriter.write("}");
+
+ if (this.isProcessingCdata() == false) {
+ this.outputWriter.write(")");
+ }
+ } catch (IOException e) {
+ throw new SAXException("Write error",e);
+ }
+
+ } else {
+ char[] cs = next.toString().toCharArray();
+ super.characters(cs, 0, cs.length);
+ }
+
+ if (iterator.hasNext()) {
+ try {
+ this.outputWriter.write(',');
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ }
+
+ protected void encodeText(char[] chars, int start, int length)
+ throws SAXException, IOException {
+ if (!isProcessingCdata()) {
+ String str = new String(chars, start, length);
+ encodeExpressionString(str);
+ } else {
+ super.encodeText(chars, start, length);
+ }
+ }
+
+}
Added:
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
===================================================================
---
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
(rev 0)
+++
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -0,0 +1,162 @@
+/**
+ *
+ */
+package org.richfaces.renderkit;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Iterator;
+import java.util.Properties;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+
+import org.ajax4jsf.framework.ajax.xmlfilter.tidy.TidyParser;
+import org.ajax4jsf.framework.ajax.xmlfilter.tidy.TidyXMLFilter;
+import org.ajax4jsf.framework.renderer.HeaderResourcesRendererBase;
+import org.ajax4jsf.framework.renderer.RendererBase;
+import org.richfaces.json.JSContentHandler;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * @author Nick Belaevski - mailto:nbelaevski@exadel.com
+ * created 22.06.2007
+ *
+ */
+public abstract class TemplateEncoderRendererBase extends HeaderResourcesRendererBase {
+ private TransformerFactory transformerFactory;
+
+ public TemplateEncoderRendererBase() {
+ super();
+
+ transformerFactory = TransformerFactory.newInstance();
+ }
+
+ public final boolean getRendersChildren() {
+ return true;
+ }
+
+ private void writeScriptBody(ResponseWriter writer, String string) throws IOException {
+ Properties tidyProperties = new Properties();
+ InputStream propertiesStream = null;
+ try {
+ propertiesStream =
TidyXMLFilter.class.getResourceAsStream("tidy.properties");
+ tidyProperties.load(propertiesStream);
+ } finally {
+ if (propertiesStream != null) {
+ propertiesStream.close();
+ }
+ }
+
+ 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("[");
+
+ try {
+ Transformer transformer;
+
+ synchronized (transformerFactory) {
+ transformer = transformerFactory.newTransformer();
+ }
+
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+
+ JSContentHandler contentHandler = new MacroDefinitionJSContentHandler(writer,
"Richfaces.eval(\"", "\", context)");
+ Result result = new SAXResult(contentHandler);
+
+ for (int i = 0; i < bodyChildrenLength; i++) {
+ if (i != 0) {
+ writer.write(", ");
+ }
+ transformer.transform(new DOMSource(bodyChildren.item(i)), result);
+ }
+ } catch (TransformerException e) {
+ throw new IOException(e.getMessage());
+ }
+
+ writer.write("];\n");
+ }
+
+ private void encodeComponent(FacesContext context, UIComponent component, RendererBase
rendererBase) throws IOException {
+ if (!component.isRendered()) {
+ return;
+ }
+
+ rendererBase.encodeBegin(context, component);
+ if (component.getRendersChildren() || rendererBase.getRendersChildren()) {
+ rendererBase.encodeChildren(context, component);
+ } else {
+ if (component.getChildCount() > 0) {
+ for (Iterator it = component.getChildren().iterator(); it.hasNext();) {
+ UIComponent child = (UIComponent) it.next();
+ renderChild(context, child);
+ }
+ }
+ }
+ rendererBase.encodeEnd(context, component);
+ }
+
+ protected void writeScriptBody(FacesContext context, UIComponent component, boolean
children,
+ RendererBase rendererBase)
+ throws IOException {
+ ResponseWriter writer = context.getResponseWriter();
+ StringWriter dumpingWriter = new StringWriter();
+ ResponseWriter clonedWriter = writer.cloneWithWriter(dumpingWriter);
+ context.setResponseWriter(clonedWriter);
+ try {
+
+ if (rendererBase == null) {
+ if (children) {
+ this.renderChildren(context, component);
+ } else {
+ this.renderChild(context, component);
+ }
+ } else {
+ if (children) {
+ if (component.getChildCount() > 0)
+ for (Iterator it = component.getChildren().iterator(); it.hasNext();) {
+ UIComponent child = (UIComponent) it.next();
+
+ encodeComponent(context, child, rendererBase);
+ }
+ } else {
+ encodeComponent(context, component, rendererBase);
+ }
+ }
+
+ } finally {
+ clonedWriter.flush();
+ context.setResponseWriter(writer);
+ }
+
+ writeScriptBody(writer, dumpingWriter.toString());
+ }
+
+ public void encodeChildren(FacesContext context, UIComponent component)
+ throws IOException {
+ ResponseWriter writer = context.getResponseWriter();
+ writer.startElement("script", component);
+ writer.write("var evaluator = ");
+ writeScriptBody(context, component, true, null);
+ writer.write("new Insertion.Top($('" + component.getClientId(context) +
"'), evaluator.invoke('getOuterHTML', window).join(''));");
+ writer.endElement("script");
+ }
+}
Modified:
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/json/json-dom.js
===================================================================
---
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/json/json-dom.js 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/json/json-dom.js 2007-06-25
16:48:09 UTC (rev 1303)
@@ -143,9 +143,14 @@
E.prototype.getOuterHTML = function(context){
// AJAX_Log("Get Outer HTML for object:"+Dump(this);
var html = "<"+this.tag;
- var inner = this.getInnerHTML();
+ var inner = this.getInnerHTML(context);
for( var i in this.attrs ){
+ if (!this.attrs.hasOwnProperty(i)) {
+ continue ;
+ }
+
var attrValue = this.attrs[i];
+
if (typeof attrValue == "function") {
attrValue = attrValue(context);
}
@@ -162,7 +167,27 @@
}
return html;
};
+
+
+ // Escaped Text node
+ ET = function(text) {
+ this.value = text;
+ }
+
+ ET.prototype = new JSNode();
+ ET.prototype.getOuterHTML = function(context){
+ var value = this.value;
+ if (typeof value == "function") {
+ value = value(context);
+ }
+
+ if (value) {
+ return value;
+ }
+ return "";
+ }
+
// Text node
T = function(text) {
this.value = text;
Modified:
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/utils.js
===================================================================
---
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/utils.js 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/utils.js 2007-06-25
16:48:09 UTC (rev 1303)
@@ -98,4 +98,18 @@
}
return null;
-};
\ No newline at end of file
+};
+
+Richfaces.eval = function(template, object) {
+ var value = '';
+
+ try {
+ with (object) {
+ value = eval(template) ;
+ }
+ } catch (e) {
+ LOG.warn('Exception: ' + e.message + '\n[' + template + ']');
+ }
+
+ return value;
+};
Modified:
trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
===================================================================
---
trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -32,7 +32,6 @@
import org.richfaces.RichMacroDefinitionLexer;
import org.richfaces.RichMacroDefinitionParser;
import org.richfaces.RichMacroDefinitionParser.expression_return;
-import org.richfaces.renderkit.TemplateUtil.MacroTemplateHandler;
/**
* @author Nick Belaevski - mailto:nbelaevski@exadel.com
@@ -41,25 +40,8 @@
*/
public class TemplateUtilTest extends TestCase {
- private static class MacroTemplateHandlerImpl implements MacroTemplateHandler {
- private boolean handled = false;
-
- public String handle(String body) {
- handled = true;
- return "#" + body + "#";
- }
-
- public boolean isHandled() {
- return handled;
- }
- };
-
private String doWrite(String in) throws IOException {
- MacroTemplateHandlerImpl templateHandlerImpl = new MacroTemplateHandlerImpl();
- TemplateUtil testWriter = new TemplateUtil(templateHandlerImpl);
- testWriter.append(in);
- String result = testWriter.getResult();
- return result;
+ return in;
}
private void printTree(Tree tree, int indent) {
@@ -81,33 +63,8 @@
RichMacroDefinitionParser macroParser = new RichMacroDefinitionParser(new
CommonTokenStream(lexer));
expression_return expression = macroParser.expression();
List result = expression.result;
- ExpressionHolder holder = (ExpressionHolder) result.get(0);
+ Expression holder = (Expression) result.get(0);
assertEquals("aa{b}}a", holder.getExpression());
assertEquals("\\ a}b{c", result.get(1));
}
-
- public void testWriting() throws Exception {
- String[][] testStrings = new String[][] {
- {"\'a\' + #bcd# + \'e\'", "a{bcd}e"},
- {"\'a{bcd}e\'", "a\\{bcd}e"},
- {"\'a\\\\{bcd}e\'", "a\\\\{bcd}e"},
- {"\'a{bc}d\'", "a{bc\\}d"},
- {"\'a\' + #bc\\d# + \'e\'", "a{bc\\d}e"},
- {"\'a\' + #bc\\\\d# + \'e\'", "a{bc\\\\d}e"},
- {"\'a{bc\\\\}d\'", "a{bc\\\\}d"},
- {"#ab}# + \'c\'", "{ab\\}}c"},
- {"\'a\' + #{bc#", "a{\\{bc}"},
- {"#{aa}#", "{{aa}}"},
- {"\'aa\\\\}\'", "aa\\}"},
- {"\'{aa\\\\\'", "{aa\\"}
- };
-
- for (int i = 0; i < testStrings.length; i++) {
- String[] strings = testStrings[i];
-
- String written = doWrite(strings[1]);
- //System.out.println(strings[0] + " : " + strings[1] + " : " +
written);
- assertEquals(strings[0], written);
- }
- }
}
Modified:
trunk/richfaces/drag-drop/src/main/java/org/richfaces/renderkit/DragIndicatorRendererBase.java
===================================================================
---
trunk/richfaces/drag-drop/src/main/java/org/richfaces/renderkit/DragIndicatorRendererBase.java 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/drag-drop/src/main/java/org/richfaces/renderkit/DragIndicatorRendererBase.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -22,7 +22,6 @@
package org.richfaces.renderkit;
import java.io.IOException;
-import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -31,30 +30,18 @@
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
-import javax.faces.render.Renderer;
-import org.ajax4jsf.framework.renderer.HeaderResourcesRendererBase;
+import org.ajax4jsf.framework.renderer.RendererBase;
import org.richfaces.component.UIDragIndicator;
import org.richfaces.component.nsutils.NSUtils;
import org.richfaces.org.apache.commons.lang.StringEscapeUtils;
-import org.richfaces.renderkit.TemplateUtil.MacroTemplateHandler;
/**
* Base renderer class for drag indicator ( marker ).
* @author shura
*
*/
-public class DragIndicatorRendererBase extends HeaderResourcesRendererBase {
- private final static MacroTemplateHandler macroTemplateHandler = new
MacroTemplateHandler() {
-
- public String handle(String body) {
- String paramAccess = "jsParams['" + body + "']";
-
- return ("((!!" + paramAccess + ") ? " + paramAccess + " :
\"\")");
- }
-
- };
-
+public class DragIndicatorRendererBase extends TemplateEncoderRendererBase {
public static final String ACCEPT_CLASS = "acceptClass";
public static final String REJECT_CLASS = "rejectClass";
public static final String RENDERER_TYPE =
"com.exadel.jsf.renderer.DropIndicatorRenderer";
@@ -79,12 +66,12 @@
FACETS_PREDEFINED.add(MULTI);
}
- private Renderer dragIndicatorViewRenderer = null;
+ private RendererBase dragIndicatorViewRenderer = null;
- private synchronized Renderer getViewRenderer() {
+ private synchronized RendererBase getViewRenderer() {
if (dragIndicatorViewRenderer == null) {
try {
- dragIndicatorViewRenderer = (Renderer)
Class.forName("org.richfaces.renderkit.html.DragIndicatorViewRenderer").newInstance();
+ dragIndicatorViewRenderer = (RendererBase)
Class.forName("org.richfaces.renderkit.html.DragIndicatorViewRenderer").newInstance();
} catch (InstantiationException e) {
throw new FacesException(e);
} catch (IllegalAccessException e) {
@@ -130,10 +117,6 @@
// return TemplateUtil.replaceParams(dumpWriter.getBuffer().toString(),
"jsParams");
// }
- public boolean getRendersChildren() {
- return true;
- }
-
protected boolean notHasFacet(UIComponent component, Object facetName) {
return component.getFacet((String) facetName) == null;
}
@@ -167,31 +150,13 @@
UIComponent facet = component.getFacet(facetName);
- StringWriter dumpWriter = new StringWriter();
-
- try {
- context.setResponseWriter(responseWriter.cloneWithWriter(dumpWriter));
+ responseWriter.write("elt.indicatorTemplates['" + facetName +
"'] = ");
- if (facet != null && facet.isRendered()) {
- renderChild(context, facet);
- } else {
-
- getViewRenderer().encodeBegin(context, component);
- getViewRenderer().encodeEnd(context, component);
-
- context.getResponseWriter().flush();
- }
- } finally {
- context.setResponseWriter(responseWriter);
+ if (facet != null && facet.isRendered()) {
+ writeScriptBody(context, facet, false, null);
+ } else {
+ writeScriptBody(context, component, false, getViewRenderer());
}
-
- responseWriter.write("elt.indicatorTemplates['" + facetName +
"'] = function(element, jsParams) {\n");
- responseWriter.write("var content = ");
- TemplateUtil templateUtil = new TemplateUtil(macroTemplateHandler);
- templateUtil.append(dumpWriter.getBuffer().toString());
- String result = templateUtil.getResult();
- responseWriter.write(result + ";\n new Insertion.Top(element,
content);\n");
- responseWriter.write("};");
}
//surround by CDATAs
Modified:
trunk/richfaces/drag-drop/src/main/resources/org/richfaces/renderkit/html/scripts/drag-indicator.js
===================================================================
---
trunk/richfaces/drag-drop/src/main/resources/org/richfaces/renderkit/html/scripts/drag-indicator.js 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/drag-drop/src/main/resources/org/richfaces/renderkit/html/scripts/drag-indicator.js 2007-06-25
16:48:09 UTC (rev 1303)
@@ -20,11 +20,15 @@
}
}
+ var parts;
+
if (single) {
- this.indicatorTemplates['single'](this, params);
+ parts = this.indicatorTemplates['single'];
} else {
- this.indicatorTemplates['multi'](this, params);
+ parts = this.indicatorTemplates['multi'];
}
+
+ new Insertion.Top(this, parts.invoke('getOuterHTML',
params).join(''));
},
show: function() {
Modified:
trunk/richfaces/drag-drop/src/main/templates/org/richfaces/htmlDragIndicator.jspx
===================================================================
---
trunk/richfaces/drag-drop/src/main/templates/org/richfaces/htmlDragIndicator.jspx 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/richfaces/drag-drop/src/main/templates/org/richfaces/htmlDragIndicator.jspx 2007-06-25
16:48:09 UTC (rev 1303)
@@ -16,6 +16,7 @@
new org.ajax4jsf.framework.ajax.AjaxScript(),
new org.ajax4jsf.dnd.DnDScript(),
/org/richfaces/renderkit/html/scripts/utils.js,
+ /org/richfaces/renderkit/html/scripts/json/json-dom.js,
/org/richfaces/renderkit/html/scripts/dnd/dnd-common.js,
scripts/drag-indicator.js
</h:scripts>
Modified:
trunk/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
===================================================================
---
trunk/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2007-06-25
16:31:51 UTC (rev 1302)
+++
trunk/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2007-06-25
16:48:09 UTC (rev 1303)
@@ -21,173 +21,19 @@
package org.richfaces.renderkit;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.util.Properties;
-
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.context.ResponseWriter;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Result;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXResult;
-
-import org.ajax4jsf.framework.ajax.xmlfilter.tidy.TidyParser;
-import org.ajax4jsf.framework.ajax.xmlfilter.tidy.TidyXMLFilter;
-import org.ajax4jsf.framework.renderer.HeaderResourcesRendererBase;
import org.richfaces.component.UICalendar;
-import org.richfaces.json.JSContentHandler;
-import org.richfaces.org.apache.commons.lang.StringEscapeUtils;
-import org.richfaces.renderkit.TemplateUtil.MacroTemplateHandler;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.Attributes;
-import org.xml.sax.SAXException;
/**
* @author Nick Belaevski - mailto:nbelaevski@exadel.com
* created 08.06.2007
*
*/
-public class CalendarRendererBase extends HeaderResourcesRendererBase {
+public class CalendarRendererBase extends TemplateEncoderRendererBase {
- private static final MacroTemplateHandler macroTemplateHandler = new
MacroTemplateHandler() {
-
- public String handle(String body) {
- return "calendarEval(\"" +
- StringEscapeUtils.escapeJavaScript(body) + "\", context)";
- }
-
- };
-
- private TransformerFactory transformerFactory;
-
- public CalendarRendererBase() {
- super();
-
- transformerFactory = TransformerFactory.newInstance();
- }
-
/* (non-Javadoc)
* @see org.ajax4jsf.framework.renderer.RendererBase#getComponentClass()
*/
protected Class getComponentClass() {
return UICalendar.class;
}
-
- public boolean getRendersChildren() {
- return true;
- }
-
- public void encodeChildren(FacesContext context, UIComponent component)
- throws IOException {
- ResponseWriter writer = context.getResponseWriter();
- StringWriter dumpingWriter = new StringWriter();
- ResponseWriter clonedWriter = writer.cloneWithWriter(dumpingWriter);
- context.setResponseWriter(clonedWriter);
- try {
- renderChildren(context, component);
- } finally {
- clonedWriter.flush();
- context.setResponseWriter(writer);
- }
-
- Properties tidyProperties = new Properties();
- InputStream propertiesStream = null;
- try {
- propertiesStream =
TidyXMLFilter.class.getResourceAsStream("tidy.properties");
- tidyProperties.load(propertiesStream);
- } finally {
- if (propertiesStream != null) {
- propertiesStream.close();
- }
- }
-
- TidyParser tidyParser = new TidyParser(tidyProperties);
- Document parsedHtml = tidyParser.parseHtmlByTidy(new
StringReader(dumpingWriter.toString()), null);
- NodeList nodeList = parsedHtml.getDocumentElement().getChildNodes();
- Node bodyNode = nodeList.item(nodeList.getLength() - 1);
- NodeList bodyChildren = bodyNode.getChildNodes();
- int bodyChildrenLength = bodyChildren.getLength();
-
- writer.startElement("script", component);
- writer.write("calendarEval = function(template, object)" +
-"{" +
-" var value=\"\";"+
-" try { with (object) value=eval(template) } catch (e) { LOG.warn('Exception:
'+e.message + '\\n[' + template + ']'); }"+
-" return value;};");
-
- writer.write("var evaluator = new Object(); evaluator.parts = [ ");
-
- try {
- Transformer transformer;
-
- synchronized (transformerFactory) {
- transformer = transformerFactory.newTransformer();
- }
-
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");
-
- JSContentHandler contentHandler = new JSContentHandler(writer) {
- protected void encodeAttributeValue(Attributes attributes,
- int idx) throws SAXException, IOException {
-
- String value = attributes.getValue(idx);
- TemplateUtil templateUtil = new TemplateUtil(macroTemplateHandler);
- templateUtil.append(value);
- if (templateUtil.isMacroDefined()) {
- outputWriter.write("function (context) { return " +
templateUtil.getResult() + ";}");
- } else {
- outputWriter.write(templateUtil.getResult());
- }
- }
-
- protected void encodeText(char[] chars, int start, int length)
- throws SAXException, IOException {
- if (!isProcessingCdata()) {
- String str = new String(chars, start, length);
- TemplateUtil templateUtil = new TemplateUtil(macroTemplateHandler);
- try {
- templateUtil.append(str);
- if (templateUtil.isMacroDefined()) {
- str = "function (context) { return " + templateUtil.getResult() +
";}";
- } else {
- str = templateUtil.getResult();
- }
-
- outputWriter.write(str);
- } catch (IOException e) {
- throw new SAXException(e.getMessage(), e);
- }
- } else {
- super.encodeText(chars, start, length);
- }
- }
- };
- Result result = new SAXResult(contentHandler);
-
- for (int i = 0; i < bodyChildrenLength; i++) {
- if (i != 0) {
- writer.write(", ");
- }
- transformer.transform(new DOMSource(bodyChildren.item(i)), result);
- }
- } catch (TransformerException e) {
- throw new IOException(e.getMessage());
- }
-
- writer.write("]; evaluator.evaluate = function (context) { var result =
'';");
- writer.write(" for (var i = 0; i < this.parts.length; i++) result +=
this.parts[i].getOuterHTML(context); return result;};\n");
-
- writer.write("new Insertion.Top($('" + component.getClientId(context) +
"'), evaluator.evaluate(window));");
- writer.endElement("script");
- }
}
Modified: trunk/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx
===================================================================
--- trunk/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx 2007-06-25
16:31:51 UTC (rev 1302)
+++ trunk/sandbox/calendar/src/main/templates/org/richfaces/htmlCalendar.jspx 2007-06-25
16:48:09 UTC (rev 1303)
@@ -11,7 +11,7 @@
component="org.richfaces.component.UICalendar"
<f:clientid var="clientId"/>
- <h:scripts>new org.ajax4jsf.framework.resource.PrototypeScript(),new
org.ajax4jsf.framework.ajax.AjaxScript(),/org/richfaces/renderkit/html/scripts/json/json-dom.js</h:scripts>
+ <h:scripts>new org.ajax4jsf.framework.resource.PrototypeScript(),new
org.ajax4jsf.framework.ajax.AjaxScript(),/org/richfaces/renderkit/html/scripts/utils.js,/org/richfaces/renderkit/html/scripts/json/json-dom.js</h:scripts>
<div id="#{clientId}"
x:passThruWithExclusions="value,name,type,id"