KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > util > FilterTextManipulator


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.adapter.util;
22
23 import java.util.Collections JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /** Includes "filters" if the key is present in the filter tag. For
28  * example:
29  *
30  * <p>
31  * "I am a /~size:FAT~/ man."
32  *
33  * <ul>
34  * <li>Would produce "I am a FAT man." if size was a key in the map.</li>
35  * <li>Would produce "I am a man." if size was <b>not</b> a key in the map.</li>
36  * </ul>
37  * </p>
38  *
39  * @author mwilson
40  */

41 public class FilterTextManipulator implements TextManipulator
42 {
43    private String JavaDoc startToken = "/~";
44
45    private String JavaDoc endToken = "~/";
46
47    /**
48     * @see net.mlw.vlh.adapter.util.TextManipulator#manipulate(java.lang.String, java.lang.Object)
49     */

50    public StringBuffer JavaDoc manipulate(StringBuffer JavaDoc text, Map JavaDoc model)
51    {
52       if (model == null)
53       {
54          model = Collections.EMPTY_MAP;
55       }
56
57       boolean inverse = false;
58
59       for (int start = 0, end = text.indexOf(endToken); ((end = text.indexOf(endToken)) >= 0);)
60       {
61          start = text.lastIndexOf(startToken, end);
62          String JavaDoc key = text.substring(start + 2, text.indexOf(":", start));
63
64          //If this is an or statement
65
if (key.indexOf(',') != -1)
66          {
67             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(key, ",");st.hasMoreElements();)
68             {
69                Object JavaDoc modelValue = model.get((key = st.nextToken()));
70                if (modelValue instanceof String JavaDoc && (((String JavaDoc) modelValue).length() == 0))
71                {
72                   continue;
73                }
74                else if (modelValue != null)
75                {
76                   break;
77                }
78             }
79          }
80          else if (key.indexOf('|') != -1)
81          {
82             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(key, "|");st.hasMoreElements();)
83             {
84                Object JavaDoc modelValue = model.get((key = st.nextToken()));
85                if (modelValue instanceof String JavaDoc && (((String JavaDoc) modelValue).length() == 0))
86                {
87                   continue;
88                }
89                else if (modelValue != null)
90                {
91                   break;
92                }
93             }
94          }
95          else if (key.indexOf('&') != -1)
96          {
97             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(key, "&");st.hasMoreElements();)
98             {
99                Object JavaDoc modelValue = model.get((key = st.nextToken()));
100                if (modelValue == null || modelValue instanceof String JavaDoc && (((String JavaDoc) modelValue).length() == 0))
101                {
102                   break;
103                }
104             }
105          }
106
107          if ((inverse = key.startsWith("!")))
108          {
109             key = key.substring(1, key.length());
110          }
111
112          Object JavaDoc modelValue = model.get(key);
113          //If its an empty string replace it with a null.
114
if (modelValue instanceof String JavaDoc && (((String JavaDoc) modelValue).length() == 0))
115          {
116             modelValue = null;
117          }
118
119          if (inverse)
120          {
121             if ((modelValue == null))
122             {
123                text.replace(start, end + 2, text.substring(text.indexOf(":", start) + 1, end));
124             }
125             else
126             {
127                text.replace(start, end + 2, "");
128             }
129          }
130          else
131          {
132             if ((modelValue != null))
133             {
134                text.replace(start, end + 2, text.substring(text.indexOf(":", start) + 1, end));
135             }
136             else
137             {
138                text.replace(start, end + 2, "");
139             }
140          }
141
142       }
143
144       return text;
145    }
146
147    /**
148     * @param endToken The endToken to set.
149     */

150    public void setEndToken(String JavaDoc endToken)
151    {
152       this.endToken = endToken;
153    }
154
155    /**
156     * @param startToken The startToken to set.
157     */

158    public void setStartToken(String JavaDoc startToken)
159    {
160       this.startToken = startToken;
161    }
162 }
Popular Tags