View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.search;
21  
22  import java.io.Serializable;
23  import java.util.Arrays;
24  import java.util.Hashtable;
25  import java.util.StringTokenizer;
26  
27  import org.xnap.XNap;
28  
29  /***
30   * This class provides the default search filter implementation.
31   */
32  public class DefaultSearchFilter 
33      implements Cloneable, SearchFilter, Serializable {
34  
35      //--- Constant(s) ---
36  
37      //--- Data field(s) ---
38  
39      private Hashtable valueByKey = new Hashtable();
40  
41      //--- Constructor(s) ---
42  
43      public DefaultSearchFilter()
44      {
45      }
46  
47      //--- Method(s) ---
48  
49  	public Object clone()
50  	{
51  		DefaultSearchFilter f = new DefaultSearchFilter();
52  		f.valueByKey = (Hashtable)valueByKey.clone();
53  		return f;
54  	}
55  
56      public Object get(String key)
57      {
58  		return valueByKey.get(key);
59      }
60  
61      /***
62       * Returns the media type.
63       *
64       * @return null, if none was set; the media type, otherwise
65       */
66      public MediaType getMediaType()
67      {
68  		return (MediaType)get(MEDIA_TYPE);
69      }
70  
71      /***
72       * Returns the minimum filesize.
73       *
74       * @return -1, if none was set
75       */
76      public long getMinFilesize()
77      {
78  		Long i = (Long)get(MIN_FILESIZE);
79  		return (i != null) ? i.longValue() : -1;
80      }
81  
82      /***
83       * Returns the maximum filesize.
84       *
85       * @return -1, if none was set
86       */
87      public long getMaxFilesize()
88      {
89  		Long i = (Long)get(MAX_FILESIZE);
90  		return (i != null) ? i.longValue() : -1;
91      }
92  
93      /***
94       * Returns the path.
95       *
96       * @return null, if none was set
97       */
98      public String[] getPath()
99      {
100 		return (String[])get(PATH);
101     }
102 
103     /***
104      * Returns the search text.
105      *
106      * @return null, if none was set; the search text, otherwise
107      */ 
108     public String getText()
109     {
110 		return (String)get(TEXT);
111     }
112 
113     public boolean matches(SearchResult result)
114     {
115 		// match media type
116 		MediaType m = getMediaType();
117 		if (m != null && !m.matches(result.getFilename())) {
118 			return false;
119 		}
120 
121 		// filesize
122 		long i = getMinFilesize();
123 		if (i != -1 && result.getFilesize() < i) {
124 			return false;
125 		}
126 		i = getMaxFilesize();
127 		if (i != -1 && result.getFilesize() > i) {
128 			return false;
129 		}
130 
131 		// match search text
132 		StringTokenizer t = new StringTokenizer(getText(), " ");
133 		String filename = result.getFilename().toUpperCase();
134 	    
135 		while (t.hasMoreTokens()) {
136 			String word = t.nextToken();
137 			// word should not be matched
138 			if (word.startsWith("-") && word.length() > 1) {
139 				word = word.substring(1);
140 				if (!(filename.indexOf(word.toUpperCase()) == -1)) {
141 					return false;
142 				}
143 			}
144 			// word should be matched
145 			else {
146 				if (filename.indexOf(word.toUpperCase()) == -1) {
147 					return false;
148 				}
149 			}
150 		}
151 
152 		String[] path = getPath();
153 		if (path != null && !Arrays.equals(path, result.getPath())) {
154 			return false;
155 		}
156 
157 		return true;
158     }
159 
160     public void put(String key, Object value)
161     {
162 		if (value != null) {
163 			valueByKey.put(key, value);
164 		}
165     }
166 
167     /***
168      * Returns the value of {@link #getText()}.
169      */
170     public String toString()
171     {
172 		return getText();
173     }
174 
175     public void validate() throws Exception
176     {
177 		if (((String)get(SearchFilter.TEXT)).trim().length() == 0) {
178 			throw new Exception(XNap.tr("What are you trying to search for?"));
179 		}
180     }
181 
182 }