source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/ServiceUtil.java@ 24752

Last change on this file since 24752 was 24752, checked in by sjm84, 13 years ago

Removed a debug output statement

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1package org.greenstone.gsdl3.service;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.HashMap;
6
7import org.apache.log4j.Logger;
8import org.greenstone.gsdl3.util.GSXML;
9import org.w3c.dom.Element;
10
11public class ServiceUtil extends ServiceRack
12{
13 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ServiceUtil.class.getName());
14
15 /**********************************************************
16 * The list of services the utility service rack supports *
17 *********************************************************/
18 protected static final String GET_ALL_IMAGES_IN_COLLECTION = "GetAllImagesInCollection";
19 /*********************************************************/
20
21 String[] services = { GET_ALL_IMAGES_IN_COLLECTION };
22
23 public boolean configure(Element info, Element extra_info)
24 {
25 if (!super.configure(info, extra_info))
26 {
27 return false;
28 }
29
30 logger.info("Configuring ServiceUtil...");
31 this.config_info = info;
32
33 for (int i = 0; i < services.length; i++)
34 {
35 Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
36 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
37 service.setAttribute(GSXML.NAME_ATT, services[i]);
38 this.short_service_info.appendChild(service);
39 }
40
41 return true;
42 }
43
44 protected Element getServiceDescription(String service_id, String lang, String subset)
45 {
46 for (int i = 0; i < services.length; i++)
47 {
48 if (service_id.equals(services[i]))
49 {
50 Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
51 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
52 service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
53 return service_elem;
54 }
55 }
56
57 return null;
58 }
59
60 protected Element processGetAllImagesInCollection(Element request)
61 {
62 Element result = GSXML.createBasicResponse(this.doc, GET_ALL_IMAGES_IN_COLLECTION);
63
64 if (request == null)
65 {
66 GSXML.addError(this.doc, result, GET_ALL_IMAGES_IN_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
67 return result;
68 }
69
70 String lang = request.getAttribute(GSXML.LANG_ATT);
71 String uid = request.getAttribute(GSXML.USER_ID_ATT);
72
73 // Get the parameters of the request
74 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
75
76 if (param_list == null) {
77 GSXML.addError(this.doc, result, GET_ALL_IMAGES_IN_COLLECTION + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
78 return result; // Return the empty result
79 }
80
81 HashMap params = GSXML.extractParams(param_list, false);
82
83 String regex = (String)params.get("extregex");
84 if(regex == null)
85 {
86 GSXML.addError(this.doc, result, GET_ALL_IMAGES_IN_COLLECTION + ": No file name extensions specified", GSXML.ERROR_TYPE_SYNTAX);
87 return result;
88 }
89 regex = ".*" + regex + ".*";
90
91 String collection = (String)params.get("c");
92 if(collection == null)
93 {
94 GSXML.addError(this.doc, result, GET_ALL_IMAGES_IN_COLLECTION + ": No collection specified", GSXML.ERROR_TYPE_SYNTAX);
95 return result;
96 }
97
98 File indexDir = new File(this.site_home + File.separator + "collect" + File.separator + collection + File.separator + "index" + File.separator + "assoc");
99 ArrayList<String> images = new ArrayList<String>();
100 getImagesRecursive(indexDir, regex, images);
101
102 Element imageListElem = this.doc.createElement("imageList");
103 result.appendChild(imageListElem);
104 for(String i : images)
105 {
106 Element imageElem = this.doc.createElement("image");
107 imageElem.appendChild(this.doc.createTextNode(i));
108 imageListElem.appendChild(imageElem);
109 }
110 return result;
111 }
112
113 /********************
114 * Helper functions *
115 *******************/
116
117 protected void getImagesRecursive(File current, String regex, ArrayList<String> images)
118 {
119 if(current.isDirectory())
120 {
121 File[] files = current.listFiles();
122 for(File f : files)
123 {
124 getImagesRecursive(f, regex, images);
125 }
126 }
127 else
128 {
129 String path = current.getAbsolutePath();
130 String filename = path.substring(path.lastIndexOf(File.separator) + File.separator.length());
131
132 if(filename.matches(regex))
133 {
134 images.add(path);
135 }
136 else
137 {
138
139 }
140 }
141 }
142}
Note: See TracBrowser for help on using the repository browser.