Author: maksimkaszynski
Date: 2007-10-09 13:31:48 -0400 (Tue, 09 Oct 2007)
New Revision: 3303
Modified:
trunk/framework/impl/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
Log:
http://jira.jboss.com/jira/browse/RF-912
Modified:
trunk/framework/impl/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java
===================================================================
---
trunk/framework/impl/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java 2007-10-09
16:09:31 UTC (rev 3302)
+++
trunk/framework/impl/src/main/java/org/richfaces/renderkit/MacroDefinitionJSContentHandler.java 2007-10-09
17:31:48 UTC (rev 3303)
@@ -3,6 +3,7 @@
*/
package org.richfaces.renderkit;
+import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
@@ -22,6 +23,49 @@
private String prolog;
private String epilog;
+ /**
+ * Writer will not output duplicate commas
+ * @author Maksim Kaszynski
+ *
+ */
+ static class WellFormedWriter extends FilterWriter {
+
+ private char lastChar;
+
+ public WellFormedWriter(Writer out) {
+ super(out);
+ }
+
+ public void write(char[] cbuf, int off, int len) throws IOException {
+
+
+ //Skip comma
+ while (cbuf[off] == lastChar && lastChar == ',' && len > 0)
{
+ off++;
+ len--;
+ }
+
+ while(len > 1 && cbuf[off] == cbuf[off+1] && cbuf[off] ==
','){
+ len--;
+ off++;
+ }
+
+
+ super.write(cbuf, off, len);
+
+ lastChar = cbuf[off + len - 1];
+
+ }
+
+ public void write(int c) throws IOException {
+ if (c != lastChar || lastChar != ',') {
+ super.write(c);
+ lastChar = (char) c;
+ }
+ }
+
+ }
+
public MacroDefinitionJSContentHandler(Writer writer, String prolog,
String epilog) {
super(writer);
@@ -102,52 +146,62 @@
if (isProcessingCdata()) {
super.characters(ch, start, length);
} else {
- List parsedExpression = parseExpressiion(new String(ch, start, length));
- for (Iterator iterator = parsedExpression.iterator(); iterator
- .hasNext();) {
+ Writer oldWriter = outputWriter;
+ outputWriter = new WellFormedWriter(oldWriter);
+
+ try {
+ 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(',');
- }
+ Object next = iterator.next();
- 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 (next instanceof Expression) {
+ Expression expression = (Expression) next;
- if (this.isProcessingCdata() == false) {
- this.outputWriter.write(")");
- }
- } catch (IOException e) {
- throw new SAXException("Write error",e);
- }
+ if (this.isBeforeDocumentStart() || level < 0)
+ return;
+ try {
+ if (level != 0 && !this.closeElement(false)
+ && this.isProcessingCdata() == false) {
+ this.outputWriter.write(',');
+ }
- } else {
- char[] cs = next.toString().toCharArray();
- super.characters(cs, 0, cs.length);
- }
+ if (this.isProcessingCdata() == false) {
+ this.outputWriter.write("new ET(");
+ }
- if (iterator.hasNext()) {
- try {
- this.outputWriter.write(',');
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ 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();
+ }
+ }
+ }
+ } finally {
+ outputWriter = oldWriter;
}
}
}