Changeset 6051 for trunk/gli/src/org/greenstone/gatherer/cdm
- Timestamp:
- 2003-11-30T21:43:23+13:00 (21 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer/cdm
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/cdm/ArgumentConfiguration.java
r5936 r6051 400 400 case Argument.METADATUM: 401 401 value = new JComboBox(Gatherer.c_man.getCollection().msm.getAssignedElements()); 402 ((JComboBox)value).setEditable(true);402 //((JComboBox)value).setEditable(true); 403 403 // Now ensure we have the existing value or default value selected if either exist. 404 404 if(existing_value != null) { -
trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfiguration.java
r5994 r6051 46 46 */ 47 47 public class CollectionConfiguration 48 49 50 51 52 48 extends StaticStrings { 49 50 static public Document document; 51 52 static public void main(String[] args) { 53 53 if(args.length >= 1) { 54 55 56 57 58 54 File file = new File(args[0]); 55 CollectionConfiguration collect_cfg = new CollectionConfiguration(file); 56 collect_cfg.save(true); 57 collect_cfg.save(false); 58 collect_cfg = null; 59 59 } 60 60 else { 61 62 } 63 64 65 66 67 68 69 61 System.out.println("Usage: CollectionConfiguration <filename>"); 62 } 63 } 64 65 /** Find the best insertion position for the given DOM Element. This should try to match command tag, and if found should then try to group by name or type (eg CollectionMeta), or append to end is no such grouping exists (eg PlugIns). Failing a command match it will check against the command order for the best insertion location. 66 * @param element the command Element to be inserted 67 * @return the Element which the given command should be inserted before, or null to append to end of list 68 */ 69 static public Node findInsertionPoint(Element target_element) { 70 70 ///ystem.err.println("Find insertion point: " + target_element.getNodeName()); 71 71 String target_element_name = target_element.getNodeName(); … … 75 75 // If we found matching elements, then we have our most likely insertion location, so check within for groupings 76 76 if(matching_elements.getLength() != 0) { 77 78 79 77 ///ystem.err.println("Found matching elements."); 78 // Only CollectionMeta are grouped. 79 if(target_element_name.equals(COLLECTIONMETADATA_ELEMENT)) { 80 80 ///ystem.err.println("Dealing with collection metadata"); 81 81 // Special case: CollectionMeta can be added at either the start or end of a collection configuration file. However the start position is reserved for special metadata, so if no non-special metadata can be found we must append to the end. 82 82 // So if the command to be added is special add it immediately after any other special command 83 83 if(target_element.getAttribute(SPECIAL_ATTRIBUTE).equals(TRUE_STR)) { 84 85 86 87 84 int index = 0; 85 Element matched_element = (Element) matching_elements.item(index); 86 Element sibling_element = (Element) matched_element.getNextSibling(); 87 while(sibling_element.getAttribute(SPECIAL_ATTRIBUTE).equals(TRUE_STR)) { 88 88 index++; 89 89 matched_element = (Element) matching_elements.item(index); 90 90 sibling_element = (Element) matched_element.getNextSibling(); 91 92 91 } 92 if(sibling_element.getNodeName().equals(NEWLINE_ELEMENT)) { 93 93 Element newline_element = document.createElement(NEWLINE_ELEMENT); 94 94 document_element.insertBefore(newline_element, sibling_element); 95 96 95 } 96 return sibling_element; 97 97 } 98 98 // Otherwise try to find a matching 'name' and add after the last one in that group. 99 99 else { 100 101 102 103 104 105 100 int index = 0; 101 target_element_name = target_element.getAttribute(NAME_ATTRIBUTE); 102 boolean found = false; 103 // Skip all of the special metadata 104 Element matched_element = (Element) matching_elements.item(index); 105 while(matched_element.getAttribute(SPECIAL_ATTRIBUTE).equals(TRUE_STR)) { 106 106 index++; 107 107 matched_element = (Element) matching_elements.item(index); 108 109 110 108 } 109 // Begin search 110 while(!found && matched_element != null) { 111 111 if(matched_element.getAttribute(NAME_ATTRIBUTE).equals(target_element_name)) { 112 112 found = true; 113 113 } 114 114 else { 115 116 117 } 118 119 120 115 index++; 116 matched_element = (Element) matching_elements.item(index); 117 } 118 } 119 // If we found a match, we need to continue checking until we find the last name match. 120 if(found) { 121 121 index++; 122 122 Element previous_sibling = matched_element; 123 123 Element sibling_element = (Element) matching_elements.item(index); 124 124 while(sibling_element != null && sibling_element.getAttribute(NAME_ATTRIBUTE).equals(target_element_name)) { 125 126 127 125 previous_sibling = sibling_element; 126 index++; 127 sibling_element = (Element) matching_elements.item(index); 128 128 } 129 129 // Previous sibling now holds the command immediately before where we want to add, so find its next sibling and add to that. In this one case we can ignore new lines! 130 130 return previous_sibling.getNextSibling(); 131 132 133 131 } 132 // If not found we just add after last metadata element 133 else { 134 134 Element last_element = (Element) matching_elements.item(matching_elements.getLength() - 1); 135 135 return last_element.getNextSibling(); 136 137 } 138 139 140 136 } 137 } 138 139 } 140 else { 141 141 ///ystem.err.println("Not dealing with collection meta."); 142 142 Element matched_element = (Element) matching_elements.item(matching_elements.getLength() - 1); … … 144 144 Node sibling_element = matched_element.getNextSibling(); 145 145 if(sibling_element != null && sibling_element.getNodeName().equals(NEWLINE_ELEMENT)) { 146 147 146 Element newline_element = document.createElement(NEWLINE_ELEMENT); 147 document_element.insertBefore(newline_element, sibling_element); 148 148 } 149 149 return sibling_element; // Note that this may be null 150 150 } 151 151 } 152 152 ///ystem.err.println("No matching elements found."); … … 154 154 int command_index = -1; 155 155 for(int i = 0; command_index == -1 && i < COMMAND_ORDER.length; i++) { 156 156 if(COMMAND_ORDER[i].equals(target_element_name)) { 157 157 command_index = i; 158 158 } 159 159 } 160 160 ///ystem.err.println("Command index is: " + command_index); … … 163 163 ///ystem.err.println("Searching before the target command."); 164 164 while(preceeding_index >= 0) { 165 166 167 165 matching_elements = document_element.getElementsByTagName(COMMAND_ORDER[preceeding_index]); 166 // If we've found a match 167 if(matching_elements.getLength() > 0) { 168 168 // We add after the last element 169 169 Element matched_element = (Element) matching_elements.item(matching_elements.getLength() - 1); … … 171 171 Node sibling_element = matched_element.getNextSibling(); 172 172 if(sibling_element != null && sibling_element.getNodeName().equals(NEWLINE_ELEMENT)) { 173 174 173 Element newline_element = document.createElement(NEWLINE_ELEMENT); 174 document_element.insertBefore(newline_element, sibling_element); 175 175 } 176 176 return sibling_element; // Note that this may be null 177 178 177 } 178 preceeding_index--; 179 179 } 180 180 // If all that fails, we now move backwards through the commands … … 182 182 ///ystem.err.println("Searching after the target command."); 183 183 while(susceeding_index < COMMAND_ORDER.length) { 184 185 186 184 matching_elements = document_element.getElementsByTagName(COMMAND_ORDER[susceeding_index]); 185 // If we've found a match 186 if(matching_elements.getLength() > 0) { 187 187 // We add before the first element 188 188 Element matched_element = (Element) matching_elements.item(0); … … 190 190 Node sibling_element = matched_element.getPreviousSibling(); 191 191 if(sibling_element != null && sibling_element.getNodeName().equals(NEWLINE_ELEMENT)) { 192 193 192 Element newline_element = document.createElement(NEWLINE_ELEMENT); 193 document_element.insertBefore(newline_element, sibling_element); 194 194 } 195 195 return sibling_element; // Note that this may be null 196 197 196 } 197 susceeding_index++; 198 198 } 199 199 // Well. Apparently there are no other commands in this collection configuration. So append away... 200 200 return null; 201 202 203 201 } 202 203 static public String toString(Element command_element, boolean show_extracted_namespace) { 204 204 String command_element_name = command_element.getNodeName(); 205 205 if(command_element_name.equals(CLASSIFY_ELEMENT)) { 206 206 return self.classifyToString(command_element, show_extracted_namespace); 207 207 } 208 208 else if(command_element_name.equals(FORMAT_ELEMENT)) { 209 209 return self.formatToString(command_element, show_extracted_namespace); 210 210 } 211 211 else if(command_element_name.equals(INDEXES_ELEMENT)) { 212 212 return self.indexesToString(command_element, show_extracted_namespace); 213 213 } 214 214 else if(command_element_name.equals(INDEX_DEFAULT_ELEMENT)) { 215 215 return self.indexDefaultToString(command_element, show_extracted_namespace); 216 216 } 217 217 else if(command_element_name.equals(LANGUAGES_ELEMENT)) { 218 218 return self.languagesToString(command_element); 219 219 } 220 220 else if(command_element_name.equals(LANGUAGE_DEFAULT_ELEMENT)) { 221 221 return self.languageDefaultToString(command_element); 222 222 } 223 223 else if(command_element_name.equals(LEVELS_ELEMENT)) { 224 224 return self.levelsToString(command_element); 225 225 } 226 226 else if(command_element_name.equals(COLLECTIONMETADATA_ELEMENT)) { 227 227 return self.metadataToString(command_element, show_extracted_namespace); 228 228 } 229 229 else if(command_element_name.equals(COLLECTIONMETADATA_CREATOR_ELEMENT)) { 230 230 return self.metadataToString(command_element, show_extracted_namespace); 231 231 } 232 232 else if(command_element_name.equals(COLLECTIONMETADATA_MAINTAINER_ELEMENT)) { 233 233 return self.metadataToString(command_element, show_extracted_namespace); 234 234 } 235 235 else if(command_element_name.equals(COLLECTIONMETADATA_PUBLIC_ELEMENT)) { 236 236 return self.metadataToString(command_element, show_extracted_namespace); 237 237 } 238 238 else if(command_element_name.equals(COLLECTIONMETADATA_BETA_ELEMENT)) { 239 239 return self.metadataToString(command_element, show_extracted_namespace); 240 240 } 241 241 else if(command_element_name.equals(PLUGIN_ELEMENT)) { 242 242 return self.pluginToString(command_element, show_extracted_namespace); 243 243 } 244 244 else if(command_element_name.equals(SEARCHTYPE_ELEMENT)) { 245 245 return self.searchtypeToString(command_element); 246 246 } 247 247 else if(command_element_name.equals(SUBCOLLECTION_ELEMENT)) { 248 248 return self.subcollectionToString(command_element, show_extracted_namespace); 249 249 } 250 250 else if(command_element_name.equals(SUBCOLLECTION_DEFAULT_INDEX_ELEMENT)) { 251 251 return self.subcollectionDefaultIndexToString(command_element); 252 252 } 253 253 else if(command_element_name.equals(SUBCOLLECTION_INDEXES_ELEMENT)) { 254 254 return self.subcollectionIndexesToString(command_element); 255 255 } 256 256 else if(command_element_name.equals(SUPERCOLLECTION_ELEMENT)) { 257 257 return self.supercollectionToString(command_element); 258 258 } 259 259 else if(command_element_name.equals(UNKNOWN_ELEMENT)) { 260 260 return self.unknownToString(command_element); 261 261 } 262 262 return ""; 263 264 265 266 267 268 269 263 } 264 265 /** Parses arguments from a tokenizer and returns a HashMap of mappings. The tricky bit here is that not all entries in the HashMap are name->value pairs, as some arguments are boolean and are turned on by their presence. Arguments are denoted by a '-' prefix. 266 * @param tokenizer a CommandTokenizer based on the unconsumed portion of a command string 267 * @return a HashMap containing the arguments parsed 268 */ 269 static public HashMap parseArguments(CommandTokenizer tokenizer) { 270 270 HashMap arguments = new HashMap(); 271 271 String name = null; 272 272 String value = null; 273 273 while(tokenizer.hasMoreTokens() || name != null) { 274 275 274 // First we retrieve a name if we need one. 275 if(name == null) { 276 276 name = tokenizer.nextToken(); 277 278 279 277 } 278 // Now we attempt to retrieve a value 279 if(tokenizer.hasMoreTokens()) { 280 280 value = tokenizer.nextToken(); 281 281 // Test if the value is actually a name, and if so add the name by itself, then put value into name so that it is parsed correctly during the next loop. 282 282 if(value.startsWith(StaticStrings.MINUS_CHARACTER)) { 283 284 283 arguments.put(name, null); 284 name = value; 285 285 } 286 286 // Otherwise we have a typical name->value pair ready to go 287 287 else { 288 289 290 } 291 292 293 288 arguments.put(name, value); 289 name = null; 290 } 291 } 292 // Otherwise its a binary flag 293 else { 294 294 arguments.put(name, null); 295 295 name = null; 296 296 } 297 297 } 298 298 return arguments; 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 299 } 300 301 static private ArrayList known_metadata; 302 303 static private CollectionConfiguration self; 304 305 static final private String EXTRACTED_PREFIX = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP; 306 /** Gives the preferred ordering of commands */ 307 static final private String[] COMMAND_ORDER = {StaticStrings.COLLECTIONMETADATA_CREATOR_ELEMENT, StaticStrings.COLLECTIONMETADATA_MAINTAINER_ELEMENT, StaticStrings.COLLECTIONMETADATA_PUBLIC_ELEMENT, StaticStrings.COLLECTIONMETADATA_BETA_ELEMENT, StaticStrings.SEARCHTYPE_ELEMENT, StaticStrings.PLUGIN_ELEMENT, StaticStrings.INDEXES_ELEMENT, StaticStrings.LEVELS_ELEMENT, StaticStrings.INDEX_DEFAULT_ELEMENT, StaticStrings.LANGUAGES_ELEMENT, StaticStrings.LANGUAGE_DEFAULT_ELEMENT, StaticStrings.SUBCOLLECTION_ELEMENT, StaticStrings.SUBCOLLECTION_INDEXES_ELEMENT, StaticStrings.SUBCOLLECTION_DEFAULT_INDEX_ELEMENT, StaticStrings.SUPERCOLLECTION_ELEMENT, StaticStrings.CLASSIFY_ELEMENT, StaticStrings.FORMAT_ELEMENT, StaticStrings.COLLECTIONMETADATA_ELEMENT}; 308 309 /** ************************** Public Data Members ***************************/ 310 311 /** ************************** Private Data Members ***************************/ 312 313 private File collect_config_file; 314 315 /** ************************** Public Methods ***************************/ 316 317 public CollectionConfiguration(File collect_config_file) { 318 318 this.self = this; 319 319 this.collect_config_file = collect_config_file; … … 321 321 String collect_config_name = collect_config_file.getName(); 322 322 if(collect_config_name.equals(COLLECTCONFIGURATION_XML)) { 323 324 323 // Parse with Utility but don't use class loader 324 document = Utility.parse(collect_config_file, false); 325 325 } 326 326 // Otherwise if this is a legacy collect.cfg file then read in the template and send to magic parser 327 327 else if(collect_config_name.equals(COLLECT_CFG)) { 328 329 330 } 331 332 333 334 328 document = Utility.parse(PSEUDO_COLLECTCONFIGURATION_XML, true); 329 parse(collect_config_file); 330 } 331 } 332 333 /** This debug facility shows the currently loaded collect.cfg or CollectConfig.xml file as a DOM tree. */ 334 public void display() { 335 335 JDialog dialog = new JDialog(Gatherer.g_man, "Collection Configuration", false); 336 336 dialog.setSize(400,400); … … 340 340 refresh_button.addActionListener(new ActionListener() { 341 341 public void actionPerformed(ActionEvent event) { 342 343 } 344 342 tree.setDocument(document); 343 } 344 }); 345 345 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 346 346 content_pane.setLayout(new BorderLayout()); … … 348 348 content_pane.add(refresh_button, BorderLayout.SOUTH); 349 349 dialog.show(); 350 351 352 350 } 351 352 public Element getBeta() { 353 353 Element element = getOrCreateElementByTagName(COLLECTIONMETADATA_BETA_ELEMENT, null, null); 354 354 element.setAttribute(NAME_ATTRIBUTE, COLLECTIONMETADATA_BETA_STR); 355 355 element.setAttribute(SPECIAL_ATTRIBUTE, TRUE_STR); 356 356 return element; 357 358 359 357 } 358 359 public Element getCreator() { 360 360 Element element = getOrCreateElementByTagName(COLLECTIONMETADATA_CREATOR_ELEMENT, null, null); 361 361 element.setAttribute(NAME_ATTRIBUTE, COLLECTIONMETADATA_CREATOR_STR); 362 362 element.setAttribute(SPECIAL_ATTRIBUTE, TRUE_STR); 363 363 return element; 364 365 366 364 } 365 366 public Element getDocumentElement() { 367 367 return document.getDocumentElement(); 368 369 370 368 } 369 370 public File getFile() { 371 371 return collect_config_file; 372 373 374 375 372 } 373 374 /** Retrieve or create the languages Element. */ 375 public Element getLanguages() { 376 376 return getOrCreateElementByTagName(LANGUAGES_ELEMENT, null, null); 377 378 379 377 } 378 379 public Element getLevels() { 380 380 return getOrCreateElementByTagName(LEVELS_ELEMENT, null, null); 381 382 383 381 } 382 383 public Element getMaintainer() { 384 384 Element element = getOrCreateElementByTagName(COLLECTIONMETADATA_MAINTAINER_ELEMENT, null, null); 385 385 element.setAttribute(NAME_ATTRIBUTE, COLLECTIONMETADATA_MAINTAINER_STR); 386 386 element.setAttribute(SPECIAL_ATTRIBUTE, TRUE_STR); 387 387 return element; 388 389 390 391 388 } 389 390 /** Retrieve or create the indexes Element. Note that this method behaves differently from the other getBlah methods, in that it also has to keep in mind that indexes come in two flavours, MG and MGPP. */ 391 public Element getMGIndexes() { 392 392 return getOrCreateElementByTagName(INDEXES_ELEMENT, MGPP_ATTRIBUTE, FALSE_STR); 393 394 395 393 } 394 395 public Element getMGPPIndexes() { 396 396 return getOrCreateElementByTagName(INDEXES_ELEMENT, MGPP_ATTRIBUTE, TRUE_STR); 397 398 399 397 } 398 399 public Element getPublic() { 400 400 Element element = getOrCreateElementByTagName(COLLECTIONMETADATA_PUBLIC_ELEMENT, null, null); 401 401 element.setAttribute(NAME_ATTRIBUTE, COLLECTIONMETADATA_PUBLIC_STR); 402 402 element.setAttribute(SPECIAL_ATTRIBUTE, TRUE_STR); 403 403 return element; 404 405 406 407 404 } 405 406 /** Retrieve or create the searchtype element. */ 407 public Element getSearchType() { 408 408 ///ystem.err.println("Get or create element by tag name: " + name); 409 409 Element document_element = document.getDocumentElement(); … … 411 411 int elements_length = elements.getLength(); 412 412 if(elements_length > 0) { 413 414 413 document_element = null; 414 return (Element) elements.item(0); 415 415 } 416 416 // Create the element … … 418 418 Node target_node = findInsertionPoint(element); 419 419 if(target_node != null) { 420 420 document_element.insertBefore(element, target_node); 421 421 } 422 422 else { 423 423 document_element.appendChild(element); 424 424 } 425 425 document_element = null; … … 429 429 element.appendChild(a_searchtype_element); 430 430 return element; 431 432 433 434 431 } 432 433 /** Retrieve or create the subindexes Element. */ 434 public Element getSubIndexes() { 435 435 return getOrCreateElementByTagName(SUBCOLLECTION_INDEXES_ELEMENT, null, null); 436 437 438 439 436 } 437 438 /** Retrieve or create the supercollections Element. */ 439 public Element getSuperCollection() { 440 440 return getOrCreateElementByTagName(SUPERCOLLECTION_ELEMENT, null, null); 441 442 443 441 } 442 443 public boolean ready() { 444 444 return document != null; 445 446 447 445 } 446 447 public void save() { 448 448 save(false); 449 450 451 449 } 450 451 public void save(boolean force_xml) { 452 452 if(collect_config_file.exists()) { 453 454 455 453 File original_file = new File(collect_config_file.getParentFile(), COLLECT_CFG); 454 File backup_file = new File(collect_config_file.getParentFile(), "collect.bak"); 455 if(backup_file.exists()) { 456 456 backup_file.delete(); 457 458 457 } 458 if(!original_file.renameTo(backup_file)) { 459 459 Gatherer.println("Can't rename collect.cfg"); 460 460 } 461 461 } 462 462 if(force_xml || collect_config_file.getName().equals(COLLECTCONFIGURATION_XML)) { 463 464 463 ///ystem.err.println("Writing XML"); 464 Utility.export(document, new File(collect_config_file.getParentFile(), COLLECTCONFIGURATION_XML)); 465 465 } 466 466 else { 467 468 467 ///ystem.err.println("Writing text"); 468 try { 469 469 FileWriter file_writer = new FileWriter(collect_config_file, false); 470 470 BufferedWriter buffered_writer = new BufferedWriter(file_writer); … … 476 476 boolean just_wrote_newline = false; // Prevent two or more newlines in a row 477 477 for(int i = 0; i < command_elements.getLength(); i++) { 478 479 478 Node command_node = command_elements.item(i); 479 if(command_node instanceof Element) { 480 480 Element command_element = (Element) command_node; 481 481 // The only thing left are NewLine elements 482 482 if(command_element.getNodeName().equals(NEWLINE_ELEMENT) && !just_wrote_newline) { 483 484 483 buffered_writer.newLine(); 484 just_wrote_newline = true; 485 485 } 486 486 // Anything else we write to file, but only if it has been assigned, the exception being the Indexes element which just get commented if unassigned (a side effect of MG && MGPP compatibility) 487 487 else if(!command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(FALSE_STR) || command_element.getNodeName().equals(INDEXES_ELEMENT) || command_element.getNodeName().equals(INDEX_DEFAULT_ELEMENT) || command_element.getNodeName().equals(LEVELS_ELEMENT)){ 488 489 488 String command = toString(command_element, false); 489 if(command != null && command.length() > 0) { 490 490 write(buffered_writer, command); 491 491 buffered_writer.newLine(); 492 492 just_wrote_newline = false; 493 494 } 495 493 } 494 } 495 } 496 496 } 497 497 buffered_writer.close(); 498 498 known_metadata = null; 499 500 499 } 500 catch (Exception exception) { 501 501 Gatherer.println("Error in CollectionConfiguration.save(boolean): " + exception); 502 502 Gatherer.printStackTrace(exception); 503 504 } 505 506 507 508 509 503 } 504 } 505 } 506 507 /** ************************** Private Methods ***************************/ 508 509 private String classifyToString(Element command_element, boolean show_extracted_namespace) { 510 510 StringBuffer text = new StringBuffer(CLASSIFY_STR); 511 511 text.append(TAB_CHARACTER); … … 515 515 int option_elements_length = option_elements.getLength(); 516 516 for(int j = 0; j < option_elements_length; j++) { 517 518 517 Element option_element = (Element) option_elements.item(j); 518 if(option_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(TRUE_STR)) { 519 519 text.append(StaticStrings.MINUS_CHARACTER); 520 520 text.append(option_element.getAttribute(NAME_ATTRIBUTE)); 521 521 String value_str = MSMUtils.getValue(option_element); 522 522 if(value_str.length() > 0) { 523 524 523 // If the value happens to be the identifier of an extracted metadata element, then remove the namespace. 524 if(!show_extracted_namespace && value_str.startsWith(EXTRACTED_PREFIX)) { 525 525 value_str = value_str.substring(EXTRACTED_PREFIX.length()); 526 527 528 526 } 527 text.append(SPACE_CHARACTER); 528 if(value_str.indexOf(SPACE_CHARACTER) == -1) { 529 529 text.append(value_str); 530 531 530 } 531 else { 532 532 text.append(SPEECH_CHARACTER); 533 533 text.append(value_str); 534 534 text.append(SPEECH_CHARACTER); 535 535 } 536 536 } 537 537 value_str = null; 538 538 if(j < option_elements_length - 1) { 539 540 } 541 542 539 text.append(SPACE_CHARACTER); 540 } 541 } 542 option_element = null; 543 543 } 544 544 option_elements = null; 545 545 return text.toString(); 546 547 548 546 } 547 548 private String formatToString(Element command_element, boolean show_extracted_namespace) { 549 549 StringBuffer text = new StringBuffer(FORMAT_STR); 550 550 text.append(SPACE_CHARACTER); … … 553 553 String value_str = command_element.getAttribute(VALUE_ATTRIBUTE); 554 554 if(value_str.length() != 0) { 555 555 text.append(value_str); 556 556 } 557 557 else { … … 560 560 // Remove any references to a namespace for extracted metadata 561 561 if(!show_extracted_namespace) { 562 562 value_str.replaceAll(EXTRACTED_PREFIX, ""); 563 563 } 564 564 text.append(SPEECH_CHARACTER); … … 568 568 value_str = null; 569 569 return text.toString(); 570 571 572 573 570 } 571 572 /** Retrieve or create the indexes Element. */ 573 private Element getOrCreateElementByTagName(String name, String conditional_attribute, String required_value) { 574 574 Element document_element = document.getDocumentElement(); 575 575 NodeList elements = document_element.getElementsByTagName(name); 576 576 int elements_length = elements.getLength(); 577 577 if(elements_length > 0) { 578 578 if(conditional_attribute == null) { 579 579 document_element = null; 580 580 return (Element) elements.item(0); 581 582 581 } 582 else { 583 583 for(int i = 0; i < elements_length; i++) { 584 585 584 Element element = (Element) elements.item(i); 585 if(element.getAttribute(conditional_attribute).equals(required_value)) { 586 586 document_element = null; 587 587 return element; 588 589 590 } 591 588 } 589 element = null; 590 } 591 } 592 592 } 593 593 // Create the element … … 595 595 // If there was a property set it 596 596 if(conditional_attribute != null) { 597 597 element.setAttribute(conditional_attribute, required_value); 598 598 } 599 599 Node target_node = findInsertionPoint(element); 600 600 if(target_node != null) { 601 601 document_element.insertBefore(element, target_node); 602 602 } 603 603 else { 604 604 document_element.appendChild(element); 605 605 } 606 606 document_element = null; 607 607 return element; 608 609 610 608 } 609 610 private String indexesToString(Element command_element, boolean show_extracted_namespace) { 611 611 boolean comment_only = false; 612 612 StringBuffer text = new StringBuffer(""); 613 613 if(command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(FALSE_STR)) { 614 615 614 text.append("#"); 615 comment_only = true; 616 616 } 617 617 text.append(INDEX_STR); 618 618 text.append(TAB_CHARACTER); 619 619 if(!comment_only) { 620 620 text.append(TAB_CHARACTER); 621 621 } 622 622 NodeList index_elements = command_element.getElementsByTagName(INDEX_ELEMENT); 623 623 if (index_elements.getLength() == 0) { // no indexes 624 624 return ""; 625 625 } 626 626 // For each index, write its level, a colon, then concatenate its child content elements into a single comma separated list 627 627 int index_elements_length = index_elements.getLength(); 628 628 for(int j = 0; j < index_elements_length; j++) { 629 630 631 629 Element index_element = (Element) index_elements.item(j); 630 String level_str = index_element.getAttribute(LEVEL_ATTRIBUTE); 631 if(level_str.length() > 0) { 632 632 text.append(level_str); 633 633 text.append(StaticStrings.COLON_CHARACTER); 634 635 636 637 638 634 } 635 NodeList content_elements = index_element.getElementsByTagName(CONTENT_ELEMENT); 636 int content_elements_length = content_elements.getLength(); 637 // Don't output anything if no indexes are set 638 if(content_elements_length == 0) { 639 639 return null; 640 641 640 } 641 for(int k = 0; k < content_elements_length; k++) { 642 642 Element content_element = (Element) content_elements.item(k); 643 643 String name_str = content_element.getAttribute(NAME_ATTRIBUTE); 644 644 if(!show_extracted_namespace && name_str.startsWith(EXTRACTED_PREFIX)) { 645 645 name_str = name_str.substring(EXTRACTED_PREFIX.length()); 646 646 } 647 647 text.append(name_str); 648 648 name_str = null; 649 649 if(k < content_elements_length - 1) { 650 650 text.append(StaticStrings.COMMA_CHARACTER); 651 651 } 652 652 content_element = null; 653 654 653 } 654 if(j < index_elements_length - 1) { 655 655 text.append(SPACE_CHARACTER); 656 657 658 656 } 657 content_elements = null; 658 index_element = null; 659 659 } 660 660 index_elements = null; 661 661 return text.toString(); 662 663 664 662 } 663 664 private String indexDefaultToString(Element command_element, boolean show_extracted_namespace) { 665 665 StringBuffer text = new StringBuffer(""); 666 666 if(command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(FALSE_STR)) { 667 667 text.append("#"); 668 668 } 669 669 text.append(INDEX_DEFAULT_STR); … … 674 674 int content_elements_length = content_elements.getLength(); 675 675 for(int j = 0; j < content_elements_length; j++) { 676 677 678 676 Element content_element = (Element) content_elements.item(j); 677 String name_str = content_element.getAttribute(NAME_ATTRIBUTE); 678 if(!show_extracted_namespace && name_str.startsWith(EXTRACTED_PREFIX)) { 679 679 name_str = name_str.substring(EXTRACTED_PREFIX.length()); 680 681 682 683 680 } 681 text.append(name_str); 682 name_str = null; 683 if(j < content_elements_length - 1) { 684 684 text.append(StaticStrings.COMMA_CHARACTER); 685 686 685 } 686 content_element = null; 687 687 } 688 688 content_elements = null; 689 689 return text.toString(); 690 691 692 690 } 691 692 private String languagesToString(Element command_element) { 693 693 StringBuffer text = new StringBuffer(LANGUAGES_STR); 694 694 text.append(TAB_CHARACTER); … … 697 697 int language_elements_length = language_elements.getLength(); 698 698 if(language_elements_length == 0) { 699 699 return null; 700 700 } 701 701 for(int j = 0; j < language_elements_length; j++) { 702 703 704 702 Element language_element = (Element) language_elements.item(j); 703 text.append(language_element.getAttribute(NAME_ATTRIBUTE)); 704 if(j < language_elements_length - 1) { 705 705 text.append(SPACE_CHARACTER); 706 706 } 707 707 } 708 708 return text.toString(); 709 710 711 709 } 710 711 private String languageDefaultToString(Element command_element) { 712 712 StringBuffer text = new StringBuffer(LANGUAGE_DEFAULT_STR); 713 713 text.append(TAB_CHARACTER); 714 714 text.append(command_element.getAttribute(NAME_ATTRIBUTE)); 715 715 return text.toString(); 716 717 718 716 } 717 718 private String levelsToString(Element command_element) { 719 719 StringBuffer text = new StringBuffer(""); 720 720 if(!command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(TRUE_STR)) { 721 721 text.append("#"); 722 722 } 723 723 text.append(LEVELS_STR); … … 728 728 // Don't output anything if no levels are set. 729 729 if(content_elements_length == 0) { 730 730 return null; 731 731 } 732 732 for(int i = 0; i < content_elements_length; i++) { 733 734 735 733 Element content_element = (Element) content_elements.item(i); 734 text.append(content_element.getAttribute(NAME_ATTRIBUTE)); 735 text.append(SPACE_CHARACTER); 736 736 } 737 737 return text.substring(0, text.length() - 1); 738 739 740 738 } 739 740 static public String metadataToString(Element command_element, boolean text_value) { 741 741 boolean special = false; 742 742 // If there is no value attribute, then we don't write anything … … 745 745 // If the name is one of the special four, we don't write the collectionmeta first. Note the maintainer collectionmeta is singled out for 'prittying' reasons. 746 746 if(name_str.equals(COLLECTIONMETADATA_MAINTAINER_STR)) { 747 748 749 747 text.append(name_str); 748 text.append(TAB_CHARACTER); 749 special = true; 750 750 } 751 751 else if(name_str.equals(COLLECTIONMETADATA_BETA_STR) || name_str.equals(COLLECTIONMETADATA_CREATOR_STR) || name_str.equals(COLLECTIONMETADATA_PUBLIC_STR)) { 752 753 754 755 752 text.append(name_str); 753 text.append(TAB_CHARACTER); 754 text.append(TAB_CHARACTER); 755 special = true; 756 756 } 757 757 else { 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 758 text.append(COLLECTIONMETADATA_STR); 759 text.append(TAB_CHARACTER); 760 text.append(name_str); 761 text.append(SPACE_CHARACTER); 762 String language_str = command_element.getAttribute(LANGUAGE_ATTRIBUTE); 763 // If this is element is in english, and it is the first one found, we don't need to write the language argument. 764 //if(!language_str.equals(ENGLISH_LANGUAGE_STR) || known_metadata == null || known_metadata.contains(name_str)) { 765 // changed so that we always write the language string 766 text.append(LBRACKET_CHARACTER); 767 text.append(LANGUAGE_ARGUMENT); 768 text.append(language_str); 769 text.append(RBRACKET_CHARACTER); 770 text.append(SPACE_CHARACTER); 771 //} 772 if(known_metadata != null) { 773 773 known_metadata.add(name_str); 774 775 774 } 775 language_str = null; 776 776 } 777 777 name_str = null; 778 778 779 779 String value_str = MSMUtils.getValue(command_element); 780 780 // The value string we retrieved will be encoded for xml, so we now decode it - to text if text_value set. This parameter was originally show_extracted_namespace, but sincethis is only true for 'toString()' commands from within the CDM, its good enough to determine if this toString() will be used to display on screen, or write to collect.cfg 781 781 if(text_value == CollectionMeta.TEXT) { 782 782 value_str = Codec.transform(value_str, Codec.DOM_TO_TEXT); 783 783 } 784 784 else { 785 786 } 787 785 value_str = Codec.transform(value_str, Codec.DOM_TO_GREENSTONE); 786 } 787 788 788 // We don't wrap the email addresses in quotes, nor the other special metadata 789 789 if(special) { 790 790 text.append(value_str); 791 791 } 792 792 else { 793 794 795 793 text.append(SPEECH_CHARACTER); 794 text.append(value_str); 795 text.append(SPEECH_CHARACTER); 796 796 } 797 797 value_str = null; 798 798 return text.toString(); 799 } 800 801 /** Parse a collect.cfg into a DOM model representation. */ 802 private void parse(File collect_config_file) { 803 try { 804 Element collect_cfg_element = document.getDocumentElement(); 805 // Read in the file one command at a time. 806 FileReader in_reader = new FileReader(collect_config_file); 807 BufferedReader in = new BufferedReader(in_reader); 808 String command_str = null; 809 while((command_str = in.readLine()) != null) { 810 Element command_element = null; 811 // A command may be broken over several lines. 812 command_str = command_str.trim(); 813 boolean eof = false; 814 while(!eof && command_str.endsWith(NEWLINE_CHARACTER)) { 815 String next_line = in.readLine(); 816 if(next_line != null) { 817 next_line = next_line.trim(); 818 if(next_line.length() > 0) { 819 // Remove the new line character 820 command_str = command_str.substring(0, command_str.lastIndexOf(NEWLINE_CHARACTER)); 821 // And append the next line, which due to the test above must be non-zero length 822 command_str = command_str + next_line; 823 } 824 next_line = null; 825 } 826 // If we've reached the end of the file theres nothing more we can do 827 else { 828 eof = true; 829 } 830 } 831 // If there is still a new line character, then we remove it and hope for the best 832 if(command_str.endsWith(NEWLINE_CHARACTER)) { 833 command_str = command_str.substring(0, command_str.lastIndexOf(NEWLINE_CHARACTER)); 834 } 835 // Now we've either got a command to parse... 836 if(command_str.length() != 0) { 837 // Start trying to figure out what it is 838 StringTokenizer tokenizer = new StringTokenizer(command_str); 839 String command_type = tokenizer.nextToken().toLowerCase(); 840 tokenizer = null; 841 // Why can't you switch on strings eh? We pass it to the various subparsers who each have a bash at parsing the command. If none can parse the command, an unknown element is created 842 if(command_element == null && command_type.equals(CLASSIFY_STR)) { 843 command_element = parseClassify(command_str); 844 } 845 if(command_element == null && command_type.equals(FORMAT_STR)) { 846 command_element = parseFormat(command_str, in); 847 } 848 if(command_element == null && (command_type.equals(INDEX_STR) || command_type.equals(COMMENTED_INDEXES_STR))) { 849 command_element = parseIndex(command_str); 850 } 851 if(command_element == null && (command_type.equals(INDEX_DEFAULT_STR) || command_type.equals(COMMENTED_INDEX_DEFAULT_STR))) { 852 command_element = parseIndexDefault(command_str); 853 } 854 if(command_element == null && command_type.equals(LANGUAGES_STR)) { 855 command_element = parseLanguage(command_str); 856 } 857 if(command_element == null && command_type.equals(LANGUAGE_DEFAULT_STR)) { 858 command_element = parseLanguageDefault(command_str); 859 } 860 if(command_element == null && (command_type.equals(LEVELS_STR) || command_type.equals(COMMENTED_LEVELS_STR))) { 861 command_element = parseLevels(command_str); 862 } 863 if(command_element == null && command_type.equals(COLLECTIONMETADATA_STR)) { 864 // collectionmeta may go over more than one line, so 865 // pass in the reader 866 command_element = parseMetadata(command_str, in); 867 } 868 if(command_element == null && (command_type.equals(COLLECTIONMETADATA_BETA_STR) || command_type.equals(COLLECTIONMETADATA_PUBLIC_STR) || command_type.equals(COLLECTIONMETADATA_CREATOR_STR) || command_type.equals(COLLECTIONMETADATA_MAINTAINER_STR))) { 869 command_element = parseMetadataSpecial(command_str); 870 } 871 if(command_element == null && command_type.equals(PLUGIN_STR)) { 872 command_element = parsePlugIn(command_str); 873 } 874 if(command_element == null && command_type.equals(SEARCHTYPE_STR)) { 875 command_element = parseSearchType(command_str); 876 } 877 if(command_element == null && command_type.equals(SUBCOLLECTION_STR)) { 878 command_element = parseSubCollection(command_str); 879 } 880 if(command_element == null && command_type.equals(SUBCOLLECTION_DEFAULT_INDEX_STR)) { 881 command_element = parseSubCollectionDefaultIndex(command_str); 882 } 883 if(command_element == null && command_type.equals(SUBCOLLECTION_INDEX_STR)) { 884 command_element = parseSubCollectionIndex(command_str); 885 } 886 if(command_element == null && (command_type.equals(SUPERCOLLECTION_STR) || command_type.equals(CCS_STR))) { 887 command_element = parseSuperCollection(command_str); 888 } 889 // Doesn't match any known type 890 command_type = null; 891 if(command_element == null) { 799 } 800 801 /** Parse a collect.cfg into a DOM model representation. */ 802 private void parse(File collect_config_file) { 803 try { 804 Element collect_cfg_element = document.getDocumentElement(); 805 // Read in the file one command at a time. 806 FileReader in_reader = new FileReader(collect_config_file); 807 BufferedReader in = new BufferedReader(in_reader); 808 String command_str = null; 809 while((command_str = in.readLine()) != null) { 810 Element command_element = null; 811 // A command may be broken over several lines. 812 command_str = command_str.trim(); 813 boolean eof = false; 814 while(!eof && command_str.endsWith(NEWLINE_CHARACTER)) { 815 String next_line = in.readLine(); 816 if(next_line != null) { 817 next_line = next_line.trim(); 818 if(next_line.length() > 0) { 819 // Remove the new line character 820 command_str = command_str.substring(0, command_str.lastIndexOf(NEWLINE_CHARACTER)); 821 // And append the next line, which due to the test above must be non-zero length 822 command_str = command_str + next_line; 823 } 824 next_line = null; 825 } 826 // If we've reached the end of the file theres nothing more we can do 827 else { 828 eof = true; 829 } 830 } 831 // If there is still a new line character, then we remove it and hope for the best 832 if(command_str.endsWith(NEWLINE_CHARACTER)) { 833 command_str = command_str.substring(0, command_str.lastIndexOf(NEWLINE_CHARACTER)); 834 } 835 // Now we've either got a command to parse... 836 if(command_str.length() != 0) { 837 // Start trying to figure out what it is 838 //StringTokenizer tokenizer = new StringTokenizer(command_str); 839 // Instead of a standard string tokenizer I'm going to use the new version of CommandTokenizer, which is not only smart enough to correctly notice speech marks and correctly parse them out, but now also takes the input stream so it can rebuild tokens that stretch over several lines. 840 CommandTokenizer tokenizer = new CommandTokenizer(command_str, in); 841 String command_type = tokenizer.nextToken().toLowerCase(); 842 // Why can't you switch on strings eh? We pass it to the various subparsers who each have a bash at parsing the command. If none can parse the command, an unknown element is created 843 if(command_element == null && command_type.equals(CLASSIFY_STR)) { 844 command_element = parseClassify(command_str); 845 } 846 if(command_element == null && command_type.equals(FORMAT_STR)) { 847 command_element = parseFormat(tokenizer); // Revised to handle multiple lines 848 } 849 if(command_element == null && (command_type.equals(INDEX_STR) || command_type.equals(COMMENTED_INDEXES_STR))) { 850 command_element = parseIndex(command_str); 851 } 852 if(command_element == null && (command_type.equals(INDEX_DEFAULT_STR) || command_type.equals(COMMENTED_INDEX_DEFAULT_STR))) { 853 command_element = parseIndexDefault(command_str); 854 } 855 if(command_element == null && command_type.equals(LANGUAGES_STR)) { 856 command_element = parseLanguage(command_str); 857 } 858 if(command_element == null && command_type.equals(LANGUAGE_DEFAULT_STR)) { 859 command_element = parseLanguageDefault(command_str); 860 } 861 if(command_element == null && (command_type.equals(LEVELS_STR) || command_type.equals(COMMENTED_LEVELS_STR))) { 862 command_element = parseLevels(command_str); 863 } 864 if(command_element == null && command_type.equals(COLLECTIONMETADATA_STR)) { 865 command_element = parseMetadata(tokenizer); // Revised to handle multiple lines 866 } 867 if(command_element == null && (command_type.equals(COLLECTIONMETADATA_BETA_STR) || command_type.equals(COLLECTIONMETADATA_PUBLIC_STR) || command_type.equals(COLLECTIONMETADATA_CREATOR_STR) || command_type.equals(COLLECTIONMETADATA_MAINTAINER_STR))) { 868 command_element = parseMetadataSpecial(command_str); 869 } 870 if(command_element == null && command_type.equals(PLUGIN_STR)) { 871 command_element = parsePlugIn(command_str); 872 } 873 if(command_element == null && command_type.equals(SEARCHTYPE_STR)) { 874 command_element = parseSearchType(command_str); 875 } 876 if(command_element == null && command_type.equals(SUBCOLLECTION_STR)) { 877 command_element = parseSubCollection(command_str); 878 } 879 if(command_element == null && command_type.equals(SUBCOLLECTION_DEFAULT_INDEX_STR)) { 880 command_element = parseSubCollectionDefaultIndex(command_str); 881 } 882 if(command_element == null && command_type.equals(SUBCOLLECTION_INDEX_STR)) { 883 command_element = parseSubCollectionIndex(command_str); 884 } 885 if(command_element == null && (command_type.equals(SUPERCOLLECTION_STR) || command_type.equals(CCS_STR))) { 886 command_element = parseSuperCollection(command_str); 887 } 888 // Doesn't match any known type 889 command_type = null; 890 if(command_element == null) { 892 891 // No-one knows what to do with this command, so we create an Unknown command element 893 892 command_element = document.createElement(UNKNOWN_ELEMENT); … … 897 896 // Or an empty line to remember for later 898 897 else { 899 898 command_element = document.createElement(NEWLINE_ELEMENT); 900 899 } 901 900 // Now command element shouldn't be null so we append it to the collection config DOM 902 901 collect_cfg_element.appendChild(command_element); 903 902 } 904 903 } 905 904 catch(Exception exception) { 906 907 908 } 909 910 911 905 Gatherer.println("Error in CollectionConfiguration.parse(java.io.File): " + exception); 906 Gatherer.printStackTrace(exception); 907 } 908 } 909 910 private Element parseClassify(String command_str) { 912 911 Element command_element = null; 913 912 try { 914 915 916 913 CommandTokenizer tokenizer = new CommandTokenizer(command_str); 914 // Check the token count. The token count from a command tokenizer isn't guarenteed to be correct, but it does give the maximum number of available tokens according to the underlying StringTokenizer (some of which may actually be append together by the CommandTokenizer as being a single argument). 915 if(tokenizer.countTokens() >= 2) { // Must support "classify Phind" (no args) 917 916 command_element = document.createElement(CLASSIFY_ELEMENT); 918 917 // First token is classify … … 925 924 Iterator names = arguments.keySet().iterator(); 926 925 while(names.hasNext()) { 927 928 929 930 926 String name = (String) names.next(); 927 String value = (String) arguments.get(name); // Can be null 928 // The metadata argument gets added as the content attribute 929 if(name.equals(METADATA_ARGUMENT) && value != null) { 931 930 // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace. 932 931 if(value.indexOf(MSMUtils.NS_SEP) == -1) { 933 932 value = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP + value; 934 933 } 935 934 //command_element.setAttribute(CONTENT_ATTRIBUTE, value); 936 937 938 939 940 935 } 936 // Everything else is an Option Element 937 Element option_element = document.createElement(OPTION_ELEMENT); 938 option_element.setAttribute(NAME_ATTRIBUTE, name.substring(1)); 939 if(value != null) { 941 940 // Remove any speech marks appended in strings containing whitespace 942 941 if(value.startsWith(SPEECH_CHARACTER) && value.endsWith(SPEECH_CHARACTER)) { 943 942 value = value.substring(1, value.length() - 1); 944 943 } 945 944 MSMUtils.setValue(option_element, value); 946 947 948 949 950 951 952 945 } 946 option_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 947 option_element.setAttribute(CUSTOM_ATTRIBUTE, TRUE_STR); 948 command_element.appendChild(option_element); 949 option_element = null; 950 name = null; 951 value = null; 953 952 } 954 953 names = null; 955 954 arguments = null; 956 957 955 } 956 tokenizer = null; 958 957 } 959 958 catch(Exception error) { 960 959 } 961 960 return command_element; 962 } 963 964 private Element parseFormat(String command_str, BufferedReader in) { 961 } 962 963 private Element parseFormat(CommandTokenizer tokenizer) { 964 Element command_element = null; 965 try { 966 command_element = document.createElement(FORMAT_ELEMENT); 967 String name_str = tokenizer.nextToken(); 968 String value_str = tokenizer.nextToken(); 969 if(name_str != null && value_str != null) { 970 command_element.setAttribute(NAME_ATTRIBUTE, name_str); 971 // If the value is true or false we add it as an attribute 972 if(value_str.equalsIgnoreCase(TRUE_STR) || value_str.equalsIgnoreCase(FALSE_STR)) { 973 command_element.setAttribute(VALUE_ATTRIBUTE, value_str.toLowerCase()); 974 } 975 // Otherwise it gets added as a text node 976 else { 977 // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value 978 value_str = Codec.transform(value_str, Codec.GREENSTONE_TO_DOM); 979 MSMUtils.setValue(command_element, value_str); 980 } 981 } 982 else { 983 command_element = null; 984 } 985 name_str = null; 986 value_str = null; 987 } 988 catch (Exception exception) { 989 Gatherer.printStackTrace(exception); 990 command_element = null; 991 } 992 return command_element; 993 } 994 995 private Element parseIndex(String command_str) { 965 996 Element command_element = null; 966 997 try { 967 CommandTokenizer tokenizer = new CommandTokenizer(command_str); 968 if(tokenizer.countTokens() >= 3) { 969 command_element = document.createElement(FORMAT_ELEMENT); 970 // First token is format 971 tokenizer.nextToken(); 972 command_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken()); 973 String value_str = tokenizer.nextToken(); 974 // If the value is true or false we add it as an attribute 975 if(value_str.equalsIgnoreCase(TRUE_STR) || value_str.equalsIgnoreCase(FALSE_STR)) { 976 command_element.setAttribute(VALUE_ATTRIBUTE, value_str.toLowerCase()); 977 } 978 // Otherwise it gets added as a text node 979 else { 980 981 // now we need to handle the case where the value is enclosed in quotes (single or double) and may extend across multiple lines 982 String start_string = value_str.substring(0,1); 983 if (start_string.equals("\"") || start_string.equals("\'")) { 984 if (value_str.endsWith(start_string) && value_str.length()!=1) { 985 // we remove the quotes from the ends 986 value_str = value_str.substring(1, value_str.length() - 1); 987 } else { 988 989 // remove the first quote 990 StringBuffer value_raw = new StringBuffer(value_str.substring(1)); 991 // add the new line back in 992 value_raw.append(StaticStrings.NEW_LINE_CHAR); 993 int pos = value_raw.indexOf(start_string); 994 int old_pos = 0; 995 while (pos != -1 && value_raw.charAt(pos-1)=='\\') { 996 old_pos = pos+1; 997 pos = value_raw.indexOf(start_string, old_pos); 998 } 999 while(pos == -1) { 1000 String next_line = in.readLine(); 1001 if(next_line != null) { 1002 value_raw.append(next_line); 1003 value_raw.append(StaticStrings.NEW_LINE_CHAR); 1004 } 1005 next_line = null; 1006 pos = value_raw.indexOf(start_string, old_pos); 1007 while (pos != -1 && value_raw.charAt(pos-1)=='\\') { 1008 old_pos = pos+1; 1009 pos = value_raw.indexOf(start_string, old_pos); 1010 } 1011 } 1012 1013 value_str = value_raw.substring(0, value_raw.lastIndexOf(start_string)); 1014 value_raw = null; 1015 1016 } // else 1017 } // if starts with a quote 1018 1019 if(value_str != null) { 1020 // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value 1021 value_str = Codec.transform(value_str, Codec.GREENSTONE_TO_DOM); 1022 MSMUtils.setValue(command_element, value_str); 1023 } 1024 else { 1025 command_element = null; 1026 } 1027 start_string = null; 1028 } 1029 value_str = null; 1030 1031 } 1032 tokenizer = null; 1033 } 1034 catch (Exception exception) { 1035 } 1036 return command_element; 1037 } 1038 1039 private Element parseIndex(String command_str) { 1040 Element command_element = null; 1041 try { 1042 StringTokenizer tokenizer = new StringTokenizer(command_str); 1043 String command = tokenizer.nextToken(); 1044 command_element = document.createElement(INDEXES_ELEMENT); 1045 command_element.setAttribute(ASSIGNED_ATTRIBUTE, (command.equals(INDEX_STR) ? TRUE_STR : FALSE_STR)); 1046 command = null; 1047 if(!tokenizer.hasMoreTokens()) { 1048 998 StringTokenizer tokenizer = new StringTokenizer(command_str); 999 String command = tokenizer.nextToken(); 1000 command_element = document.createElement(INDEXES_ELEMENT); 1001 command_element.setAttribute(ASSIGNED_ATTRIBUTE, (command.equals(INDEX_STR) ? TRUE_STR : FALSE_STR)); 1002 command = null; 1003 if(!tokenizer.hasMoreTokens()) { 1004 1049 1005 // there are no indexes 1050 1006 command_element.setAttribute(ASSIGNED_ATTRIBUTE, FALSE_STR); … … 1052 1008 tokenizer = null; 1053 1009 return command_element; 1054 1055 1056 1010 } 1011 1012 while(tokenizer.hasMoreTokens()) { 1057 1013 Element index_element = document.createElement(INDEX_ELEMENT); 1058 1014 String index_str = tokenizer.nextToken(); … … 1060 1016 boolean old_index; 1061 1017 if(index_str.indexOf(COLON_CHARACTER) != -1) { 1062 1063 1064 1065 1018 old_index = true; 1019 index_element.setAttribute(LEVEL_ATTRIBUTE, index_str.substring(0, index_str.indexOf(StaticStrings.COLON_CHARACTER))); 1020 index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1); 1021 command_element.setAttribute(MGPP_ATTRIBUTE, FALSE_STR); 1066 1022 } 1067 1023 else { 1068 1069 1024 command_element.setAttribute(MGPP_ATTRIBUTE, TRUE_STR); 1025 old_index = false; 1070 1026 } 1071 1027 StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER); 1072 1028 while(content_tokenizer.hasMoreTokens()) { 1073 1074 1075 1076 1029 Element content_element = document.createElement(CONTENT_ELEMENT); 1030 String content_str = content_tokenizer.nextToken(); 1031 // Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace. 1032 if(content_str.indexOf(MSMUtils.NS_SEP) == -1) { 1077 1033 if(content_str.equals(StaticStrings.TEXT_STR) || (!old_index && content_str.equals(StaticStrings.ALLFIELDS_STR))) { 1078 1034 // Our special strings are OK. 1079 1035 } 1080 1036 else { 1081 1082 } 1083 1084 1085 1086 1037 content_str = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP + content_str; 1038 } 1039 } 1040 content_element.setAttribute(NAME_ATTRIBUTE, content_str); 1041 index_element.appendChild(content_element); 1042 content_element = null; 1087 1043 } 1088 1044 content_tokenizer = null; … … 1090 1046 command_element.appendChild(index_element); 1091 1047 index_element = null; 1092 1093 1048 } 1049 tokenizer = null; 1094 1050 } 1095 1051 catch (Exception exception) { 1096 1052 exception.printStackTrace(); 1097 1053 } 1098 1054 return command_element; 1099 1100 1101 1055 } 1056 1057 private Element parseIndexDefault(String command_str) { 1102 1058 Element command_element = null; 1103 1059 try { 1104 1105 1060 StringTokenizer tokenizer = new StringTokenizer(command_str); 1061 if(tokenizer.countTokens() >= 2) { 1106 1062 command_element = document.createElement(INDEX_DEFAULT_ELEMENT); 1107 1063 command_element.setAttribute(ASSIGNED_ATTRIBUTE, ((tokenizer.nextToken()).equals(INDEX_DEFAULT_STR) ? TRUE_STR : FALSE_STR)); … … 1111 1067 StringTokenizer content_tokenizer = new StringTokenizer(content_str, StaticStrings.COMMA_CHARACTER); 1112 1068 while(content_tokenizer.hasMoreTokens()) { 1113 1114 1115 1116 1069 Element content_element = document.createElement(CONTENT_ELEMENT); 1070 content_element.setAttribute(NAME_ATTRIBUTE, content_tokenizer.nextToken()); 1071 command_element.appendChild(content_element); 1072 content_element = null; 1117 1073 } 1118 1074 content_tokenizer = null; … … 1120 1076 content_str = null; 1121 1077 index_str = null; 1122 1123 1078 } 1079 tokenizer = null; 1124 1080 } 1125 1081 catch (Exception exception) { 1126 1082 } 1127 1083 return command_element; 1128 1129 1130 1084 } 1085 1086 private Element parseLanguage(String command_str) { 1131 1087 Element command_element = null; 1132 1088 try { 1133 1134 1135 1089 StringTokenizer tokenizer = new StringTokenizer(command_str); 1090 tokenizer.nextToken(); 1091 if(tokenizer.hasMoreTokens()) { 1136 1092 command_element = document.createElement(LANGUAGES_ELEMENT); 1137 1093 while(tokenizer.hasMoreTokens()) { 1138 1139 1140 1141 1142 } 1143 1144 1094 Element language_element = document.createElement(LANGUAGE_ELEMENT); 1095 language_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken()); 1096 command_element.appendChild(language_element); 1097 language_element = null; 1098 } 1099 } 1100 tokenizer = null; 1145 1101 } 1146 1102 catch (Exception exception) { 1147 1103 } 1148 1104 return command_element; 1149 1150 1151 1105 } 1106 1107 private Element parseLanguageDefault(String command_str) { 1152 1108 Element command_element = null; 1153 1109 try { 1154 1155 1110 StringTokenizer tokenizer = new StringTokenizer(command_str); 1111 if(tokenizer.countTokens() >= 2) { 1156 1112 command_element = document.createElement(LANGUAGE_DEFAULT_ELEMENT); 1157 1113 tokenizer.nextToken(); … … 1160 1116 command_element.setAttribute(ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR); 1161 1117 default_language_str = null; 1162 1163 1118 } 1119 tokenizer = null; 1164 1120 } 1165 1121 catch (Exception exception) { 1166 1122 } 1167 1123 return command_element; 1168 1169 1170 1124 } 1125 1126 private Element parseLevels(String command_str) { 1171 1127 Element command_element = null; 1172 1128 try { 1173 1174 1175 1176 1129 StringTokenizer tokenizer = new StringTokenizer(command_str); 1130 // First token is command type (levels) 1131 String command = tokenizer.nextToken(); 1132 if(tokenizer.hasMoreTokens()) { 1177 1133 command_element = document.createElement(LEVELS_ELEMENT); 1178 1134 command_element.setAttribute(ASSIGNED_ATTRIBUTE, (command.equals(LEVELS_STR) ? TRUE_STR : FALSE_STR)); 1179 1135 while(tokenizer.hasMoreTokens()) { 1180 1181 1182 1183 1184 } 1185 1186 1136 Element level_element = document.createElement(CONTENT_ELEMENT); 1137 level_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken()); 1138 command_element.appendChild(level_element); 1139 level_element = null; 1140 } 1141 } 1142 command = null; 1187 1143 } 1188 1144 catch(Exception exception) { 1189 1145 } 1190 1146 return command_element; 1191 } 1192 1193 private Element parseMetadata(String command_str, BufferedReader in) { 1147 } 1148 1149 private Element parseMetadata(CommandTokenizer tokenizer) { 1150 Element command_element = null; 1151 try { 1152 command_element = document.createElement(COLLECTIONMETADATA_ELEMENT); 1153 String name_str = tokenizer.nextToken(); 1154 String value_str = tokenizer.nextToken(); 1155 if(name_str != null && value_str != null) { 1156 String language_str = "en"; // By default - why do we assume English??? 1157 // Check if the value string is actually a language string 1158 if(value_str.startsWith(LBRACKET_CHARACTER) && value_str.endsWith(RBRACKET_CHARACTER)) { 1159 language_str = value_str.substring(value_str.indexOf(LANGUAGE_ARGUMENT) + 2, value_str.length() - 1); 1160 value_str = tokenizer.nextToken(); 1161 } 1162 if(value_str != null) { 1163 // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value 1164 value_str = Codec.transform(value_str, Codec.GREENSTONE_TO_DOM); 1165 command_element.setAttribute(NAME_ATTRIBUTE, name_str); 1166 command_element.setAttribute(LANGUAGE_ATTRIBUTE, language_str); 1167 command_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 1168 MSMUtils.setValue(command_element, value_str); 1169 } 1170 else { 1171 command_element = null; 1172 } 1173 language_str = null; 1174 } 1175 else { 1176 command_element = null; 1177 } 1178 name_str = null; 1179 value_str = null; 1180 } 1181 catch (Exception exception) { 1182 Gatherer.printStackTrace(exception); 1183 command_element = null; 1184 } 1185 return command_element; 1186 } 1187 1188 private Element parseMetadataSpecial(String command_str) { 1194 1189 Element command_element = null; 1195 1190 try { 1196 CommandTokenizer tokenizer = new CommandTokenizer(command_str); 1197 if(tokenizer.countTokens() >= 3) { 1198 command_element = document.createElement(COLLECTIONMETADATA_ELEMENT); 1199 // First token is command type 1200 tokenizer.nextToken(); 1201 String name_str = tokenizer.nextToken(); 1202 String value_str = tokenizer.nextToken(); 1203 String language_str = "en"; // By default - why do we assume English??? 1204 // Check if the value string is actually a language string 1205 if(value_str.startsWith(LBRACKET_CHARACTER) && value_str.endsWith(RBRACKET_CHARACTER)) { 1206 language_str = value_str.substring(value_str.indexOf(LANGUAGE_ARGUMENT) + 2, value_str.length() - 1); 1207 value_str = tokenizer.nextToken(); 1208 } 1209 1210 // now we need to handle the case where the value is enclosed in quotes (single or double) and may extend across multiple lines 1211 String start_string = value_str.substring(0,1); 1212 if (start_string.equals("\"") || start_string.equals("\'")) { 1213 1214 if (value_str.endsWith(start_string) && value_str.length()!=1) { 1215 // we remove the quotes from the ends 1216 value_str = value_str.substring(1, value_str.length() - 1); 1217 } else { 1218 1219 // remove the first quote 1220 StringBuffer value_raw = new StringBuffer(value_str.substring(1)); 1221 // add the new line back in 1222 value_raw.append(StaticStrings.NEW_LINE_CHAR); 1223 1224 int pos = value_raw.indexOf(start_string); 1225 int old_pos = 0; 1226 while (pos != -1 && value_raw.charAt(pos-1)=='\\') { 1227 old_pos = pos+1; 1228 pos = value_raw.indexOf(start_string, old_pos); 1229 } 1230 while(pos == -1) { 1231 String next_line = in.readLine(); 1232 if(next_line != null) { 1233 value_raw.append(next_line); 1234 value_raw.append(StaticStrings.NEW_LINE_CHAR); 1235 } 1236 next_line = null; 1237 pos = value_raw.indexOf(start_string, old_pos); 1238 while (pos != -1 && value_raw.charAt(pos-1)=='\\') { 1239 old_pos = pos+1; 1240 pos = value_raw.indexOf(start_string, old_pos); 1241 } 1242 } 1243 1244 value_str = value_raw.substring(0, value_raw.lastIndexOf(start_string)); 1245 value_raw = null; 1246 1247 } // else 1248 } // if starts with a quote 1249 1250 if(value_str != null) { 1251 // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value 1252 value_str = Codec.transform(value_str, Codec.GREENSTONE_TO_DOM); 1253 command_element.setAttribute(NAME_ATTRIBUTE, name_str); 1254 command_element.setAttribute(LANGUAGE_ATTRIBUTE, language_str); 1255 command_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 1256 MSMUtils.setValue(command_element, value_str); 1257 } 1258 else { 1259 command_element = null; 1260 } 1261 language_str = null; 1262 value_str = null; 1263 name_str = null; 1264 } 1265 tokenizer = null; 1266 } 1267 catch (Exception exception) { 1268 } 1269 return command_element; 1270 } 1271 1272 private Element parseMetadataSpecial(String command_str) { 1273 Element command_element = null; 1274 try { 1275 StringTokenizer tokenizer = new StringTokenizer(command_str); 1276 if(tokenizer.countTokens() >= 2) { 1191 StringTokenizer tokenizer = new StringTokenizer(command_str); 1192 if(tokenizer.countTokens() >= 2) { 1277 1193 String name_str = tokenizer.nextToken(); 1278 1194 String value_str = tokenizer.nextToken(); 1279 1195 if(name_str.equals(COLLECTIONMETADATA_BETA_STR)) { 1280 1196 command_element = document.createElement(COLLECTIONMETADATA_BETA_ELEMENT); 1281 1197 } 1282 1198 else if(name_str.equals(COLLECTIONMETADATA_CREATOR_STR)) { 1283 1199 command_element = document.createElement(COLLECTIONMETADATA_CREATOR_ELEMENT); 1284 1200 } 1285 1201 else if(name_str.equals(COLLECTIONMETADATA_MAINTAINER_STR)) { 1286 1202 command_element = document.createElement(COLLECTIONMETADATA_MAINTAINER_ELEMENT); 1287 1203 } 1288 1204 else if(name_str.equals(COLLECTIONMETADATA_PUBLIC_STR)) { 1289 1205 command_element = document.createElement(COLLECTIONMETADATA_PUBLIC_ELEMENT); 1290 1206 } 1291 1207 if(command_element != null) { 1292 1293 1294 1295 1296 1208 command_element.setAttribute(NAME_ATTRIBUTE, name_str); 1209 command_element.setAttribute(LANGUAGE_ATTRIBUTE, ENGLISH_LANGUAGE_STR); 1210 command_element.setAttribute(SPECIAL_ATTRIBUTE, TRUE_STR); 1211 command_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 1212 if(value_str.startsWith(SPEECH_CHARACTER) && value_str.endsWith(SPEECH_CHARACTER)) { 1297 1213 value_str = value_str.substring(1, value_str.length() - 1); 1298 1299 1214 } 1215 MSMUtils.setValue(command_element, value_str); 1300 1216 } 1301 1217 value_str = null; 1302 1218 name_str = null; 1303 1304 1219 } 1220 tokenizer = null; 1305 1221 } 1306 1222 catch (Exception exception) { 1307 1223 } 1308 1224 return command_element; 1309 1310 1311 1225 } 1226 1227 private Element parsePlugIn(String command_str) { 1312 1228 Element command_element = null; 1313 1229 boolean use_metadata_files = false; 1314 1230 boolean show_progress = false; 1315 1231 try { 1316 1317 1318 1232 CommandTokenizer tokenizer = new CommandTokenizer(command_str); 1233 // Check the token count. The token count from a command tokenizer isn't guarenteed to be correct, but it does give the maximum number of available tokens according to the underlying StringTokenizer (some of which may actually be append together by the CommandTokenizer as being a single argument). 1234 if(tokenizer.countTokens() >= 2) { 1319 1235 command_element = document.createElement(PLUGIN_ELEMENT); 1320 1236 // First token is plugin … … 1328 1244 Iterator names = arguments.keySet().iterator(); 1329 1245 while(names.hasNext()) { 1330 1331 1332 1333 1246 String name = (String) names.next(); 1247 String value = (String) arguments.get(name); // Can be null 1248 Element option_element = document.createElement(OPTION_ELEMENT); 1249 if(name.substring(1).equals(USE_METADATA_FILES_ARGUMENT)) { 1334 1250 use_metadata_files = true; 1335 1336 1337 1338 1339 1340 1341 1342 1251 } 1252 else if(name.substring(1).equals(SHOW_PROGRESS_ARGUMENT)) { 1253 show_progress = true; 1254 } 1255 option_element.setAttribute(NAME_ATTRIBUTE, name.substring(1)); 1256 option_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 1257 option_element.setAttribute(CUSTOM_ATTRIBUTE, TRUE_STR); // All arguments are considered to be custom until matched against base plugins arguments 1258 if(value != null) { 1343 1259 // Remove any speech marks appended in strings containing whitespace 1344 1260 if(value.startsWith(SPEECH_CHARACTER) && value.endsWith(SPEECH_CHARACTER)) { 1345 1261 value = value.substring(1, value.length() - 1); 1346 1262 } 1347 1263 if(name.equals(METADATA_ARGUMENT)) { 1348 1349 1264 // The metadata argument must be the fully qualified name of a metadata element, so if it doesn't yet have a namespace, append the extracted metadata namespace. 1265 if(value.indexOf(MSMUtils.NS_SEP) == -1) { 1350 1266 value = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP + value; 1351 1267 } 1352 1268 } 1353 1269 MSMUtils.setValue(option_element, value); 1354 1355 1356 1357 1358 1270 } 1271 command_element.appendChild(option_element); 1272 option_element = null; 1273 name = null; 1274 value = null; 1359 1275 } 1360 1276 1361 1277 // We must have some RecPlug options: use_metadata_files, and show_progress 1362 1278 if (type.equals(RECPLUG_STR)) { 1363 1279 if (!use_metadata_files) { 1364 1280 Element option_element = document.createElement(OPTION_ELEMENT); 1365 1281 option_element.setAttribute(NAME_ATTRIBUTE, USE_METADATA_FILES_ARGUMENT); … … 1368 1284 command_element.appendChild(option_element); 1369 1285 option_element = null; 1370 1371 1372 1373 1374 1375 1376 1377 1378 1286 } 1287 if(!show_progress) { 1288 Element option_element = document.createElement(OPTION_ELEMENT); 1289 option_element.setAttribute(NAME_ATTRIBUTE, SHOW_PROGRESS_ARGUMENT); 1290 option_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 1291 option_element.setAttribute(CUSTOM_ATTRIBUTE, TRUE_STR); 1292 command_element.appendChild(option_element); 1293 option_element = null; 1294 } 1379 1295 } 1380 1296 type = null; 1381 1297 names = null; 1382 1298 arguments = null; 1383 1384 1299 } 1300 tokenizer = null; 1385 1301 } 1386 1302 catch(Exception exception) { 1387 1303 } 1388 1304 return command_element; 1389 1390 1391 1305 } 1306 1307 private Element parseSearchType(String command_str) { 1392 1308 Element command_element = null; 1393 1309 try { 1394 1395 1396 1397 1310 StringTokenizer tokenizer = new StringTokenizer(command_str); 1311 // First token is command type (levels) 1312 tokenizer.nextToken(); 1313 if(tokenizer.hasMoreTokens()) { 1398 1314 command_element = document.createElement(SEARCHTYPE_ELEMENT); 1399 1315 command_element.setAttribute(ASSIGNED_ATTRIBUTE, TRUE_STR); 1400 1316 while(tokenizer.hasMoreTokens()) { 1401 1402 1403 1404 1405 } 1406 1317 Element search_element = document.createElement(CONTENT_ELEMENT); 1318 search_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken()); 1319 command_element.appendChild(search_element); 1320 search_element = null; 1321 } 1322 } 1407 1323 } 1408 1324 catch(Exception exception) { 1409 1325 } 1410 1326 return command_element; 1411 1412 1413 1327 } 1328 1329 private Element parseSubCollection(String command_str) { 1414 1330 Element command_element = null; 1415 1331 try { 1416 1417 1332 CommandTokenizer tokenizer = new CommandTokenizer(command_str); 1333 if(tokenizer.countTokens() >= 3) { 1418 1334 command_element = document.createElement(SUBCOLLECTION_ELEMENT); 1419 1335 // First token is command type … … 1427 1343 // Set inclusion/exclusion flag, remove any exclaimation mark and the speech marks 1428 1344 if(exclusion) { 1429 1430 1345 full_pattern_str = full_pattern_str.substring(2, full_pattern_str.length() - 1); 1346 command_element.setAttribute(TYPE_ATTRIBUTE, EXCLUDE_STR); 1431 1347 } 1432 1348 else { 1433 1434 1349 full_pattern_str = full_pattern_str.substring(1, full_pattern_str.length() - 1); 1350 command_element.setAttribute(TYPE_ATTRIBUTE, INCLUDE_STR); 1435 1351 } 1436 1352 StringTokenizer pattern_tokenizer = new StringTokenizer(full_pattern_str, SEPARATOR_CHARACTER); 1437 1353 if(pattern_tokenizer.countTokens() >= 2) { 1438 1439 1440 1354 String content_str = pattern_tokenizer.nextToken(); 1355 // Since the contents of indexes have to be certain keywords, or metadata elements, if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace. 1356 if(!content_str.equals(StaticStrings.FILENAME_STR) && content_str.indexOf(MSMUtils.NS_SEP) == -1) { 1441 1357 content_str = Utility.EXTRACTED_METADATA_NAMESPACE + MSMUtils.NS_SEP + content_str; 1442 1443 1444 1445 1358 } 1359 command_element.setAttribute(CONTENT_ATTRIBUTE, content_str); 1360 MSMUtils.setValue(command_element, pattern_tokenizer.nextToken()); 1361 if(pattern_tokenizer.hasMoreTokens()) { 1446 1362 command_element.setAttribute(OPTIONS_ATTRIBUTE, pattern_tokenizer.nextToken()); 1447 1363 } 1448 1364 } 1449 1365 pattern_tokenizer = null; 1450 1366 } 1451 1367 } 1452 1368 catch(Exception exception) { 1453 1369 exception.printStackTrace(); 1454 1370 } 1455 1371 return command_element; 1456 1457 1458 1372 } 1373 1374 private Element parseSubCollectionDefaultIndex(String command_str) { 1459 1375 Element command_element = null; 1460 1376 try { 1461 1462 1377 StringTokenizer tokenizer = new StringTokenizer(command_str); 1378 if(tokenizer.countTokens() == 2) { 1463 1379 command_element = document.createElement(SUBCOLLECTION_DEFAULT_INDEX_ELEMENT); 1464 1380 tokenizer.nextToken(); … … 1467 1383 StringTokenizer content_tokenizer = new StringTokenizer(content_str, StaticStrings.COMMA_CHARACTER); 1468 1384 while(content_tokenizer.hasMoreTokens()) { 1469 1470 1471 1472 1385 Element content_element = document.createElement(CONTENT_ELEMENT); 1386 content_element.setAttribute(NAME_ATTRIBUTE, content_tokenizer.nextToken()); 1387 command_element.appendChild(content_element); 1388 content_element = null; 1473 1389 } 1474 1390 content_tokenizer = null; 1475 1391 content_str = null; 1476 1477 1392 } 1393 tokenizer = null; 1478 1394 } 1479 1395 catch(Exception exception) { 1480 1396 } 1481 1397 return command_element; 1482 1483 1484 1398 } 1399 1400 private Element parseSubCollectionIndex(String command_str) { 1485 1401 Element command_element = null; 1486 1402 try { 1487 1488 1489 1403 StringTokenizer tokenizer = new StringTokenizer(command_str); 1404 tokenizer.nextToken(); 1405 if(tokenizer.hasMoreTokens()) { 1490 1406 command_element = document.createElement(SUBCOLLECTION_INDEXES_ELEMENT); 1491 1492 1407 } 1408 while(tokenizer.hasMoreTokens()) { 1493 1409 Element subcollectionindex_element = document.createElement(INDEX_ELEMENT); 1494 1410 //command_element.setAttribute(CONTENT_ATTRIBUTE, tokenizer.nextToken()); … … 1496 1412 StringTokenizer content_tokenizer = new StringTokenizer(content_str, StaticStrings.COMMA_CHARACTER); 1497 1413 while(content_tokenizer.hasMoreTokens()) { 1498 1499 1500 1501 1414 Element content_element = document.createElement(CONTENT_ELEMENT); 1415 content_element.setAttribute(NAME_ATTRIBUTE, content_tokenizer.nextToken()); 1416 subcollectionindex_element.appendChild(content_element); 1417 content_element = null; 1502 1418 } 1503 1419 content_tokenizer = null; … … 1505 1421 command_element.appendChild(subcollectionindex_element); 1506 1422 subcollectionindex_element = null; 1507 1508 1423 } 1424 tokenizer = null; 1509 1425 } 1510 1426 catch (Exception exception) { 1511 1427 } 1512 1428 return command_element; 1513 1514 1515 1429 } 1430 1431 private Element parseSuperCollection(String command_str) { 1516 1432 Element command_element = null; 1517 1433 try { 1518 1519 1434 StringTokenizer tokenizer = new StringTokenizer(command_str); 1435 if(tokenizer.countTokens() >= 3) { 1520 1436 command_element = document.createElement(SUPERCOLLECTION_ELEMENT); 1521 1437 tokenizer.nextToken(); 1522 1438 while(tokenizer.hasMoreTokens()) { 1523 1524 1525 1526 1527 } 1528 1529 1439 Element collection_element = document.createElement(COLLECTION_ELEMENT); 1440 collection_element.setAttribute(NAME_ATTRIBUTE, tokenizer.nextToken()); 1441 command_element.appendChild(collection_element); 1442 collection_element = null; 1443 } 1444 } 1445 tokenizer = null; 1530 1446 } 1531 1447 catch(Exception exception) { 1532 1448 } 1533 1449 return command_element; 1534 1535 1536 1450 } 1451 1452 private String pluginToString(Element command_element, boolean show_extracted_namespace) { 1537 1453 StringBuffer text = new StringBuffer(); 1538 1454 if(!command_element.getAttribute(SEPARATOR_ATTRIBUTE).equals(TRUE_STR)) { 1539 1540 1541 1542 1543 1544 1545 1546 1455 text.append(PLUGIN_STR); 1456 text.append(TAB_CHARACTER); 1457 text.append(TAB_CHARACTER); 1458 text.append(command_element.getAttribute(TYPE_ATTRIBUTE)); 1459 // Retrieve, and output, the arguments 1460 NodeList option_elements = command_element.getElementsByTagName(OPTION_ELEMENT); 1461 int option_elements_length = option_elements.getLength(); 1462 if(option_elements_length > 0) { 1547 1463 text.append(SPACE_CHARACTER); 1548 1464 for(int j = 0; j < option_elements_length; j++) { 1549 1550 1465 Element option_element = (Element) option_elements.item(j); 1466 if(option_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(TRUE_STR)) { 1551 1467 text.append(StaticStrings.MINUS_CHARACTER); 1552 1468 text.append(option_element.getAttribute(NAME_ATTRIBUTE)); 1553 1469 String value_str = MSMUtils.getValue(option_element); 1554 1470 if(!show_extracted_namespace && value_str.startsWith(EXTRACTED_PREFIX)) { 1555 1471 value_str = value_str.substring(EXTRACTED_PREFIX.length()); 1556 1472 } 1557 1473 if(value_str.length() > 0) { 1558 1559 1474 text.append(SPACE_CHARACTER); 1475 if(value_str.indexOf(SPACE_CHARACTER) == -1) { 1560 1476 text.append(value_str); 1561 1562 1477 } 1478 else { 1563 1479 text.append(SPEECH_CHARACTER); 1564 1480 text.append(value_str); 1565 1481 text.append(SPEECH_CHARACTER); 1566 1482 } 1567 1483 } 1568 1484 value_str = null; 1569 1485 if(j < option_elements_length - 1) { 1570 1571 } 1572 1573 1574 } 1575 1576 1486 text.append(SPACE_CHARACTER); 1487 } 1488 } 1489 option_element = null; 1490 } 1491 } 1492 option_elements = null; 1577 1493 } 1578 1494 return text.toString(); 1579 1580 1581 1495 } 1496 1497 private String searchtypeToString(Element command_element) { 1582 1498 if(command_element.getAttribute(ASSIGNED_ATTRIBUTE).equals(TRUE_STR)) { 1583 1584 1585 1586 1587 1499 StringBuffer text = new StringBuffer(SEARCHTYPE_STR); 1500 text.append(TAB_CHARACTER); 1501 NodeList search_elements = command_element.getElementsByTagName(CONTENT_ELEMENT); 1502 int search_elements_length = search_elements.getLength(); 1503 for(int i = 0; i < search_elements_length; i++) { 1588 1504 Element search_element = (Element) search_elements.item(i); 1589 1505 text.append(search_element.getAttribute(NAME_ATTRIBUTE)); 1590 1506 text.append(SPACE_CHARACTER); 1591 1592 1507 } 1508 return text.substring(0, text.length() - 1); 1593 1509 } 1594 1510 else { 1595 1596 } 1597 1598 1599 1511 return null; 1512 } 1513 } 1514 1515 private String subcollectionToString(Element command_element, boolean show_extracted_namespace) { 1600 1516 StringBuffer text = new StringBuffer(SUBCOLLECTION_STR); 1601 1517 text.append(SPACE_CHARACTER); … … 1605 1521 text.append(SPEECH_CHARACTER); 1606 1522 if(command_element.getAttribute(TYPE_ATTRIBUTE).equals(EXCLUDE_STR)) { 1607 1523 text.append(EXCLAIMATION_CHARACTER); 1608 1524 } 1609 1525 String content_str = command_element.getAttribute(CONTENT_ATTRIBUTE); 1610 1526 if(!show_extracted_namespace && content_str.startsWith(EXTRACTED_PREFIX)) { 1611 1527 content_str = content_str.substring(EXTRACTED_PREFIX.length()); 1612 1528 } 1613 1529 text.append(content_str); … … 1618 1534 String options_str = command_element.getAttribute(OPTIONS_ATTRIBUTE); 1619 1535 if(options_str.length() > 0) { 1620 1536 text.append(options_str); 1621 1537 } 1622 1538 options_str = null; 1623 1539 text.append(SPEECH_CHARACTER); 1624 1540 return text.toString(); 1625 1626 1627 1541 } 1542 1543 private String subcollectionDefaultIndexToString(Element command_element) { 1628 1544 StringBuffer text = new StringBuffer(SUBCOLLECTION_DEFAULT_INDEX_STR); 1629 1545 text.append(TAB_CHARACTER); … … 1631 1547 int content_elements_length = content_elements.getLength(); 1632 1548 for(int j = 0; j < content_elements_length; j++) { 1633 1634 1635 1549 Element content_element = (Element) content_elements.item(j); 1550 text.append(content_element.getAttribute(NAME_ATTRIBUTE)); 1551 if(j < content_elements_length - 1) { 1636 1552 text.append(StaticStrings.COMMA_CHARACTER); 1637 1553 } 1638 1554 } 1639 1555 return text.toString(); 1640 1641 1642 1556 } 1557 1558 private String subcollectionIndexesToString(Element command_element) { 1643 1559 StringBuffer text = new StringBuffer(SUBCOLLECTION_INDEX_STR); 1644 1560 text.append(TAB_CHARACTER); … … 1647 1563 int subcollectionindex_elements_length = subcollectionindex_elements.getLength(); 1648 1564 if(subcollectionindex_elements_length == 0) { 1649 1565 return null; 1650 1566 } 1651 1567 for(int j = 0; j < subcollectionindex_elements_length; j++) { 1652 1653 1654 1655 1568 Element subcollectionindex_element = (Element) subcollectionindex_elements.item(j); 1569 NodeList content_elements = subcollectionindex_element.getElementsByTagName(CONTENT_ELEMENT); 1570 int content_elements_length = content_elements.getLength(); 1571 for(int k = 0; k < content_elements_length; k++) { 1656 1572 Element content_element = (Element) content_elements.item(k); 1657 1573 text.append(content_element.getAttribute(NAME_ATTRIBUTE)); 1658 1574 if(k < content_elements_length - 1) { 1659 1660 } 1661 1662 1575 text.append(StaticStrings.COMMA_CHARACTER); 1576 } 1577 } 1578 if(j < subcollectionindex_elements_length - 1) { 1663 1579 text.append(SPACE_CHARACTER); 1664 1580 } 1665 1581 } 1666 1582 return text.toString(); 1667 1668 1669 1583 } 1584 1585 private String supercollectionToString(Element command_element) { 1670 1586 NodeList content_elements = command_element.getElementsByTagName(COLLECTION_ELEMENT); 1671 1587 int content_elements_length = content_elements.getLength(); 1672 1588 if(content_elements_length > 1) { 1673 1674 1675 1589 StringBuffer text = new StringBuffer(SUPERCOLLECTION_STR); 1590 text.append(TAB_CHARACTER); 1591 for(int j = 0; j < content_elements_length; j++) { 1676 1592 Element content_element = (Element) content_elements.item(j); 1677 1593 text.append(content_element.getAttribute(NAME_ATTRIBUTE)); 1678 1594 if(j < content_elements_length - 1) { 1679 1680 } 1681 1682 1595 text.append(SPACE_CHARACTER); 1596 } 1597 } 1598 return text.toString(); 1683 1599 } 1684 1600 return null; 1685 1686 1687 1601 } 1602 1603 private String unknownToString(Element command_element) { 1688 1604 return MSMUtils.getValue(command_element); 1689 1690 1691 1692 1693 1694 1695 1605 } 1606 1607 /** Write the text to the buffer. This is used so we don't have to worry about storing intermediate String values just so we can calaulate length and offset. 1608 * @param writer the BufferedWriter to which the str will be written 1609 * @param str the String to be written 1610 */ 1611 private void write(BufferedWriter writer, String str) 1696 1612 throws IOException { 1697 1613 writer.write(str, 0, str.length()); 1698 1614 } 1699 1615 } -
trunk/gli/src/org/greenstone/gatherer/cdm/CollectionDesignManager.java
r5995 r6051 96 96 classifier_manager = new ClassifierManager(); 97 97 general_manager = new GeneralManager(); 98 98 99 99 searchtype_manager = new SearchTypeManager(collect_config.getSearchType()); 100 100 if(searchtype_manager.isMGPPEnabled()) { … … 104 104 index_manager = new IndexManager(collect_config.getMGIndexes()); 105 105 } 106 106 107 107 metadataset_view = new MetadataSetView(); 108 108 plugin_manager = new PlugInManager(); … … 156 156 general_manager.gainFocus(); 157 157 } 158 158 159 159 /** Retrieve the name of the collection configuration file which is being used as the source of the information in this object. 160 160 * @return The files absolute path as a <strong>String</strong>. … … 192 192 collection_released = true; 193 193 } 194 194 195 195 general_manager.loseFocus(); 196 196 collect_config.save(); 197 197 198 198 // Readd collection 199 199 ///ystem.err.println("Would have added collection if it had been released."); … … 206 206 } 207 207 } 208 208 209 /** Ensures that the collection is now public. Useful for collections that are based on another, or that are about to be exported */ 210 public void setCollectionAsPublic() { 211 CollectionMeta public_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getPublic()); 212 if(public_collectionmeta != null) { 213 public_collectionmeta.setValue(CollectionConfiguration.TRUE_STR); 214 public_collectionmeta = null; 215 } 216 } 217 209 218 /** Method used during a global search and replace to highlight the appropriate record within the Collection Design Managers version of the Metadata Set Manager (view only). 210 219 * @param element The name of the desired element as a <strong>String</strong>. … … 221 230 private class CDMChangeListener 222 231 implements ActionListener, DocumentListener { 223 232 224 233 public void actionPerformed(ActionEvent event) { 225 234 Gatherer.c_man.getCollection().setSaved(false); … … 230 239 Gatherer.c_man.getCollection().setSaved(false); 231 240 } 232 241 233 242 /** Gives notification that there was an insert into the document. */ 234 243 public void insertUpdate(DocumentEvent e) { 235 244 Gatherer.c_man.getCollection().setSaved(false); 236 245 } 237 246 238 247 /** Gives notification that a portion of the document has been removed. */ 239 248 public void removeUpdate(DocumentEvent e) { -
trunk/gli/src/org/greenstone/gatherer/cdm/CommandTokenizer.java
r6033 r6051 38 38 39 39 /************************************************************************************** 40 * Title: 40 * Title: Gatherer 41 41 * Description: The Gatherer: a tool for gathering and enriching a digital collection. 42 * Company: 43 * Written: 44 * Revised: 42 * Company: The University of Waikato 43 * Written: 07/05/02 44 * Revised: 03/10/02 - Commented 45 45 **************************************************************************************/ 46 import java.io.BufferedReader; 46 47 import java.util.StringTokenizer; 48 import org.greenstone.gatherer.Gatherer; 49 import org.greenstone.gatherer.util.StaticStrings; 47 50 48 51 /** This class provides an extension to the standard StringTokenizer in that it recognizes quotes (or some form of bracketting) enclose a single token so in something like: <br>format Search '<table><img src=... </table>'<br> the formatting string is parsed as a single token. Unfortunately this makes countTokens() unreliable for exact measurement of tokens remaining, and only useful for determining if there are tokens left to be processed (includes any that have already been read into command buffer). … … 50 53 * @version 2.3 51 54 */ 52 public class CommandTokenizer 53 extends StringTokenizer { 54 55 private int last_type = -1; 56 57 static final public int BRACKET_ENCLOSED = 0; 58 static final public int DOUBLE_QUOTE_ENCLOSED = 1; 59 static final public int NORMAL = 2; 60 static final public int QUOTE_ENCLOSED = 3; 61 62 /** Constructor. 63 * @param command The command <strong>String</strong> you wish to tokenize. 64 */ 65 public CommandTokenizer(String command) { 66 super(command); 67 } 68 69 /* private int getLastType() { 70 return last_type; 71 } */ 72 73 /* private boolean isComment() { 74 return (last_type == DOUBLE_QUOTE_ENCLOSED || last_type == QUOTE_ENCLOSED); 75 } */ 76 77 /** Method to retrieve the next token from the command, taking care to group tokens enclosed in speech marks. 78 * @return A <strong>String</strong> containing the next token from the command. 79 */ 80 public String nextToken() { 81 String result = null; 82 if(hasMoreTokens()) { 83 StringBuffer buffer = new StringBuffer(super.nextToken()); 84 switch(buffer.charAt(0)) { 85 case '\"': 86 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\"') && hasMoreTokens()) { 87 buffer.append(" "); 88 buffer.append(super.nextToken()); 89 ///ystem.err.println("Current Buffer = '" + buffer.toString() + "'"); 90 } 91 ///ystem.err.println("Final Buffer = '" + buffer.toString() + "'"); 92 last_type = DOUBLE_QUOTE_ENCLOSED; 93 break; 94 case '\'': 95 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\'') && hasMoreTokens()) { 96 buffer.append(" "); 97 buffer.append(super.nextToken()); 98 } 99 last_type = QUOTE_ENCLOSED; 100 break; 101 case '[': 102 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != ']') && hasMoreTokens()) { 103 buffer.append(" "); 104 buffer.append(super.nextToken()); 105 } 106 last_type = BRACKET_ENCLOSED; 107 break; 108 default: 109 last_type = NORMAL; 110 } 111 result = buffer.toString(); 112 } 113 return result; 114 } 115 116 /** Unfortunately the StringBuffer doesn't have a built in endsWith method, so I'll just have to implement my own. 117 * @param str The <strong>StringBuffer</strong> we are checking the end of. 118 * @param target The <strong>String</strong> fragment we are searching for. 119 * @return <i>true</i> if str ends with target, <i>false</i> otherwise. 120 */ 121 private boolean endsWith(StringBuffer str, String target) { 122 String temp = str.toString(); 123 if(temp.endsWith(target) != (str.lastIndexOf(target) == str.length() - target.length())) { 124 ///ystem.err.println("Holy error that'll crash the HFile creator if it happens twice, Batman!"); 125 ///ystem.err.println("String = '" + temp + "'"); 126 ///ystem.err.println("Target = '" + target + "'"); 127 } 128 return str.lastIndexOf(target) == str.length() - target.length(); 129 } 55 public class CommandTokenizer { 56 57 static final public int BRACKET_ENCLOSED = 0; 58 static final public int DOUBLE_QUOTE_ENCLOSED = 1; 59 static final public int NORMAL = 2; 60 static final public int QUOTE_ENCLOSED = 3; 61 62 private BufferedReader in_stream; 63 private int count = -1; 64 private StringTokenizer internal_tokenizer; 65 66 /** Basic Constructor. Used to parse tokens from a string keeping tokens surrounded by speechmarks or square brackets intact. Thus something like:<br> 67 * collectionmeta collectionextra [l = en] "Hello World"<br> 68 * is tokenized thus<br> 69 * {'collectionmeta', 'collectionextra', 'l = en', 'Hello World'} 70 * @param command the command String you wish to tokenize 71 */ 72 public CommandTokenizer(String command) { 73 this.internal_tokenizer = new StringTokenizer(command); 74 this.in_stream = null; 75 } 76 77 /** Advanced Constructor. As above but with one major difference. Since it is provided an input stream (presumably where the command string originated from), it is able to parse a quote enclosed command token that stretches over several lines. Each newline is preserved in the resulting token. There is an extra bitchslap here as comething like a collection extra might have html code in them that contain escaped speechmarks, so extra care must be taken not to break at them. Thus something like:<br> 78 * collectionmeta collectionextra [l = en] "<br> 79 * an example of the crazy as description we sometimes get which includes of all things something like <a href=\"this.html\"<br> 80 * >this</a> which you could easily see might be a problem if I parse this niavely."<br> 81 * is tokenized thus<br> 82 * {'collectionmeta', 'collectionextra', 'l = en', '\nan example of the crazy as description we sometimes get which includes of all things something like <a href=\"this.html\"\n>this</a> which you could easily see might be a problem if I parse this niavely.'} 83 * @param command the command String you wish to tokenize 84 * @param in_stream a BufferedReader from which the tokenizer can draw further lines as necessary 85 */ 86 public CommandTokenizer(String command, BufferedReader in_stream) { 87 ///atherer.println("***** CommandTokenizer *****\nparse:\t" + command + "\n****************************"); 88 this.internal_tokenizer = new StringTokenizer(command); 89 this.in_stream = in_stream; 90 } 91 92 /** Returns the minumum number of remaining tokens before the tokenizer runs out of string. There may be more tokens than this count, but never less. The discrepancy is due to internal functionality and the fact we can't read ahead in the string or associated stream without risking the need for unpredictable push-back 93 * @return the minumum number of tokens available as an int 94 */ 95 public int countTokens() { 96 if(count == 0 && internal_tokenizer.countTokens() > 1) { 97 return 1; 98 } 99 if(count == -1) { 100 count = internal_tokenizer.countTokens(); 101 } 102 return count; 103 } 104 105 /** Determine if there are still tokens available. 106 * @return true if there are more tokens, false otherwise 107 */ 108 public boolean hasMoreTokens() { 109 return internal_tokenizer.hasMoreTokens(); 110 } 111 112 /** Method to retrieve the next token from the command, taking care to group tokens enclosed in speech marks. 113 * @return a String containing the next token from the command 114 */ 115 public String nextToken() { 116 String result = null; 117 if(internal_tokenizer.hasMoreTokens()) { 118 StringBuffer buffer = new StringBuffer(internal_tokenizer.nextToken()); 119 switch(buffer.charAt(0)) { 120 case StaticStrings.DOUBLEQUOTE_CHAR: 121 result = buildToken(buffer, StaticStrings.DOUBLEQUOTE_CHAR, true); 122 break; 123 case StaticStrings.SINGLEQUOTE_CHAR: 124 result = buildToken(buffer, StaticStrings.SINGLEQUOTE_CHAR, true); 125 break; 126 case StaticStrings.OPENBRACKET_CHAR: 127 result = buildToken(buffer, StaticStrings.CLOSEBRACKET_CHAR, false); 128 break; 129 default: 130 result = buffer.toString(); 131 } 132 buffer = null; 133 } 134 // Because of our tricky counting system we never want to have negative tokens remaining. In fact, unless the internal string buffer is empty, we will return a count of 1 anyway 135 if(count > 0) { 136 count = count - 1; 137 } 138 ///atherer.println("----- CommandTokenizer -----\ntoken:\t" + result + "\n----------------------------"); 139 return result; 140 } 141 142 /** Parse in the next token. paying heed to enclosing characters demands, escaped characters, newlines and empty buffers and consequential unexpected end of tokens 143 * @param buffer the StringBuffer in which the partial token is stored (at the first bit that caused this method to be called) 144 * @param end_char the sentinel char we are watching for as it encloses a token 145 * @param strip_characters a boolean denoting whether the enclosing characters should be stripped off 146 * @return the token, either in its entirity less the enclosing characters if required or, if an unexpected end occured, whatever we parsed without its starting enclosing character, again only if required. In fact if we weren't asked to strip characters then we add the enclosing character back in 147 */ 148 private String buildToken(StringBuffer buffer, char end_char, boolean strip_characters) { 149 while(buffer.charAt(buffer.length() - 1) != end_char || (buffer.length() > 3 && buffer.charAt(buffer.length() - 2) == StaticStrings.BACKSLASH_CHAR)) { 150 try { 151 // The first version is for the basic tokenizer which has no idea of an input stream, so runs out tokens at the same time as the internal tokenizer does 152 if(internal_tokenizer.hasMoreTokens()) { 153 buffer.append(StaticStrings.SPACE_CHAR); 154 buffer.append(internal_tokenizer.nextToken()); 155 } 156 // While the second version can draw more lines from the stream until eof occurs 157 else if(in_stream != null) { 158 String line_str = null; 159 while(!internal_tokenizer.hasMoreTokens() && (line_str = in_stream.readLine()) != null) { 160 ///atherer.println("+++++ CommandTokenizer +++++\nappend:\t" + line_str + "\n+++++++++++++++++++++++++++++"); 161 // Its at this stage the our token count becomes completely patu 162 internal_tokenizer = new StringTokenizer(line_str); 163 buffer.append(StaticStrings.NEW_LINE_CHAR); // A new line in the final token 164 } 165 line_str = null; 166 if(internal_tokenizer.hasMoreTokens()) { 167 // Don't add a space if we just added a newline 168 if(buffer.charAt(buffer.length() - 1) != StaticStrings.NEW_LINE_CHAR) { 169 buffer.append(StaticStrings.SPACE_CHAR); 170 } 171 buffer.append(internal_tokenizer.nextToken()); 172 } 173 // We've prematurely run out of content, so throw the dummy, or at least return whatever we managed to parse sans its opening character 174 else { 175 if(strip_characters) { 176 return buffer.substring(1); 177 } 178 else { 179 buffer.append(end_char); 180 return buffer.toString(); 181 } 182 } 183 } 184 // We've prematurely run out of content, so throw the dummy, or at least return whatever we managed to parse sans its opening character 185 else { 186 if(strip_characters) { 187 return buffer.substring(1); 188 } 189 else { 190 buffer.append(end_char); 191 return buffer.toString(); 192 } 193 } 194 } 195 // Exception throw when we attempted reading from the input stream, so throw the dummy, or at least return whatever we managed to parse sans its opening character 196 catch(Exception exception) { 197 Gatherer.printStackTrace(exception); 198 if(strip_characters) { 199 return buffer.substring(1); 200 } 201 else { 202 buffer.append(end_char); 203 return buffer.toString(); 204 } 205 } 206 } 207 // Return the string sans enclosing characters 208 if(buffer.length() > 0 && strip_characters) { 209 return buffer.substring(1, buffer.length() - 1); 210 } 211 else { 212 return buffer.toString(); 213 } 214 } 130 215 } -
trunk/gli/src/org/greenstone/gatherer/cdm/GeneralManager.java
r6003 r6051 133 133 /** This class represents the visual component of the general options stored in the CollectionDesignManager. */ 134 134 private class GeneralControl 135 extends JPanel 135 extends JPanel 136 136 implements Control { 137 137 private boolean ready = false; … … 262 262 instruction_panel.add(title_label, BorderLayout.NORTH); 263 263 instruction_panel.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER); 264 264 265 265 creator_panel.setLayout(new BorderLayout()); 266 266 creator_panel.add(creator_label, BorderLayout.WEST); … … 334 334 small_icon_textfield.setText(icon_collection_small_collectionmeta.getValue(CollectionMeta.TEXT)); 335 335 small_icon_textfield.setCaretPosition(0); 336 // I forgot to keep the checkboxes up to date. Why hasn't this caused problems before today? 337 public_checkbox.setSelected(public_collectionmeta.getValue(CollectionMeta.TEXT).equals(CollectionConfiguration.TRUE_STR)); 338 beta_checkbox.setSelected(beta_collectionmeta.getValue(CollectionMeta.TEXT).equals(CollectionConfiguration.TRUE_STR)); 336 339 ready = true; 337 340 } … … 343 346 beta_collectionmeta.setValue((beta_checkbox.isSelected() ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR)); 344 347 public_collectionmeta.setValue((public_checkbox.isSelected() ? CollectionConfiguration.TRUE_STR : CollectionConfiguration.FALSE_STR)); 345 creator_collectionmeta.setValue(creator_emailfield.getText()); 348 String creator_email_str = creator_emailfield.getText(); 349 creator_collectionmeta.setValue(creator_email_str); 350 Gatherer.config.setEmail(creator_email_str); // Store the email address in the configuration 346 351 collection_extra_collectionmeta.setValue(description_textarea.getText()); 347 352 icon_collection_collectionmeta.setValue(icon_textfield.getText()); … … 356 361 private class BrowseListener 357 362 implements ActionListener { 358 363 359 364 public void actionPerformed(ActionEvent event) { 360 365 // Open an almost standard file browser to the images folder of the current collection … … 405 410 } 406 411 } 407 412 408 413 /** ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */ 409 private class ImageFilter 414 private class ImageFilter 410 415 extends javax.swing.filechooser.FileFilter { 411 416 412 417 private Pattern pattern = null; 413 418 414 419 public ImageFilter() { 415 420 pattern = Pattern.compile(".*\\.(gif|png|jpe?g)"); 416 421 } 417 422 418 423 // Accept all directories and all .col files 419 424 public boolean accept(File f) { … … 422 427 return f.isDirectory() || matcher.matches(); 423 428 } 424 429 425 430 // The description of this filter 426 431 public String getDescription() { -
trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java
r5931 r6051 331 331 Gatherer.c_man.configurationChanged(); 332 332 } 333 333 334 334 /** This method is reponsible for changing the underlying Index commands from MG to MGPP and back again. This turns out to be easyish for MG->MGPP and very hard for the reverse. For the former we remove the level fragment and make sure the same levels are set, then we produce a list of the sources involved, breaking down comma seperated lists and making sure each item it unique. Changing back the other way turns out to be impossible, so we don't (beyond generating document:text, section:text and paragraph:text if text is an index and the respective levels are present). In either case we start by creating a comment containing the old index information. 335 335 * @param state true to enable MGPP indexes, false to use standard MG style ones … … 378 378 index = null; 379 379 } 380 // Replace mg element with mgpp element 380 // Replace mg element with mgpp element 381 381 setRoot(mgpp_element); 382 382 … … 426 426 } 427 427 else { 428 // Replace mg element with mgpp element 428 // Replace mg element with mgpp element 429 429 setRoot(mgpp_element); 430 430 } … … 444 444 default_index.setAssigned(true); 445 445 } 446 // If mg element has no indexes, and the current mgpp index include a text one, then generate text indexes for each of the registered levels. 446 // If mg element has no indexes, and the current mgpp index include a text one, then generate text indexes for each of the registered levels. 447 447 NodeList indexes = mgpp_element.getElementsByTagName(CollectionConfiguration.INDEX_ELEMENT); 448 448 if(indexes.getLength() == 0) { … … 489 489 /** This class creates a set of controls for editing the indexes. */ 490 490 private class IndexControl 491 extends JPanel 491 extends JPanel 492 492 implements Control { 493 493 … … 728 728 else if(source_list.isSelectionEmpty()) { 729 729 add_enabled = false; 730 } 730 } 731 731 // If we get this far, create a dummy index and see if its already assigned in the collection 732 732 else { … … 806 806 return component; 807 807 } 808 808 809 809 } 810 810 811 811 private class MoveListener 812 812 implements ActionListener { 813 813 814 814 private boolean move_up; 815 815 … … 849 849 private class SetDefaultListener 850 850 implements ActionListener { 851 851 852 852 public void actionPerformed(ActionEvent event) { 853 853 Index index = (Index) index_list.getSelectedValue(); … … 879 879 validateAddButton(); 880 880 } 881 881 882 882 /** Gives notification that there was an insert into the document. */ 883 883 public void insertUpdate(DocumentEvent e) { 884 884 validateAddButton(); 885 885 } 886 886 887 887 /** Gives notification that a portion of the document has been removed. */ 888 888 public void removeUpdate(DocumentEvent e) { … … 928 928 // Create Indexes 929 929 JPanel indexes_panel = new JPanel(); 930 930 931 931 JPanel current_indexes_panel = new JPanel(); 932 932 … … 1053 1053 index_spacer_panel.add(index_body_panel, BorderLayout.NORTH); 1054 1054 index_spacer_panel.add(index_empty_panel, BorderLayout.CENTER); 1055 1055 1056 1056 indexes_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 1057 1057 indexes_panel.setLayout(new BorderLayout()); … … 1061 1061 // Create Levels 1062 1062 JPanel levels_panel = new JPanel(); 1063 1063 1064 1064 JPanel current_levels_panel = new JPanel(); 1065 1065 … … 1119 1119 add_level_button.setMnemonic(KeyEvent.VK_A); 1120 1120 Dictionary.registerBoth(add_level_button, "CDM.IndexManager.MGPP.Add_Level", "CDM.IndexManager.MGPP.Add_Level_Tooltip"); 1121 1121 1122 1122 remove_level_button = new JButton("CDM.IndexManager.MGPP.Remove_Level"); 1123 1123 remove_level_button.setEnabled(false); … … 1173 1173 level_spacer_panel.add(level_body_panel, BorderLayout.NORTH); 1174 1174 level_spacer_panel.add(level_empty_panel, BorderLayout.CENTER); 1175 1175 1176 1176 levels_panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 1177 1177 levels_panel.setLayout(new BorderLayout()); … … 1181 1181 // Create General 1182 1182 JPanel header_panel = new JPanel(); 1183 1183 1184 1184 title_label = new JLabel(); 1185 1185 title_label.setHorizontalAlignment(JLabel.CENTER); … … 1376 1376 validateAddButton(); 1377 1377 } 1378 1378 1379 1379 /** Gives notification that a portion of the document has been removed. 1380 1380 * @param event a DocumentEvent containing information about the text removed … … 1383 1383 validateAddButton(); 1384 1384 } 1385 1385 1386 1386 /** Change the enable state of the add button depending on the current value in the search type combobox. */ 1387 1387 public void validateAddButton() { … … 1438 1438 validateAddButton(); 1439 1439 } 1440 1440 1441 1441 /** Gives notification that a portion of the document has been removed. 1442 1442 * @param event a DocumentEvent containing information about the text removed … … 1445 1445 validateAddButton(); 1446 1446 } 1447 1447 1448 1448 /** Change the enable state of the add button depending on the current value in the search type combobox. */ 1449 1449 public void validateAddButton() { … … 1500 1500 1501 1501 /** Replace really only replaces the string. */ 1502 private class ReplaceIndexActionListener 1502 private class ReplaceIndexActionListener 1503 1503 implements ActionListener { 1504 1504 … … 1526 1526 } 1527 1527 } 1528 1528 1529 1529 private class RemoveIndexActionListener 1530 1530 implements ActionListener { -
trunk/gli/src/org/greenstone/gatherer/cdm/MetadataSetView.java
r5593 r6051 158 158 /** This class creates and lays-out the various controls for reviewing the metadata sets, and their commands as they would appear in the collection configuration file. */ 159 159 private class MetadataSetControl 160 extends JPanel 160 extends JPanel 161 161 implements Control { 162 162 /** The label denoting the element list. */ … … 324 324 String namespace = set.getNamespace(); 325 325 // Watch out for the greenstone set, with its namespace of "" 326 if(namespace == null || namespace.length() == 0) { 327 namespace = "greenstone"; 328 } 329 return "metadataset " + namespace + " \"" + set.getFile().getName() + "\""; 326 return "metadataset " + namespace + " \"" + set.getName() + "\""; 330 327 } 331 328 } -
trunk/gli/src/org/greenstone/gatherer/cdm/PlugInManager.java
r5926 r6051 967 967 */ 968 968 private class RemoveListener 969 implements ActionListener { 970 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured on one of our target controls. 971 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 972 */ 973 public void actionPerformed(ActionEvent event) { 974 if(!plugin_list.isSelectionEmpty()) { 975 Object [] objects = plugin_list.getSelectedValues(); 976 for(int i = 0; i < objects.length; i++) { 977 if(objects[i] instanceof PlugIn) { 978 removePlugIn((PlugIn)objects[i]); 969 implements ActionListener { 970 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured on one of our target controls. 971 * @param event An <strong>ActionEvent</strong> containing information garnered from the control action. 972 */ 973 public void actionPerformed(ActionEvent event) { 974 int selected_index = plugin_list.getSelectedIndex(); 975 if(selected_index != -1) { 976 PlugIn selected_plugin = (PlugIn) plugin_list.getSelectedValue(); 977 removePlugIn(selected_plugin); 978 selected_plugin = null; 979 // Select the next plugin if available 980 if(selected_index < plugin_list.getModel().getSize()) { 981 // If the new selection is above the separator we can remove it 982 if(selected_index < findSeparatorIndex()) { 983 plugin_list.setSelectedIndex(selected_index); 984 remove.setEnabled(true); 985 } 986 // Otherwise select the first non-removable plugin 987 else { 988 plugin_list.setSelectedIndex(selected_index + 1); 989 remove.setEnabled(false); 990 } 991 } 992 else { 993 remove.setEnabled(false); 994 } 995 // Refresh the available plugins 996 plugin.setModel(new DefaultComboBoxModel(getAvailable())); 979 997 } 980 } 981 // Refresh the available plugins 982 plugin.setModel(new DefaultComboBoxModel(getAvailable())); 983 } 984 remove.setEnabled(false); 985 } 998 else { 999 remove.setEnabled(false); 1000 } 1001 /* 1002 if(!plugin_list.isSelectionEmpty()) { 1003 Object [] objects = plugin_list.getSelectedValues(); 1004 for(int i = 0; i < objects.length; i++) { 1005 if(objects[i] instanceof PlugIn) { 1006 removePlugIn((PlugIn)objects[i]); 1007 } 1008 } 1009 // Refresh the available plugins 1010 plugin.setModel(new DefaultComboBoxModel(getAvailable())); 1011 } 1012 */ 1013 } 986 1014 } 987 1015 } -
trunk/gli/src/org/greenstone/gatherer/cdm/SearchTypeManager.java
r5928 r6051 29 29 /************************************************************************************** 30 30 * Written: 16/07/03 31 * Revised: 31 * Revised: 32 32 **************************************************************************************/ 33 33 import java.awt.*; … … 82 82 return controls; 83 83 } 84 85 public String getSearchTypes() { 86 StringBuffer search_types = new StringBuffer(); 87 ArrayList types = children(); 88 for (int i=0; i<types.size(); i++) { 89 if (i>0) { 90 search_types.append(","); 91 } 92 search_types.append(((SearchType)types.get(i)).getName()); 93 } 94 return search_types.toString(); 95 } 96 /** Be examining the SearchType 'root' we were created with, determine if mgpp is enabled. 97 * @return true if MGPP is enabled, false otherwise 98 */ 99 public boolean isMGPPEnabled() { 100 return root.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR); 101 } 84 85 /** Return a list of the currently assigned search types as a comma separated string. 86 * @return a String 87 */ 88 public String getSearchTypes() { 89 StringBuffer search_types = new StringBuffer(); 90 ArrayList types = children(); 91 for (int i = 0; i < types.size(); i++) { 92 if (i>0) { 93 search_types.append(","); 94 } 95 search_types.append(((SearchType)types.get(i)).getName()); 96 } 97 return search_types.toString(); 98 } 99 100 /** Be examining the SearchType 'root' we were created with, determine if mgpp is enabled. 101 * @return true if MGPP is enabled, false otherwise 102 */ 103 public boolean isMGPPEnabled() { 104 return root.getAttribute(CollectionConfiguration.ASSIGNED_ATTRIBUTE).equals(CollectionConfiguration.TRUE_STR); 105 } 102 106 103 107 public void moveSearchType(SearchType search_type, boolean direction) { … … 127 131 remove(search_type); 128 132 add(index, search_type); 129 Gatherer.c_man.configurationChanged(); 133 Gatherer.c_man.configurationChanged(); 130 134 } 131 135 … … 147 151 private JButton move_up_button; 148 152 private JButton remove_button; 149 153 150 154 private JCheckBox enable_advanced_searches_checkbox; 151 155 … … 267 271 inner_panel.add(current_search_types_panel, BorderLayout.CENTER); 268 272 inner_panel.add(search_type_panel, BorderLayout.SOUTH); 269 273 270 274 spacer_panel.setLayout(new BorderLayout()); 271 275 spacer_panel.add(inner_panel, BorderLayout.NORTH); … … 280 284 public void destroy() { 281 285 } 282 286 283 287 public void gainFocus() { 284 288 instructions_textarea.setCaretPosition(0); … … 413 417 414 418 /** Listens for changes in the search types combobox, and enabled add button appropriately. */ 415 private class SearchTypesActionDocumentListener 419 private class SearchTypesActionDocumentListener 416 420 implements ActionListener, DocumentListener { 417 421 /** Called whenever a selection action occurs on the combobox. … … 434 438 validateAddButton(); 435 439 } 436 440 437 441 /** Gives notification that a portion of the document has been removed. 438 442 * @param event a DocumentEvent containing information about the text removed … … 441 445 validateAddButton(); 442 446 } 443 447 444 448 /** Change the enable state of the add button depending on the current value in the search type combobox. */ 445 449 private void validateAddButton() { -
trunk/gli/src/org/greenstone/gatherer/cdm/SuperCollectionManager.java
r5590 r6051 37 37 import org.greenstone.gatherer.checklist.CheckList; 38 38 import org.greenstone.gatherer.checklist.Entry; 39 import org.greenstone.gatherer.collection. CollectionConfiguration;39 import org.greenstone.gatherer.collection.BasicCollectionConfiguration; 40 40 import org.greenstone.gatherer.util.StaticStrings; 41 41 import org.greenstone.gatherer.util.Utility; … … 43 43 44 44 /** This class contains the information about what supercollection has been specified (if any) and methods for changing this information. Note that there is a major difference between this manager and the others in that its DOM model is never used directly in any list component. It is only used to decide whether a certain entry in the actual checklist is checked. */ 45 public class SuperCollectionManager 45 public class SuperCollectionManager 46 46 extends DOMProxyListModel { 47 47 … … 107 107 /** Provides controls for altering the SuperCollection settings. */ 108 108 private class SuperCollectionControl 109 extends JPanel 109 extends JPanel 110 110 implements Control { 111 111 … … 119 119 // Creation 120 120 JPanel header_panel = new JPanel(); 121 121 122 122 JLabel title_label = new JLabel(); 123 123 title_label.setHorizontalAlignment(JLabel.CENTER); … … 141 141 header_panel.add(title_label, BorderLayout.NORTH); 142 142 header_panel.add(new JScrollPane(instructions), BorderLayout.CENTER); 143 143 144 144 setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 145 145 setLayout(new BorderLayout()); … … 198 198 File collect_cfg_file = new File(possible_collections[i], Utility.CONFIG_DIR); 199 199 if(collect_cfg_file.exists()) { 200 CollectionConfiguration collect_cfg = newCollectionConfiguration(collect_cfg_file);200 BasicCollectionConfiguration collect_cfg = new BasicCollectionConfiguration(collect_cfg_file); 201 201 String collection_title = collect_cfg.getName(); 202 202 String collection_name = possible_collections[i].getName(); … … 210 210 collection_checklist_model.add(entry); 211 211 entry = null; 212 212 213 213 } 214 214 collection_name = null;
Note:
See TracChangeset
for help on using the changeset viewer.