1
/*******************************************************************************
2
* Copyright (c) 2007, 2008 Symbian Software Limited 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
9
* Bala Torati (Symbian) - Initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.core.templateengine;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.util.HashMap;
17
import java.util.List;
20
import javax.xml.parsers.DocumentBuilderFactory;
21
import javax.xml.parsers.ParserConfigurationException;
22
import javax.xml.transform.Result;
23
import javax.xml.transform.TransformerConfigurationException;
24
import javax.xml.transform.TransformerException;
25
import javax.xml.transform.TransformerFactory;
26
import javax.xml.transform.dom.DOMSource;
27
import javax.xml.transform.stream.StreamResult;
29
import org.w3c.dom.Document;
30
import org.w3c.dom.Element;
31
import org.w3c.dom.Node;
35
* Processes the shared default values. Updates and Persists new key - value (default) pair
37
public class SharedDefaults extends HashMap<String, String> {
38
private static final long serialVersionUID = 0000000000L;
40
public Document document;
41
private File parsedXML;
42
private File backUpSharedXML;
45
* HashMap's for persistence
47
private HashMap<String, String> sharedDefaultsMap;
48
private HashMap<String, String> persistDataMap;
49
private HashMap<String, String> tableDataMap;
52
* Two XML files here supports to provide consistent writing of data into
53
* them even during some destructive events which can happen during data
56
private static final String SHARED_DEFAULTS_DOT_XML = "shareddefaults.xml"; //$NON-NLS-1$
57
private static final String SHARED_DEFAULTS_DOT_BACKUP_DOT_XML = "shareddefaults.backup.xml"; //$NON-NLS-1$
60
* Static reference string for getting (GET) and storing (SET)
64
public static final String SET = "SET"; //$NON-NLS-1$
65
public static final String GET = "GET"; //$NON-NLS-1$
68
* Specifies the folder name present in the plugin
70
public static final String ResourceFolder = "resources"; //$NON-NLS-1$
73
* Static reference of Singleton SharedDefault Instance
75
private static SharedDefaults SHAREDDEFAULTS = new SharedDefaults();
78
* @return the shared SharedDefaults Instance
80
public static SharedDefaults getInstance() {
81
return SHAREDDEFAULTS;
85
* Default Constructor for creating and instantiating objects. On the
86
* startup of Template Engine, if it checks for the existence of
87
* TempSharedDefaultsXML file, then it is determined that the last Template
88
* Engine process under went some System destructive events and takes up
89
* reconstructive process to regain the consistent data by persisting all
90
* information first into temporary file and then into actual file.
93
public SharedDefaults() {
94
sharedDefaultsMap = new HashMap<String, String>();
95
persistDataMap = new HashMap<String, String>();
96
tableDataMap = new HashMap<String, String>();
98
// The conditional controls here is provided to have consistent
99
// data storage in the file during System crash or
100
// Power shutdown during data persistence into the file.
102
parsedXML = TemplateEngineHelper.getSharedDefaultLocation(SHARED_DEFAULTS_DOT_XML);
103
backUpSharedXML = TemplateEngineHelper.getSharedDefaultLocation(SHARED_DEFAULTS_DOT_BACKUP_DOT_XML);
105
if (backUpSharedXML.exists())
108
initSharedDefaults();
112
* This method instantiates the SharedDefaults process by gathering XML
113
* document and creating shared key-value pair in HashMap. Also creates a
114
* new XML file if none exists and adds the default XML format.
117
private void initSharedDefaults() {
122
long length = parsedXML.length();
123
// Adds defaultXML format if the file length is zero
125
parsedXML = createDefaultXMLFormat(parsedXML);
127
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parsedXML.toURI().toURL().openStream());
128
} catch (Exception exp) {
129
TemplateEngineUtil.log(exp);
132
List<Element> sharedElementList = TemplateEngine.getChildrenOfElement(document.getDocumentElement());
133
int listSize = sharedElementList.size();
134
for (int i = 0; i < listSize; i++) {
135
Element xmlElement = sharedElementList.get(i);
136
key = xmlElement.getAttribute(TemplateEngineHelper.ID);
137
value = xmlElement.getAttribute(TemplateEngineHelper.VALUE);
138
if (key != null && !key.trim().equals("")) { //$NON-NLS-1$
139
sharedDefaultsMap.put(key, value);
145
* This method updates the HashMap with new key-value pair into the XML file
150
public void updateShareDefaultsMap(Map<String, String> sharedMap) {
151
sharedDefaultsMap.putAll(sharedMap);
152
persistSharedValueMap();
156
* This method persists the latest data (HashMap) in the XML file New data
157
* obtained from the PreferencePage GUI.
160
public void persistSharedValueMap() {
161
generateSharedXML(backUpSharedXML);
162
generateSharedXML(parsedXML);
167
* This method returns the latest key value pair (HashMap)
172
public Map<String, String> getSharedDefaultsMap() {
173
return sharedDefaultsMap;
177
* Adds data to the backend XML (persistence) Data obtained from the
178
* PreferencePage GUI.
181
public void addToBackEndStorage(String name, String value) {
182
if (sharedDefaultsMap != null) {
183
tableDataMap.putAll(sharedDefaultsMap);
186
tableDataMap.put(name, value);
187
updateShareDefaultsMap(tableDataMap);
191
* Updates backend with changed value for a specific key(name)
196
public void updateToBackEndStorage(String updateName, String updateValue) {
198
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parsedXML.toURI().toURL().openStream());
199
} catch (Exception exp) {
200
TemplateEngineUtil.log(exp);
203
persistDataMap.putAll(sharedDefaultsMap);
204
List<Element> sharedElementList = TemplateEngine.getChildrenOfElement(document.getDocumentElement());
205
int elementListSize = sharedElementList.size();
207
for (int i = 0; i < elementListSize; i++) {
208
Element xmlElement = sharedElementList.get(i);
209
String name = xmlElement.getAttribute(TemplateEngineHelper.ID);
211
if (updateName.equals(name)) {
212
persistDataMap.put(updateName, updateValue);
216
updateShareDefaultsMap(persistDataMap);
220
* Deletes the key-value pair from the backend with Key as identifier.
224
public void deleteBackEndStorage(String[] deleteName) {
226
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parsedXML.toURI().toURL().openStream());
227
} catch (Exception exp) {
228
TemplateEngineUtil.log(exp);
231
List<Element> sharedElementList = TemplateEngine.getChildrenOfElement(document.getDocumentElement());
232
int elementListSize = sharedElementList.size();
233
for (int i = 0; i < elementListSize; i++) {
235
Element xmlElement = sharedElementList.get(i);
236
String name = xmlElement.getAttribute(TemplateEngineHelper.ID);
238
for (int k = 0; k < deleteName.length; k++) {
239
if (deleteName[k].equals(name)) {
240
xmlElement.removeAttribute(name);
241
sharedDefaultsMap.remove(name);
246
updateShareDefaultsMap(sharedDefaultsMap);
250
* This method returns the default XMLFormat for the newly created XML file
256
private File createDefaultXMLFormat(File xmlFile) {
259
d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
260
} catch (ParserConfigurationException e) {
261
TemplateEngineUtil.log(e);
264
Node rootElement = d.appendChild(d.createElement("SharedRoot")); //$NON-NLS-1$
265
Element element = (Element) rootElement.appendChild(d.createElement("SharedProperty")); //$NON-NLS-1$
266
element.setAttribute(TemplateEngineHelper.ID, ""); //$NON-NLS-1$
267
element.setAttribute(TemplateEngineHelper.VALUE, ""); //$NON-NLS-1$
269
DOMSource domSource = new DOMSource(d);
270
TransformerFactory transFactory = TransformerFactory.newInstance();
273
FileOutputStream fos= null;
275
fos= new FileOutputStream(xmlFile);
276
Result fileResult = new StreamResult(fos);
277
transFactory.newTransformer().transform(domSource, fileResult);
283
} catch(IOException ioe) {
284
TemplateEngineUtil.log(ioe);
285
} catch(TransformerConfigurationException tce) {
286
TemplateEngineUtil.log(tce);
287
} catch(TransformerException te) {
288
TemplateEngineUtil.log(te);
294
* This method generates XML file for backupshareddefaults and
295
* shareddefaults to support consistent persistency
298
private void generateSharedXML(File xmlFile) {
301
d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
302
} catch (ParserConfigurationException e) {
303
TemplateEngineUtil.log(e);
306
Node rootElement = d.appendChild(d.createElement("SharedRoot")); //$NON-NLS-1$
308
for(String key : sharedDefaultsMap.keySet()) {
309
Element element = (Element) rootElement.appendChild(d.createElement("SharedProperty")); //$NON-NLS-1$
310
element.setAttribute(TemplateEngineHelper.ID, key);
311
element.setAttribute(TemplateEngineHelper.VALUE, sharedDefaultsMap.get(key));
314
DOMSource domSource = new DOMSource(d);
315
TransformerFactory transFactory = TransformerFactory.newInstance();
316
Result fileResult = new StreamResult(xmlFile);
318
transFactory.newTransformer().transform(domSource, fileResult);
319
} catch (Throwable t) {
320
TemplateEngineUtil.log(t);
325
* This method swaps the backup file name to XML file containing latest or
329
private void swapXML() {
330
if (parsedXML.exists())
332
backUpSharedXML.renameTo(parsedXML);