Ignore:
Timestamp:
2003-12-01T19:52:02+13:00 (20 years ago)
Author:
jmt12
Message:

Have rearranged where and how strings are feed through the Codec. After several hours work and a dozen paper trials I discovered the TEXT_TO_DOM conversion was completely pointless (DOM does it itself). Also the quotes only need to be dealt to if they are being sent to the collect.cfg file. Hopefully I've got it all going now - including using that pesky pipe character that I would rather not have to deal with. And everything seems to be ok - I tested all the dangerous characters including square brackets and amperstamp. I also tried hierarchies, and then as the piece'd'resistance I tried a hierarchies with dangerous characters. All good. I'm all about the working metadata.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/msm/GDMDocument.java

    r6051 r6069  
    190190        String node_value = null;
    191191        if(model != null && model.isHierarchy()) {
    192             node_value = Codec.transform(metadata.getValueNode().getFullPath(false), Codec.TEXT_TO_DOM);
     192            //node_value = /odec.transform(metadata.getValueNode().getFullPath(false), /odec.TEXT_TO_DOM);
     193            node_value = metadata.getValueNode().getFullPath(false);
    193194        }
    194195        else {
    195             node_value = metadata.getAbsoluteValue();
    196         }
    197         ///ystem.err.println("Creating node in GDMDocument: '" + node_value + "'");
     196            node_value = metadata.getAbsoluteValue();
     197        }
     198        System.err.println("Creating node in GDMDocument: '" + node_value + "'");
    198199        metadata_element.appendChild(base_document.createTextNode(node_value));
    199200        // Retrieve the first description element for this fileset (there should only be one, but I'll play it safe).
     
    253254    return getMetadata(filename, remove, metadatum_so_far, file, append_folder_level, false);
    254255    }
    255     /** Retrieve the metadata associated with the given filename. Keep track of what metadata should be overwritten and what should be accumulated. Also make note of the source file, and remove the metadata if required. Finally if purge is set retrieve every piece of metadata in this file. */
    256     public ArrayList getMetadata(String filename, boolean remove, ArrayList metadatum_so_far, File file, boolean append_folder_level, boolean purge) {
     256    /** Retrieve the metadata associated with the given filename. Keep track of what metadata should be overwritten and what should be accumulated. Also make note of the source file, and remove the metadata if required. Finally if purge is set retrieve every piece of metadata in this file. */
     257    public ArrayList getMetadata(String filename, boolean remove, ArrayList metadatum_so_far, File file, boolean append_folder_level, boolean purge) {
    257258    Gatherer.println("Get metadata for " + filename);
    258259    ArrayList metadatum = null;
    259260    if(metadatum_so_far == null) {
    260         metadatum = new ArrayList();
     261        metadatum = new ArrayList();
    261262    }
    262263    else {
    263         metadatum = metadatum_so_far;
     264        metadatum = metadatum_so_far;
    264265    }
    265266    try {
    266         // Retrieve the document element.
    267         Element directorymetadata_element = base_document.getDocumentElement();
    268         // Iterate through the filesets, checking the FileName child element against    the target file's name using regular expression matching.
    269         NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT);
    270         for(int i = 0; i < fileset_elements.getLength(); i++) {
     267        // Retrieve the document element.
     268        Element directorymetadata_element = base_document.getDocumentElement();
     269        // Iterate through the filesets, checking the FileName child element against    the target file's name using regular expression matching.
     270        NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT);
     271        for(int i = 0; i < fileset_elements.getLength(); i++) {
    271272        Element fileset_element = (Element) fileset_elements.item(i);
    272273        NodeList filename_elements = fileset_element.getElementsByTagName(FILENAME_ELEMENT);
    273274        for(int j = 0; j < filename_elements.getLength(); j++) {
    274             Element filename_element = (Element) filename_elements.item(j);
    275             String filename_text = MSMUtils.getValue(filename_element);
    276             if((filename != null && (filename.matches(filename_text) || (append_folder_level && filename.indexOf(File.separator) != -1 && filename_text.equals(filename.substring(0, filename.indexOf(File.separator)))))) || ((filename == null || append_folder_level) && filename_text.equals(DIRECTORY_FILENAME)) || purge) {
     275            Element filename_element = (Element) filename_elements.item(j);
     276            String filename_text = MSMUtils.getValue(filename_element);
     277            if((filename != null && (filename.matches(filename_text) || (append_folder_level && filename.indexOf(File.separator) != -1 && filename_text.equals(filename.substring(0, filename.indexOf(File.separator)))))) || ((filename == null || append_folder_level) && filename_text.equals(DIRECTORY_FILENAME)) || purge) {
    277278            // If they match add all of the metadata found in the Description child element, remembering to abide by desired mode (accumulate vs. overwrite).
    278279            // Normal metadata
    279280            NodeList description_elements = fileset_element.getElementsByTagName(DESCRIPTION_ELEMENT);
    280281            for(int k = 0; k < description_elements.getLength(); k++) {
    281                 Element description_element = (Element) description_elements.item(k);
    282                 // We have to do this for each type of metadata
    283                 for(int z = 0; z < ALL_METADATA_TYPES.length; z++) {
    284                     NodeList metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[z]);
    285                     for(int l = 0; l < metadata_elements.getLength(); l++) {
    286                          Element metadata_element = (Element) metadata_elements.item(l);
    287                          String raw_element = metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    288                          //String language = metadata_element.getAttribute("language");
    289                          String mode = metadata_element.getAttribute(MODE_ATTRIBUTE);
    290                          String raw_value = MSMUtils.getValue(metadata_element);
    291                          // Raw value is in GREENSTONE form, convert to DOM
    292                          raw_value = Codec.transform(raw_value, Codec.GREENSTONE_TO_DOM);
    293                          // ***** LEGACY SUPPORT *****
    294                          // If this raw_value contains a '\' character, but no '\\', '[' or ']' characters, then replace the '\' with a '\\'
    295                          if(raw_value.indexOf(StaticStrings.ESCAPE_STR) != -1) {
    296                              Gatherer.println("Detected Legacy Path: " + raw_value);
    297                              raw_value = raw_value.replaceAll(StaticStrings.ESCAPE_PATTERN, StaticStrings.PIPE_STR);
    298                              Gatherer.println("Updated Path To: " + raw_value);
    299                              MSMUtils.setValue(metadata_element, raw_value);
    300                          }
    301                          // **************************
    302                          // Using the element string and value, retrieve a matching Metadata object from the cache
    303                          Metadata metadata = null;
    304                          // If this element has hierarchy values then we must ensure the raw value is a full path, not an index.
    305                          // Try to retrieve an already comstructed piece of metadata from file - but not if we are purging, as this will stuff up anything that is still using that metadata - such as the GTable
    306                          if(GDMManager.metadata_cache.contains(raw_element, raw_value) && !purge) {
    307                              ///ystem.err.println("HIT! Retrieve metadata from cache: " + raw_element + " -> " + raw_value + "\n");
    308                              metadata = (Metadata) GDMManager.metadata_cache.get(raw_element, raw_value);
    309                          }
    310                          else {
    311                              ElementWrapper element = Gatherer.c_man.getCollection().msm.getElement(raw_element);
    312                              if (element != null) {
    313                                  GValueNode value = Metadata.getDefaultValueNode(element, raw_value);
    314                                  ///ystem.err.println("Miss. Create new metadata: " + raw_element + " -> " + raw_value + "\n");
    315                                  metadata = new Metadata(element, value);
    316                                  if(!purge) {
    317                                      GDMManager.metadata_cache.put(raw_element, raw_value, metadata);
    318                                  }
    319                                  ///ystem.err.println("Added metadata to cache: " + raw_element + " -> " + raw_value + "\n");
    320                                  value = null;
    321                                  element = null;
    322                              }
    323                          }
    324                          // check whether the metadata is null
    325                          if (metadata != null) {
    326                              // We determine whether this metadata is file or folder level
    327                              if(filename != null) {
    328                                  ///ystem.err.println("Filename   = " + filename);
    329                                  ///ystem.err.println("filename_text = " + filename_text);
    330                                  // If can only be file level if there is no folder path details in filename and if the filename matched the filename text node (it may have matched .* instead)!
    331                                  if(filename.indexOf(File.separator) == -1 && filename.equals(filename_text)) {
    332                                      metadata.setFileLevel(true);
    333                                      ///ystem.err.println("File level!!!");
    334                                  }
    335                                  else {
    336                                      metadata.setFileLevel(false);
    337                                      ///ystem.err.println("Inherited!!!");
    338                                  }
    339                              }
    340                              else {
    341                                  ///ystem.err.println("Filename is null therefore this is file level metadata.");
    342                                  metadata.setFileLevel(true);
    343                              }
    344                              metadata.setFile(file);
    345                              // If mode is overwrite, then remove any previous values for this metadata element.
    346                              if(mode.equals("accumulate")) {
    347                                  metadata.setAccumulate(true);
    348                              }
    349                              else {
    350                                  metadata.setAccumulate(false);
    351                                  ///ystem.err.println("Metadata overwrites: " + metadata);
    352                                  for(int m = metadatum.size() - 1; m >= 0; m--) {
    353                                      Metadata old_metadata = (Metadata) metadatum.get(m);
    354                                      if(old_metadata.getElement().equals(metadata.getElement())) {
    355                                          metadatum.remove(m);
    356                                          ///ystem.err.println("Removing overridden metadata: " + old_metadata);
    357                                      }
    358                                      old_metadata = null;
    359                                  }
    360                              }
    361                              mode = null;
    362                              // Add the completed metadata and clean up
    363                              ///ystem.err.println("Adding metadata: " + metadata);
    364                              metadatum.add(metadata);
    365                              // Having found our metadata check if the value from the xml matches the one from the gvaluenode. If not update it. This happens whenever hierarchy information is involved (indexes rapidly become obsolete).
    366                              // If remove was set, remove it. We can only remove pure file level metadata, or folder level iff we were asked for folder level.
    367                              if(remove && ((filename != null && filename.matches(filename_text) && !filename_text.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME)))) {
    368                                  ///ystem.err.println("Removing " + metadata + " from " + file);
    369                                  description_element.removeChild(metadata_element);
    370                                  // Remove the description element if empty.
    371                                  if(!description_element.hasChildNodes()) {
    372                                      fileset_element.removeChild(description_element);
    373                                  }
    374                              }
    375                              else {
    376                                  //String current_value = metadata.getAbsoluteValue();
    377                                  String current_value = Codec.transform(metadata.getValueNode().getFullPath(false), Codec.TEXT_TO_DOM);
    378                                  ///ystem.err.println("Checking the current hfile: " + current_value);
    379                                  ///ystem.err.println("Against whats in the hfile: " + current_value);
    380                                  if(!raw_value.equals(current_value)) {
    381                                      // Remove old text
    382                                      while(metadata_element.hasChildNodes()) {
    383                                          metadata_element.removeChild(metadata_element.getFirstChild());
    384                                      }
    385                                      // Add new.
    386                                      metadata_element.appendChild(base_document.createTextNode(current_value));
    387                                  }
    388                              }
    389                          }
    390                          metadata = null;
    391                          raw_value = null;
    392                          raw_element = null;
    393                          metadata_element = null;
    394                      }
    395                      metadata_elements = null;
     282                Element description_element = (Element) description_elements.item(k);
     283                // We have to do this for each type of metadata
     284                for(int z = 0; z < ALL_METADATA_TYPES.length; z++) {
     285                NodeList metadata_elements = description_element.getElementsByTagName(ALL_METADATA_TYPES[z]);
     286                for(int l = 0; l < metadata_elements.getLength(); l++) {
     287                    Element metadata_element = (Element) metadata_elements.item(l);
     288                    String raw_element = metadata_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     289                    //String language = metadata_element.getAttribute("language");
     290                    String mode = metadata_element.getAttribute(MODE_ATTRIBUTE);
     291                    String raw_value = MSMUtils.getValue(metadata_element);
     292                    //
     293                    //raw_value = Codec.transform(raw_value, Codec.DOM_TO_);
     294                    ///ystem.err.println("Retrieved raw value: " + raw_value);
     295                    // ***** LEGACY SUPPORT *****
     296                    // If this raw_value contains a '\' character, but no '\\', '[' or ']' characters, then replace the '\' with a '\\'
     297                    if(raw_value.indexOf(StaticStrings.ESCAPE_STR) != -1) {
     298                    ///ystem.err.println("Blarg");
     299                    Gatherer.println("Detected Legacy Path: " + raw_value);
     300                    raw_value = raw_value.replaceAll(StaticStrings.ESCAPE_PATTERN, StaticStrings.PIPE_STR);
     301                    Gatherer.println("Updated Path To: " + raw_value);
     302                    MSMUtils.setValue(metadata_element, raw_value);
     303                    }
     304                    // **************************
     305                    // Using the element string and value, retrieve a matching Metadata object from the cache
     306                    Metadata metadata = null;
     307                    // If this element has hierarchy values then we must ensure the raw value is a full path, not an index.
     308                    // Try to retrieve an already comstructed piece of metadata from file - but not if we are purging, as this will stuff up anything that is still using that metadata - such as the GTable
     309                    if(GDMManager.metadata_cache.contains(raw_element, raw_value) && !purge) {
     310                    ///ystem.err.println("HIT! Retrieve metadata from cache: " + raw_element + " -> " + raw_value + "\n");
     311                    metadata = (Metadata) GDMManager.metadata_cache.get(raw_element, raw_value);
     312                    }
     313                    else {
     314                    ElementWrapper element = Gatherer.c_man.getCollection().msm.getElement(raw_element);
     315                    if (element != null) {
     316                        GValueNode value = Metadata.getDefaultValueNode(element, raw_value);
     317                        ///ystem.err.println("Miss. Create new metadata: " + raw_element + " -> " + raw_value + "\n");
     318                        metadata = new Metadata(element, value);
     319                        if(!purge) {
     320                        GDMManager.metadata_cache.put(raw_element, raw_value, metadata);
     321                        }
     322                        ///ystem.err.println("Added metadata to cache: " + raw_element + " -> " + raw_value + "\n");
     323                        value = null;
     324                        element = null;
     325                    }
     326                    }
     327                    // check whether the metadata is null
     328                    if (metadata != null) {
     329                    // We determine whether this metadata is file or folder level
     330                    if(filename != null) {
     331                        ///ystem.err.println("Filename    = " + filename);
     332                        ///ystem.err.println("filename_text = " + filename_text);
     333                        // If can only be file level if there is no folder path details in filename and if the filename matched the filename text node (it may have matched .* instead)!
     334                        if(filename.indexOf(File.separator) == -1 && filename.equals(filename_text)) {
     335                        metadata.setFileLevel(true);
     336                        ///ystem.err.println("File level!!!");
     337                        }
     338                        else {
     339                        metadata.setFileLevel(false);
     340                        ///ystem.err.println("Inherited!!!");
     341                        }
     342                    }
     343                    else {
     344                        ///ystem.err.println("Filename is null therefore this is file level metadata.");
     345                        metadata.setFileLevel(true);
     346                    }
     347                    metadata.setFile(file);
     348                    // If mode is overwrite, then remove any previous values for this metadata element.
     349                    if(mode.equals("accumulate")) {
     350                        metadata.setAccumulate(true);
     351                    }
     352                    else {
     353                        metadata.setAccumulate(false);
     354                        ///ystem.err.println("Metadata overwrites: " + metadata);
     355                        for(int m = metadatum.size() - 1; m >= 0; m--) {
     356                        Metadata old_metadata = (Metadata) metadatum.get(m);
     357                        if(old_metadata.getElement().equals(metadata.getElement())) {
     358                            metadatum.remove(m);
     359                            ///ystem.err.println("Removing overridden metadata: " + old_metadata);
     360                        }
     361                        old_metadata = null;
     362                        }
     363                    }
     364                    mode = null;
     365                    // Add the completed metadata and clean up
     366                    ///ystem.err.println("Adding metadata: " + metadata);
     367                    metadatum.add(metadata);
     368                    // Having found our metadata check if the value from the xml matches the one from the gvaluenode. If not update it. This happens whenever hierarchy information is involved (indexes rapidly become obsolete).
     369                    // If remove was set, remove it. We can only remove pure file level metadata, or folder level iff we were asked for folder level.
     370                    if(remove && ((filename != null && filename.matches(filename_text) && !filename_text.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME)))) {
     371                        ///ystem.err.println("Removing " + metadata + " from " + file);
     372                        description_element.removeChild(metadata_element);
     373                        // Remove the description element if empty.
     374                        if(!description_element.hasChildNodes()) {
     375                        fileset_element.removeChild(description_element);
     376                        }
     377                    }
     378                    else {
     379                        //String current_value = metadata.getAbsoluteValue();
     380                        //String current_value = Codec.transform(metadata.getValueNode().getFullPath(false), Codec.TEXT_TO_DOM);
     381                        String current_value = metadata.getValueNode().getFullPath(false);
     382                        //System.err.println("Checking the current mdv path: " + current_value);
     383                        //System.err.println("Against whats in the metadata file: " + raw_value);
     384                        if(!raw_value.equals(current_value)) {
     385                        // Remove old text
     386                        while(metadata_element.hasChildNodes()) {
     387                            metadata_element.removeChild(metadata_element.getFirstChild());
     388                        }
     389                        // Add new.
     390                        metadata_element.appendChild(base_document.createTextNode(current_value));
     391                        }
     392                    }
     393                    }
     394                    metadata = null;
     395                    raw_value = null;
     396                    raw_element = null;
     397                    metadata_element = null;
    396398                }
    397                 description_element = null;
     399                metadata_elements = null;
     400                }
     401                description_element = null;
    398402            }
    399403            description_elements = null;
    400             }
    401             filename_text = null;
    402             filename_element = null;
     404            }
     405            filename_text = null;
     406            filename_element = null;
    403407        }
    404408        // If the file set no longer has any description entries, remove it entirely
    405409        NodeList description_elements = fileset_element.getElementsByTagName(DESCRIPTION_ELEMENT);
    406410        if(description_elements.getLength() == 0) {
    407             directorymetadata_element.removeChild(fileset_element);
     411            directorymetadata_element.removeChild(fileset_element);
    408412        }
    409413        description_elements = null;
    410414        filename_elements = null;
    411415        fileset_element = null;
    412         }
    413         fileset_elements = null;
    414         directorymetadata_element = null;
     416        }
     417        fileset_elements = null;
     418        directorymetadata_element = null;
    415419    }
    416420    catch (Exception error) {
    417         Gatherer.self.printStackTrace(error);
     421        Gatherer.self.printStackTrace(error);
    418422    }
    419423    ///ystem.err.println("Found " + metadatum.size() + " pieces of metadata.");
    420424    return metadatum;
    421     }
    422 
     425    }
     426   
    423427    /** Determine if this document has been saved recently, and thus xml file version is up to date. */
    424428    public boolean isUpToDate() {
Note: See TracChangeset for help on using the changeset viewer.