001package de.deepamehta.core.util; 002 003import de.deepamehta.core.Identifiable; 004import de.deepamehta.core.JSONEnabled; 005import de.deepamehta.core.Topic; 006import de.deepamehta.core.model.TopicModel; 007 008import org.codehaus.jettison.json.JSONArray; 009import org.codehaus.jettison.json.JSONObject; 010 011import java.net.URL; 012import java.util.ArrayList; 013import java.util.Collection; 014import java.util.HashMap; 015import java.util.Iterator; 016import java.util.List; 017import java.util.Map; 018import java.util.logging.Logger; 019 020 021 022public class DeepaMehtaUtils { 023 024 private static Logger logger = Logger.getLogger("de.deepamehta.core.util.DeepaMehtaUtils"); 025 026 private static final String DM4_HOST_URL = System.getProperty("dm4.host.url"); // ### TODO: default value (#734) 027 static { 028 logger.info("Host setting:\ndm4.host.url=\"" + DM4_HOST_URL + "\""); 029 } 030 031 032 033 // ************ 034 // *** URLs *** 035 // ************ 036 037 038 039 /** 040 * Checks if an URL refers to this DeepaMehta installation. 041 * The check relies on the "dm4.host.url" system property. 042 */ 043 public static boolean isDeepaMehtaURL(URL url) { 044 try { 045 return url.toString().startsWith(DM4_HOST_URL); 046 } catch (Exception e) { 047 throw new RuntimeException("Checking for DeepaMehta URL failed (url=\"" + url + "\")", e); 048 } 049 } 050 051 052 053 // ******************* 054 // *** Collections *** 055 // ******************* 056 057 058 059 public static List<Long> idList(Iterable<? extends Identifiable> items) { 060 List<Long> ids = new ArrayList(); 061 for (Identifiable item : items) { 062 ids.add(item.getId()); 063 } 064 return ids; 065 } 066 067 public static List<TopicModel> toTopicModels(Iterable<? extends Topic> topics) { 068 List<TopicModel> topicModels = new ArrayList(); 069 for (Topic topic : topics) { 070 topicModels.add(topic.getModel()); 071 } 072 return topicModels; 073 } 074 075 public static String topicNames(Iterable<? extends Topic> topics) { 076 StringBuilder names = new StringBuilder(); 077 Iterator<? extends Topic> i = topics.iterator(); 078 while (i.hasNext()) { 079 Topic topic = i.next(); 080 names.append('"').append(topic.getSimpleValue()).append('"'); 081 if (i.hasNext()) { 082 names.append(", "); 083 } 084 } 085 return names.toString(); 086 } 087 088 089 090 // ************ 091 // *** JSON *** 092 // ************ 093 094 095 096 // === Generic === 097 098 public static Map toMap(JSONObject o) { 099 return toMap(o, new HashMap()); 100 } 101 102 public static Map toMap(JSONObject o, Map map) { 103 try { 104 Iterator<String> i = o.keys(); 105 while (i.hasNext()) { 106 String key = i.next(); 107 map.put(key, o.get(key)); // throws JSONException 108 } 109 return map; 110 } catch (Exception e) { 111 throw new RuntimeException("Converting JSONObject to Map failed", e); 112 } 113 } 114 115 // --- 116 117 public static List toList(JSONArray o) { 118 try { 119 List list = new ArrayList(); 120 for (int i = 0; i < o.length(); i++) { 121 list.add(o.get(i)); // throws JSONException 122 } 123 return list; 124 } catch (Exception e) { 125 throw new RuntimeException("Converting JSONArray to List failed", e); 126 } 127 } 128 129 // === DeepaMehta specific === 130 131 public static JSONArray toJSONArray(Iterable<? extends JSONEnabled> items) { 132 JSONArray array = new JSONArray(); 133 for (JSONEnabled item : items) { 134 array.put(item.toJSON()); 135 } 136 return array; 137 } 138}