1 11 12 package org.eclipse.ui.internal.intro.universal.contentdetect; 13 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStreamReader ; 20 import java.io.OutputStreamWriter ; 21 import java.net.URL ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 26 import org.eclipse.core.runtime.Platform; 27 import org.eclipse.osgi.service.datalocation.Location; 28 import org.eclipse.ui.IMemento; 29 import org.eclipse.ui.XMLMemento; 30 31 public class ContentDetectHelper { 32 33 public static final int NO_STATE = -1; 34 private static final String EXTENSION_COUNT_XML = "extensionCount.xml"; private static final String EXTENSION_NAMES_XML = "extensionNames.xml"; private static final String INTROCOUNT = "introcount"; private static final String CONTRIBUTOR = "contributor"; private static final String NAME = "name"; private static final String ROOT = "root"; private static final String PLUGIN_ID = "org.eclipse.ui.intro.universal"; 42 private File configurationDirectory; 43 44 private File getConfigurationLocation() { 45 if (configurationDirectory == null) { 46 Location location = Platform.getConfigurationLocation(); 47 if (location != null) { 48 URL configURL = location.getURL(); 49 if (configURL != null && configURL.getProtocol().startsWith("file")) { configurationDirectory = new File (configURL.getFile(), PLUGIN_ID); 51 if (configurationDirectory != null && !configurationDirectory.exists()) { 52 configurationDirectory.mkdirs(); 53 } 54 } 55 } 56 } 57 return configurationDirectory; 58 } 59 60 public void saveExtensionCount(int count) { 61 XMLMemento writeMemento = XMLMemento.createWriteRoot(ROOT); 62 writeMemento.putInteger(INTROCOUNT, count); 63 saveMemento(writeMemento, EXTENSION_COUNT_XML); 64 } 65 66 public int getExtensionCount() { 67 XMLMemento readMemento = getReadMemento(EXTENSION_COUNT_XML); 68 if (readMemento == null) { 69 return NO_STATE; 70 } 71 72 Integer extensionCount = readMemento.getInteger(INTROCOUNT); 73 if (extensionCount == null) { 74 return NO_STATE; 75 } 76 77 return extensionCount.intValue(); 78 } 79 80 public void saveContributors(Set contributors) { 81 XMLMemento writeMemento = XMLMemento.createWriteRoot(ROOT); 82 for (Iterator iter = contributors.iterator(); iter.hasNext();) { 83 IMemento childMemento = writeMemento.createChild(CONTRIBUTOR); 84 childMemento.putString(NAME, (String )iter.next()); 85 } 86 saveMemento(writeMemento, EXTENSION_NAMES_XML); 87 } 88 89 public Set getContributors() { 90 Set contributors = new HashSet (); 91 XMLMemento readMemento = getReadMemento(EXTENSION_NAMES_XML); 92 if (readMemento == null) { 93 return contributors; 94 } 95 IMemento[] children = readMemento.getChildren(CONTRIBUTOR); 96 for (int c = 0; c < children.length; c++ ) { 97 contributors.add(children[c].getString(NAME)); 98 } 99 return contributors; 100 } 101 102 private XMLMemento getReadMemento(String filename) { 103 XMLMemento memento; 104 InputStreamReader reader = null; 105 106 try { 107 final File stateFile = getStateFile(filename); 108 109 FileInputStream input = new FileInputStream (stateFile); 110 reader = new InputStreamReader (input, "utf-8"); memento = XMLMemento.createReadRoot(reader); 112 113 114 } catch (FileNotFoundException e) { 115 memento = null; 116 } catch (Exception e) { 118 memento = null; 120 } finally { 121 try { 122 if (reader != null) 123 reader.close(); 124 } catch (IOException e) { 125 } 127 } 128 return memento; 129 } 130 131 private void saveMemento(XMLMemento memento, String filename) { 132 File stateFile = getStateFile(filename); 134 OutputStreamWriter writer = null; 135 try { 136 FileOutputStream stream = new FileOutputStream (stateFile); 137 writer = new OutputStreamWriter (stream, "utf-8"); memento.save(writer); 139 } catch (IOException e) { 140 stateFile.delete(); 141 } finally { 142 try { 143 if (writer != null) 144 writer.close(); 145 } catch (IOException e) { 146 } 147 } 148 } 149 150 private File getStateFile(String filename) { 151 if (getConfigurationLocation() == null) { 152 return null; 153 } 154 File stateFile = new File (getConfigurationLocation(), filename); 155 return stateFile; 156 } 157 158 public Set findNewContributors(Set contributors, Set previousContributors) { 159 Set result = new HashSet (contributors); 160 for (Iterator iter = previousContributors.iterator(); iter.hasNext();) { 161 result.remove(iter.next()); 162 } 163 return result; 164 } 165 166 public void deleteStateFiles() { 167 try { 168 File stateFile = new File (getConfigurationLocation(), EXTENSION_COUNT_XML); 169 stateFile.delete(); 170 stateFile = new File (getConfigurationLocation(), EXTENSION_NAMES_XML); 171 stateFile.delete(); 172 } catch (RuntimeException e) { 173 } 174 } 175 176 } 177 | Popular Tags |