/* 
 * Qlue Web Application Framework
 * Copyright 2009-2012 Ivan Ristic <ivanr@webkreator.com>
 *
 * Licensed 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.
 */
package com.webkreator.qlue.view.velocity;

import com.webkreator.qlue.view.Canoe;
import org.apache.velocity.app.event.ReferenceInsertionEventHandler;

/**
 * This class is a bridge between Canoe and Velocity.
 */
public class CanoeReferenceInsertionHandler implements
		ReferenceInsertionEventHandler {

	public static final String SAFE_REFERENCE_NAME = "_x";

	public static final String SAFE_REFERENCE_PREFIX1 = "$"
			+ SAFE_REFERENCE_NAME + ".";

	public static final String SAFE_REFERENCE_PREFIX2 = "$!"
			+ SAFE_REFERENCE_NAME + ".";

	protected Canoe qlueWriter;

	public CanoeReferenceInsertionHandler(Canoe qlueWriter) {
		this.qlueWriter = qlueWriter;
	}

	/**
	 * Encodes text for output.
	 */
	@Override
	public Object referenceInsert(String arg0, Object arg1) {
		// We ignore references that start with the prefix
		// we consider to be safe. This allows developers to
		// bypass the automatic encoding mechanism and prepare
		// output themselves.
		if (arg0.startsWith(SAFE_REFERENCE_PREFIX1)||arg0.startsWith(SAFE_REFERENCE_PREFIX2)) {
			return arg1;
		}

		// Give up if there's nothing to output
		if (arg1 == null) {
			return null;
		}

		// Now encode the text using the correct encoder
		return Canoe.encode(arg1.toString(), qlueWriter.currentContext());
	}
}

@@ -920,19 +920,17 @@
 plements
-%0A%09%09
+ 
 Referenc
@@ -955,17 +955,20 @@
 dler %7B%0A%0A
-%09
+    
 public s
@@ -1011,25 +1011,28 @@
 ME = %22_x%22;%0A%0A
-%09
+    
 public stati
@@ -1074,20 +1074,17 @@
 X1 = %22$%22
-%0A%09%09%09
+ 
 + SAFE_R
@@ -1105,17 +1105,20 @@
 + %22.%22;%0A%0A
-%09
+    
 public s
@@ -1165,20 +1165,17 @@
 2 = %22$!%22
-%0A%09%09%09
+ 
 + SAFE_R
@@ -1196,17 +1196,20 @@
 + %22.%22;%0A%0A
-%09
+    
 protecte
@@ -1229,17 +1229,20 @@
 riter;%0A%0A
-%09
+    
 public C
@@ -1291,18 +1291,24 @@
 iter) %7B%0A
-%09%09
+        
 this.qlu
@@ -1333,52 +1333,19 @@
 er;%0A
-%09%7D%0A%0A%09/**%0A%09 * Encodes text for output.%0A%09 */%0A%09
+    %7D%0A%0A    
 @Ove
@@ -1350,17 +1350,20 @@
 verride%0A
-%09
+    
 public O
@@ -1412,18 +1412,24 @@
 arg1) %7B%0A
-%09%09
+        
 // We ig
@@ -1471,18 +1471,24 @@
  prefix%0A
-%09%09
+        
 // we co
@@ -1532,18 +1532,24 @@
 pers to%0A
-%09%09
+        
 // bypas
@@ -1595,18 +1595,24 @@
 prepare%0A
-%09%09
+        
 // outpu
@@ -1625,18 +1625,24 @@
 selves.%0A
-%09%09
+        
 if (arg0
@@ -1680,10 +1680,12 @@
 IX1)
+ 
 %7C%7C
+ 
 arg0
@@ -1719,27 +1719,36 @@
 PREFIX2)) %7B%0A
-%09%09%09
+            
 return arg1;
@@ -1748,23 +1748,35 @@
 n arg1;%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 // Give 
@@ -1807,18 +1807,24 @@
  output%0A
-%09%09
+        
 if (arg1
@@ -1839,11 +1839,20 @@
 ) %7B%0A
-%09%09%09
+            
 retu
@@ -1864,21 +1864,39 @@
 ll;%0A
-%09%09%7D%0A%0A%09%09// Now
+        %7D%0A%0A        // Otherwise
  enc
@@ -1938,10 +1938,16 @@
 der%0A
-%09%09
+        
 retu
@@ -2013,9 +2013,12 @@
 ));%0A
-%09
+    
 %7D%0A%7D%0A

Tidy up.