Changeset 24812 for main/trunk/greenstone3
- Timestamp:
- 2011-11-14T13:01:00+13:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/DocumentAction.java
r24116 r24812 1 1 /* 2 * DocumentAction.java3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org4 *5 * This program is free software; you can redistribute it and/or modify6 * it under the terms of the GNU General Public License as published by7 * the Free Software Foundation; either version 2 of the License, or8 * (at your option) any later version.9 *10 * This program is distributed in the hope that it will be useful,11 * but WITHOUT ANY WARRANTY; without even the implied warranty of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU General Public License16 * along with this program; if not, write to the Free Software17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18 */2 * DocumentAction.java 3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 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., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 19 package org.greenstone.gsdl3.action; 20 20 … … 24 24 25 25 // XML classes 26 import org.w3c.dom.Document; 27 import org.w3c.dom.Element; 28 import org.w3c.dom.Node; 26 import org.w3c.dom.Document; 27 import org.w3c.dom.Element; 28 import org.w3c.dom.Node; 29 29 import org.w3c.dom.Text; 30 30 import org.w3c.dom.NodeList; … … 38 38 import org.apache.log4j.*; 39 39 40 /** Action class for retrieving Documents via the message router41 */ 42 public class DocumentAction extends Action{40 /** Action class for retrieving Documents via the message router */ 41 public class DocumentAction extends Action 42 { 43 43 44 44 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.DocumentAction.class.getName()); … … 48 48 public static final String GOTO_PAGE_ARG = "gp"; 49 49 public static final String ENRICH_DOC_ARG = "end"; 50 51 /** if this is set to true, when a document is displayed, any annotation 52 * type services (enrich) will be offered to the user as well */ 53 protected boolean provide_annotations = false; 54 50 51 /** 52 * if this is set to true, when a document is displayed, any annotation type 53 * services (enrich) will be offered to the user as well 54 */ 55 protected boolean provide_annotations = false; 56 55 57 protected boolean highlight_query_terms = false; 56 58 57 public boolean configure() { 59 public boolean configure() 60 { 58 61 super.configure(); 59 String highlight = (String)config_params.get("highlightQueryTerms"); 60 if (highlight != null && highlight.equals("true")) { 62 String highlight = (String) config_params.get("highlightQueryTerms"); 63 if (highlight != null && highlight.equals("true")) 64 { 61 65 highlight_query_terms = true; 62 66 } 63 String annotate = (String)config_params.get("displayAnnotationService"); 64 if (annotate != null && annotate.equals("true")) { 67 String annotate = (String) config_params.get("displayAnnotationService"); 68 if (annotate != null && annotate.equals("true")) 69 { 65 70 provide_annotations = true; 66 71 } 67 72 return true; 68 73 } 69 public Node process (Node message_node) 74 75 public Node process(Node message_node) 70 76 { 71 77 // for now, no subaction eventually we may want to have subactions such as text assoc or something ? 72 78 73 79 Element message = this.converter.nodeToElement(message_node); 74 80 … … 79 85 80 86 // get the request - assume only one 81 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);82 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);87 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 88 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 83 89 HashMap params = GSXML.extractParams(cgi_paramList, false); 84 90 85 91 // just in case there are some that need to get passed to the services 86 HashMap service_params = (HashMap)params.get("s0"); 87 88 92 HashMap service_params = (HashMap) params.get("s0"); 93 89 94 String has_rl = null; 90 95 String has_href = null; … … 95 100 String uid = request.getAttribute(GSXML.USER_ID_ATT); 96 101 String document_name = (String) params.get(GSParams.DOCUMENT); 97 if ((document_name == null || document_name.equals("")) && (has_href == null || has_href.equals(""))) { 102 if ((document_name == null || document_name.equals("")) && (has_href == null || has_href.equals(""))) 103 { 98 104 logger.error("no document specified!"); 99 105 return result; 100 106 } 101 107 String document_type = (String) params.get(GSParams.DOCUMENT_TYPE); 102 if (document_type == null) { 108 if (document_type == null) 109 { 103 110 document_type = "simple"; 104 111 } … … 106 113 boolean get_siblings = false; 107 114 String sibs = (String) params.get(SIBLING_ARG); 108 if (sibs != null && sibs.equals("1")) { 115 if (sibs != null && sibs.equals("1")) 116 { 109 117 get_siblings = true; 110 118 } 111 119 112 120 String sibling_num = (String) params.get(GOTO_PAGE_ARG); 113 if (sibling_num != null && !sibling_num.equals("")) { 121 if (sibling_num != null && !sibling_num.equals("")) 122 { 114 123 // we have to modify the doc name 115 document_name = document_name +"."+sibling_num+".ss";116 } 117 124 document_name = document_name + "." + sibling_num + ".ss"; 125 } 126 118 127 boolean expand_document = false; 119 128 String ed_arg = (String) params.get(GSParams.EXPAND_DOCUMENT); 120 if (ed_arg != null && ed_arg.equals("1")) { 129 if (ed_arg != null && ed_arg.equals("1")) 130 { 121 131 expand_document = true; 122 132 } 123 124 133 125 134 boolean expand_contents = false; 126 if (expand_document) { // we always expand the contents with the text 135 if (expand_document) 136 { // we always expand the contents with the text 127 137 expand_contents = true; 128 } else { 138 } 139 else 140 { 129 141 String ec_arg = (String) params.get(GSParams.EXPAND_CONTENTS); 130 if (ec_arg != null && ec_arg.equals("1")) { 142 if (ec_arg != null && ec_arg.equals("1")) 143 { 131 144 expand_contents = true; 132 145 } … … 134 147 135 148 //append site metadata 136 addSiteMetadata( 149 addSiteMetadata(page_response, lang, uid); 137 150 138 151 // get the additional data needed for the page 139 152 getBackgroundData(page_response, collection, lang, uid); 140 Element format_elem = (Element) GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM);141 153 Element format_elem = (Element) GSXML.getChildByTagName(page_response, GSXML.FORMAT_ELEM); 154 142 155 // the_document is where all the doc info - structure and metadata etc 143 156 // is added into, to be returned in the page … … 149 162 150 163 // create a basic doc list containing the current node 151 Element basic_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM +GSXML.LIST_MODIFIER);164 Element basic_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 152 165 Element current_doc = this.doc.createElement(GSXML.DOC_NODE_ELEM); 153 166 basic_doc_list.appendChild(current_doc); 154 if (document_name.length()!=0){ 167 if (document_name.length() != 0) 168 { 155 169 current_doc.setAttribute(GSXML.NODE_ID_ATT, document_name); 156 }else if (has_href.length()!=0){ 170 } 171 else if (has_href.length() != 0) 172 { 157 173 current_doc.setAttribute(GSXML.NODE_ID_ATT, has_href); 158 174 current_doc.setAttribute("externalURL", has_rl); … … 160 176 161 177 // Create a parameter list to specify the required structure information 162 Element ds_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 163 164 if (service_params != null) { 178 Element ds_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 179 180 if (service_params != null) 181 { 165 182 GSXML.addParametersToList(this.doc, ds_param_list, service_params); 166 183 } 167 184 168 Element ds_param = null; 185 Element ds_param = null; 169 186 boolean get_structure = false; 170 187 boolean get_structure_info = false; 171 if (document_type.equals("paged")) { 188 if (document_type.equals("paged")) 189 { 172 190 get_structure_info = true; 173 191 // get teh info needed for paged naviagtion … … 184 202 ds_param.setAttribute(GSXML.NAME_ATT, "info"); 185 203 ds_param.setAttribute(GSXML.VALUE_ATT, "siblingPosition"); 186 187 } else if (document_type.equals("hierarchy")){ 204 205 } 206 else if (document_type.equals("hierarchy")) 207 { 188 208 get_structure = true; 189 if (expand_contents) { 209 if (expand_contents) 210 { 190 211 ds_param = this.doc.createElement(GSXML.PARAM_ELEM); 191 212 ds_param_list.appendChild(ds_param); 192 213 ds_param.setAttribute(GSXML.NAME_ATT, "structure"); 193 214 ds_param.setAttribute(GSXML.VALUE_ATT, "entire"); 194 } else { 215 } 216 else 217 { 195 218 // get the info needed for table of contents 196 219 ds_param = this.doc.createElement(GSXML.PARAM_ELEM); … … 202 225 ds_param.setAttribute(GSXML.NAME_ATT, "structure"); 203 226 ds_param.setAttribute(GSXML.VALUE_ATT, "children"); 204 if (get_siblings) { 227 if (get_siblings) 228 { 205 229 ds_param = this.doc.createElement(GSXML.PARAM_ELEM); 206 230 ds_param_list.appendChild(ds_param); … … 209 233 } 210 234 } 211 } else { 235 } 236 else 237 { 212 238 // we dont need any structure 213 239 } 214 240 215 241 boolean has_dummy = false; 216 if (get_structure || get_structure_info) { 242 if (get_structure || get_structure_info) 243 { 217 244 218 245 // Build a request to obtain the document structure 219 246 Element ds_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 220 247 String to = GSPath.appendLink(collection, "DocumentStructureRetrieve");// Hard-wired? 221 Element ds_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);248 Element ds_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid); 222 249 ds_message.appendChild(ds_request); 223 250 ds_request.appendChild(ds_param_list); 224 251 225 252 // create a doc_node_list and put in the doc_node that we are interested in 226 253 ds_request.appendChild(basic_doc_list); 227 254 228 255 // Process the document structure retrieve message 229 256 Element ds_response_message = (Element) this.mr.process(ds_message); 230 if (processErrorElements(ds_response_message, page_response)) { 257 if (processErrorElements(ds_response_message, page_response)) 258 { 231 259 return result; 232 260 } 233 261 234 262 // get the info and print out 235 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM +GSXML.LIST_MODIFIER);263 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 236 264 path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM); 237 265 path = GSPath.appendLink(path, "nodeStructureInfo"); 238 266 Element ds_response_struct_info = (Element) GSXML.getNodeByPath(ds_response_message, path); 239 267 // get the doc_node bit 240 if (ds_response_struct_info != null) { 241 the_document.appendChild(this.doc.importNode(ds_response_struct_info, true)); 242 } 243 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER); 268 if (ds_response_struct_info != null) 269 { 270 the_document.appendChild(this.doc.importNode(ds_response_struct_info, true)); 271 } 272 path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 244 273 path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM); 245 274 path = GSPath.appendLink(path, GSXML.NODE_STRUCTURE_ELEM); 246 275 Element ds_response_structure = (Element) GSXML.getNodeByPath(ds_response_message, path); 247 248 if (ds_response_structure != null) { 276 277 if (ds_response_structure != null) 278 { 249 279 // add the contents of the structure bit into the_document 250 280 NodeList structs = ds_response_structure.getChildNodes(); 251 for (int i=0; i<structs.getLength();i++) { 281 for (int i = 0; i < structs.getLength(); i++) 282 { 252 283 the_document.appendChild(this.doc.importNode(structs.item(i), true)); 253 284 } 254 } else { 285 } 286 else 287 { 255 288 // no structure nodes, so put in a dummy doc node 256 289 Element doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM); 257 if (document_name.length()!=0){ 290 if (document_name.length() != 0) 291 { 258 292 doc_node.setAttribute(GSXML.NODE_ID_ATT, document_name); 259 }else if (has_href.length()!=0){ 293 } 294 else if (has_href.length() != 0) 295 { 260 296 doc_node.setAttribute(GSXML.NODE_ID_ATT, has_href); 261 297 doc_node.setAttribute("externalURL", has_rl); … … 264 300 has_dummy = true; 265 301 } 266 } else { // a simple type - we dont have a dummy node for simple 302 } 303 else 304 { // a simple type - we dont have a dummy node for simple 267 305 // should think about this more 268 306 // no structure request, so just put in a dummy doc node 269 307 Element doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM); 270 if (document_name.length()!=0){ 308 if (document_name.length() != 0) 309 { 271 310 doc_node.setAttribute(GSXML.NODE_ID_ATT, document_name); 272 }else if (has_href.length()!=0){ 311 } 312 else if (has_href.length() != 0) 313 { 273 314 doc_node.setAttribute(GSXML.NODE_ID_ATT, has_href); 274 315 doc_node.setAttribute("externalURL", has_rl); … … 277 318 has_dummy = true; 278 319 } 279 320 280 321 // Build a request to obtain some document metadata 281 322 Element dm_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 282 String to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); 323 String to = GSPath.appendLink(collection, "DocumentMetadataRetrieve"); // Hard-wired? 283 324 Element dm_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid); 284 325 dm_message.appendChild(dm_request); 285 326 // Create a parameter list to specify the required metadata information 286 327 287 328 HashSet meta_names = new HashSet(); 288 329 meta_names.add("Title"); // the default 289 if (format_elem != null) { 330 if (format_elem != null) 331 { 290 332 extractMetadataNames(format_elem, meta_names); 291 333 } 292 334 293 335 Element dm_param_list = createMetadataParamList(meta_names); 294 if (service_params != null) { 336 if (service_params != null) 337 { 295 338 GSXML.addParametersToList(this.doc, dm_param_list, service_params); 296 339 } 297 340 298 341 dm_request.appendChild(dm_param_list); 299 300 342 301 343 // create the doc node list for the metadata request 302 Element dm_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM +GSXML.LIST_MODIFIER);344 Element dm_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 303 345 dm_request.appendChild(dm_doc_list); 304 346 305 347 // Add each node from the structure response into the metadata request 306 348 NodeList doc_nodes = the_document.getElementsByTagName(GSXML.DOC_NODE_ELEM); 307 for (int i = 0; i < doc_nodes.getLength(); i++) { 349 for (int i = 0; i < doc_nodes.getLength(); i++) 350 { 308 351 Element doc_node = (Element) doc_nodes.item(i); 309 352 String doc_node_id = doc_node.getAttribute(GSXML.NODE_ID_ATT); … … 313 356 dm_doc_list.appendChild(dm_doc_node); 314 357 dm_doc_node.setAttribute(GSXML.NODE_ID_ATT, doc_node_id); 315 dm_doc_node.setAttribute(GSXML.NODE_TYPE_ATT, 316 doc_node.getAttribute(GSXML.NODE_TYPE_ATT)); 358 dm_doc_node.setAttribute(GSXML.NODE_TYPE_ATT, doc_node.getAttribute(GSXML.NODE_TYPE_ATT)); 317 359 } 318 360 … … 321 363 Element doc_meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid); 322 364 dm_message.appendChild(doc_meta_request); 323 Element doc_meta_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 324 if (service_params != null) { 365 Element doc_meta_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 366 if (service_params != null) 367 { 325 368 GSXML.addParametersToList(this.doc, doc_meta_param_list, service_params); 326 369 } … … 333 376 334 377 // create the doc node list for the metadata request 335 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM +GSXML.LIST_MODIFIER);378 Element doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 336 379 doc_meta_request.appendChild(doc_list); 337 380 338 381 Element doc_node = this.doc.createElement(GSXML.DOC_NODE_ELEM); 339 382 // the node we want is the root document node 340 if (document_name.length()!=0){ 341 doc_node.setAttribute(GSXML.NODE_ID_ATT, document_name+".rt"); 342 }else if (has_href.length()!=0){ 343 doc_node.setAttribute(GSXML.NODE_ID_ATT, has_href+".rt"); 383 if (document_name.length() != 0) 384 { 385 doc_node.setAttribute(GSXML.NODE_ID_ATT, document_name + ".rt"); 386 } 387 else if (has_href.length() != 0) 388 { 389 doc_node.setAttribute(GSXML.NODE_ID_ATT, has_href + ".rt"); 344 390 doc_node.setAttribute("externalURL", has_rl); 345 391 } 346 392 doc_list.appendChild(doc_node); 347 393 Element dm_response_message = (Element) this.mr.process(dm_message); 348 if (processErrorElements(dm_response_message, page_response)) { 394 if (processErrorElements(dm_response_message, page_response)) 395 { 349 396 return result; 350 397 } 351 398 352 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM +GSXML.LIST_MODIFIER);399 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 353 400 Element dm_response_doc_list = (Element) GSXML.getNodeByPath(dm_response_message, path); 354 401 355 402 // Merge the metadata with the structure information 356 403 NodeList dm_response_docs = dm_response_doc_list.getChildNodes(); 357 for (int i = 0; i < doc_nodes.getLength(); i++) { 404 for (int i = 0; i < doc_nodes.getLength(); i++) 405 { 358 406 GSXML.mergeMetadataLists(doc_nodes.item(i), dm_response_docs.item(i)); 359 407 } 360 408 // get the top level doc metadata out 361 Element doc_meta_response = (Element) dm_response_message.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1);362 Element top_doc_node = (Element) GSXML.getNodeByPath(doc_meta_response, "documentNodeList/documentNode");409 Element doc_meta_response = (Element) dm_response_message.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1); 410 Element top_doc_node = (Element) GSXML.getNodeByPath(doc_meta_response, "documentNodeList/documentNode"); 363 411 GSXML.mergeMetadataLists(the_document, top_doc_node); 364 412 365 413 // Build a request to obtain some document content 366 414 Element dc_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 367 to = GSPath.appendLink(collection, "DocumentContentRetrieve"); 415 to = GSPath.appendLink(collection, "DocumentContentRetrieve"); // Hard-wired? 368 416 Element dc_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid); 369 417 dc_message.appendChild(dc_request); 370 371 418 372 419 // Create a parameter list to specify the request parameters - empty for now 373 Element dc_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 374 if (service_params != null) { 420 Element dc_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 421 if (service_params != null) 422 { 375 423 GSXML.addParametersToList(this.doc, dc_param_list, service_params); 376 424 } … … 380 428 // get the content 381 429 // the doc list for the content request is the same as the one for the structure request unless we want the whole document, in which case its the same as for the metadata request. 382 if (expand_document) { 430 if (expand_document) 431 { 383 432 dc_request.appendChild(dm_doc_list); 384 } else { 433 } 434 else 435 { 385 436 dc_request.appendChild(basic_doc_list); 386 437 } 387 logger.debug("request = " +converter.getString(dc_message));438 logger.debug("request = " + converter.getString(dc_message)); 388 439 Element dc_response_message = (Element) this.mr.process(dc_message); 389 if (processErrorElements(dc_response_message, page_response)) { 440 if (processErrorElements(dc_response_message, page_response)) 441 { 390 442 return result; 391 443 } … … 393 445 Element dc_response_doc_list = (Element) GSXML.getNodeByPath(dc_response_message, path); 394 446 395 if (expand_document) { 447 if (expand_document) 448 { 396 449 // Merge the content with the structure information 397 450 NodeList dc_response_docs = dc_response_doc_list.getChildNodes(); 398 for (int i = 0; i < doc_nodes.getLength(); i++) { 399 Node content = GSXML.getChildByTagName((Element)dc_response_docs.item(i), "nodeContent"); 400 if (content != null) { 401 if (highlight_query_terms) { 402 content = highlightQueryTerms(request, (Element)content); 451 for (int i = 0; i < doc_nodes.getLength(); i++) 452 { 453 Node content = GSXML.getChildByTagName((Element) dc_response_docs.item(i), "nodeContent"); 454 if (content != null) 455 { 456 if (highlight_query_terms) 457 { 458 content = highlightQueryTerms(request, (Element) content); 403 459 } 404 460 doc_nodes.item(i).appendChild(this.doc.importNode(content, true)); … … 406 462 //GSXML.mergeMetadataLists(doc_nodes.item(i), dm_response_docs.item(i)); 407 463 } 408 } else { 464 } 465 else 466 { 409 467 //path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM); 410 468 Element dc_response_doc = (Element) GSXML.getChildByTagName(dc_response_doc_list, GSXML.DOC_NODE_ELEM); 411 469 Element dc_response_doc_content = (Element) GSXML.getChildByTagName(dc_response_doc, GSXML.NODE_CONTENT_ELEM); 412 470 Element dc_response_doc_external = (Element) GSXML.getChildByTagName(dc_response_doc, "external"); 413 414 if (dc_response_doc_content == null) { 471 472 if (dc_response_doc_content == null) 473 { 415 474 // no content to add 416 if (dc_response_doc_external !=null){ 475 if (dc_response_doc_external != null) 476 { 417 477 String modified_doc_id = dc_response_doc.getAttribute(GSXML.NODE_ID_ATT); 418 478 419 479 the_document.setAttribute("selectedNode", modified_doc_id); 420 480 the_document.setAttribute("external", dc_response_doc_external.getAttribute("external_link")); … … 422 482 return result; 423 483 } 424 if (highlight_query_terms) { 484 if (highlight_query_terms) 485 { 425 486 dc_response_doc.removeChild(dc_response_doc_content); 426 487 427 488 dc_response_doc_content = highlightQueryTerms(request, dc_response_doc_content); 428 489 dc_response_doc.appendChild(dc_response_doc.getOwnerDocument().importNode(dc_response_doc_content, true)); 429 490 } 430 431 432 if (provide_annotations) { 433 String service_selected = (String)params.get(ENRICH_DOC_ARG); 434 if (service_selected != null && service_selected.equals("1")) { 491 492 if (provide_annotations) 493 { 494 String service_selected = (String) params.get(ENRICH_DOC_ARG); 495 if (service_selected != null && service_selected.equals("1")) 496 { 435 497 // now we can modifiy the response doc if needed 436 String enrich_service = (String) params.get(GSParams.SERVICE);498 String enrich_service = (String) params.get(GSParams.SERVICE); 437 499 // send a message to the service 438 500 Element enrich_message = this.doc.createElement(GSXML.MESSAGE_ELEM); … … 440 502 enrich_message.appendChild(enrich_request); 441 503 // check for parameters 442 HashMap e_service_params = (HashMap)params.get("s1"); 443 if (e_service_params != null) { 444 Element enrich_pl = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 445 GSXML.addParametersToList(this.doc, enrich_pl, e_service_params); 504 HashMap e_service_params = (HashMap) params.get("s1"); 505 if (e_service_params != null) 506 { 507 Element enrich_pl = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 508 GSXML.addParametersToList(this.doc, enrich_pl, e_service_params); 446 509 enrich_request.appendChild(enrich_pl); 447 510 } 448 Element e_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM +GSXML.LIST_MODIFIER);511 Element e_doc_list = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER); 449 512 enrich_request.appendChild(e_doc_list); 450 513 e_doc_list.appendChild(this.doc.importNode(dc_response_doc, true)); 451 514 452 515 Node enrich_response = this.mr.process(enrich_message); 453 454 String [] links = {GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER, GSXML.DOC_NODE_ELEM, GSXML.NODE_CONTENT_ELEM};516 517 String[] links = { GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER, GSXML.DOC_NODE_ELEM, GSXML.NODE_CONTENT_ELEM }; 455 518 path = GSPath.createPath(links); 456 dc_response_doc_content = (Element) GSXML.getNodeByPath(enrich_response, path);457 458 } 519 dc_response_doc_content = (Element) GSXML.getNodeByPath(enrich_response, path); 520 521 } 459 522 } // if provide_annotations 460 523 461 462 524 // use the returned id rather than the sent one cos there may have 463 525 // been modifiers such as .pr that are removed. 464 526 String modified_doc_id = dc_response_doc.getAttribute(GSXML.NODE_ID_ATT); 465 527 the_document.setAttribute("selectedNode", modified_doc_id); 466 if (has_dummy) { 528 if (has_dummy) 529 { 467 530 // change the id if necessary and add the content 468 Element dummy_node = (Element) doc_nodes.item(0);469 531 Element dummy_node = (Element) doc_nodes.item(0); 532 470 533 dummy_node.setAttribute(GSXML.NODE_ID_ATT, modified_doc_id); 471 534 dummy_node.appendChild(this.doc.importNode(dc_response_doc_content, true)); 472 535 // hack for simple type 473 if (document_type.equals("simple")) { 536 if (document_type.equals("simple")) 537 { 474 538 // we dont want the internal docNode, just want the content and metadata in the document 475 539 // rethink this!! … … 478 542 NodeList dummy_children = dummy_node.getChildNodes(); 479 543 //for (int i=0; i<dummy_children.getLength(); i++) { 480 for (int i=dummy_children.getLength()-1; i>=0; i--) { 544 for (int i = dummy_children.getLength() - 1; i >= 0; i--) 545 { 481 546 // special case as we don't want more than one metadata list 482 if (dummy_children.item(i).getNodeName().equals(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER)) { 547 if (dummy_children.item(i).getNodeName().equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER)) 548 { 483 549 GSXML.mergeMetadataFromList(the_document, dummy_children.item(i)); 484 } else { 550 } 551 else 552 { 485 553 the_document.appendChild(dummy_children.item(i)); 486 554 } 487 555 } 488 556 } 489 } else { 557 } 558 else 559 { 490 560 // Merge the document content with the metadata and structure information 491 for (int i = 0; i < doc_nodes.getLength(); i++) { 561 for (int i = 0; i < doc_nodes.getLength(); i++) 562 { 492 563 Node dn = doc_nodes.item(i); 493 String dn_id = ((Element)dn).getAttribute(GSXML.NODE_ID_ATT); 494 if (dn_id.equals(modified_doc_id)) { 564 String dn_id = ((Element) dn).getAttribute(GSXML.NODE_ID_ATT); 565 if (dn_id.equals(modified_doc_id)) 566 { 495 567 dn.appendChild(this.doc.importNode(dc_response_doc_content, true)); 496 568 break; … … 502 574 return result; 503 575 } 504 505 /** tell the param class what its arguments are 506 * if an action has its own arguments, this should add them to the params 507 * object - particularly important for args that should not be saved */ 508 public boolean getActionParameters(GSParams params) { 576 577 /** 578 * tell the param class what its arguments are if an action has its own 579 * arguments, this should add them to the params object - particularly 580 * important for args that should not be saved 581 */ 582 public boolean getActionParameters(GSParams params) 583 { 509 584 params.addParameter(GOTO_PAGE_ARG, false); 510 585 params.addParameter(ENRICH_DOC_ARG, false); … … 512 587 } 513 588 514 515 /** this method gets the collection description, the format info, the516 * list of enrich services, etc - stuff that is needed for the page,517 * but is the same whatever the query is - should be cached */518 protected boolean getBackgroundData(Element page_response,519 String collection, String lang,520 String uid){589 /** 590 * this method gets the collection description, the format info, the list of 591 * enrich services, etc - stuff that is needed for the page, but is the same 592 * whatever the query is - should be cached 593 */ 594 protected boolean getBackgroundData(Element page_response, String collection, String lang, String uid) 595 { 521 596 522 597 // create a message to process - contains requests for the collection … … 531 606 // the enrich_services request - only do this if provide_annotations is true 532 607 533 if (provide_annotations) { 608 if (provide_annotations) 609 { 534 610 Element enrich_services_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid); 535 611 enrich_services_request.setAttribute(GSXML.INFO_ATT, "serviceList"); 536 612 info_message.appendChild(enrich_services_request); 537 613 } 538 539 Element info_response = (Element) this.mr.process(info_message);614 615 Element info_response = (Element) this.mr.process(info_message); 540 616 541 617 // the collection is the first response 542 618 NodeList responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM); 543 619 Element format_resp = (Element) responses.item(0); 544 545 Element format_elem = (Element)GSXML.getChildByTagName(format_resp, GSXML.FORMAT_ELEM); 546 if (format_elem != null) { 620 621 Element format_elem = (Element) GSXML.getChildByTagName(format_resp, GSXML.FORMAT_ELEM); 622 if (format_elem != null) 623 { 547 624 logger.debug("doc action found a format statement"); 548 625 // set teh format type 549 format_elem.setAttribute(GSXML.TYPE_ATT, "display"); 626 format_elem.setAttribute(GSXML.TYPE_ATT, "display"); 550 627 page_response.appendChild(this.doc.importNode(format_elem, true)); 551 628 } 552 629 553 if (provide_annotations) { 554 Element services_resp = (Element)responses.item(1); 630 if (provide_annotations) 631 { 632 Element services_resp = (Element) responses.item(1); 555 633 556 634 // a new message for the mr 557 635 Element enrich_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 558 636 559 637 NodeList e_services = services_resp.getElementsByTagName(GSXML.SERVICE_ELEM); 560 638 boolean service_found = false; 561 for (int j=0; j<e_services.getLength(); j++) { 562 if (((Element)e_services.item(j)).getAttribute(GSXML.TYPE_ATT).equals("enrich")) { 563 Element s = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, ((Element)e_services.item(j)).getAttribute(GSXML.NAME_ATT), lang, uid); 639 for (int j = 0; j < e_services.getLength(); j++) 640 { 641 if (((Element) e_services.item(j)).getAttribute(GSXML.TYPE_ATT).equals("enrich")) 642 { 643 Element s = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, ((Element) e_services.item(j)).getAttribute(GSXML.NAME_ATT), lang, uid); 564 644 enrich_message.appendChild(s); 565 645 service_found = true; 566 646 } 567 647 } 568 if (service_found) { 569 Element enrich_response = (Element)this.mr.process(enrich_message); 570 648 if (service_found) 649 { 650 Element enrich_response = (Element) this.mr.process(enrich_message); 651 571 652 NodeList e_responses = enrich_response.getElementsByTagName(GSXML.RESPONSE_ELEM); 572 653 Element service_list = this.doc.createElement(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER); 573 for (int i=0; i<e_responses.getLength(); i++) { 574 Element e_resp = (Element)e_responses.item(i); 575 Element e_service = (Element)this.doc.importNode(GSXML.getChildByTagName(e_resp, GSXML.SERVICE_ELEM), true); 654 for (int i = 0; i < e_responses.getLength(); i++) 655 { 656 Element e_resp = (Element) e_responses.item(i); 657 Element e_service = (Element) this.doc.importNode(GSXML.getChildByTagName(e_resp, GSXML.SERVICE_ELEM), true); 576 658 e_service.setAttribute(GSXML.NAME_ATT, e_resp.getAttribute(GSXML.FROM_ATT)); 577 659 service_list.appendChild(e_service); … … 581 663 } // if provide_annotations 582 664 return true; 583 665 584 666 } 585 667 586 /** this involves a bit of a hack to get the equivalent query terms - has to requery the query service - uses the last selected service name. (if it ends in query). should this action do the query or should it send a message to the query action? but that will involve lots of extra stuff. also doesn't handle phrases properly - just highlights all the terms found in the text. 587 */ 588 protected Element highlightQueryTerms(Element request, Element dc_response_doc_content) { 668 /** 669 * this involves a bit of a hack to get the equivalent query terms - has to 670 * requery the query service - uses the last selected service name. (if it 671 * ends in query). should this action do the query or should it send a 672 * message to the query action? but that will involve lots of extra stuff. 673 * also doesn't handle phrases properly - just highlights all the terms found 674 * in the text. 675 */ 676 protected Element highlightQueryTerms(Element request, Element dc_response_doc_content) 677 { 589 678 590 679 // do the query again to get term info 591 Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);680 Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 592 681 HashMap params = GSXML.extractParams(cgi_param_list, false); 593 594 HashMap previous_params = (HashMap)params.get("p"); 595 if (previous_params == null) { 682 683 HashMap previous_params = (HashMap) params.get("p"); 684 if (previous_params == null) 685 { 596 686 return dc_response_doc_content; 597 687 } 598 String service_name = (String)previous_params.get(GSParams.SERVICE); 599 if (service_name == null || !service_name.endsWith("Query")) { // hack for now - we only do highlighting if we were in a query last - ie not if we were in a browse thingy 688 String service_name = (String) previous_params.get(GSParams.SERVICE); 689 if (service_name == null || !service_name.endsWith("Query")) 690 { // hack for now - we only do highlighting if we were in a query last - ie not if we were in a browse thingy 600 691 logger.debug("invalid service, not doing highlighting"); 601 692 return dc_response_doc_content; 602 693 } 603 String collection = (String) params.get(GSParams.COLLECTION);694 String collection = (String) params.get(GSParams.COLLECTION); 604 695 String lang = request.getAttribute(GSXML.LANG_ATT); 605 696 String uid = request.getAttribute(GSXML.USER_ID_ATT); 606 697 String to = GSPath.appendLink(collection, service_name); 607 698 608 699 Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 609 700 Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid); 610 701 mr_query_message.appendChild(mr_query_request); 611 702 612 703 // paramList 613 HashMap service_params = (HashMap) params.get("s1");614 704 HashMap service_params = (HashMap) params.get("s1"); 705 615 706 Element query_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 616 707 GSXML.addParametersToList(this.doc, query_param_list, service_params); … … 618 709 619 710 // do the query 620 Element mr_query_response = (Element) this.mr.process(mr_query_message);621 622 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.TERM_ELEM +GSXML.LIST_MODIFIER);711 Element mr_query_response = (Element) this.mr.process(mr_query_message); 712 713 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.TERM_ELEM + GSXML.LIST_MODIFIER); 623 714 Element query_term_list_element = (Element) GSXML.getNodeByPath(mr_query_response, path); 624 if (query_term_list_element == null) { 715 if (query_term_list_element == null) 716 { 625 717 // no term info 626 718 logger.error("No query term information.\n"); … … 630 722 String content = GSXML.getNodeText(dc_response_doc_content); 631 723 632 String metadata_path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.METADATA_ELEM +GSXML.LIST_MODIFIER);724 String metadata_path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER); 633 725 Element metadata_list = (Element) GSXML.getNodeByPath(mr_query_response, metadata_path); 634 726 635 727 HashSet query_term_variants = new HashSet(); 636 728 NodeList equivalent_terms_nodelist = query_term_list_element.getElementsByTagName("equivTermList"); 637 if (equivalent_terms_nodelist == null || equivalent_terms_nodelist.getLength() == 0)729 if (equivalent_terms_nodelist == null || equivalent_terms_nodelist.getLength() == 0) 638 730 { 639 731 NodeList terms_nodelist = query_term_list_element.getElementsByTagName("term"); 640 if (terms_nodelist != null && terms_nodelist.getLength() > 0)641 { 642 for (int i = 0; i < terms_nodelist.getLength(); i++)643 { 644 String termValue = ((Element) terms_nodelist.item(i)).getAttribute("name");732 if (terms_nodelist != null && terms_nodelist.getLength() > 0) 733 { 734 for (int i = 0; i < terms_nodelist.getLength(); i++) 735 { 736 String termValue = ((Element) terms_nodelist.item(i)).getAttribute("name"); 645 737 String termValueU = null; 646 738 String termValueL = null; 647 648 if (termValue.length() > 1)739 740 if (termValue.length() > 1) 649 741 { 650 742 termValueU = termValue.substring(0, 1).toUpperCase() + termValue.substring(1); … … 656 748 termValueL = termValue.substring(0, 1).toLowerCase(); 657 749 } 658 750 659 751 query_term_variants.add(termValueU); 660 752 query_term_variants.add(termValueL); … … 664 756 else 665 757 { 666 for (int i = 0; i < equivalent_terms_nodelist.getLength(); i++) { 758 for (int i = 0; i < equivalent_terms_nodelist.getLength(); i++) 759 { 667 760 Element equivalent_terms_element = (Element) equivalent_terms_nodelist.item(i); 668 761 String[] equivalent_terms = GSXML.getAttributeValuesFromList(equivalent_terms_element, GSXML.NAME_ATT); 669 for (int j = 0; j < equivalent_terms.length; j++) { 762 for (int j = 0; j < equivalent_terms.length; j++) 763 { 670 764 query_term_variants.add(equivalent_terms[j]); 671 765 } … … 682 776 boolean in_term = false; 683 777 boolean in_phrase = false; 684 for (int i = 0; i < performed_query.length(); i++) { 778 for (int i = 0; i < performed_query.length(); i++) 779 { 685 780 char character = performed_query.charAt(i); 686 781 boolean is_character_letter_or_digit = Character.isLetterOrDigit(character); 687 782 688 783 // Has a query term just started? 689 if (in_term == false && is_character_letter_or_digit == true) { 784 if (in_term == false && is_character_letter_or_digit == true) 785 { 690 786 in_term = true; 691 787 term_start = i; … … 693 789 694 790 // Or has a term just finished? 695 else if (in_term == true && is_character_letter_or_digit == false) { 791 else if (in_term == true && is_character_letter_or_digit == false) 792 { 696 793 in_term = false; 697 794 String term = performed_query.substring(term_start, i); 698 795 699 796 Element term_element = GSXML.getNamedElement(query_term_list_element, GSXML.TERM_ELEM, GSXML.NAME_ATT, term); 700 if (term_element != null) { 701 797 if (term_element != null) 798 { 799 702 800 HashSet phrase_query_p_term_x_variants = new HashSet(); 703 801 704 802 NodeList term_equivalent_terms_nodelist = term_element.getElementsByTagName("equivTermList"); 705 if (term_equivalent_terms_nodelist == null || term_equivalent_terms_nodelist.getLength() == 0)803 if (term_equivalent_terms_nodelist == null || term_equivalent_terms_nodelist.getLength() == 0) 706 804 { 707 805 String termValueU = null; 708 806 String termValueL = null; 709 710 if (term.length() > 1)807 808 if (term.length() > 1) 711 809 { 712 810 termValueU = term.substring(0, 1).toUpperCase() + term.substring(1); … … 718 816 termValueL = term.substring(0, 1).toLowerCase(); 719 817 } 720 818 721 819 phrase_query_p_term_x_variants.add(termValueU); 722 820 phrase_query_p_term_x_variants.add(termValueL); … … 724 822 else 725 823 { 726 for (int j = 0; j < term_equivalent_terms_nodelist.getLength(); j++) { 824 for (int j = 0; j < term_equivalent_terms_nodelist.getLength(); j++) 825 { 727 826 Element term_equivalent_terms_element = (Element) term_equivalent_terms_nodelist.item(j); 728 827 String[] term_equivalent_terms = GSXML.getAttributeValuesFromList(term_equivalent_terms_element, GSXML.NAME_ATT); 729 for (int k = 0; k < term_equivalent_terms.length; k++) { 828 for (int k = 0; k < term_equivalent_terms.length; k++) 829 { 730 830 phrase_query_p_term_x_variants.add(term_equivalent_terms[k]); 731 831 } … … 733 833 } 734 834 phrase_query_p_term_variants_list.add(phrase_query_p_term_x_variants); 735 736 if (in_phrase == false) { 835 836 if (in_phrase == false) 837 { 737 838 phrase_query_term_variants_hierarchy.add(phrase_query_p_term_variants_list); 738 839 phrase_query_p_term_variants_list = new ArrayList(); … … 741 842 } 742 843 // Watch for phrases (surrounded by quotes) 743 if (character == '\"') { 844 if (character == '\"') 845 { 744 846 // Has a phrase just started? 745 if (in_phrase == false) { 847 if (in_phrase == false) 848 { 746 849 in_phrase = true; 747 850 } 748 851 // Or has a phrase just finished? 749 else if (in_phrase == true) { 852 else if (in_phrase == true) 853 { 750 854 in_phrase = false; 751 855 phrase_query_term_variants_hierarchy.add(phrase_query_p_term_variants_list); … … 760 864 } 761 865 762 763 866 /** 764 * Highlights query terms in a piece of text.765 */867 * Highlights query terms in a piece of text. 868 */ 766 869 private Element highlightQueryTermsInternal(String content, HashSet query_term_variants, ArrayList phrase_query_term_variants_hierarchy) 767 870 { … … 775 878 boolean in_word = false; 776 879 boolean preceding_word_matched = false; 777 for (int i = 0; i < content_characters.length; i++) { 880 for (int i = 0; i < content_characters.length; i++) 881 { 778 882 boolean is_character_letter_or_digit = Character.isLetterOrDigit(content_characters[i]); 779 883 780 884 // Has a word just started? 781 if (in_word == false && is_character_letter_or_digit == true) { 885 if (in_word == false && is_character_letter_or_digit == true) 886 { 782 887 in_word = true; 783 888 word_start = i; … … 785 890 786 891 // Or has a word just finished? 787 else if (in_word == true && is_character_letter_or_digit == false) { 892 else if (in_word == true && is_character_letter_or_digit == false) 893 { 788 894 in_word = false; 789 895 790 896 // Check if the word matches any of the query term equivalents 791 897 String word = new String(content_characters, word_start, (i - word_start)); 792 if (query_term_variants.contains(word)) { 898 if (query_term_variants.contains(word)) 899 { 793 900 // We have found a matching word, so remember its location 794 901 word_matches.add(new WordMatch(word, word_start, i, preceding_word_matched)); 795 902 preceding_word_matched = true; 796 903 } 797 else { 904 else 905 { 798 906 preceding_word_matched = false; 799 907 } … … 802 910 803 911 // Don't forget the last word... 804 if (in_word == true) { 912 if (in_word == true) 913 { 805 914 // Check if the word matches any of the query term equivalents 806 915 String word = new String(content_characters, word_start, (content_characters.length - word_start)); 807 if (query_term_variants.contains(word)) { 916 if (query_term_variants.contains(word)) 917 { 808 918 // We have found a matching word, so remember its location 809 919 word_matches.add(new WordMatch(word, word_start, content_characters.length, preceding_word_matched)); … … 816 926 // Deal with phrases now 817 927 ArrayList partial_phrase_matches = new ArrayList(); 818 for (int i = 0; i < word_matches.size(); i++) { 928 for (int i = 0; i < word_matches.size(); i++) 929 { 819 930 WordMatch word_match = (WordMatch) word_matches.get(i); 820 931 821 932 // See if any partial phrase matches are extended by this word 822 if (word_match.preceding_word_matched) { 823 for (int j = partial_phrase_matches.size() - 1; j >= 0; j--) { 933 if (word_match.preceding_word_matched) 934 { 935 for (int j = partial_phrase_matches.size() - 1; j >= 0; j--) 936 { 824 937 PartialPhraseMatch partial_phrase_match = (PartialPhraseMatch) partial_phrase_matches.remove(j); 825 938 ArrayList phrase_query_p_term_variants_list = (ArrayList) phrase_query_term_variants_hierarchy.get(partial_phrase_match.query_phrase_number); 826 939 HashSet phrase_query_p_term_x_variants = (HashSet) phrase_query_p_term_variants_list.get(partial_phrase_match.num_words_matched); 827 if (phrase_query_p_term_x_variants.contains(word_match.word)) { 940 if (phrase_query_p_term_x_variants.contains(word_match.word)) 941 { 828 942 partial_phrase_match.num_words_matched++; 829 943 830 944 // Has a complete phrase match occurred? 831 if (partial_phrase_match.num_words_matched == phrase_query_p_term_variants_list.size()) { 945 if (partial_phrase_match.num_words_matched == phrase_query_p_term_variants_list.size()) 946 { 832 947 // Check for overlaps by looking at the previous highlight range 833 if (!highlight_end_positions.isEmpty()) { 948 if (!highlight_end_positions.isEmpty()) 949 { 834 950 int last_highlight_index = highlight_end_positions.size() - 1; 835 951 int last_highlight_end = ((Integer) highlight_end_positions.get(last_highlight_index)).intValue(); 836 if (last_highlight_end > partial_phrase_match.start_position) { 952 if (last_highlight_end > partial_phrase_match.start_position) 953 { 837 954 // There is an overlap, so remove the previous phrase match 838 955 int last_highlight_start = ((Integer) highlight_start_positions.remove(last_highlight_index)).intValue(); … … 846 963 } 847 964 // No, but add the partial match back into the list for next time 848 else { 965 else 966 { 849 967 partial_phrase_matches.add(partial_phrase_match); 850 968 } … … 852 970 } 853 971 } 854 else { 972 else 973 { 855 974 partial_phrase_matches.clear(); 856 975 } 857 976 858 977 // See if this word is at the start of any of the phrases 859 for (int p = 0; p < phrase_query_term_variants_hierarchy.size(); p++) { 978 for (int p = 0; p < phrase_query_term_variants_hierarchy.size(); p++) 979 { 860 980 ArrayList phrase_query_p_term_variants_list = (ArrayList) phrase_query_term_variants_hierarchy.get(p); 861 981 HashSet phrase_query_p_term_1_variants = (HashSet) phrase_query_p_term_variants_list.get(0); 862 if (phrase_query_p_term_1_variants.contains(word_match.word)) { 982 if (phrase_query_p_term_1_variants.contains(word_match.word)) 983 { 863 984 // If this phrase is just one word long, we have a complete match 864 if (phrase_query_p_term_variants_list.size() == 1) { 985 if (phrase_query_p_term_variants_list.size() == 1) 986 { 865 987 highlight_start_positions.add(new Integer(word_match.start_position)); 866 988 highlight_end_positions.add(new Integer(word_match.end_position)); 867 989 } 868 990 // Otherwise we have the start of a potential phrase match 869 else { 991 else 992 { 870 993 partial_phrase_matches.add(new PartialPhraseMatch(word_match.start_position, p)); 871 994 } … … 878 1001 879 1002 int last_wrote = 0; 880 for (int i = 0; i < highlight_start_positions.size(); i++) { 1003 for (int i = 0; i < highlight_start_positions.size(); i++) 1004 { 881 1005 int highlight_start = ((Integer) highlight_start_positions.get(i)).intValue(); 882 1006 int highlight_end = ((Integer) highlight_end_positions.get(i)).intValue(); 883 1007 884 1008 // Print anything before the highlight range 885 if (last_wrote < highlight_start) { 1009 if (last_wrote < highlight_start) 1010 { 886 1011 String preceding_text = new String(content_characters, last_wrote, (highlight_start - last_wrote)); 887 1012 content_element.appendChild(this.doc.createTextNode(preceding_text)); … … 889 1014 890 1015 // Print the highlight text, annotated 891 if (highlight_end > last_wrote) { 1016 if (highlight_end > last_wrote) 1017 { 892 1018 String highlight_text = new String(content_characters, highlight_start, (highlight_end - highlight_start)); 893 1019 Element annotation_element = GSXML.createTextElement(this.doc, "annotation", highlight_text); … … 899 1025 900 1026 // Finish off any unwritten text 901 if (last_wrote < content_characters.length) { 1027 if (last_wrote < content_characters.length) 1028 { 902 1029 String remaining_text = new String(content_characters, last_wrote, (content_characters.length - last_wrote)); 903 1030 content_element.appendChild(this.doc.createTextNode(remaining_text)); … … 906 1033 return content_element; 907 1034 } 908 909 1035 910 1036 static private class WordMatch … … 924 1050 } 925 1051 926 927 1052 static private class PartialPhraseMatch 928 1053 {
Note:
See TracChangeset
for help on using the changeset viewer.