KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > contentdetect > ContentDetectHelper


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.intro.universal.contentdetect;
13
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
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 JavaDoc EXTENSION_COUNT_XML = "extensionCount.xml"; //$NON-NLS-1$
35
private static final String JavaDoc EXTENSION_NAMES_XML = "extensionNames.xml"; //$NON-NLS-1$
36
private static final String JavaDoc INTROCOUNT = "introcount"; //$NON-NLS-1$
37
private static final String JavaDoc CONTRIBUTOR = "contributor"; //$NON-NLS-1$
38
private static final String JavaDoc NAME = "name"; //$NON-NLS-1$
39
private static final String JavaDoc ROOT = "root"; //$NON-NLS-1$
40
private static final String JavaDoc PLUGIN_ID = "org.eclipse.ui.intro.universal"; //$NON-NLS-1$
41

42     private File JavaDoc configurationDirectory;
43
44     private File JavaDoc getConfigurationLocation() {
45         if (configurationDirectory == null) {
46             Location location = Platform.getConfigurationLocation();
47             if (location != null) {
48                 URL JavaDoc configURL = location.getURL();
49                 if (configURL != null && configURL.getProtocol().startsWith("file")) { //$NON-NLS-1$
50
configurationDirectory = new File JavaDoc(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 JavaDoc 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 JavaDoc contributors) {
81         XMLMemento writeMemento = XMLMemento.createWriteRoot(ROOT);
82         for (Iterator JavaDoc iter = contributors.iterator(); iter.hasNext();) {
83             IMemento childMemento = writeMemento.createChild(CONTRIBUTOR);
84             childMemento.putString(NAME, (String JavaDoc)iter.next());
85         }
86         saveMemento(writeMemento, EXTENSION_NAMES_XML);
87     }
88     
89     public Set JavaDoc getContributors() {
90         Set JavaDoc contributors = new HashSet JavaDoc();
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 JavaDoc filename) {
103         XMLMemento memento;
104         InputStreamReader JavaDoc reader = null;
105
106         try {
107             final File JavaDoc stateFile = getStateFile(filename);
108
109             FileInputStream JavaDoc input = new FileInputStream JavaDoc(stateFile);
110             reader = new InputStreamReader JavaDoc(input, "utf-8"); //$NON-NLS-1$
111
memento = XMLMemento.createReadRoot(reader);
112
113             
114         } catch (FileNotFoundException JavaDoc e) {
115             memento = null;
116             // Do nothing, the file will not exist the first time the workbench in used.
117
} catch (Exception JavaDoc e) {
118             // TODO should we log an error?
119
memento = null;
120         } finally {
121             try {
122                 if (reader != null)
123                     reader.close();
124             } catch (IOException JavaDoc e) {
125                 // TODO should we log an error?
126
}
127         }
128         return memento;
129     }
130     
131     private void saveMemento(XMLMemento memento, String JavaDoc filename) {
132         // Save the IMemento to a file.
133
File JavaDoc stateFile = getStateFile(filename);
134         OutputStreamWriter JavaDoc writer = null;
135         try {
136             FileOutputStream JavaDoc stream = new FileOutputStream JavaDoc(stateFile);
137             writer = new OutputStreamWriter JavaDoc(stream, "utf-8"); //$NON-NLS-1$
138
memento.save(writer);
139         } catch (IOException JavaDoc e) {
140             stateFile.delete();
141         } finally {
142             try {
143                 if (writer != null)
144                     writer.close();
145             } catch (IOException JavaDoc e) {
146             }
147         }
148     }
149     
150     private File JavaDoc getStateFile(String JavaDoc filename) {
151         if (getConfigurationLocation() == null) {
152             return null;
153         }
154         File JavaDoc stateFile = new File JavaDoc(getConfigurationLocation(), filename);
155         return stateFile;
156     }
157     
158     public Set JavaDoc findNewContributors(Set JavaDoc contributors, Set JavaDoc previousContributors) {
159         Set JavaDoc result = new HashSet JavaDoc(contributors);
160         for (Iterator JavaDoc iter = previousContributors.iterator(); iter.hasNext();) {
161             result.remove(iter.next());
162         }
163         return result;
164     }
165
166     public void deleteStateFiles() {
167         try {
168             File JavaDoc stateFile = new File JavaDoc(getConfigurationLocation(), EXTENSION_COUNT_XML);
169             stateFile.delete();
170             stateFile = new File JavaDoc(getConfigurationLocation(), EXTENSION_NAMES_XML);
171             stateFile.delete();
172         } catch (RuntimeException JavaDoc e) {
173         }
174     }
175
176 }
177
Popular Tags