1 6 7 28 package javax.xml.soap; 29 30 import java.util.Iterator ; 31 import java.util.Vector ; 32 33 45 public class MimeHeaders { 46 private Vector headers; 47 48 52 public MimeHeaders() { 53 headers = new Vector (); 54 } 55 56 65 public String [] getHeader(String name) { 66 Vector values = new Vector (); 67 68 for(int i = 0; i < headers.size(); i++) { 69 MimeHeader hdr = (MimeHeader ) headers.elementAt(i); 70 if (hdr.getName().equalsIgnoreCase(name) 71 && hdr.getValue() != null) 72 values.addElement(hdr.getValue()); 73 } 74 75 if (values.size() == 0) 76 return null; 77 78 String r[] = new String [values.size()]; 79 values.copyInto(r); 80 return r; 81 } 82 83 99 public void setHeader(String name, String value) 100 { 101 boolean found = false; 102 103 if ((name == null) || name.equals("")) 104 throw new IllegalArgumentException ("Illegal MimeHeader name"); 105 106 for(int i = 0; i < headers.size(); i++) { 107 MimeHeader hdr = (MimeHeader ) headers.elementAt(i); 108 if (hdr.getName().equalsIgnoreCase(name)) { 109 if (!found) { 110 headers.setElementAt(new MimeHeader (hdr.getName(), 111 value), i); 112 found = true; 113 } 114 else 115 headers.removeElementAt(i--); 116 } 117 } 118 119 if (!found) 120 addHeader(name, value); 121 } 122 123 137 public void addHeader(String name, String value) 138 { 139 if ((name == null) || name.equals("")) 140 throw new IllegalArgumentException ("Illegal MimeHeader name"); 141 142 int pos = headers.size(); 143 144 for(int i = pos - 1 ; i >= 0; i--) { 145 MimeHeader hdr = (MimeHeader ) headers.elementAt(i); 146 if (hdr.getName().equalsIgnoreCase(name)) { 147 headers.insertElementAt(new MimeHeader (name, value), 148 i+1); 149 return; 150 } 151 } 152 headers.addElement(new MimeHeader (name, value)); 153 } 154 155 162 public void removeHeader(String name) { 163 for(int i = 0; i < headers.size(); i++) { 164 MimeHeader hdr = (MimeHeader ) headers.elementAt(i); 165 if (hdr.getName().equalsIgnoreCase(name)) 166 headers.removeElementAt(i--); 167 } 168 } 169 170 173 public void removeAllHeaders() { 174 headers.removeAllElements(); 175 } 176 177 178 184 public Iterator getAllHeaders() { 185 return headers.iterator(); 186 } 187 188 class MatchingIterator implements Iterator { 189 private boolean match; 190 private Iterator iterator; 191 private String [] names; 192 private Object nextHeader; 193 194 MatchingIterator(String [] names, boolean match) { 195 this.match = match; 196 this.names = names; 197 this.iterator = headers.iterator(); 198 } 199 200 private Object nextMatch() { 201 next: 202 while (iterator.hasNext()) { 203 MimeHeader hdr = (MimeHeader ) iterator.next(); 204 205 if (names == null) 206 return match ? null : hdr; 207 208 for(int i = 0; i < names.length; i++) 209 if (hdr.getName().equalsIgnoreCase(names[i])) 210 if (match) 211 return hdr; 212 else 213 continue next; 214 if (!match) 215 return hdr; 216 } 217 return null; 218 } 219 220 221 public boolean hasNext() { 222 if (nextHeader == null) 223 nextHeader = nextMatch(); 224 return nextHeader != null; 225 } 226 227 public Object next() { 228 if (nextHeader != null) { 231 Object ret = nextHeader; 232 nextHeader = null; 233 return ret; 234 } 235 if (hasNext()) 236 return nextHeader; 237 return null; 238 } 239 240 public void remove() { 241 iterator.remove(); 242 } 243 } 244 245 246 255 public Iterator getMatchingHeaders(String [] names) { 256 return new MatchingIterator(names, true); 257 } 258 259 268 public Iterator getNonMatchingHeaders(String [] names) { 269 return new MatchingIterator(names, false); 270 } 271 } 272 | Popular Tags |