1 11 package org.eclipse.help.internal.util; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.LinkedHashSet ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 import java.util.Set ; 26 import java.util.StringTokenizer ; 27 28 import org.eclipse.core.runtime.IConfigurationElement; 29 import org.eclipse.core.runtime.IProduct; 30 import org.eclipse.core.runtime.Platform; 31 import org.eclipse.core.runtime.Plugin; 32 import org.eclipse.core.runtime.Preferences; 33 import org.eclipse.help.internal.HelpData; 34 import org.eclipse.help.internal.HelpPlugin; 35 import org.osgi.framework.Bundle; 36 37 45 public class ProductPreferences { 46 47 private static final String TRUE = String.valueOf(true); 48 private static Properties [] productPreferences; 49 private static SequenceResolver orderResolver; 50 private static Map preferencesToPluginIdMap; 51 private static Map preferencesToProductIdMap; 52 private static List primaryTocOrdering; 53 private static List [] secondaryTocOrderings; 54 55 60 public static List getTocOrder(List itemsToOrder) { 61 return getOrderedList(itemsToOrder, getPrimaryTocOrdering(), getSecondaryTocOrderings()); 62 } 63 64 69 public static List getPrimaryTocOrdering() { 70 if (primaryTocOrdering == null) { 71 IProduct product = Platform.getProduct(); 72 if (product != null) { 73 String pluginId = product.getDefiningBundle().getSymbolicName(); 74 Preferences prefs = HelpPlugin.getDefault().getPluginPreferences(); 75 String helpDataFile = prefs.getString(HelpPlugin.HELP_DATA_KEY); 76 String baseTOCS = prefs.getString(HelpPlugin.BASE_TOCS_KEY); 77 primaryTocOrdering = getTocOrdering(pluginId, helpDataFile, baseTOCS); 78 } 79 if (primaryTocOrdering == null) { 81 primaryTocOrdering = new ArrayList (); 82 } 83 } 84 return primaryTocOrdering; 85 } 86 87 91 public static List [] getSecondaryTocOrderings() { 92 if (secondaryTocOrderings == null) { 93 List list = new ArrayList (); 94 Properties [] productPreferences = getProductPreferences(false); 95 for (int i=0;i<productPreferences.length;++i) { 96 String pluginId = (String )preferencesToPluginIdMap.get(productPreferences[i]); 97 String helpDataFile = (String )productPreferences[i].get(HelpPlugin.PLUGIN_ID + '/' + HelpPlugin.HELP_DATA_KEY); 98 String baseTOCS = (String )productPreferences[i].get(HelpPlugin.PLUGIN_ID + '/' + HelpPlugin.BASE_TOCS_KEY); 99 List ordering = getTocOrdering(pluginId, helpDataFile, baseTOCS); 100 if (ordering != null) { 101 list.add(ordering); 102 } 103 } 104 secondaryTocOrderings = (List [])list.toArray(new List [list.size()]); 105 } 106 return secondaryTocOrderings; 107 } 108 109 114 public static List getTocOrdering(String pluginId, String helpDataFile, String baseTOCS) { 115 if (helpDataFile != null && helpDataFile.length() > 0) { 116 Bundle bundle = Platform.getBundle(pluginId); 117 URL helpDataUrl = bundle.getEntry(helpDataFile); 118 HelpData helpData = new HelpData(helpDataUrl); 119 return helpData.getTocOrder(); 120 } 121 else { 122 if (baseTOCS != null) { 123 return tokenize(baseTOCS); 124 } 125 } 126 return null; 127 } 128 129 135 public static boolean getBoolean(Plugin plugin, String key) { 136 Properties [] properties = getProductPreferences(true); 137 String defaultValue = plugin.getPluginPreferences().getDefaultString(key); 138 String currentValue = plugin.getPluginPreferences().getString(key); 139 String pluginId = plugin.getBundle().getSymbolicName(); 140 if (currentValue != null && currentValue.equalsIgnoreCase(TRUE)) { 141 return true; 142 } 143 for (int i=0;i<properties.length;++i) { 144 String value = (String )properties[i].get(pluginId + '/' + key); 145 if (value == null) { 146 value = defaultValue; 147 } 148 if (value != null && value.equalsIgnoreCase(TRUE)) { 149 return true; 150 } 151 } 152 return false; 153 } 154 155 161 public static List getOrderedList(Plugin plugin, String key, List items) { 162 List primary = tokenize(plugin.getPluginPreferences().getString(key)); 163 Properties [] productPreferences = getProductPreferences(false); 164 List secondaryLists = new ArrayList (); 165 for (int i=0;i<productPreferences.length;++i) { 166 String value = productPreferences[i].getProperty(plugin.getBundle().getSymbolicName() + '/' + key); 167 if (value != null) { 168 secondaryLists.add(tokenize(value)); 169 } 170 } 171 List [] secondary = (List [])secondaryLists.toArray(new List [secondaryLists.size()]); 172 return getOrderedList(items, primary, secondary); 173 } 174 175 180 public static List getOrderedList(List items, List order) { 181 return getOrderedList(items, order, null); 182 } 183 184 189 public static List getOrderedList(List items, List primary, List [] secondary) { 190 List result = new ArrayList (); 191 LinkedHashSet set = new LinkedHashSet (items); 192 if (orderResolver == null) { 193 orderResolver = new SequenceResolver(); 194 } 195 List order = orderResolver.getSequence(primary, secondary); 196 Iterator iter = order.iterator(); 197 while (iter.hasNext()) { 198 Object obj = iter.next(); 199 if (set.contains(obj)) { 200 result.add(obj); 201 set.remove(obj); 202 } 203 } 204 result.addAll(set); 205 return result; 206 } 207 208 public static synchronized String getPluginId(Properties prefs) { 209 return (String )preferencesToPluginIdMap.get(prefs); 210 } 211 212 public static synchronized String getProductId(Properties prefs) { 213 return (String )preferencesToProductIdMap.get(prefs); 214 } 215 216 220 public static synchronized Properties [] getProductPreferences(boolean includeActiveProduct) { 221 if (productPreferences == null) { 222 String activeProductId = null; 223 IProduct activeProduct = Platform.getProduct(); 224 if (activeProduct != null) { 225 activeProductId = activeProduct.getId(); 226 } 227 Collection collection = new ArrayList (); 228 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.core.runtime.products"); for (int i=0;i<elements.length;++i) { 230 if (elements[i].getName().equals("product")) { String productId = elements[i].getDeclaringExtension().getUniqueIdentifier(); 232 if (includeActiveProduct || activeProductId == null || !activeProductId.equals(productId)) { 233 String contributor = elements[i].getContributor().getName(); 234 IConfigurationElement[] propertyElements = elements[i].getChildren("property"); for (int j=0;j<propertyElements.length;++j) { 236 String name = propertyElements[j].getAttribute("name"); if (name != null && name.equals("preferenceCustomization")) { String value = propertyElements[j].getAttribute("value"); if (value != null) { 240 Properties properties = loadPropertiesFile(contributor, value); 241 if (properties != null) { 242 collection.add(properties); 243 } 244 if (preferencesToPluginIdMap == null) { 245 preferencesToPluginIdMap = new HashMap (); 246 } 247 preferencesToPluginIdMap.put(properties, contributor); 248 if (preferencesToProductIdMap == null) { 249 preferencesToProductIdMap = new HashMap (); 250 } 251 preferencesToProductIdMap.put(properties, productId); 252 } 253 } 254 } 255 } 256 } 257 } 258 productPreferences = (Properties [])collection.toArray(new Properties [collection.size()]); 259 } 260 return productPreferences; 261 } 262 263 268 public static Set getUniqueValues(Plugin plugin, String key, Properties [] properties) { 269 Set set = new HashSet (); 270 String defaultValue = plugin.getPluginPreferences().getDefaultString(key); 271 String currentValue = plugin.getPluginPreferences().getString(key); 272 String pluginId = plugin.getBundle().getSymbolicName(); 273 for (int i=0;i<properties.length;++i) { 274 String value = (String )properties[i].get(pluginId + '/' + key); 275 set.add(value != null ? value : defaultValue); 276 } 277 set.add(currentValue != null ? currentValue : defaultValue); 278 return set; 279 } 280 281 287 public static String getValue(String key, Properties primary, Properties [] secondary) { 288 String value = null; 289 if (primary != null) { 290 value = primary.getProperty(key); 291 } 292 if (value == null) { 293 for (int i=0;i<secondary.length;++i) { 294 if (secondary[i] != primary) { 295 value = secondary[i].getProperty(key); 296 if (value != null) { 297 break; 298 } 299 } 300 } 301 } 302 return value; 303 } 304 305 309 public static Properties loadPropertiesFile(String bundleId, String path) { 310 Bundle bundle = Platform.getBundle(bundleId); 311 if (bundle != null) { 312 URL url = bundle.getEntry(path); 313 if (url != null) { 314 InputStream in = null; 315 try { 316 in = url.openStream(); 317 Properties properties = new Properties (); 318 properties.load(in); 319 return properties; 320 } 321 catch (IOException e) { 322 HelpPlugin.logError("Error opening product's plugin customization file: " + bundleId + "/" + path, e); } 325 finally { 326 if (in != null) { 327 try { 328 in.close(); 329 } 330 catch (IOException e) { 331 } 333 } 334 } 335 } 336 } 337 return null; 338 } 339 340 347 public static List tokenize(String str) { 348 if (str != null) { 349 StringTokenizer tok = new StringTokenizer (str, " \n\r\t;,"); List list = new ArrayList (); 351 while (tok.hasMoreElements()) { 352 list.add(tok.nextToken()); 353 } 354 return list; 355 } 356 return new ArrayList (); 357 } 358 } 359 | Popular Tags |