1 58 package org.apache.ecs.filter; 59 60 import org.apache.ecs.Filter; 61 import java.text.StringCharacterIterator ; 62 import java.text.CharacterIterator ; 63 64 73 public class StringFilter extends java.util.Hashtable implements Filter 74 { 75 76 public StringFilter() 77 { 78 super(4); 79 } 80 81 82 public String getInfo() 83 { 84 return "StringFilter"; 85 } 86 87 90 public String process(String to_process) 91 { System.out.println("\nString to Process in StringFilter = "+to_process); 92 String [] value = split(to_process); 93 StringBuffer new_value = new StringBuffer (); 94 for(int x = 0; x < value.length; x++) 95 { 96 if(hasAttribute(value[x])) 97 new_value.append((String )get(value[x])); 98 else 99 new_value.append(value[x]); 100 if(x != value.length - 1) 101 new_value.append(" "); 102 } 103 return(new_value.toString()); 104 } 105 106 109 public Filter addAttribute(String attribute,Object entity) 110 { 111 put(attribute,entity); 112 return(this); 113 } 114 115 118 public Filter removeAttribute(String attribute) 119 { 120 try 121 { 122 remove(attribute); 123 } 124 catch(NullPointerException exc) 125 { } 127 return(this); 128 } 129 130 133 public boolean hasAttribute(String attribute) 134 { 135 return(containsKey(attribute)); 136 } 137 138 142 private String [] split(String to_split) 143 { 144 145 if ( to_split == null || to_split.length() == 0 ) 146 { 147 String [] array = new String [0]; 148 return array; 149 } 150 151 StringBuffer sb = new StringBuffer (to_split.length()+50); 152 StringCharacterIterator sci = new StringCharacterIterator (to_split); 153 int length = 0; 154 155 for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) 156 { 157 if(String.valueOf(c).equals(" ")) 158 length++; 159 else if(sci.getEndIndex()-1 == sci.getIndex()) 160 length++; 161 } 162 163 String [] array = new String [length]; 164 length = 0; 165 String tmp = new String (); 166 for (char c = sci.first(); c!= CharacterIterator.DONE; c = sci.next()) 167 { 168 if(String.valueOf(c).equals(" ")) 169 { 170 array[length] = tmp; 171 tmp = new String (); 172 length++; 173 } 174 else if(sci.getEndIndex()-1 == sci.getIndex()) 175 { 176 tmp = tmp+String.valueOf(sci.last()); 177 array[length] = tmp; 178 tmp = new String (); 179 length++; 180 } 181 else 182 tmp += String.valueOf(c); 183 } 184 return(array); 185 } 186 } 187 | Popular Tags |