1 25 26 package net.killingar.forum.internal; 27 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.regex.Matcher ; 32 import java.util.regex.Pattern ; 33 34 public class FieldData 35 { 36 Map fields = new HashMap (); 37 static final Pattern p = Pattern.compile("[\n\r]\\p{Print}+\\:[\n\r]"); 38 39 public FieldData() 40 { 41 } 42 43 public FieldData(String in) 44 { 45 in = "\n"+in; 46 47 Matcher matcher = p.matcher(in); 48 49 String [] values = p.split(in); 50 51 for (int i = 0; matcher.find(); i++) 52 { 53 String g = matcher.group(); 54 fields.put(g.substring(1, g.length()-2), values[i+1]); 55 } 56 } 57 58 public Map getFields() { return fields; } 59 60 public String getParam(String in) 61 { 62 return (String )fields.get(in); 63 } 64 65 public String toString() 66 { 67 return fields.toString(); 68 } 69 70 public int getSize() 71 { 72 return fields.size(); 73 } 74 75 public String encodeFieldData() 76 { 77 StringBuffer sb = new StringBuffer (); 78 boolean first = true; 79 for (Iterator i = fields.keySet().iterator(); i.hasNext();) 80 { 81 String s = (String )i.next(); 82 if (first) 83 first = false; 84 else 85 sb.append("\n"); 86 87 sb.append(s); 88 sb.append(":\n"); 89 sb.append(fieldEscape((String )fields.get(s))); 90 } 91 92 return sb.toString(); 93 } 94 95 private static String fieldEscape(String s) 96 { 97 if (s == null) 98 return ""; 99 else 100 return s.replaceAll(":\n", ": \n"); 101 } 102 } 103 | Popular Tags |