Author: byron
Date: Tue Feb 10 13:35:34 2009
New Revision: 742963
URL: http://svn.apache.org/viewvc?rev=742963&view=rev
Log:
Add a FilterWriter class that provides a way to intercept the rendering of
references to the writer which can be easily manipulated with directives
Added:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/Filter.java
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/FilterWriter.java
Modified:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/VelocityWriter.java
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
Added:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/Filter.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/Filter.java?rev=742963&view=auto
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/Filter.java
(added)
+++
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/Filter.java
Tue Feb 10 13:35:34 2009
@@ -0,0 +1,32 @@
+package org.apache.velocity.io;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+
+/**
+ * Velocity will call the writeReference method on any Writer that
+ * implements this interface when rendering references within a template.
+ */
+
+public interface Filter
+{
+ public void writeReference(String ref) throws IOException;
+}
Added:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/FilterWriter.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/FilterWriter.java?rev=742963&view=auto
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/FilterWriter.java
(added)
+++
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/FilterWriter.java
Tue Feb 10 13:35:34 2009
@@ -0,0 +1,70 @@
+package org.apache.velocity.io;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * FilterWriter provides a means to intercept template rendering to
+ * a Writer. The important function of this writer is to distinguish
+ * the writing of template references to the rendering of static content.
+ * Velocity will use the method writeReference(String) for rendering references
+ * , such as $foo, to the writer. Other template content will be rendered
+ * with the standard write methods.
+ */
+
+public class FilterWriter extends Writer implements Filter
+{
+ protected Writer writer = null;
+ public FilterWriter(Writer w)
+ {
+ writer = w;
+ }
+
+ protected FilterWriter()
+ {
+ }
+
+ public void close() throws IOException
+ {
+ writer.close();
+ }
+
+ public void flush() throws IOException
+ {
+ writer.flush();
+ }
+
+ public void write(char[] cbuf, int off, int len) throws IOException
+ {
+ writer.write(cbuf, off, len);
+ }
+
+ /**
+ * Send the content of a reference, e.g.; $foo, to the writer.
+ * The default implementation is to call the wrapped Writer's
+ * write(String) method.
+ */
+ public void writeReference(String ref) throws IOException
+ {
+ writer.write(ref);
+ }
+}
Modified:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/VelocityWriter.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/VelocityWriter.java?rev=742963&r1=742962&r2=742963&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/VelocityWriter.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/io/VelocityWriter.java
Tue Feb 10 13:35:34 2009
@@ -31,7 +31,7 @@
* @author Anil K. Vijendran
* @version $Id$
*/
-public final class VelocityWriter extends Writer
+public final class VelocityWriter extends FilterWriter
{
/**
* constant indicating that the Writer is not buffering output
@@ -53,8 +53,6 @@
private int bufferSize;
private boolean autoFlush;
- private Writer writer;
-
private char cb[];
private int nextChar;
Modified:
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL:
http://svn.apache.org/viewvc/velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=742963&r1=742962&r2=742963&view=diff
==============================================================================
---
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
(original)
+++
velocity/engine/branches/2.0_Exp/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
Tue Feb 10 13:35:34 2009
@@ -30,6 +30,7 @@
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.TemplateInitException;
import org.apache.velocity.exception.VelocityException;
+import org.apache.velocity.io.Filter;
import org.apache.velocity.runtime.Renderable;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.directive.Block.Reference;
@@ -479,8 +480,15 @@
*/
writer.write(escPrefix);
writer.write(morePrefix);
- writer.write(toString);
-
+ if (writer instanceof Filter)
+ {
+ ((Filter)writer).writeReference(toString);
+ }
+ else
+ {
+ writer.write(toString);
+ }
+
return true;
}
}