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.util.*;
23  import java.util.Iterator;
24  import java.util.StringTokenizer;
25  
26  import javax.swing.Icon;
27  
28  /***
29   * This class is the default implementation for search results.
30   */
31  public abstract class AbstractSearchResult implements SearchResult {
32  
33      // --- Constants(s) ---
34  
35      //--- Data Field(s) ---
36  
37      private TreeMap valueByKey = new TreeMap();
38      private String separatorChars;
39  
40      //--- Constructor(s) ---
41  
42      /***
43       * Constructs the search result object.
44       *
45       * @param separatorCharss the path separator chars
46       */
47      public AbstractSearchResult(String separatorChars)
48      {
49  		this.separatorChars = separatorChars;
50      }
51  
52      public AbstractSearchResult()
53      {
54  		this("");
55      }
56  
57      // --- Method(s) ---
58  
59      /***
60       * Returns a value. Uses a {@link Hashtable Hashtable}
61       * to retreive the value.
62       *
63       * @param key the key 
64       */
65      public Object get(String key)
66      {
67  		return valueByKey.get(key);
68      }
69  
70      /***
71       * Returns null.
72       */
73      public Icon getIcon()
74      {
75  		return null;
76      }
77  
78      /***
79       * Returns the path. Uses a 
80       * {@link StringTokenizer StringTokenizer} to split
81       * {@link #getFilename()} by 
82       * <code>separatorCharss</code>.
83       */
84      public String[] getPath()
85      {
86  		StringTokenizer t = new StringTokenizer(getFilename(), separatorChars);
87  		if (t.countTokens() > 1) {
88  			String[] path = new String[t.countTokens() - 1];
89  			for (int i = 0; i < path.length; i++) {
90  				path[i] = t.nextToken();
91  			}
92  
93  			return path;
94  		}
95  		else {
96  			return null;
97  		}
98      }
99  
100     /***
101      * Splits {@link #getFilename()} by the last
102      * occurence of <code>separatorChars</code> and returns the remainder.
103      */
104     public String getShortFilename() 
105     {
106 		if (separatorChars.length() > 0) {
107 			String s = getFilename();
108 			// search for the last occurence of any separator char
109 			int index = -1;
110 			for (int i = 0; i < separatorChars.length(); i++) {
111 				index = Math.max
112 					(index, s.lastIndexOf(separatorChars.charAt(i)));
113 			}
114 			return s.substring(index + 1);
115 		}
116 		else {
117 			return getFilename();
118 		}
119     }
120 
121 	/***
122 	 * Returns 1.
123 	 */
124 	public int getSourcesCount()
125 	{
126 		return 1;
127 	}
128 
129     public Iterator keys()
130     {
131 		return valueByKey.keySet().iterator();
132     }
133 
134     /***
135      * Sets the value of <code>key</code> to <code>value</code>.
136      */
137     public void put(String key, Object value)
138     {
139 		if (key != null && value != null) {
140 			valueByKey.put(key, value);
141 		}
142     }
143 
144 }