Author: nbelaevski
Date: 2007-06-18 18:58:52 -0400 (Mon, 18 Jun 2007)
New Revision: 1214
Added:
trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
Modified:
trunk/richfaces/common/src/main/java/org/richfaces/json/JSContentHandler.java
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateUtil.java
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/json/json-dom.js
trunk/richfaces/drag-drop/src/main/java/org/richfaces/renderkit/DragIndicatorRendererBase.java
trunk/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp
trunk/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
Log:
- TemplateUtil updated for new macrodefs grammar
- DragIndicatorRendererBase updated for new TemplateUtil API compliance
- Fixed some issues in JSContentHandler & json-dom.js - still plenty of work
- CalendarRendererBase updated for new TemplateUtil API compliance
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-18
18:11:04 UTC (rev 1213)
+++
trunk/richfaces/common/src/main/java/org/richfaces/json/JSContentHandler.java 2007-06-18
22:58:52 UTC (rev 1214)
@@ -1,6 +1,28 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces 3.0 - 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.json;
import java.io.IOException;
+import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
@@ -47,9 +69,9 @@
private static final char S_ELEMENT_END[] = "])".toCharArray();
- private static final char S_TEXT_START[] = "new T('".toCharArray();
+ private static final char S_TEXT_START[] = "new T(".toCharArray();
- private static final char S_TEXT_END[] = "')".toCharArray();
+ private static final char S_TEXT_END[] = ")".toCharArray();
private static final char S_CDATA_START[] = "new D('".toCharArray();
@@ -124,7 +146,7 @@
if (this.beforeDocumentStart || level < 0)
return;
try {
- if (!this.closeElement(false) && this.processing_cdata == false) {
+ if (level != 0 && !this.closeElement(false) &&
this.processing_cdata == false) {
this.outputWriter.write(C_COMMA);
}
if (DEBUG) {
@@ -135,7 +157,7 @@
if (this.processing_cdata == false) {
this.outputWriter.write(S_TEXT_START);
}
- this.encode(ch, start, length);
+ this.encodeText(ch, start, length);
if (this.processing_cdata == false) {
this.outputWriter.write(S_TEXT_END);
}
@@ -196,9 +218,6 @@
* further <code></element></code> is required.
*/
protected boolean closeElement(boolean end_element) throws SAXException {
- if (!end_element && level == 0)
- return true;
-
if (!hanging_element)
return (false);
try {
@@ -380,6 +399,7 @@
this.outputWriter.write(attrName);
this.outputWriter.write(C_QUOTE); // [']
this.outputWriter.write(C_NSSEP); // [:]
+ //TODO by nick - fix namespace handling - they shouldn't be encoded
this.encodeAttributeValue(attributes, x);
}
@@ -394,12 +414,15 @@
}
protected void encodeAttributeValue(Attributes attributes, int idx) throws
SAXException, IOException {
- //TODO by nick - fix namespace handling
this.outputWriter.write(C_QUOTE); // [']
this.encode(attributes.getValue(idx));
this.outputWriter.write(C_QUOTE); // [']
}
+ protected void encodeText(char[] chars, int start, int length) throws SAXException,
IOException {
+ this.encode(chars, start, length);
+ }
+
/* (non-Javadoc)
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
java.lang.String)
*/
@@ -415,7 +438,7 @@
if (this.beforeDocumentStart || level < 0)
return;
try {
- if (!this.closeElement(false)) {
+ if (!this.closeElement(false) && level != 0) {
this.outputWriter.write(C_COMMA);
}
if (DEBUG) {
@@ -476,7 +499,7 @@
if (this.beforeDocumentStart || level < 0)
return;
try {
- if (!this.closeElement(false)) {
+ if (!this.closeElement(false) && level != 0) {
this.outputWriter.write(C_COMMA);
}
if (DEBUG) {
@@ -580,4 +603,7 @@
}
}
+ protected boolean isProcessingCdata() {
+ return processing_cdata;
+ }
}
Modified: trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateUtil.java
===================================================================
---
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateUtil.java 2007-06-18
18:11:04 UTC (rev 1213)
+++
trunk/richfaces/common/src/main/java/org/richfaces/renderkit/TemplateUtil.java 2007-06-18
22:58:52 UTC (rev 1214)
@@ -21,8 +21,7 @@
package org.richfaces.renderkit;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.io.IOException;
import org.richfaces.org.apache.commons.lang.StringEscapeUtils;
@@ -36,36 +35,133 @@
public static interface MacroTemplateHandler {
public String handle(String body);
};
+
+ private StringBuffer definitionBuffer = new StringBuffer();
+ private StringBuffer result = new StringBuffer();
+
+ private int macroDefDepth = 0;
+ private boolean escapeCharOccured = false;
+ private MacroTemplateHandler templateHandler;
+ private boolean macroDefined = false;
- private final static Pattern PARAMETER_REPLACER_PATTERN =
Pattern.compile("(\\\\)?\\{([^\\}]+)\\}");
-
- public static String replaceParams(String string, MacroTemplateHandler handler) {
- Matcher m = PARAMETER_REPLACER_PATTERN.matcher(string);
- StringBuffer sb = new StringBuffer("\"");
-
- int lastPosition = 0;
-
- while (m.find()) {
- String firstGroup = m.group(1);
- if (firstGroup == null || firstGroup.length() == 0) {
- sb.append(StringEscapeUtils.escapeJavaScript(string.substring(lastPosition,
m.start(2) - 1)));
- sb.append("\" + " + handler.handle(m.group(2)) + " +
\"");
+ public TemplateUtil(MacroTemplateHandler templateHandler) {
+ super();
+ this.templateHandler = templateHandler;
+ }
+
+ private Boolean literalMode = null;
+
+ private void doWriteChar(char c) throws IOException {
+ if (macroDefDepth == 0) {
+ if (literalMode == null) {
+ result.append('\'');
+ literalMode = Boolean.TRUE;
+ } else if (Boolean.FALSE.equals(literalMode)) {
+ result.append(" + \'");
+ literalMode = Boolean.TRUE;
+ }
+
+ if (escapeCharOccured) {
+ result.append("\\\\");
+ }
+
+ result.append(StringEscapeUtils.escapeJavaScript(String.valueOf(c)));
+ } else {
+ if (escapeCharOccured) {
+ definitionBuffer.append('\\');
+ }
+
+ definitionBuffer.append(c);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.Writer#write(char[], int, int)
+ */
+ public TemplateUtil append(String string) throws IOException {
+ char[] cbuf = string.toCharArray();
+ for (int i = 0; i < cbuf.length; i++) {
+ char c = cbuf[i];
+
+ if (c == '{') {
+ if (escapeCharOccured) {
+ escapeCharOccured = false;
+ doWriteChar(c);
+ } else {
+ if (macroDefDepth++ != 0) {
+ doWriteChar(c);
+ }
+ }
+ } else if (c == '}') {
+ if (escapeCharOccured) {
+ if (macroDefDepth == 0) {
+ doWriteChar(c);
+ escapeCharOccured = false;
+ } else {
+ escapeCharOccured = false;
+ doWriteChar(c);
+ }
+ } else {
+ if (macroDefDepth > 0 && --macroDefDepth == 0) {
+ if (Boolean.TRUE.equals(literalMode)) {
+ result.append('\'');
+ }
+
+ if (literalMode != null) {
+ result.append(" + ");
+ }
+
+ literalMode = Boolean.FALSE;
+
+ macroDefined = true;
+ result.append(templateHandler.handle(definitionBuffer.toString()));
+ definitionBuffer = new StringBuffer();
+ } else {
+ doWriteChar(c);
+ }
+ }
} else {
- StringBuffer sbUnEsc = new StringBuffer();
- sbUnEsc.append(string.substring(lastPosition, m.end(1) - 1));
- sbUnEsc.append("{");
- sbUnEsc.append(m.group(2));
- sbUnEsc.append("}");
+ if (c == '\\') {
+ if (escapeCharOccured) {
+ escapeCharOccured = false;
+ doWriteChar(c);
+ }
- sb.append(StringEscapeUtils.escapeJavaScript(sbUnEsc.toString()));
+ escapeCharOccured = true;
+ } else {
+ doWriteChar(c);
+ escapeCharOccured = false;
+ }
}
+ }
+
+ return this;
+ }
- lastPosition = m.end(2) + 1;
+ public String getResult() throws IOException {
+ boolean tmpEscapeCharOccured = escapeCharOccured;
+ escapeCharOccured = false;
+
+ if (macroDefDepth > 0) {
+ macroDefDepth = 0;
+ doWriteChar('{');
+ result.append(StringEscapeUtils.escapeJavaScript(definitionBuffer.toString()));
}
- if (lastPosition < string.length()) {
- sb.append(StringEscapeUtils.escapeJavaScript(string.substring(lastPosition)));
+
+ if (tmpEscapeCharOccured) {
+ doWriteChar('\\');
}
- sb.append("\"");
- return sb.toString();
+
+ if (Boolean.TRUE.equals(literalMode)) {
+ result.append('\'');
+ }
+
+ literalMode = null;
+
+ return result.toString();
}
+
+ public boolean isMacroDefined() {
+ return macroDefined;
+ }
}
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-18
18:11:04 UTC (rev 1213)
+++
trunk/richfaces/common/src/main/resources/org/richfaces/renderkit/html/scripts/json/json-dom.js 2007-06-18
22:58:52 UTC (rev 1214)
@@ -170,7 +170,16 @@
T.prototype = new JSNode();
T.prototype.getOuterHTML = function(context){
- return this.xmlescape(this.value);
+ var value = this.value;
+ if (typeof value == "function") {
+ value = value(context);
+ }
+
+ if (value) {
+ return this.xmlescape(value);
+ }
+
+ return "";
}
// Comment node
Added: trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
===================================================================
--- trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java
(rev 0)
+++
trunk/richfaces/common/src/test/java/org/richfaces/renderkit/TemplateUtilTest.java 2007-06-18
22:58:52 UTC (rev 1214)
@@ -0,0 +1,82 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces 3.0 - 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 java.io.IOException;
+
+import org.richfaces.renderkit.TemplateUtil.MacroTemplateHandler;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Nick Belaevski - mailto:nbelaevski@exadel.com
+ * created 17.06.2007
+ *
+ */
+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;
+ }
+
+ 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-18
18:11:04 UTC (rev 1213)
+++
trunk/richfaces/drag-drop/src/main/java/org/richfaces/renderkit/DragIndicatorRendererBase.java 2007-06-18
22:58:52 UTC (rev 1214)
@@ -187,7 +187,9 @@
responseWriter.write("elt.indicatorTemplates['" + facetName +
"'] = function(element, jsParams) {\n");
responseWriter.write("var content = ");
- String result = TemplateUtil.replaceParams(dumpWriter.getBuffer().toString(),
macroTemplateHandler);
+ 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("};");
}
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-18
18:11:04 UTC (rev 1213)
+++
trunk/sandbox/calendar/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2007-06-18
22:58:52 UTC (rev 1214)
@@ -37,7 +37,6 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.stream.StreamSource;
import org.ajax4jsf.framework.ajax.xmlfilter.tidy.TidyParser;
import org.ajax4jsf.framework.ajax.xmlfilter.tidy.TidyXMLFilter;
@@ -128,7 +127,12 @@
writer.write("var evaluator = new Object(); evaluator.parts = [ ");
try {
- Transformer transformer = transformerFactory.newTransformer();
+ Transformer transformer;
+
+ synchronized (transformerFactory) {
+ transformer = transformerFactory.newTransformer();
+ }
+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
@@ -137,8 +141,36 @@
int idx) throws SAXException, IOException {
String value = attributes.getValue(idx);
- outputWriter.write("function (context) { return " +
TemplateUtil.replaceParams(value, macroTemplateHandler) + ";}");
+ 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);
Modified: trunk/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp
===================================================================
--- trunk/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp 2007-06-18
18:11:04 UTC (rev 1213)
+++ trunk/sandbox-samples/calendar-sample/src/main/webapp/pages/index.jsp 2007-06-18
22:58:52 UTC (rev 1214)
@@ -21,7 +21,7 @@
<f:verbatim><h3>Templating test</h3></f:verbatim>
<calendar:calendar>
- <f:verbatim><input type="text" value="$ {test} $"
/></f:verbatim>
+ <f:verbatim><input type="text" value="$ {test} $"
/><![CDATA[a<b]]>{test}</f:verbatim>
<f:verbatim><input type="radio" checked="{test}"
/><input type="radio" checked="{test1}"
/></f:verbatim>
</calendar:calendar>