1 package org.tigris.scarab.util; 2 3 48 49 import org.apache.velocity.app.event.ReferenceInsertionEventHandler; 50 import org.apache.velocity.app.event.NullSetEventHandler; 51 52 70 public class ReferenceInsertionFilter 71 implements ReferenceInsertionEventHandler, NullSetEventHandler 72 { 73 public boolean shouldLogOnNullSet(String lhs, String rhs) 74 { 75 return false; 76 } 77 78 public Object referenceInsert(String reference, Object value) 79 { 80 if (value == null) 82 { 83 return null; 84 } 85 86 89 Object result = value; 91 if (value instanceof String ) 92 { 93 if ( 94 !reference.startsWith("$renderer") && 97 !reference.startsWith("$intake.declare") && 99 !reference.startsWith("$l10n") 101 ) 102 { 103 result = filter((String )value); 105 } 106 } 107 else if (!(value instanceof SkipFiltering)) 109 { 110 result = filter(value.toString()); 112 } 113 121 return result; 122 } 123 124 128 public static String filter(String value) 129 { 130 if (value == null) 131 { 132 return (null); 133 } 134 char content[] = new char[value.length()]; 135 value.getChars(0, value.length(), content, 0); 136 StringBuffer result = new StringBuffer (content.length + 50); 137 for (int i = 0; i < content.length; i++) 138 { 139 switch (content[i]) 140 { 141 case '<': 142 result.append("<"); 143 break; 144 case '>': 145 result.append(">"); 146 break; 147 case '&': 148 if (i+1 < content.length && content[i+1] == '#') 149 { 150 result.append('&'); 151 } 152 else 153 { 154 result.append("&"); 155 } 156 break; 157 case '"': 158 result.append("""); 159 break; 160 default: 161 result.append(content[i]); 162 } 163 } 164 return (result.toString()); 165 } 166 } 167 | Popular Tags |