KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > jsptag > ControlledVocabularyTag


1 /*
2  * ControlledVocabularyTag.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/02/08 14:33:09 $
7  *
8  * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts Institute of
9  * Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met: -
13  * Redistributions of source code must retain the above copyright notice, this
14  * list of conditions and the following disclaimer. - Redistributions in binary
15  * form must reproduce the above copyright notice, this list of conditions and
16  * the following disclaimer in the documentation and/or other materials provided
17  * with the distribution. - Neither the name of the Hewlett-Packard Company nor
18  * the name of the Massachusetts Institute of Technology nor the names of their
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */

34 package org.dspace.app.webui.jsptag;
35
36 import java.io.File JavaDoc;
37 import java.io.FilenameFilter JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.jsp.JspException JavaDoc;
44 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
45
46 import org.apache.log4j.Logger;
47 import org.dspace.app.webui.util.XMLUtil;
48 import org.dspace.core.ConfigurationManager;
49 import org.w3c.dom.Document JavaDoc;
50
51 /**
52  * A Tag to load and display controlled vocabularies
53  *
54  * @author Miguel Ferreira
55  * @version $Revision: 1.1 $
56  *
57  */

58 public class ControlledVocabularyTag extends TagSupport JavaDoc
59 {
60     // path to the jsp that outputs the results of this tag
61
private static final String JavaDoc CONTROLLEDVOCABULARY_JSPTAG = "/controlledvocabulary/controlledvocabularyTag.jsp";
62
63     // the log
64
private static Logger log = Logger.getLogger(ControlledVocabularyTag.class);
65
66     // a tag attribute that contains the words used to trim the vocabulary tree
67
private String JavaDoc filter;
68
69     // a tag attribute that activates multiple selection of vocabulary terms
70
private boolean allowMultipleSelection;
71
72     // a tag attribute that specifies the vocabulary to be displayed
73
private String JavaDoc vocabulary;
74
75     // an hashtable containing all the loaded vocabularies
76
public Hashtable JavaDoc controlledVocabularies;
77
78     /**
79      * Process tag
80      */

81     public int doStartTag() throws JspException JavaDoc
82     {
83         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext
84                 .getRequest();
85
86         String JavaDoc vocabulariesPath = ConfigurationManager
87                 .getProperty("dspace.dir")
88                 + "/config/controlled-vocabularies/";
89         String JavaDoc addonBaseDirectory = pageContext.getServletContext()
90                 .getRealPath("")
91                 + "/controlledvocabulary/";
92         String JavaDoc vocabularyPrunningXSLT = addonBaseDirectory
93                 + "vocabularyprune.xsl";
94         String JavaDoc controlledVocabulary2HtmlXSLT = addonBaseDirectory
95                 + "vocabulary2html.xsl";
96
97         // Load vocabularies on startup
98
controlledVocabularies = (Hashtable JavaDoc) pageContext.getServletContext()
99                 .getAttribute("controlledvocabulary.controlledVocabularies");
100         if (controlledVocabularies == null)
101         {
102             controlledVocabularies = loadControlledVocabularies(vocabulariesPath);
103             pageContext.getServletContext().setAttribute(
104                     "controlledvocabulary.controlledVocabularies",
105                     controlledVocabularies);
106         }
107
108         try
109         {
110             Hashtable JavaDoc prunnedVocabularies = needsFiltering() ? filterVocabularies(
111                     controlledVocabularies, vocabularyPrunningXSLT)
112                     : controlledVocabularies;
113
114             String JavaDoc html = "";
115             if (vocabulary != null && !vocabulary.equals(""))
116             {
117                 html = renderVocabularyAsHTML((Document JavaDoc) prunnedVocabularies
118                         .get(vocabulary + ".xml"),
119                         controlledVocabulary2HtmlXSLT,
120                         isAllowMultipleSelection(), request.getContextPath());
121             }
122             else
123             {
124                 html = renderVocabulariesAsHTML(prunnedVocabularies,
125                         controlledVocabulary2HtmlXSLT,
126                         isAllowMultipleSelection(), request.getContextPath());
127             }
128             request.getSession().setAttribute(
129                     "controlledvocabulary.vocabularyHTML", html);
130
131             pageContext.include(CONTROLLEDVOCABULARY_JSPTAG);
132
133         }
134         catch (Exception JavaDoc e)
135         {
136             log.warn("Exception", e);
137         }
138
139         return SKIP_BODY;
140     }
141
142     /**
143      * End processing tag
144      */

145     public int doEndTag()
146     {
147         return EVAL_PAGE;
148     }
149
150     /**
151      * Do we gave a filter to apply to the controlled vocabularies?
152      *
153      * @return true if a filter was provided.
154      */

155     private boolean needsFiltering()
156     {
157         return getFilter() != null && getFilter().length() > 0;
158     }
159
160     /**
161      * Converts a XML Vocabulary to a HTML tree
162      *
163      * @param vocabularies
164      * A hashtable with all the XML taxonomies/vocabularies loaded as
165      * values
166      * @param xslt
167      * the filename of the stylesheet to apply the XML taxonomies
168      * @param allowMultipleSelection
169      * include checkboxes next to the taxonomy terms
170      * @param contextPath
171      * The context path
172      * @return the HTML that represents the vocabularies
173      */

174     private String JavaDoc renderVocabulariesAsHTML(Hashtable JavaDoc vocabularies,
175             String JavaDoc xslt, boolean allowMultipleSelection, String JavaDoc contextPath)
176     {
177         String JavaDoc result = "";
178         Iterator JavaDoc iter = vocabularies.values().iterator();
179         while (iter.hasNext())
180         {
181             Document JavaDoc controlledVocabularyXML = (Document JavaDoc) iter.next();
182             result += renderVocabularyAsHTML(controlledVocabularyXML, xslt,
183                     allowMultipleSelection, contextPath);
184         }
185         return result;
186     }
187
188     /**
189      * Applies a filter to the vocabularies, i.e. it prunes the trees by
190      * removing all the branches that do not contain the words in the filter.
191      *
192      * @param vocabularies
193      * A hashtable with all the XML taxonomies/vocabularies loaded as
194      * values
195      * @param vocabularyPrunningXSLT
196      * the filename of the stylesheet that trimms the taxonomies
197      * @return An hashtable with all the filtered vocabularies
198      */

199     private Hashtable JavaDoc filterVocabularies(Hashtable JavaDoc vocabularies,
200             String JavaDoc vocabularyPrunningXSLT)
201     {
202         Hashtable JavaDoc prunnedVocabularies = new Hashtable JavaDoc();
203         Enumeration JavaDoc enumeration = vocabularies.keys();
204         while (enumeration.hasMoreElements())
205         {
206             String JavaDoc controlledVocabularyKey = (String JavaDoc) enumeration.nextElement();
207             Document JavaDoc controlledVocabulary = (Document JavaDoc) vocabularies
208                     .get(controlledVocabularyKey);
209             prunnedVocabularies.put(controlledVocabularyKey, filterVocabulary(
210                     controlledVocabulary, vocabularyPrunningXSLT, getFilter()));
211         }
212         return prunnedVocabularies;
213     }
214
215     /**
216      * Renders a taxonomy as HTML by applying a stylesheet.
217      *
218      * @param vocabulary
219      * The XML document representing a taxonomy
220      * @param controlledVocabulary2HtmlXSLT
221      * The filename of the stylesheet that converts the taxonomy to
222      * HTML
223      * @param allowMultipleSelection
224      * include checkboxes next to the taxonomy terms
225      * @param contextPath
226      * The context path
227      * @return the provided taxonomy as HTML.
228      */

229     public String JavaDoc renderVocabularyAsHTML(Document JavaDoc vocabulary,
230             String JavaDoc controlledVocabulary2HtmlXSLT,
231             boolean allowMultipleSelection, String JavaDoc contextPath)
232     {
233         if (vocabulary == null)
234             return "";
235
236         String JavaDoc result = "";
237         try
238         {
239
240             Hashtable JavaDoc parameters = new Hashtable JavaDoc();
241             parameters.put("allowMultipleSelection",
242                     allowMultipleSelection ? "yes" : "no");
243             parameters.put("contextPath", contextPath);
244             result = XMLUtil.transformDocumentAsString(vocabulary, parameters,
245                     controlledVocabulary2HtmlXSLT);
246         }
247         catch (Exception JavaDoc e)
248         {
249             e.printStackTrace();
250         }
251         return result;
252     }
253
254     /**
255      * Applies a filter to the provided vocabulary, i.e. it prunes the tree by
256      * removing all the branches that do not contain the words in the filter.
257      *
258      * @param vocabulary
259      * The vocabulary to be trimmed
260      * @param vocabularyPrunningXSLT
261      * The filename of the stylesheet that trims the vocabulary
262      * @param filter
263      * The filter to be applied
264      * @return The trimmed vocabulary.
265      */

266     public Document JavaDoc filterVocabulary(Document JavaDoc vocabulary,
267             String JavaDoc vocabularyPrunningXSLT, String JavaDoc filter)
268     {
269         if (vocabulary == null)
270             return null;
271
272         try
273         {
274             Hashtable JavaDoc parameters = new Hashtable JavaDoc();
275             parameters.put("filter", filter);
276             Document JavaDoc prunnedVocabulary = XMLUtil.transformDocument(vocabulary,
277                     parameters, vocabularyPrunningXSLT);
278             return prunnedVocabulary;
279         }
280         catch (Exception JavaDoc e)
281         {
282             e.printStackTrace();
283             return null;
284         }
285
286     }
287
288     /**
289      * Loads into memory all the vocabularies found in the given directory. All
290      * files with .xml extension are considered to be controlled vocabularies.
291      *
292      * @param directory
293      * where the files are positioned
294      * @return an hashtable with the filenames of the vocabularies as keys and
295      * the XML documents representing the vocabularies as values.
296      */

297     private static Hashtable JavaDoc loadControlledVocabularies(String JavaDoc directory)
298     {
299         Hashtable JavaDoc controlledVocabularies = new Hashtable JavaDoc();
300         File JavaDoc dir = new File JavaDoc(directory);
301
302         FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc()
303         {
304             public boolean accept(File JavaDoc dir, String JavaDoc name)
305             {
306                 return name.endsWith(".xml");
307             }
308         };
309         String JavaDoc[] children = dir.list(filter);
310
311         if (children != null && children.length > 0)
312         {
313             for (int i = 0; i < children.length; i++)
314             {
315                 String JavaDoc filename = children[i];
316
317                 try
318                 {
319                     Document JavaDoc controlledVocabulary = XMLUtil.loadXML(directory
320                             + filename);
321                     controlledVocabularies.put(filename, controlledVocabulary);
322                     log.warn("Loaded vocabulary: " + filename);
323                 }
324                 catch (Exception JavaDoc e)
325                 {
326                     log.warn("Failed to load vocabulary from " + filename, e);
327                 }
328             }
329         }
330         else
331         {
332             log.warn("Could not find any vocabularies...");
333         }
334         return controlledVocabularies;
335
336     }
337
338     /**
339      * Gets the filter provided as parameter to the tag
340      *
341      * @return the filter
342      */

343     public String JavaDoc getFilter()
344     {
345         return filter;
346     }
347
348     /**
349      * Sets the filter
350      *
351      * @param filter
352      * the filter
353      */

354     public void setFilter(String JavaDoc filter)
355     {
356         this.filter = filter;
357     }
358
359     /**
360      * Returns the value of the multiple selection parameter
361      *
362      * @return true if the multiple selection was selected
363      */

364     public boolean isAllowMultipleSelection()
365     {
366         return allowMultipleSelection;
367     }
368
369     /**
370      * Defines if we want to be able to select multiple terms of the taxonomy
371      *
372      * @param allowMultipleSelection
373      * true if we want to be able to select more than on term
374      */

375     public void setAllowMultipleSelection(boolean allowMultipleSelection)
376     {
377         this.allowMultipleSelection = allowMultipleSelection;
378     }
379
380     /**
381      * Gets the name of the vocabulary to be displayed
382      *
383      * @return the name of the vocabulary
384      */

385     public String JavaDoc getVocabulary()
386     {
387         return vocabulary;
388     }
389
390     /**
391      * Sets the name of the vocabulary to be displayed. If no name is provided,
392      * all vocabularies loaded will be rendered to the output
393      *
394      * @param vocabulary
395      * the name of the vocabulary to be selected
396      */

397     public void setVocabulary(String JavaDoc vocabulary)
398     {
399         this.vocabulary = vocabulary;
400     }
401
402 }
403
Popular Tags