Changeset 36244


Ignore:
Timestamp:
2022-06-07T10:27:36+12:00 (3 weeks ago)
Author:
kjdon
Message:

updating this for new searchmeta changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectCfgReadWrite.java

    r36194 r36244  
    6767      return indexDefaultToString (command_element);
    6868    }
     69    else if(command_element_name.equals (StaticStrings.SORTS_ELEMENT)) {
     70      return sortsToString (command_element);
     71    }
     72    else if(command_element_name.equals (StaticStrings.SORT_DEFAULT_ELEMENT)) {
     73      return sortDefaultToString (command_element);
     74    }
    6975    else if(command_element_name.equals (StaticStrings.LANGUAGES_ELEMENT)) {
    7076      return languagesToString (command_element);
     
    256262    return text.toString ();
    257263  }
    258    
     264     static private String sortsToString (Element command_element) {
     265    boolean comment_only = false;
     266    StringBuffer text = new StringBuffer ("");
     267    if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
     268    System.err.println("sorts not assigned, returning false");
     269    return "";
     270    //  text.append ("#");
     271    //comment_only = true;
     272    }
     273    text.append (StaticStrings.SORT_STR);
     274    text.append (StaticStrings.TAB_CHARACTER);
     275    //    if(!comment_only) { why???
     276    //  text.append (StaticStrings.TAB_CHARACTER);
     277    // }
     278    NodeList index_elements = command_element.getElementsByTagName (StaticStrings.SORT_ELEMENT);
     279    if (index_elements.getLength () == 0) { // no indexes
     280      return "";
     281    }
     282    // For each sortfield,  concatenate its child content elements into a single comma separated list
     283    int index_elements_length = index_elements.getLength ();
     284    for(int j = 0; j < index_elements_length; j++) {
     285      Element index_element = (Element) index_elements.item (j);
     286      NodeList content_elements = index_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
     287      int content_elements_length = content_elements.getLength ();
     288      // Don't output anything if no indexes are set
     289      if(content_elements_length == 0) {
     290    return null;
     291      }
     292      for(int k = 0; k < content_elements_length; k++) {
     293    Element content_element = (Element) content_elements.item (k);
     294    String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
     295    text.append (name_str);
     296    name_str = null;
     297    if(k < content_elements_length - 1) {
     298      text.append (StaticStrings.COMMA_CHARACTER);
     299    }
     300    content_element = null;
     301      }
     302      if(j < index_elements_length - 1) {
     303    text.append (StaticStrings.SPACE_CHARACTER);
     304      }
     305      content_elements = null;
     306      index_element = null;
     307    }
     308    index_elements = null;
     309    return text.toString ();
     310  }
     311   
     312  static private String sortDefaultToString (Element command_element) {
     313    StringBuffer text = new StringBuffer ("");
     314    if(command_element.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.FALSE_STR)) {
     315    return "";
     316    }
     317    text.append (StaticStrings.SORT_DEFAULT_STR);
     318    text.append (StaticStrings.TAB_CHARACTER);
     319    NodeList content_elements = command_element.getElementsByTagName (StaticStrings.CONTENT_ELEMENT);
     320    int content_elements_length = content_elements.getLength ();
     321    for(int j = 0; j < content_elements_length; j++) {
     322      Element content_element = (Element) content_elements.item (j);
     323      String name_str = content_element.getAttribute (StaticStrings.NAME_ATTRIBUTE);
     324      text.append (name_str);
     325      name_str = null;
     326      if(j < content_elements_length - 1) {
     327    text.append (StaticStrings.COMMA_CHARACTER);
     328      }
     329      content_element = null;
     330    }
     331    content_elements = null;
     332    return text.toString ();
     333  }
     334 
    259335  static private String languagesToString (Element command_element) {
    260336    StringBuffer text = new StringBuffer (StaticStrings.LANGUAGES_STR);
     
    560636        command_element = parseIndexDefault (command_str, document);
    561637      }
     638      if(command_element == null && command_type.equals (StaticStrings.SORT_STR)) {
     639        command_element = parseSortfields (command_str, document);
     640      }
     641      if(command_element == null && command_type.equals (StaticStrings.SORT_DEFAULT_STR) ) {
     642                       
     643        command_element = parseSortfieldDefault (command_str, document);
     644      }
    562645      if(command_element == null && command_type.equals (StaticStrings.LANGUAGES_STR)) {
    563646        command_element = parseLanguage (command_str, document);
     
    873956  }
    874957   
     958  static private Element parseSortfields (String command_str, Document document) {
     959    Element command_element = null;
     960    try {
     961      StringTokenizer tokenizer = new StringTokenizer (command_str);
     962      String command = tokenizer.nextToken ();
     963      command_element = document.createElement (StaticStrings.SORTS_ELEMENT);
     964      command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE,  StaticStrings.TRUE_STR);
     965      command = null;
     966      if(!tokenizer.hasMoreTokens ()) {
     967               
     968    // there are no sortfields
     969    command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
     970    tokenizer = null;
     971    return command_element;
     972      }
     973           
     974      while(tokenizer.hasMoreTokens ()) {
     975    Element index_element = document.createElement (StaticStrings.SORT_ELEMENT);
     976    String index_str = tokenizer.nextToken ();
     977    StringTokenizer content_tokenizer = new StringTokenizer (index_str, StaticStrings.COMMA_CHARACTER);
     978    while(content_tokenizer.hasMoreTokens ()) {
     979      Element content_element = document.createElement (StaticStrings.CONTENT_ELEMENT);
     980      String content_str = content_tokenizer.nextToken ();
     981      // 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.
     982      if(content_str.indexOf (StaticStrings.NS_SEP) == -1) {
     983        if(content_str.equals (StaticStrings.RANK_STR) || content_str.equals (StaticStrings.NONE_STR) ) {
     984          // Our special strings are OK.
     985        }
     986        else {
     987          content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
     988        }
     989      }
     990      content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_str);
     991      index_element.appendChild (content_element);
     992      content_element = null;
     993    }
     994    content_tokenizer = null;
     995    index_str = null;
     996    command_element.appendChild (index_element);
     997    index_element = null;
     998      }
     999      tokenizer = null;
     1000    }
     1001    catch (Exception exception) {
     1002      exception.printStackTrace ();
     1003    }
     1004    return command_element;
     1005  }
     1006
     1007  static private Element parseSortfieldDefault (String command_str, Document document) {
     1008    Element command_element = null;
     1009    try {
     1010      StringTokenizer tokenizer = new StringTokenizer (command_str);
     1011      if(tokenizer.countTokens () >= 2) {
     1012    command_element = document.createElement (StaticStrings.SORT_DEFAULT_ELEMENT);
     1013    command_element.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, ((tokenizer.nextToken ()).equals (StaticStrings.SORT_DEFAULT_STR) ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR));
     1014    String index_str = tokenizer.nextToken ();
     1015               
     1016    String content_str = index_str;
     1017               
     1018               
     1019    StringTokenizer content_tokenizer = new StringTokenizer (content_str, StaticStrings.COMMA_CHARACTER);
     1020    while(content_tokenizer.hasMoreTokens ()) {
     1021      Element content_element = document.createElement (StaticStrings.CONTENT_ELEMENT);
     1022      content_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, content_tokenizer.nextToken ());
     1023      command_element.appendChild (content_element);
     1024      content_element = null;
     1025    }
     1026    content_tokenizer = null;
     1027    content_str = null;
     1028    content_str = null;
     1029    index_str = null;
     1030      }
     1031      tokenizer = null;
     1032    }
     1033    catch (Exception exception) {
     1034    }
     1035    return command_element;
     1036  }
     1037
    8751038  static private Element parseLanguage (String command_str, Document document) {
    8761039    Element command_element = null;
     
    9821145  static private Element parseMetadata (CommandTokenizer tokenizer, Document document) {
    9831146    Element command_element = null;
    984     try {
    985       command_element = document.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
     1147    boolean is_search_meta = false;
     1148    try {
     1149     
    9861150      String name_str = tokenizer.nextToken ();
     1151      if (name_str.startsWith(StaticStrings.DOT_CHARACTER)) {
     1152      is_search_meta = true;
     1153      name_str = name_str.substring(1); // remove the dot
     1154      }
    9871155      String value_str = tokenizer.nextToken ();
    9881156      if(name_str != null && value_str != null) {
     
    9951163    if(value_str != null) {
    9961164      // Ready the value str (which can contain all sorts of funky characters) for writing as a DOM value
     1165        if (is_search_meta) {
     1166        command_element = document.createElement (StaticStrings.SEARCHMETADATA_ELEMENT);
     1167        command_element.setAttribute(StaticStrings.TYPE_ATTRIBUTE, SearchMeta.TYPE_SEARCH);
     1168        } else {
     1169        command_element = document.createElement (StaticStrings.COLLECTIONMETADATA_ELEMENT);
     1170        }
    9971171      value_str = Codec.transform (value_str, Codec.GREENSTONE_TO_DOM);
    9981172      command_element.setAttribute (StaticStrings.NAME_ATTRIBUTE, name_str);
Note: See TracChangeset for help on using the changeset viewer.