1 12 package org.eclipse.core.internal.preferences; 13 14 import java.io.*; 15 import java.util.*; 16 import org.eclipse.core.internal.runtime.RuntimeLog; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.preferences.*; 19 import org.eclipse.osgi.util.NLS; 20 import org.osgi.service.prefs.BackingStoreException; 21 import org.osgi.service.prefs.Preferences; 22 23 36 public class EclipsePreferences implements IEclipsePreferences, IScope { 37 38 public static final String DEFAULT_PREFERENCES_DIRNAME = ".settings"; public static final String PREFS_FILE_EXTENSION = "prefs"; protected static final IEclipsePreferences[] EMPTY_NODE_ARRAY = new IEclipsePreferences[0]; 41 protected static final String [] EMPTY_STRING_ARRAY = new String [0]; 42 private static final String FALSE = "false"; private static final String TRUE = "true"; protected static final String VERSION_KEY = "eclipse.preferences.version"; protected static final String VERSION_VALUE = "1"; protected static final String PATH_SEPARATOR = String.valueOf(IPath.SEPARATOR); 47 protected static final String DOUBLE_SLASH = "//"; protected static final String EMPTY_STRING = ""; 50 private String cachedPath; 51 protected Map children; 52 protected boolean dirty = false; 53 protected boolean loading = false; 54 protected final String name; 55 protected final EclipsePreferences parent; 57 protected ImmutableMap properties = ImmutableMap.EMPTY; 58 protected boolean removed = false; 59 private ListenerList nodeChangeListeners; 60 private ListenerList preferenceChangeListeners; 61 62 public static boolean DEBUG_PREFERENCE_GENERAL = false; 63 public static boolean DEBUG_PREFERENCE_SET = false; 64 public static boolean DEBUG_PREFERENCE_GET = false; 65 66 protected final static String debugPluginName = "org.eclipse.equinox.preferences"; 68 static { 69 DEBUG_PREFERENCE_GENERAL = PreferencesOSGiUtils.getDefault().getBooleanDebugOption(debugPluginName + "/general", false); DEBUG_PREFERENCE_SET = PreferencesOSGiUtils.getDefault().getBooleanDebugOption(debugPluginName + "/set", false); DEBUG_PREFERENCE_GET = PreferencesOSGiUtils.getDefault().getBooleanDebugOption(debugPluginName + "/get", false); } 73 74 public EclipsePreferences() { 75 this(null, null); 76 } 77 78 protected EclipsePreferences(EclipsePreferences parent, String name) { 79 super(); 80 this.parent = parent; 81 this.name = name; 82 } 83 84 87 public String absolutePath() { 88 if (cachedPath == null) { 89 if (parent == null) 90 cachedPath = PATH_SEPARATOR; 91 else { 92 String parentPath = parent.absolutePath(); 93 if (parentPath.length() == 1) 96 cachedPath = parentPath + name(); 97 else 98 cachedPath = parentPath + PATH_SEPARATOR + name(); 99 } 100 } 101 return cachedPath; 102 } 103 104 public void accept(IPreferenceNodeVisitor visitor) throws BackingStoreException { 105 if (!visitor.visit(this)) 106 return; 107 IEclipsePreferences[] toVisit = getChildren(true); 108 for (int i = 0; i < toVisit.length; i++) 109 toVisit[i].accept(visitor); 110 } 111 112 protected synchronized IEclipsePreferences addChild(String childName, IEclipsePreferences child) { 113 if (children == null) 115 children = Collections.synchronizedMap(new HashMap()); 116 children.put(childName, child == null ? (Object ) childName : child); 117 return child; 118 } 119 120 123 public void addNodeChangeListener(INodeChangeListener listener) { 124 checkRemoved(); 125 if (nodeChangeListeners == null) 126 nodeChangeListeners = new ListenerList(); 127 nodeChangeListeners.add(listener); 128 if (DEBUG_PREFERENCE_GENERAL) 129 PrefsMessages.message("Added preference node change listener: " + listener + " to: " + absolutePath()); } 131 132 135 public void addPreferenceChangeListener(IPreferenceChangeListener listener) { 136 checkRemoved(); 137 if (preferenceChangeListeners == null) 138 preferenceChangeListeners = new ListenerList(); 139 preferenceChangeListeners.add(listener); 140 if (DEBUG_PREFERENCE_GENERAL) 141 PrefsMessages.message("Added preference property change listener: " + listener + " to: " + absolutePath()); } 143 144 private IEclipsePreferences calculateRoot() { 145 IEclipsePreferences result = this; 146 while (result.parent() != null) 147 result = (IEclipsePreferences) result.parent(); 148 return result; 149 } 150 151 155 protected void checkRemoved() { 156 if (removed) 157 throw new IllegalStateException (NLS.bind(PrefsMessages.preferences_removedNode, name)); 158 } 159 160 163 public String [] childrenNames() { 164 checkRemoved(); 166 return internalChildNames(); 167 } 168 169 protected String [] internalChildNames() { 170 Map temp = children; 171 if (temp == null || temp.size() == 0) 172 return EMPTY_STRING_ARRAY; 173 return (String []) temp.keySet().toArray(EMPTY_STRING_ARRAY); 174 } 175 176 179 public void clear() { 180 checkRemoved(); 182 String [] keys = properties.keys(); 185 for (int i = 0; i < keys.length; i++) 186 remove(keys[i]); 187 makeDirty(); 188 } 189 190 protected String [] computeChildren(IPath root) { 191 if (root == null) 192 return EMPTY_STRING_ARRAY; 193 IPath dir = root.append(DEFAULT_PREFERENCES_DIRNAME); 194 final ArrayList result = new ArrayList(); 195 final String extension = '.' + PREFS_FILE_EXTENSION; 196 File file = dir.toFile(); 197 File[] totalFiles = file.listFiles(); 198 if (totalFiles != null) { 199 for (int i = 0; i < totalFiles.length; i++) { 200 if (totalFiles[i].isFile()) { 201 String filename = totalFiles[i].getName(); 202 if (filename.endsWith(extension)) { 203 String shortName = filename.substring(0, filename.length() - extension.length()); 204 result.add(shortName); 205 } 206 } 207 } 208 } 209 return (String []) result.toArray(EMPTY_STRING_ARRAY); 210 } 211 212 protected IPath computeLocation(IPath root, String qualifier) { 213 return root == null ? null : root.append(DEFAULT_PREFERENCES_DIRNAME).append(qualifier).addFileExtension(PREFS_FILE_EXTENSION); 214 } 215 216 220 protected static void convertFromProperties(EclipsePreferences node, Properties table, boolean notify) { 221 String version = table.getProperty(VERSION_KEY); 222 if (version == null || !VERSION_VALUE.equals(version)) { 223 } 225 table.remove(VERSION_KEY); 226 for (Iterator i = table.keySet().iterator(); i.hasNext();) { 227 String fullKey = (String ) i.next(); 228 String value = table.getProperty(fullKey); 229 if (value != null) { 230 String [] splitPath = decodePath(fullKey); 231 String path = splitPath[0]; 232 path = makeRelative(path); 233 String key = splitPath[1]; 234 if (DEBUG_PREFERENCE_SET) 235 PrefsMessages.message("Setting preference: " + path + '/' + key + '=' + value); EclipsePreferences childNode = (EclipsePreferences) node.internalNode(path, false, null); 238 String oldValue = childNode.internalPut(key, value); 239 if (notify && !value.equals(oldValue)) 241 node.firePreferenceEvent(key, oldValue, value); 242 } 243 } 244 PreferencesService.getDefault().shareStrings(); 245 } 246 247 251 protected Properties convertToProperties(Properties result, String prefix) throws BackingStoreException { 252 boolean addSeparator = prefix.length() != 0; 254 ImmutableMap temp = properties; 256 String [] keys = temp.keys(); 257 for (int i = 0, imax = keys.length; i < imax; i++) { 258 String value = temp.get(keys[i]); 259 if (value != null) 260 result.put(encodePath(prefix, keys[i]), value); 261 } 262 IEclipsePreferences[] childNodes = getChildren(true); 264 for (int i = 0; i < childNodes.length; i++) { 265 EclipsePreferences child = (EclipsePreferences) childNodes[i]; 266 String fullPath = addSeparator ? prefix + PATH_SEPARATOR + child.name() : child.name(); 267 child.convertToProperties(result, fullPath); 268 } 269 PreferencesService.getDefault().shareStrings(); 270 return result; 271 } 272 273 276 public IEclipsePreferences create(IEclipsePreferences nodeParent, String nodeName) { 277 return create((EclipsePreferences) nodeParent, nodeName, null); 278 } 279 280 protected boolean isLoading() { 281 return loading; 282 } 283 284 protected void setLoading(boolean isLoading) { 285 loading = isLoading; 286 } 287 288 public IEclipsePreferences create(EclipsePreferences nodeParent, String nodeName, Object context) { 289 EclipsePreferences result = internalCreate(nodeParent, nodeName, context); 290 nodeParent.addChild(nodeName, result); 291 IEclipsePreferences loadLevel = result.getLoadLevel(); 292 293 if (loadLevel == null) 295 return result; 296 297 if (result != loadLevel) 299 return result; 300 301 if (isAlreadyLoaded(result) || result.isLoading()) 303 return result; 304 try { 305 result.setLoading(true); 306 result.loadLegacy(); 307 result.load(); 308 result.loaded(); 309 result.flush(); 310 } catch (BackingStoreException e) { 311 IPath location = result.getLocation(); 312 String message = NLS.bind(PrefsMessages.preferences_loadException, location == null ? EMPTY_STRING : location.toString()); 313 IStatus status = new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e); 314 RuntimeLog.log(status); 315 } finally { 316 result.setLoading(false); 317 } 318 return result; 319 } 320 321 324 public void flush() throws BackingStoreException { 325 checkRemoved(); 327 328 IEclipsePreferences loadLevel = getLoadLevel(); 329 330 if (loadLevel == null) { 332 String [] childrenNames = childrenNames(); 333 for (int i = 0; i < childrenNames.length; i++) 334 node(childrenNames[i]).flush(); 335 return; 336 } 337 338 if (this != loadLevel) { 340 loadLevel.flush(); 341 return; 342 } 343 344 if (!dirty) 347 return; 348 dirty = false; 351 try { 352 save(); 353 } catch (BackingStoreException e) { 354 dirty = true; 356 throw e; 357 } 358 } 359 360 363 public String get(String key, String defaultValue) { 364 String value = internalGet(key); 365 return value == null ? defaultValue : value; 366 } 367 368 371 public boolean getBoolean(String key, boolean defaultValue) { 372 String value = internalGet(key); 373 return value == null ? defaultValue : TRUE.equalsIgnoreCase(value); 374 } 375 376 379 public byte[] getByteArray(String key, byte[] defaultValue) { 380 String value = internalGet(key); 381 return value == null ? defaultValue : Base64.decode(value.getBytes()); 382 } 383 384 388 protected synchronized boolean childExists(String childName) { 389 if (children == null) 390 return false; 391 return children.get(childName) != null; 392 } 393 394 398 protected IEclipsePreferences getChild(String key, Object context, boolean create) { 399 synchronized (this) { 400 if (children == null) 401 return null; 402 Object value = children.get(key); 403 if (value == null) 404 return null; 405 if (value instanceof IEclipsePreferences) 406 return (IEclipsePreferences) value; 407 if (!create) 410 return null; 411 } 412 return addChild(key, create(this, key, context)); 413 } 414 415 418 protected IEclipsePreferences[] getChildren(boolean create) { 419 ArrayList result = new ArrayList(); 420 String [] names = internalChildNames(); 421 for (int i = 0; i < names.length; i++) { 422 IEclipsePreferences child = getChild(names[i], null, create); 423 if (child != null) 424 result.add(child); 425 } 426 return (IEclipsePreferences[]) result.toArray(EMPTY_NODE_ARRAY); 427 } 428 429 432 public double getDouble(String key, double defaultValue) { 433 String value = internalGet(key); 434 double result = defaultValue; 435 if (value != null) 436 try { 437 result = Double.parseDouble(value); 438 } catch (NumberFormatException e) { 439 } 441 return result; 442 } 443 444 447 public float getFloat(String key, float defaultValue) { 448 String value = internalGet(key); 449 float result = defaultValue; 450 if (value != null) 451 try { 452 result = Float.parseFloat(value); 453 } catch (NumberFormatException e) { 454 } 456 return result; 457 } 458 459 462 public int getInt(String key, int defaultValue) { 463 String value = internalGet(key); 464 int result = defaultValue; 465 if (value != null) 466 try { 467 result = Integer.parseInt(value); 468 } catch (NumberFormatException e) { 469 } 471 return result; 472 } 473 474 protected IEclipsePreferences getLoadLevel() { 475 return null; 476 } 477 478 481 protected IPath getLocation() { 482 return null; 483 } 484 485 488 public long getLong(String key, long defaultValue) { 489 String value = internalGet(key); 490 long result = defaultValue; 491 if (value != null) 492 try { 493 result = Long.parseLong(value); 494 } catch (NumberFormatException e) { 495 } 497 return result; 498 } 499 500 protected EclipsePreferences internalCreate(EclipsePreferences nodeParent, String nodeName, Object context) { 501 return new EclipsePreferences(nodeParent, nodeName); 502 } 503 504 508 protected String internalGet(String key) { 509 if (key == null) 511 throw new NullPointerException (); 512 checkRemoved(); 514 String result = properties.get(key); 515 if (DEBUG_PREFERENCE_GET) 516 PrefsMessages.message("Getting preference value: " + absolutePath() + '/' + key + "->" + result); return result; 518 } 519 520 523 protected IEclipsePreferences internalNode(String path, boolean notify, Object context) { 524 525 checkRemoved(); 527 528 if (path.length() == 0) 530 return this; 531 532 if (path.charAt(0) == IPath.SEPARATOR) 536 return (IEclipsePreferences) calculateRoot().node(path.substring(1)); 537 538 int index = path.indexOf(IPath.SEPARATOR); 539 String key = index == -1 ? path : path.substring(0, index); 540 boolean added = false; 541 IEclipsePreferences child = getChild(key, context, true); 542 if (child == null) { 543 child = create(this, key, context); 544 added = true; 545 } 546 if (added && notify) 548 fireNodeEvent(new NodeChangeEvent(this, child), true); 549 return (IEclipsePreferences) child.node(index == -1 ? EMPTY_STRING : path.substring(index + 1)); 550 } 551 552 557 protected String internalPut(String key, String newValue) { 558 checkRemoved(); 560 String oldValue = properties.get(key); 561 if (oldValue != null && oldValue.equals(newValue)) 562 return oldValue; 563 if (DEBUG_PREFERENCE_SET) 564 PrefsMessages.message("Setting preference: " + absolutePath() + '/' + key + '=' + newValue); properties = properties.put(key, newValue); 566 return oldValue; 567 } 568 569 572 protected boolean isAlreadyLoaded(IEclipsePreferences node) { 573 return true; 574 } 575 576 579 public String [] keys() { 580 checkRemoved(); 582 return properties.keys(); 583 } 584 585 protected void load() throws BackingStoreException { 586 load(getLocation()); 587 } 588 589 protected static Properties loadProperties(IPath location) throws BackingStoreException { 590 if (DEBUG_PREFERENCE_GENERAL) 591 PrefsMessages.message("Loading preferences from file: " + location); InputStream input = null; 593 Properties result = new Properties(); 594 try { 595 input = new BufferedInputStream(new FileInputStream(location.toFile())); 596 result.load(input); 597 } catch (FileNotFoundException e) { 598 if (DEBUG_PREFERENCE_GENERAL) 600 PrefsMessages.message("Preference file does not exist: " + location); return result; 602 } catch (IOException e) { 603 String message = NLS.bind(PrefsMessages.preferences_loadException, location); 604 log(new Status(IStatus.INFO, PrefsMessages.OWNER_NAME, IStatus.INFO, message, e)); 605 throw new BackingStoreException(message); 606 } finally { 607 if (input != null) 608 try { 609 input.close(); 610 } catch (IOException e) { 611 } 613 } 614 return result; 615 } 616 617 protected void load(IPath location) throws BackingStoreException { 618 if (location == null) { 619 if (DEBUG_PREFERENCE_GENERAL) 620 PrefsMessages.message("Unable to determine location of preference file for node: " + absolutePath()); return; 622 } 623 Properties fromDisk = loadProperties(location); 624 convertFromProperties(this, fromDisk, false); 625 } 626 627 protected void loaded() { 628 } 630 631 protected void loadLegacy() { 632 } 634 635 public static void log(IStatus status) { 636 RuntimeLog.log(status); 637 } 638 639 protected void makeDirty() { 640 EclipsePreferences node = this; 641 while (node != null && !node.removed) { 642 node.dirty = true; 643 node = (EclipsePreferences) node.parent(); 644 } 645 } 646 647 public boolean isDirty() { 648 return dirty; 649 } 650 651 654 public String name() { 655 return name; 656 } 657 658 661 public Preferences node(String pathName) { 662 return internalNode(pathName, true, null); 663 } 664 665 protected void fireNodeEvent(final NodeChangeEvent event, final boolean added) { 666 if (nodeChangeListeners == null) 667 return; 668 Object [] listeners = nodeChangeListeners.getListeners(); 669 for (int i = 0; i < listeners.length; i++) { 670 final INodeChangeListener listener = (INodeChangeListener) listeners[i]; 671 ISafeRunnable job = new ISafeRunnable() { 672 public void handleException(Throwable exception) { 673 } 675 676 public void run() throws Exception { 677 if (added) 678 listener.added(event); 679 else 680 listener.removed(event); 681 } 682 }; 683 SafeRunner.run(job); 684 } 685 } 686 687 690 public boolean nodeExists(String path) throws BackingStoreException { 691 if (path.length() == 0) 693 return !removed; 694 695 checkRemoved(); 698 699 if (path.charAt(0) == IPath.SEPARATOR) 702 return calculateRoot().nodeExists(path.substring(1)); 703 704 int index = path.indexOf(IPath.SEPARATOR); 705 boolean noSlash = index == -1; 706 707 if (noSlash) 709 return childExists(path); 710 711 String childName = path.substring(0, index); 713 if (!childExists(childName)) 714 return false; 715 IEclipsePreferences child = getChild(childName, null, true); 716 if (child == null) 717 return false; 718 return child.nodeExists(path.substring(index + 1)); 719 } 720 721 724 public Preferences parent() { 725 checkRemoved(); 727 return parent; 728 } 729 730 733 protected void firePreferenceEvent(String key, Object oldValue, Object newValue) { 734 if (preferenceChangeListeners == null) 735 return; 736 Object [] listeners = preferenceChangeListeners.getListeners(); 737 final PreferenceChangeEvent event = new PreferenceChangeEvent(this, key, oldValue, newValue); 738 for (int i = 0; i < listeners.length; i++) { 739 final IPreferenceChangeListener listener = (IPreferenceChangeListener) listeners[i]; 740 ISafeRunnable job = new ISafeRunnable() { 741 public void handleException(Throwable exception) { 742 } 744 745 public void run() throws Exception { 746 listener.preferenceChange(event); 747 } 748 }; 749 SafeRunner.run(job); 750 } 751 } 752 753 756 public void put(String key, String newValue) { 757 if (key == null || newValue == null) 758 throw new NullPointerException (); 759 String oldValue = internalPut(key, newValue); 760 if (!newValue.equals(oldValue)) { 761 makeDirty(); 762 firePreferenceEvent(key, oldValue, newValue); 763 } 764 } 765 766 769 public void putBoolean(String key, boolean value) { 770 if (key == null) 771 throw new NullPointerException (); 772 String newValue = value ? TRUE : FALSE; 773 String oldValue = internalPut(key, newValue); 774 if (!newValue.equals(oldValue)) { 775 makeDirty(); 776 firePreferenceEvent(key, oldValue, newValue); 777 } 778 } 779 780 783 public void putByteArray(String key, byte[] value) { 784 if (key == null || value == null) 785 throw new NullPointerException (); 786 String newValue = new String (Base64.encode(value)); 787 String oldValue = internalPut(key, newValue); 788 if (!newValue.equals(oldValue)) { 789 makeDirty(); 790 firePreferenceEvent(key, oldValue, newValue); 791 } 792 } 793 794 797 public void putDouble(String key, double value) { 798 if (key == null) 799 throw new NullPointerException (); 800 String newValue = Double.toString(value); 801 String oldValue = internalPut(key, newValue); 802 if (!newValue.equals(oldValue)) { 803 makeDirty(); 804 firePreferenceEvent(key, oldValue, newValue); 805 } 806 } 807 808 811 public void putFloat(String key, float value) { 812 if (key == null) 813 throw new NullPointerException (); 814 String newValue = Float.toString(value); 815 String oldValue = internalPut(key, newValue); 816 if (!newValue.equals(oldValue)) { 817 makeDirty(); 818 firePreferenceEvent(key, oldValue, newValue); 819 } 820 } 821 822 825 public void putInt(String key, int value) { 826 if (key == null) 827 throw new NullPointerException (); 828 String newValue = Integer.toString(value); 829 String oldValue = internalPut(key, newValue); 830 if (!newValue.equals(oldValue)) { 831 makeDirty(); 832 firePreferenceEvent(key, oldValue, newValue); 833 } 834 } 835 836 839 public void putLong(String key, long value) { 840 if (key == null) 841 throw new NullPointerException (); 842 String newValue = Long.toString(value); 843 String oldValue = internalPut(key, newValue); 844 if (!newValue.equals(oldValue)) { 845 makeDirty(); 846 firePreferenceEvent(key, oldValue, newValue); 847 } 848 } 849 850 853 public void remove(String key) { 854 String oldValue = properties.get(key); 855 if (oldValue == null) 856 return; 857 properties = properties.removeKey(key); 858 makeDirty(); 859 firePreferenceEvent(key, oldValue, null); 860 } 861 862 865 public void removeNode() throws BackingStoreException { 866 checkRemoved(); 868 String [] keys = keys(); 871 for (int i = 0; i < keys.length; i++) 872 remove(keys[i]); 873 if (parent != null && !(parent instanceof RootPreferences)) { 876 removed = true; 878 parent.removeNode(this); 879 } 880 IEclipsePreferences[] childNodes = getChildren(false); 881 for (int i = 0; i < childNodes.length; i++) 882 try { 883 childNodes[i].removeNode(); 884 } catch (IllegalStateException e) { 885 } 888 } 889 890 894 protected void removeNode(IEclipsePreferences child) { 895 boolean wasRemoved = false; 896 synchronized (this) { 897 if (children != null) { 898 wasRemoved = children.remove(child.name()) != null; 899 if (wasRemoved) 900 makeDirty(); 901 if (children.isEmpty()) 902 children = null; 903 } 904 } 905 if (wasRemoved) 906 fireNodeEvent(new NodeChangeEvent(this, child), false); 907 } 908 909 912 protected void removeNode(String key) { 913 synchronized (this) { 914 if (children != null) { 915 boolean wasRemoved = children.remove(key) != null; 916 if (wasRemoved) 917 makeDirty(); 918 if (children.isEmpty()) 919 children = null; 920 } 921 } 922 } 923 924 927 public void removeNodeChangeListener(INodeChangeListener listener) { 928 checkRemoved(); 929 if (nodeChangeListeners == null) 930 return; 931 nodeChangeListeners.remove(listener); 932 if (nodeChangeListeners.size() == 0) 933 nodeChangeListeners = null; 934 if (DEBUG_PREFERENCE_GENERAL) 935 PrefsMessages.message("Removed preference node change listener: " + listener + " from: " + absolutePath()); } 937 938 941 public void removePreferenceChangeListener(IPreferenceChangeListener listener) { 942 checkRemoved(); 943 if (preferenceChangeListeners == null) 944 return; 945 preferenceChangeListeners.remove(listener); 946 if (preferenceChangeListeners.size() == 0) 947 preferenceChangeListeners = null; 948 if (DEBUG_PREFERENCE_GENERAL) 949 PrefsMessages.message("Removed preference property change listener: " + listener + " from: " + absolutePath()); } 951 952 protected void save() throws BackingStoreException { 953 save(getLocation()); 954 } 955 956 protected void save(IPath location) throws BackingStoreException { 957 if (location == null) { 958 if (DEBUG_PREFERENCE_GENERAL) 959 PrefsMessages.message("Unable to determine location of preference file for node: " + absolutePath()); return; 961 } 962 if (DEBUG_PREFERENCE_GENERAL) 963 PrefsMessages.message("Saving preferences to file: " + location); Properties table = convertToProperties(new Properties(), EMPTY_STRING); 965 if (table.isEmpty()) { 966 if (location.toFile().exists() && !location.toFile().delete()) { 968 String message = NLS.bind(PrefsMessages.preferences_failedDelete, location); 969 log(new Status(IStatus.WARNING, PrefsMessages.OWNER_NAME, IStatus.WARNING, message, null)); 970 } 971 return; 972 } 973 table.put(VERSION_KEY, VERSION_VALUE); 974 OutputStream output = null; 975 FileOutputStream fos = null; 976 try { 977 File parentFile = location.toFile().getParentFile(); 979 if (parentFile == null) 980 return; 981 parentFile.mkdirs(); 982 fos = new FileOutputStream(location.toOSString(), false); 984 output = new BufferedOutputStream(fos); 985 table.store(output, null); 986 output.flush(); 987 fos.getFD().sync(); 988 } catch (IOException e) { 989 String message = NLS.bind(PrefsMessages.preferences_saveException, location); 990 log(new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e)); 991 throw new BackingStoreException(message); 992 } finally { 993 if (output != null) 994 try { 995 output.close(); 996 } catch (IOException e) { 997 } 999 } 1000 } 1001 1002 1010 public void shareStrings(StringPool pool) { 1011 properties.shareStrings(pool); 1012 IEclipsePreferences[] myChildren = getChildren(false); 1013 for (int i = 0; i < myChildren.length; i++) 1014 if (myChildren[i] instanceof EclipsePreferences) 1015 ((EclipsePreferences) myChildren[i]).shareStrings(pool); 1016 } 1017 1018 1024 public static String encodePath(String path, String key) { 1025 String result; 1026 int pathLength = path == null ? 0 : path.length(); 1027 if (key.indexOf(IPath.SEPARATOR) == -1) { 1028 if (pathLength == 0) 1029 result = key; 1030 else 1031 result = path + IPath.SEPARATOR + key; 1032 } else { 1033 if (pathLength == 0) 1034 result = DOUBLE_SLASH + key; 1035 else 1036 result = path + DOUBLE_SLASH + key; 1037 } 1038 return result; 1039 } 1040 1041 1045 public static String getSegment(String path, int segment) { 1046 int start = path.indexOf(IPath.SEPARATOR) == 0 ? 1 : 0; 1047 int end = path.indexOf(IPath.SEPARATOR, start); 1048 if (end == path.length() - 1) 1049 end = -1; 1050 for (int i = 0; i < segment; i++) { 1051 if (end == -1) 1052 return null; 1053 start = end + 1; 1054 end = path.indexOf(IPath.SEPARATOR, start); 1055 } 1056 if (end == -1) 1057 end = path.length(); 1058 return path.substring(start, end); 1059 } 1060 1061 public static int getSegmentCount(String path) { 1062 StringTokenizer tokenizer = new StringTokenizer(path, String.valueOf(IPath.SEPARATOR)); 1063 return tokenizer.countTokens(); 1064 } 1065 1066 1069 public static String makeRelative(String path) { 1070 String result = path; 1071 if (path == null) 1072 return EMPTY_STRING; 1073 if (path.length() > 0 && path.charAt(0) == IPath.SEPARATOR) 1074 result = path.length() == 0 ? EMPTY_STRING : path.substring(1); 1075 return result; 1076 } 1077 1078 1085 public static String [] decodePath(String fullPath) { 1086 String key = null; 1087 String path = null; 1088 1089 int index = fullPath.indexOf(DOUBLE_SLASH); 1091 if (index == -1) { 1092 int lastIndex = fullPath.lastIndexOf(IPath.SEPARATOR); 1095 if (lastIndex == -1) { 1096 key = fullPath; 1097 } else { 1098 path = fullPath.substring(0, lastIndex); 1099 key = fullPath.substring(lastIndex + 1); 1100 } 1101 } else { 1102 path = fullPath.substring(0, index); 1105 key = fullPath.substring(index + 2); 1106 } 1107 1108 if (path != null) 1110 if (path.length() == 0) 1111 path = null; 1112 else if (path.charAt(0) == IPath.SEPARATOR) 1113 path = path.substring(1); 1114 1115 return new String [] {path, key}; 1116 } 1117 1118 1121 1122 public void sync() throws BackingStoreException { 1123 checkRemoved(); 1125 IEclipsePreferences node = getLoadLevel(); 1126 if (node == null) { 1127 if (DEBUG_PREFERENCE_GENERAL) 1128 PrefsMessages.message("Preference node is not a load root: " + absolutePath()); return; 1130 } 1131 if (node instanceof EclipsePreferences) { 1132 ((EclipsePreferences) node).load(); 1133 node.flush(); 1134 } 1135 } 1136 1137 public String toDeepDebugString() { 1138 final StringBuffer buffer = new StringBuffer (); 1139 IPreferenceNodeVisitor visitor = new IPreferenceNodeVisitor() { 1140 public boolean visit(IEclipsePreferences node) throws BackingStoreException { 1141 buffer.append(node); 1142 buffer.append('\n'); 1143 String [] keys = node.keys(); 1144 for (int i = 0; i < keys.length; i++) { 1145 buffer.append(node.absolutePath()); 1146 buffer.append(PATH_SEPARATOR); 1147 buffer.append(keys[i]); 1148 buffer.append('='); 1149 buffer.append(node.get(keys[i], "*default*")); buffer.append('\n'); 1151 } 1152 return true; 1153 } 1154 }; 1155 try { 1156 accept(visitor); 1157 } catch (BackingStoreException e) { 1158 System.out.println("Exception while calling #toDeepDebugString()"); e.printStackTrace(); 1160 } 1161 return buffer.toString(); 1162 } 1163 1164 public String toString() { 1165 return absolutePath(); 1166 } 1167} 1168 | Popular Tags |