Changeset 26571 for main/trunk/gli


Ignore:
Timestamp:
2012-12-11T14:30:48+13:00 (11 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some chances

File:
1 edited

Legend:

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

    r26355 r26571  
    5353    static final private String PLUGOUT_ELEMENT = "plugout";//used by building flax collections
    5454
    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));
     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));
     58
    5859    /**
    5960     * *************************************************************************
    60      * ******************************* The code in this file is used
    61      * for greenstone 3 collection configuration, i.e., read ColletionConfig.xml
     61     * ******************************* The code in this file is used for
     62     * greenstone 3 collection configuration, i.e., read ColletionConfig.xml
    6263     * into the internal DOM tree, and convert the internal DOM tree back to
    6364     * CollectionConfig.xml.
     
    175176                // Handling 'index' element
    176177                index_element.setAttribute(StaticStrings.LEVEL_ATTRIBUTE, index_str.substring(0, index_str.indexOf(StaticStrings.COLON_CHARACTER)));
    177            
     178
    178179                index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
    179180            }
    180                 //Each index may have a list of comma-separated strings.
    181                 //split them into 'content' elements in the internal structure
    182                 StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER);
    183                 //index_str = "";
    184                 while (content_tokenizer.hasMoreTokens())
    185                 {
    186                     // Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.
    187 
    188                     Element content_element = to.createElement(StaticStrings.CONTENT_ELEMENT);
    189                     String content_str = content_tokenizer.nextToken();
    190                     // Since the contents of indexes have to be certain keywords, or metadata elements,
    191                     //if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
    192                     if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR)))
    193                     {
    194                             content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
    195                        
    196                     }
    197 
    198                     content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
    199                     index_element.appendChild(content_element);
    200                     content_element = null;
    201                 } // while ends
    202 
    203                 indexes_element.appendChild(index_element);
    204 
    205                 // Handling 'displayItem' elements and Constructing 'collectionmetadata' elements
    206                 // Use the fully qualified index names
    207                 ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
    208                 appendArrayList(toElement, collectionmetadata_list);
     181            //Each index may have a list of comma-separated strings.
     182            //split them into 'content' elements in the internal structure
     183            StringTokenizer content_tokenizer = new StringTokenizer(index_str, StaticStrings.COMMA_CHARACTER);
     184            //index_str = "";
     185            while (content_tokenizer.hasMoreTokens())
     186            {
     187                // Replace index_str to be qualified name, eg. dc.Subject and keywords insread of dc.Subject.
     188
     189                Element content_element = to.createElement(StaticStrings.CONTENT_ELEMENT);
     190                String content_str = content_tokenizer.nextToken();
     191                // Since the contents of indexes have to be certain keywords, or metadata elements,
     192                //if the content isn't a keyword and doesn't yet have a namespace, append the extracted metadata namespace.
     193                if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR)))
     194                {
     195                    content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
     196
     197                }
     198
     199                content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
     200                index_element.appendChild(content_element);
     201                content_element = null;
     202            } // while ends
     203
     204            indexes_element.appendChild(index_element);
     205
     206            // Handling 'displayItem' elements and Constructing 'collectionmetadata' elements
     207            // Use the fully qualified index names
     208            ArrayList collectionmetadata_list = doDisplayItemList(to, e, StaticStrings.NAME_ATTRIBUTE, index_str_display);
     209            appendArrayList(toElement, collectionmetadata_list);
    209210        } //for loop ends
    210211        appendProperly(toElement, indexes_element);
     
    269270                System.err.println("Something is wrong! the index should NOT be level:source tuplets style.");
    270271                index_str = index_str.substring(index_str.indexOf(StaticStrings.COLON_CHARACTER) + 1);
    271                
     272
    272273            }
    273274            //Each index may have a list of comma-separated strings.
     
    281282                String content_str = content_tokenizer.nextToken();
    282283                // 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.
    283                 if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR)|| content_str.equals(StaticStrings.ALLFIELDS_STR)))
    284                 {
    285                         content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
     284                if (content_str.indexOf(StaticStrings.NS_SEP) == -1 && !(content_str.equals(StaticStrings.TEXT_STR) || content_str.equals(StaticStrings.ALLFIELDS_STR)))
     285                {
     286                    content_str = StaticStrings.EXTRACTED_NAMESPACE + content_str;
    286287                }
    287288                content_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, content_str);
     
    338339
    339340    }
    340   static private void doGlobalFormat(Document to, Element from)
    341   {
    342     // look for a top level format element
    343     Element fe = (Element) XMLTools.getChildByTagName(from, StaticStrings.FORMAT_STR);
    344     to.getDocumentElement().appendChild(doFormat(to, fe, StaticStrings.GLOBAL_STR));
    345   }
     341
     342    static private void doGlobalFormat(Document to, Element from)
     343    {
     344        // look for a top level format element
     345        Element fe = (Element) XMLTools.getChildByTagName(from, StaticStrings.FORMAT_STR);
     346        to.getDocumentElement().appendChild(doFormat(to, fe, StaticStrings.GLOBAL_STR));
     347    }
     348
    346349    static private void doDisplayFormat(Document to, Element from)
    347350    {
     
    640643        { // not an empty format statement
    641644            String gsf_text = XMLTools.xmlNodeToString(format);
    642            
     645
    643646            //We don't want the <format> elements in the string
    644647            int startFormatIndex = gsf_text.indexOf(StaticStrings.FORMAT_START_TAG) + StaticStrings.FORMAT_START_TAG.length();
     
    10031006        XMLTools.duplicateElementList(to, toElement, replace_elements, true);
    10041007    }
     1008
    10051009    //  int num_elems = replace_elements.getLength();
    10061010    //  if (num_elems < 1)
     
    10221026        XMLTools.duplicateElementList(to, toElement, replace_elements, true);
    10231027    }
    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 */
     1028
     1029    // int num_elems = replace_elements.getLength();
     1030    // if (num_elems < 1)
     1031    // {
     1032    //  return;
     1033    // }
     1034    // for (int i = 0; i < num_elems; i++)
     1035    // {
     1036    //  Element to_element = XMLTools.duplicateElement(to, (Element) replace_elements.item(i), true);
     1037    //  toElement.appendChild(to_element);
     1038    // }
     1039    //  }
     1040
     1041    /*
     1042     * replacelist currently not editable in GLI, just copy it in and back out
     1043     * again
     1044     */
    10371045    static private void doReplaceList(Document to, Element from)
    10381046    {
     
    10621070        toElement.appendChild(to_element);
    10631071    }
    1064  
     1072
    10651073    /**
    10661074     * serviceRackList is currently not editable in GLI - just copy it in from
     
    11981206        // as displayItems in the collectionConfig.xml than they do in memory
    11991207        Map attributeMap = new HashMap(4);
    1200         attributeMap.put(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR,StaticStrings.DESCRIPTION_STR);
    1201         attributeMap.put(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR,StaticStrings.NAME_STR);
    1202         attributeMap.put(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR,StaticStrings.SMALLICON_STR);
    1203         attributeMap.put(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR,StaticStrings.ICON_STR);
     1208        attributeMap.put(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR, StaticStrings.DESCRIPTION_STR);
     1209        attributeMap.put(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR, StaticStrings.NAME_STR);
     1210        attributeMap.put(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTIONSMALL_STR, StaticStrings.SMALLICON_STR);
     1211        attributeMap.put(StaticStrings.COLLECTIONMETADATA_ICONCOLLECTION_STR, StaticStrings.ICON_STR);
    12041212
    12051213        NodeList e_list = from.getDocumentElement().getElementsByTagName(StaticStrings.COLLECTIONMETADATA_ELEMENT);
    12061214        // if such elements don't exist, don't bother
    1207         if (e_list != null) {
    1208            
    1209             for (int j = 0; j < e_list.getLength(); j++) {
    1210             Element e = (Element) e_list.item(j);
    1211             if (e.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR)) {
    1212                 continue;
    1213             }
    1214             String text = XMLTools.getNodeText(e);
    1215             String lang = e.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
    1216             String name_value = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    1217            
    1218             String name_mapping = (String)attributeMap.get(name_value);
    1219             if(name_mapping != null) {
    1220                 name_value = name_mapping;
    1221             }
    1222 
    1223             Element displayItem = constructElement(StaticStrings.DISPLAYITEM_STR, name_value, StaticStrings.LANG_STR, lang, text, to);
    1224             displayItemList.appendChild(displayItem);
    1225             }
    1226            
     1215        if (e_list != null)
     1216        {
     1217
     1218            for (int j = 0; j < e_list.getLength(); j++)
     1219            {
     1220                Element e = (Element) e_list.item(j);
     1221                if (e.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.FALSE_STR))
     1222                {
     1223                    continue;
     1224                }
     1225                String text = XMLTools.getNodeText(e);
     1226                String lang = e.getAttribute(StaticStrings.LANGUAGE_ATTRIBUTE);
     1227                String name_value = e.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     1228
     1229                String name_mapping = (String) attributeMap.get(name_value);
     1230                if (name_mapping != null)
     1231                {
     1232                    name_value = name_mapping;
     1233                }
     1234
     1235                Element displayItem = constructElement(StaticStrings.DISPLAYITEM_STR, name_value, StaticStrings.LANG_STR, lang, text, to);
     1236                displayItemList.appendChild(displayItem);
     1237            }
     1238
    12271239        }
    12281240        destination.appendChild(displayItemList);
     
    14161428    }
    14171429
    1418   // convert global format statement
     1430    // convert global format statement
    14191431    static private void convertGlobalFormat(Document from, Document to)
    14201432    {
    1421       Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.GLOBAL_STR);
    1422 
    1423     to.getDocumentElement().appendChild(convertFormat(to, e));
    1424 
    1425     }
    1426  
     1433        Element e = XMLTools.getNamedElement(from.getDocumentElement(), StaticStrings.FORMAT_STR, StaticStrings.NAME_ATTRIBUTE, StaticStrings.GLOBAL_STR);
     1434
     1435        to.getDocumentElement().appendChild(convertFormat(to, e));
     1436
     1437    }
     1438
    14271439    // Convert plugins in the internal(i.e. Document from) to collectionconfig.xml (i.e. Document to)
    14281440    static private void convertPlugin(Document from, Document to)
     
    21022114    }
    21032115
    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   }
     2116    // handle top level elements which GLI knows nothing about
     2117    // we store them internally in a Unknown element for easy access when
     2118    // we write them out.
     2119    static private void doUnknownElements(Document to, Element from)
     2120    {
     2121        Element toElement = to.getDocumentElement();
     2122        Element unknownElement = to.createElement(StaticStrings.UNKNOWN_ELEMENT);
     2123        toElement.appendChild(unknownElement);
     2124
     2125        Node child = from.getFirstChild();
     2126        while (child != null)
     2127        {
     2128            System.err.println("child name=" + child.getNodeName());
     2129            if (child.getNodeType() == Node.ELEMENT_NODE && !known_element_names.contains(child.getNodeName()))
     2130            {
     2131                unknownElement.appendChild(XMLTools.duplicateElement(to, (Element) child, true));
     2132            }
     2133            child = child.getNextSibling();
     2134        }
     2135
     2136    }
     2137
     2138    // just copy all children of Unknown element into output doc.
     2139    static private void convertUnknownElements(Document from, Document to)
     2140    {
     2141
     2142        Element toElement = to.getDocumentElement();
     2143        Node unknown_element = XMLTools.getChildByTagName(from.getDocumentElement(), StaticStrings.UNKNOWN_ELEMENT);
     2144
     2145        Node child = unknown_element.getFirstChild();
     2146        while (child != null)
     2147        {
     2148            Element to_element = XMLTools.duplicateElement(to, (Element) child, true);
     2149            toElement.appendChild(to_element);
     2150
     2151            child = child.getNextSibling();
     2152        }
     2153
     2154    }
    21402155
    21412156    // Append the element son to the element mother in the appropriate position.
     
    24252440        {
    24262441            doMGPPIndexes(dOc, searchNode);
    2427         }       
     2442        }
    24282443
    24292444        doDefaultIndex(dOc, searchNode);
     
    24432458        doReplaceList(dOc, fromElement);
    24442459        doServiceRackList(dOc, fromElement);
    2445         doUnknownElements(dOc, fromElement); 
     2460        doUnknownElements(dOc, fromElement);
    24462461        // the official displayItems in the displayItemList element have already been handled above
    24472462        // and created as collectionmetadata elements in the dOc object
     
    24492464        NodeList collectionMetadataList = dOc.getDocumentElement().getElementsByTagName(StaticStrings.COLLECTIONMETADATA_ELEMENT);
    24502465        Set setOfUniqueColMetaNames = new HashSet();
    2451         for(int i = 0; i < collectionMetadataList.getLength(); i++) {
    2452             Element colMeta = (Element)collectionMetadataList.item(i);
    2453             String name = colMeta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    2454             setOfUniqueColMetaNames.add(name);
     2466        for (int i = 0; i < collectionMetadataList.getLength(); i++)
     2467        {
     2468            Element colMeta = (Element) collectionMetadataList.item(i);
     2469            String name = colMeta.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     2470            setOfUniqueColMetaNames.add(name);
    24552471        }
    24562472
    24572473        if (displayItemListNode != null)
    24582474        {
    2459             NodeList nl = ((Element)displayItemListNode).getElementsByTagName(StaticStrings.DISPLAYITEM_STR);
    2460 
    2461             // make a list of all unique attribute names that are specifically not
    2462             // description, smallicon, icon and name, since these are already processed above
    2463             Set setOfUniqueDisplayItemNames = new LinkedHashSet();
    2464             for(int i = 0; i < nl.getLength(); i++) {
    2465             Element displayItem = (Element)nl.item(i);
    2466             String name = displayItem.getAttribute(StaticStrings.NAME_ATTRIBUTE);
    2467 
    2468             if(name.equals("")) continue; // no name attribute
    2469             if(setOfUniqueColMetaNames.contains(name)) continue;
    2470 
    2471             if(name.equals(StaticStrings.DESCRIPTION_STR)) continue;
    2472             if(name.equals(StaticStrings.SMALLICON_STR)) continue;
    2473             if(name.equals(StaticStrings.ICON_STR)) continue;
    2474             if(name.equals(StaticStrings.NAME_STR)) continue;
    2475             // don't add displayItems that are handled by the indexers, etc. E.g. document:ex.Title
    2476             if(name.indexOf(":") != -1) continue;
    2477 
    2478             // otherwise
    2479             setOfUniqueDisplayItemNames.add(name); // Set will ensure no duplicate names
    2480             }
    2481            
    2482             Iterator i = setOfUniqueDisplayItemNames.iterator();
    2483             while (i.hasNext()) {
    2484             String displayItemName = (String)i.next();
    2485 
    2486             ArrayList custom_displayItem = doDisplayItemList(dOc, displayItemListNode, displayItemName, displayItemName);       
    2487             appendArrayList(toElement, custom_displayItem);
    2488             }
     2475            NodeList nl = ((Element) displayItemListNode).getElementsByTagName(StaticStrings.DISPLAYITEM_STR);
     2476
     2477            // make a list of all unique attribute names that are specifically not
     2478            // description, smallicon, icon and name, since these are already processed above
     2479            Set setOfUniqueDisplayItemNames = new LinkedHashSet();
     2480            for (int i = 0; i < nl.getLength(); i++)
     2481            {
     2482                Element displayItem = (Element) nl.item(i);
     2483                String name = displayItem.getAttribute(StaticStrings.NAME_ATTRIBUTE);
     2484
     2485                if (name.equals(""))
     2486                    continue; // no name attribute
     2487                if (setOfUniqueColMetaNames.contains(name))
     2488                    continue;
     2489
     2490                if (name.equals(StaticStrings.DESCRIPTION_STR))
     2491                    continue;
     2492                if (name.equals(StaticStrings.SMALLICON_STR))
     2493                    continue;
     2494                if (name.equals(StaticStrings.ICON_STR))
     2495                    continue;
     2496                if (name.equals(StaticStrings.NAME_STR))
     2497                    continue;
     2498                // don't add displayItems that are handled by the indexers, etc. E.g. document:ex.Title
     2499                if (name.indexOf(":") != -1)
     2500                    continue;
     2501
     2502                // otherwise
     2503                setOfUniqueDisplayItemNames.add(name); // Set will ensure no duplicate names
     2504            }
     2505
     2506            Iterator i = setOfUniqueDisplayItemNames.iterator();
     2507            while (i.hasNext())
     2508            {
     2509                String displayItemName = (String) i.next();
     2510
     2511                ArrayList custom_displayItem = doDisplayItemList(dOc, displayItemListNode, displayItemName, displayItemName);
     2512                appendArrayList(toElement, custom_displayItem);
     2513            }
    24892514        }
    24902515
     
    25082533        //first parse an empty skeleton of xml config file
    25092534        //The aim is to convert the internal structure into this skeleton
    2510       // This skeleton just has the CollectionConfig element and nothing else
     2535        // This skeleton just has the CollectionConfig element and nothing else
    25112536        Document skeleton = XMLTools.parseXMLFile("xml/CollectionConfig.xml", true);
    25122537        //Element internal = dOc.getDocumentElement();
Note: See TracChangeset for help on using the changeset viewer.