1 11 package org.eclipse.help.internal.search; 12 13 import java.io.*; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Enumeration ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.StringTokenizer ; 21 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.help.internal.base.util.*; 24 import org.osgi.framework.*; 25 26 31 public class PluginVersionInfo extends HelpProperties { 32 private static final long serialVersionUID = 1L; 33 34 protected static final String SEPARATOR = "\n"; 37 File dir; 38 39 boolean doComparison = true; 40 41 boolean hasChanged = false; 42 43 boolean ignoreSavedVersions; 44 45 Collection added = new ArrayList (); 46 47 Collection removed = new ArrayList (); 48 49 63 public PluginVersionInfo(String name, Collection docBundleIds, File dir, 64 boolean ignoreSavedVersions) { 65 super(name, dir); 66 this.dir = dir; 67 this.ignoreSavedVersions = ignoreSavedVersions; 68 createTable(docBundleIds); 69 } 70 71 protected void createTable(Collection docBundleIds) { 72 for (Iterator it = docBundleIds.iterator(); it.hasNext();) { 74 String bundleId = (String ) it.next(); 75 Bundle pluginBundle = Platform.getBundle(bundleId); 76 if (pluginBundle == null) { 77 continue; 78 } 79 StringBuffer pluginVersionAndFragments = new StringBuffer (); 80 appendBundleInformation(pluginVersionAndFragments, bundleId, 81 (String ) pluginBundle.getHeaders().get( 82 Constants.BUNDLE_VERSION)); 83 Bundle[] fragmentBundles = Platform.getFragments(pluginBundle); 84 if (fragmentBundles != null) { 85 for (int f = 0; f < fragmentBundles.length; f++) { 86 if (fragmentBundles[f].getState() == Bundle.INSTALLED 87 || fragmentBundles[f].getState() == Bundle.UNINSTALLED) 88 continue; 89 appendBundleInformation(pluginVersionAndFragments, 90 fragmentBundles[f].getSymbolicName(), 91 (String ) fragmentBundles[f].getHeaders().get( 92 Constants.BUNDLE_VERSION)); 93 } 94 } 95 this.put(bundleId, pluginVersionAndFragments.toString()); 96 } 97 } 98 99 protected void appendBundleInformation(StringBuffer buffer, String id, 100 String version) { 101 if (buffer.length()>0) 102 buffer.append(SEPARATOR); 103 buffer.append(id); 104 buffer.append(SEPARATOR); 105 buffer.append(version); 106 } 107 108 114 public boolean detectChange() { 115 if (!doComparison) 116 return hasChanged; 117 HelpProperties oldContrs = new HelpProperties(this.name, dir); 119 if (!ignoreSavedVersions) { 120 oldContrs.restore(); 121 } 122 hasChanged = false; 124 for (Enumeration keysEnum = this.keys(); keysEnum.hasMoreElements();) { 125 String oneContr = (String ) keysEnum.nextElement(); 126 if (!oldContrs.containsKey(oneContr)) { 127 added.add(oneContr); 129 } else { 130 String versions = (String ) this.get(oneContr); 131 String oldVersions = (String ) oldContrs.get(oneContr); 132 if (!compare(versions, oldVersions)) { 133 added.add(oneContr); 135 } 136 } 137 } 138 for (Enumeration keysEnum = oldContrs.keys(); keysEnum 139 .hasMoreElements();) { 140 String oneContr = (String ) keysEnum.nextElement(); 141 if (!this.containsKey(oneContr)) { 142 removed.add(oneContr); 144 } else { 145 String versions = (String ) this.get(oneContr); 146 String oldVersions = (String ) oldContrs.get(oneContr); 147 if (!compare(versions, oldVersions)) { 148 removed.add(oneContr); 150 } 151 } 152 } 153 hasChanged = added.size() > 0 || removed.size() > 0; 154 doComparison = false; 155 return hasChanged; 156 } 157 158 162 public Collection getAdded() { 163 if (doComparison) 164 detectChange(); 165 return added; 166 } 167 168 172 public Collection getRemoved() { 173 if (doComparison) 174 detectChange(); 175 return removed; 176 } 177 178 184 public boolean save() { 185 if (super.save()) { 186 doComparison = false; 187 hasChanged = false; 188 ignoreSavedVersions = false; 189 added = new ArrayList (); 190 removed = new ArrayList (); 191 return true; 192 } 193 return false; 194 } 195 196 203 private boolean compare(String versions, String oldVersions) { 204 Map versionMap = new HashMap (); 205 for (StringTokenizer t = new StringTokenizer (versions, SEPARATOR, false); t 206 .hasMoreTokens();) { 207 String pluginOrFragment = t.nextToken(); 208 if (t.hasMoreTokens()) { 209 versionMap.put(pluginOrFragment, t.nextToken()); 210 } 211 } 212 Map oldVersionMap = new HashMap (); 213 for (StringTokenizer t = new StringTokenizer (oldVersions, SEPARATOR, 214 false); t.hasMoreTokens();) { 215 String pluginOrFragment = t.nextToken(); 216 if (t.hasMoreTokens()) { 217 oldVersionMap.put(pluginOrFragment, t.nextToken()); 218 } 219 } 220 return versionMap.equals(oldVersionMap); 221 } 222 } 223 | Popular Tags |