KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > FieldData


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

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