1 34 package org.dspace.app.webui.jsptag; 35 36 import java.io.File ; 37 import java.io.FilenameFilter ; 38 import java.util.Enumeration ; 39 import java.util.Hashtable ; 40 import java.util.Iterator ; 41 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.jsp.JspException ; 44 import javax.servlet.jsp.tagext.TagSupport ; 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 ; 50 51 58 public class ControlledVocabularyTag extends TagSupport 59 { 60 private static final String CONTROLLEDVOCABULARY_JSPTAG = "/controlledvocabulary/controlledvocabularyTag.jsp"; 62 63 private static Logger log = Logger.getLogger(ControlledVocabularyTag.class); 65 66 private String filter; 68 69 private boolean allowMultipleSelection; 71 72 private String vocabulary; 74 75 public Hashtable controlledVocabularies; 77 78 81 public int doStartTag() throws JspException 82 { 83 HttpServletRequest request = (HttpServletRequest ) pageContext 84 .getRequest(); 85 86 String vocabulariesPath = ConfigurationManager 87 .getProperty("dspace.dir") 88 + "/config/controlled-vocabularies/"; 89 String addonBaseDirectory = pageContext.getServletContext() 90 .getRealPath("") 91 + "/controlledvocabulary/"; 92 String vocabularyPrunningXSLT = addonBaseDirectory 93 + "vocabularyprune.xsl"; 94 String controlledVocabulary2HtmlXSLT = addonBaseDirectory 95 + "vocabulary2html.xsl"; 96 97 controlledVocabularies = (Hashtable ) 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 prunnedVocabularies = needsFiltering() ? filterVocabularies( 111 controlledVocabularies, vocabularyPrunningXSLT) 112 : controlledVocabularies; 113 114 String html = ""; 115 if (vocabulary != null && !vocabulary.equals("")) 116 { 117 html = renderVocabularyAsHTML((Document ) 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 e) 135 { 136 log.warn("Exception", e); 137 } 138 139 return SKIP_BODY; 140 } 141 142 145 public int doEndTag() 146 { 147 return EVAL_PAGE; 148 } 149 150 155 private boolean needsFiltering() 156 { 157 return getFilter() != null && getFilter().length() > 0; 158 } 159 160 174 private String renderVocabulariesAsHTML(Hashtable vocabularies, 175 String xslt, boolean allowMultipleSelection, String contextPath) 176 { 177 String result = ""; 178 Iterator iter = vocabularies.values().iterator(); 179 while (iter.hasNext()) 180 { 181 Document controlledVocabularyXML = (Document ) iter.next(); 182 result += renderVocabularyAsHTML(controlledVocabularyXML, xslt, 183 allowMultipleSelection, contextPath); 184 } 185 return result; 186 } 187 188 199 private Hashtable filterVocabularies(Hashtable vocabularies, 200 String vocabularyPrunningXSLT) 201 { 202 Hashtable prunnedVocabularies = new Hashtable (); 203 Enumeration enumeration = vocabularies.keys(); 204 while (enumeration.hasMoreElements()) 205 { 206 String controlledVocabularyKey = (String ) enumeration.nextElement(); 207 Document controlledVocabulary = (Document ) vocabularies 208 .get(controlledVocabularyKey); 209 prunnedVocabularies.put(controlledVocabularyKey, filterVocabulary( 210 controlledVocabulary, vocabularyPrunningXSLT, getFilter())); 211 } 212 return prunnedVocabularies; 213 } 214 215 229 public String renderVocabularyAsHTML(Document vocabulary, 230 String controlledVocabulary2HtmlXSLT, 231 boolean allowMultipleSelection, String contextPath) 232 { 233 if (vocabulary == null) 234 return ""; 235 236 String result = ""; 237 try 238 { 239 240 Hashtable parameters = new Hashtable (); 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 e) 248 { 249 e.printStackTrace(); 250 } 251 return result; 252 } 253 254 266 public Document filterVocabulary(Document vocabulary, 267 String vocabularyPrunningXSLT, String filter) 268 { 269 if (vocabulary == null) 270 return null; 271 272 try 273 { 274 Hashtable parameters = new Hashtable (); 275 parameters.put("filter", filter); 276 Document prunnedVocabulary = XMLUtil.transformDocument(vocabulary, 277 parameters, vocabularyPrunningXSLT); 278 return prunnedVocabulary; 279 } 280 catch (Exception e) 281 { 282 e.printStackTrace(); 283 return null; 284 } 285 286 } 287 288 297 private static Hashtable loadControlledVocabularies(String directory) 298 { 299 Hashtable controlledVocabularies = new Hashtable (); 300 File dir = new File (directory); 301 302 FilenameFilter filter = new FilenameFilter () 303 { 304 public boolean accept(File dir, String name) 305 { 306 return name.endsWith(".xml"); 307 } 308 }; 309 String [] 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 filename = children[i]; 316 317 try 318 { 319 Document controlledVocabulary = XMLUtil.loadXML(directory 320 + filename); 321 controlledVocabularies.put(filename, controlledVocabulary); 322 log.warn("Loaded vocabulary: " + filename); 323 } 324 catch (Exception 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 343 public String getFilter() 344 { 345 return filter; 346 } 347 348 354 public void setFilter(String filter) 355 { 356 this.filter = filter; 357 } 358 359 364 public boolean isAllowMultipleSelection() 365 { 366 return allowMultipleSelection; 367 } 368 369 375 public void setAllowMultipleSelection(boolean allowMultipleSelection) 376 { 377 this.allowMultipleSelection = allowMultipleSelection; 378 } 379 380 385 public String getVocabulary() 386 { 387 return vocabulary; 388 } 389 390 397 public void setVocabulary(String vocabulary) 398 { 399 this.vocabulary = vocabulary; 400 } 401 402 } 403 | Popular Tags |