Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DisplayItemUtil.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DisplayItemUtil.java	(revision 36506)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/DisplayItemUtil.java	(revision 36507)
@@ -71,4 +71,42 @@
   }
   
+    /** Choose the best displayItem from a list - that matches lang - and return a copy of it created by doc. */
+    public static Element chooseBestMatchDisplayItem(Document doc, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
+
+
+	// is there one with the specified language?
+	Element best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
+	if (best_di != null) {
+	    return (Element)doc.importNode(best_di, true);
+	}
+
+	// if not, have we got one with a key?
+	best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.KEY_ATT, null);
+	if (best_di != null) {
+	    // look up the dictionary
+	    String value = getTextString(best_di.getAttribute(GSXML.KEY_ATT), lang, best_di.getAttribute(GSXML.DICTIONARY_ATT), class_loader);
+	    if (value != null) {
+		// copy the node now. Don't want to be modifying the underlying list as can lead to concurrent access problems.
+		best_di = (Element)doc.importNode(best_di, true);
+		GSXML.setNodeText(best_di, value);
+		return best_di;
+	    }
+	}
+	// ok, key one didn't work, can we use default lang?
+	if (lang != default_lang) {
+	  best_di = GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, default_lang);
+	  if (best_di != null) {
+	    return (Element)doc.importNode(best_di, true);
+	  }
+	}
+	// STILL haven't found one, lets use the first one with a lang att (so we don't just get the key one back
+	best_di = (Element) GSXML.getNamedElement(display_item_list, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, null);
+	if (best_di != null) {
+	    return (Element)doc.importNode(best_di, true);
+	}
+	return null;
+
+    }
+
   /** Finds the best language specific match for each displayItem in display_item_list and adds it to description */
   public static boolean addLanguageSpecificDisplayItems(Element description, Element display_item_list, String lang, String default_lang, ClassLoader class_loader) {
@@ -78,35 +116,9 @@
     for (int i = 0; i < items.getLength(); i++)
       { // for each key
-	Element m = (Element) items.item(i);
-	// is there one with the specified language?
-	Element new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, lang);
-	if (new_m == null) {
-	  // if not, have we got one with a key?
-	  new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.KEY_ATT, null);
-	  if (new_m != null) {
-	    // look up the dictionary
-	    String value = getTextString(new_m.getAttribute(GSXML.KEY_ATT), lang, new_m.getAttribute(GSXML.DICTIONARY_ATT), class_loader);
-	    if (value != null) {
-	      // copy the node now. Don't want to be modifying the underlying list as can lead to concurrent access problems.
-	      new_m = (Element)doc.importNode(new_m, true);
-	      GSXML.setNodeText(new_m, value);
-	    }
-	    else {
-	      // haven't found the key in the dictionary, ignore this display item
-	      new_m = null;
-	    }
-	  }
-	}
-	if (new_m == null && lang != default_lang) {
-	  // still haven't got a value. can we use the default lang?
-	  new_m = GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, default_lang);
-	}
-	if (new_m == null)
-	  {
-	    // STILL haven't found one, lets use the first one with a lang att (so we don't just get the key one back
-	    new_m = (Element) GSXML.getNamedElement(m, GSXML.DISPLAY_TEXT_ELEM, GSXML.LANG_ATT, null);
-	  }
+	Element di_list = (Element) items.item(i);
+	Element new_m = chooseBestMatchDisplayItem(doc, di_list, lang, default_lang, class_loader);
+
 	if (new_m != null) {
-	  description.appendChild(doc.importNode(new_m, true));
+	  description.appendChild(new_m);
 	}
       } // for each key
