Changeset 26355 for main/trunk/gli


Ignore:
Timestamp:
2012-10-18T15:44:38+13:00 (12 years ago)
Author:
kjdon
Message:

indexOptions now not inside a containing element. handles replaceList elements. saves unknown top level elements and writes them out again.

File:
1 edited

Legend:

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

    r26056 r26355  
    3030import java.io.File;
    3131import java.util.ArrayList;
     32import java.util.Arrays;
    3233import java.util.HashMap;
    3334import java.util.HashSet;
     
    5253    static final private String PLUGOUT_ELEMENT = "plugout";//used by building flax collections
    5354
     55  // a list of all known top level elements
     56  static final private String known_element_names_array[] = {StaticStrings.METADATALIST_STR, StaticStrings.DISPLAYITEMLIST_STR, StaticStrings.FORMAT_STR, StaticStrings.SEARCH_STR, StaticStrings.INFODB_STR, StaticStrings.BROWSE_STR, StaticStrings.IMPORT_STR,  StaticStrings.DISPLAY_STR, StaticStrings.REPLACELISTREF_STR, StaticStrings.REPLACELIST_STR, StaticStrings.SERVICE_RACK_LIST_ELEMENT};
     57  static final private Set known_element_names = new HashSet(Arrays.asList(known_element_names_array));
    5458    /**
    5559     * *************************************************************************
    56      * ******************************* The code from this point below are used
     60     * ******************************* The code in this file is used
    5761     * for greenstone 3 collection configuration, i.e., read ColletionConfig.xml
    5862     * into the internal DOM tree, and convert the internal DOM tree back to
     
    160164            Element index_element = to.createElement(StaticStrings.INDEX_ELEMENT);//<Index>
    161165
    162             // For mg, it's the 'Old G2.38 and earlier' that use level:source tuplets, but we double check it anyway
    163             boolean old_index = true;
    164166            if (index_str.indexOf(StaticStrings.COLON_CHARACTER) == -1)
    165167            {
    166168                // It doesn't contain ':' character
    167169                System.err.println("Something is wrong! the index should be level:source tuplets.");
    168                 old_index = false;
     170                // assume document level
     171                index_element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, StaticStrings.DOCUMENT_STR);
    169172            }
    170173            else
     
    172175                // Handling 'index' element
    173176                index_element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, index_str.substring(0, index_str.indexOf(StaticStrings.COLON_CHARACTER)));
     177           
    174178                index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
    175 
     179            }
    176180                //Each index may have a list of comma-separated strings.
    177181                //split them into 'content' elements in the internal structure
     
    186190                    // Since the contents of indexes have to be certain keywords, or metadata elements,
    187191                    //if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
    188                     if (content_str.indexOf(StaticStrings.NS_SEP) == -1)
     192                    if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR)))
    189193                    {
    190                         if (content_str.equals(StaticStrings.TEXT_STR) || (!old_index && content_str.equals(StaticStrings.ALLFIELDS_STR)))
    191                         {
    192                             // in this case, do nothing
    193                         }
    194                         else
    195                         {
    196194                            content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
    197                         }
     195                       
    198196                    }
    199197
     
    209207                ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
    210208                appendArrayList(toElement, collectionmetadata_list);
    211             } //else ends
    212209        } //for loop ends
    213210        appendProperly(toElement, indexes_element);
     
    268265            // Handling 'index' element
    269266            // Double check to make sure it's not colon separated style index.
    270             boolean old_index = false;
    271267            if (index_str.indexOf(StaticStrings.COLON_CHARACTER) != -1)
    272268            {
    273269                System.err.println("Something is wrong! the index should NOT be level:source tuplets style.");
    274                 old_index = true;
     270                index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
     271               
    275272            }
    276273            //Each index may have a list of comma-separated strings.
    277274            //split them into 'content' elements in the internal structure
    278275            StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER);
    279             //index_str = "";
    280276            while (content_tokenizer.hasMoreTokens())
    281277            {
     
    285281                String content_str = content_tokenizer.nextToken();
    286282                // 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.
    287                 if (content_str.indexOf(StaticStrings.NS_SEP) == -1)
    288                 {
    289                     if (content_str.equals(StaticStrings.TEXT_STR))
    290                     {
    291                         // in this case, do nothing
    292                     }
    293                     else
    294                     {
     283                if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR)|| content_str.equals(StaticStrings.ALLFIELDS_STR)))
     284                {
    295285                        content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
    296                     }
    297286                }
    298287                content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
     
    10121001
    10131002        NodeList replace_elements = from.getElementsByTagName(StaticStrings.REPLACELISTREF_STR);
    1014         int num_elems = replace_elements.getLength();
    1015         if (num_elems < 1)
    1016         {
    1017             return;
    1018         }
    1019         for (int i = 0; i < num_elems; i++)
    1020         {
    1021             Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
    1022             toElement.appendChild(to_element);
    1023         }
    1024     }
     1003        XMLTools.duplicateElementList(to, toElement, replace_elements, true);
     1004    }
     1005    //  int num_elems = replace_elements.getLength();
     1006    //  if (num_elems < 1)
     1007    //  {
     1008    //      return;
     1009    //  }
     1010    //  for (int i = 0; i < num_elems; i++)
     1011    //  {
     1012    //      Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
     1013    //      toElement.appendChild(to_element);
     1014    //  }
     1015    // }
    10251016
    10261017    static private void convertReplaceListRef(Document from, Document to)
     
    10291020
    10301021        NodeList replace_elements = from.getDocumentElement().getElementsByTagName(StaticStrings.REPLACELISTREF_STR);
    1031         int num_elems = replace_elements.getLength();
    1032         if (num_elems < 1)
    1033         {
    1034             return;
    1035         }
    1036         for (int i = 0; i < num_elems; i++)
    1037         {
    1038             Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
    1039             toElement.appendChild(to_element);
    1040         }
    1041     }
    1042 
     1022        XMLTools.duplicateElementList(to, toElement, replace_elements, true);
     1023    }
     1024        // int num_elems = replace_elements.getLength();
     1025        // if (num_elems < 1)
     1026        // {
     1027        //  return;
     1028        // }
     1029        // for (int i = 0; i < num_elems; i++)
     1030        // {
     1031        //  Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
     1032        //  toElement.appendChild(to_element);
     1033        // }
     1034  //    }
     1035
     1036  /* replacelist currently not editable in GLI, just copy it in and back out again */
     1037    static private void doReplaceList(Document to, Element from)
     1038    {
     1039        Element toElement = to.getDocumentElement();
     1040
     1041        Node rl_element = XMLTools.getChildByTagName(from, StaticStrings.REPLACELIST_STR);
     1042        if (rl_element == null)
     1043        {
     1044            return; // such an element not found
     1045        }
     1046
     1047        Element to_element = XMLTools.duplicateElement(to, (Element) rl_element, true);
     1048        toElement.appendChild(to_element);
     1049    }
     1050
     1051    static private void convertReplaceList(Document from, Document to)
     1052    {
     1053        Element toElement = to.getDocumentElement();
     1054
     1055        Node rl_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.REPLACELIST_STR);
     1056        if (rl_element == null)
     1057        {
     1058            return; // such an element not found
     1059        }
     1060
     1061        Element to_element = XMLTools.duplicateElement(to, (Element) rl_element, true);
     1062        toElement.appendChild(to_element);
     1063    }
     1064 
    10431065    /**
    10441066     * serviceRackList is currently not editable in GLI - just copy it in from
     
    10971119    {
    10981120        Element toElement = to.getDocumentElement();
    1099         Node index_option_node = XMLTools.getChildByTagName(searchNode, StaticStrings.INDEXOPTION_STR);
    1100         if (index_option_node == null)
    1101         {
    1102             return;
    1103         }
    1104         NodeList option_children = ((Element) index_option_node).getElementsByTagName(StaticStrings.OPTION_STR);
    1105         int option_nodes = option_children.getLength();
     1121        //Node index_option_node = XMLTools.getChildByTagName(searchNode, StaticStrings.INDEXOPTION_STR);
     1122        //if (index_option_node == null)
     1123        //{
     1124        //  return;
     1125        //}
     1126        //NodeList option_children = ((Element) index_option_node).getElementsByTagName(StaticStrings.OPTION_STR);
     1127        NodeList option_children = ((Element) searchNode).getElementsByTagName(StaticStrings.INDEXOPTION_STR);
     1128        int num_options = option_children.getLength();
    11061129
    11071130        // for lucene, there is no 'indexOption'. We build a default 'indexOption' and 'assigned=false' in case the user switches to mg or mgpp
    1108         if (option_nodes < 1)
     1131        if (num_options < 1)
    11091132        {
    11101133            Element index_option = to.createElement(StaticStrings.INDEXOPTIONS_ELEMENT);
     
    11261149        index_option.setAttribute(StaticStrings.NAME_STR, StaticStrings.INDEXOPTIONS_STR);
    11271150
    1128         for (int i = 0; i < option_nodes; i++)
     1151        for (int i = 0; i < num_options; i++)
    11291152        {
    11301153            String option_str = ((Element) option_children.item(i)).getAttribute(StaticStrings.NAME_ATTRIBUTE);
     
    20562079            DebugStream.println("There should be an IndexOption element which is assigned 'true': possible bug.");
    20572080        }
    2058         Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
     2081        //Element indexOptionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
    20592082        NodeList option_elements = index_option.getElementsByTagName(StaticStrings.OPTION_ELEMENT);
    20602083        int num_elements = option_elements.getLength();
     
    20642087            return;//
    20652088        }
    2066         search.appendChild(indexOptionEl);
     2089        //search.appendChild(indexOptionEl);
    20672090
    20682091        for (int k = 0; k < num_elements; k++)
     
    20702093            Element e = (Element) option_elements.item(k);
    20712094            String name_att = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    2072             Element optionEl = to.createElement(StaticStrings.OPTION_STR);
     2095            Element optionEl = to.createElement(StaticStrings.INDEXOPTION_STR);
    20732096            optionEl.setAttribute(StaticStrings.NAME_ATTRIBUTE, name_att);
    2074             indexOptionEl.appendChild(optionEl);
    2075         }
    2076 
    2077     }
     2097            // default value?? on/off
     2098            //indexOptionEl.appendChild(optionEl);
     2099            search.appendChild(optionEl);
     2100        }
     2101
     2102    }
     2103
     2104  // handle top level elements which GLI knows nothing about
     2105  // we store them internally in a Unknown element for easy access when
     2106  // we write them out.
     2107  static private void doUnknownElements(Document to, Element from) {
     2108    Element toElement = to.getDocumentElement();
     2109    Element unknownElement = to.createElement(StaticStrings.UNKNOWN_ELEMENT);
     2110    toElement.appendChild(unknownElement);
     2111   
     2112    Node child = from.getFirstChild();
     2113    while (child != null)
     2114      {
     2115    System.err.println("child name=" +child.getNodeName());
     2116    if (child.getNodeType() == Node.ELEMENT_NODE && ! known_element_names.contains(child.getNodeName())) {
     2117      unknownElement.appendChild(XMLTools.duplicateElement(to, (Element) child, true));
     2118    }
     2119    child = child.getNextSibling();
     2120      }
     2121   
     2122  }
     2123
     2124    // just copy all children of Unknown element into output doc.
     2125  static private void convertUnknownElements(Document from, Document to) {
     2126
     2127    Element toElement = to.getDocumentElement();
     2128    Node unknown_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.UNKNOWN_ELEMENT);
     2129
     2130    Node child = unknown_element.getFirstChild();
     2131    while (child != null)
     2132      {
     2133    Element to_element = XMLTools.duplicateElement(to, (Element) child, true);
     2134    toElement.appendChild(to_element);
     2135   
     2136    child = child.getNextSibling();
     2137      }
     2138
     2139  }
    20782140
    20792141    // Append the element son to the element mother in the appropriate position.
     
    23792441        doDisplayFormat(dOc, fromElement);
    23802442        doReplaceListRef(dOc, fromElement);
     2443        doReplaceList(dOc, fromElement);
    23812444        doServiceRackList(dOc, fromElement);
    2382 
     2445        doUnknownElements(dOc, fromElement);
    23832446        // the official displayItems in the displayItemList element have already been handled above
    23842447        // and created as collectionmetadata elements in the dOc object
     
    24452508        //first parse an empty skeleton of xml config file
    24462509        //The aim is to convert the internal structure into this skeleton
     2510      // This skeleton just has the CollectionConfig element and nothing else
    24472511        Document skeleton = XMLTools.parseXMLFile("xml/CollectionConfig.xml", true);
    24482512        //Element internal = dOc.getDocumentElement();
     
    24622526        convertDisplayFormat(dOc, skeleton);
    24632527        convertReplaceListRef(dOc, skeleton);
     2528        convertReplaceList(dOc, skeleton);
    24642529        convertServiceRackList(dOc, skeleton);
     2530        convertUnknownElements(dOc, skeleton); // try to catch everything GLI doesn't know about
    24652531
    24662532        return skeleton;
Note: See TracChangeset for help on using the changeset viewer.