Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7872ba64e37e6143f367adce9bc85826a4cf9feb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
 * Copyright (c) 2007 Symbian Software Limited and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.templateengine;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


/**
 * Processes the shared default values. Updates and Persists new key - value (default) pair
 */

public class SharedDefaults extends HashMap/*<String, String>*/ {
	private static final long serialVersionUID = 0000000000L;

	public Document document;
	private File parsedXML;
	private File backUpSharedXML;

	/**
	 * HashMap's for persistence
	 */
	private HashMap/*<String, String>*/ sharedDefaultsMap;
	private HashMap/*<String, String>*/ persistDataMap;
	private HashMap/*<String, String>*/ tableDataMap;

	/**
	 * Two XML files here supports to provide consistent writing of data into
	 * them even during some destructive events which can happen during data
	 * persistence
	 */
	private static final String SHARED_DEFAULTS_DOT_XML = "shareddefaults.xml"; //$NON-NLS-1$
	private static final String SHARED_DEFAULTS_DOT_BACKUP_DOT_XML = "shareddefaults.backup.xml"; //$NON-NLS-1$

	/**
	 * Static reference string for getting (GET) and storing (SET)
	 * shareddefaults.xml
	 */

	public static final String SET = "SET"; //$NON-NLS-1$ 
	public static final String GET = "GET"; //$NON-NLS-1$

	/**
	 * Specifies the folder name present in the plugin
	 */
	public static final String ResourceFolder = "resources"; //$NON-NLS-1$

	/**
	 * Static reference of Singleton SharedDefault Instance
	 */
	private static SharedDefaults SHAREDDEFAULTS = new SharedDefaults();

	/**
	 * Returns static SharedDefaults Instance
	 * 
	 * @return
	 */

	public static SharedDefaults getInstance() {
		return SHAREDDEFAULTS;
	}

	/**
	 * Default Constructor for creating and instantiating objects. On the
	 * startup of Template Engine, if it checks for the existence of
	 * TempSharedDefaultsXML file, then it is determined that the last Template
	 * Engine process under went some System destructive events and takes up
	 * reconstructive process to regain the consistent data by persisting all
	 * information first into temporary file and then into actual file.
	 */

	public SharedDefaults() {
		sharedDefaultsMap = new HashMap/*<String, String>*/();
		persistDataMap = new HashMap/*<String, String>*/();
		tableDataMap = new HashMap/*<String, String>*/();

		// The conditional controls here is provided to have consistent
		// data storage in the file during System crash or
		// Power shutdown during data persistence into the file.

		parsedXML = TemplateEngineHelper.getSharedDefaultLocation(SHARED_DEFAULTS_DOT_XML);
		backUpSharedXML = TemplateEngineHelper.getSharedDefaultLocation(SHARED_DEFAULTS_DOT_BACKUP_DOT_XML);

		if (backUpSharedXML.exists())
			swapXML();

		initSharedDefaults();
	}

	/**
	 * This method instantiates the SharedDefaults process by gathering XML
	 * document and creating shared key-value pair in HashMap. Also creates a
	 * new XML file if none exists and adds the default XML format.
	 */

	private void initSharedDefaults() {
		String key = null;
		String value = null;

		try {
			long length = parsedXML.length();
			// Adds defaultXML format if the file length is zero
			if (length == 0) {
				parsedXML = createDefaultXMLFormat(parsedXML);
			}
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parsedXML.toURL().openStream());
		} catch (Exception exp) {
			TemplateEngineUtil.log(exp);
		}

		List/*<Element>*/ sharedElementList = TemplateEngine.getChildrenOfElement(document.getDocumentElement());
		int listSize = sharedElementList.size();
		for (int i = 0; i < listSize; i++) {
			Element xmlElement = (Element) sharedElementList.get(i);
			key = xmlElement.getAttribute(TemplateEngineHelper.ID);
			value = xmlElement.getAttribute(TemplateEngineHelper.VALUE);
			if (key != null && !key.trim().equals("")) {
				sharedDefaultsMap.put(key, value);
			}
		}
	}

	/**
	 * This method updates the HashMap with new key-value pair into the XML file
	 * 
	 * @param sharedMap
	 */

	public void updateShareDefaultsMap(Map/*<String, String>*/ sharedMap) {
		sharedDefaultsMap.putAll(sharedMap);
		persistSharedValueMap();
	}

	/**
	 * This method persists the latest data (HashMap) in the XML file New data
	 * obtained from the PreferencePage GUI.
	 */

	public void persistSharedValueMap() {
		generateSharedXML(backUpSharedXML);
		generateSharedXML(parsedXML);
		swapXML();
	}

	/**
	 * This method returns the latest key value pair (HashMap)
	 * 
	 * @return HashMap
	 */

	public Map/*<String, String>*/ getSharedDefaultsMap() {
		return sharedDefaultsMap;
	}

	/**
	 * Adds data to the backend XML (persistence) Data obtained from the
	 * PreferencePage GUI.
	 */

	public void addToBackEndStorage(String name, String value) {
		if (sharedDefaultsMap != null) {
			tableDataMap.putAll(sharedDefaultsMap);
		}

		tableDataMap.put(name, value);
		updateShareDefaultsMap(tableDataMap);
	}

	/**
	 * Updates backend with changed value for a specific key(name)
	 * 
	 * @param updateName
	 * @param updateValue
	 */

	public void updateToBackEndStorage(String updateName, String updateValue) {
		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parsedXML.toURL().openStream());
		} catch (Exception exp) {
			TemplateEngineUtil.log(exp);
		}

		persistDataMap.putAll(sharedDefaultsMap);
		List/*<Element>*/ sharedElementList = TemplateEngine.getChildrenOfElement(document.getDocumentElement());
		int elementListSize = sharedElementList.size();

		for (int i = 0; i < elementListSize; i++) {
			Element xmlElement = (Element) sharedElementList.get(i);
			String name = xmlElement.getAttribute(TemplateEngineHelper.ID);

			if (updateName.equals(name)) {
				persistDataMap.put(updateName, updateValue);
			}
		}

		updateShareDefaultsMap(persistDataMap);
	}

	/**
	 * Deletes the key-value pair from the backend with Key as identifier.
	 * 
	 * @param deleteName
	 */
	public void deleteBackEndStorage(String[] deleteName) {

		try {
			document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(parsedXML.toURL().openStream());
		} catch (Exception exp) {
			TemplateEngineUtil.log(exp);
		}

		List/*<Element>*/ sharedElementList = TemplateEngine.getChildrenOfElement(document.getDocumentElement());
		int elementListSize = sharedElementList.size();
		for (int i = 0; i < elementListSize; i++) {

			Element xmlElement = (Element) sharedElementList.get(i);
			String name = xmlElement.getAttribute(TemplateEngineHelper.ID);

			for (int k = 0; k < deleteName.length; k++) {
				if (deleteName[k].equals(name)) {
					xmlElement.removeAttribute(name);
					sharedDefaultsMap.remove(name);
				}
			}
		}

		updateShareDefaultsMap(sharedDefaultsMap);
	}

	/**
	 * This method returns the default XMLFormat for the newly created XML file
	 * 
	 * @param parsedXML
	 * @return
	 */

	private File createDefaultXMLFormat(File xmlFile) {
		Document d;
		try {
			d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			TemplateEngineUtil.log(e);
			return xmlFile;
		}
		Node rootElement = d.appendChild(d.createElement("SharedRoot")); //$NON-NLS-1$
		Element element = (Element) rootElement.appendChild(d.createElement("SharedProperty")); //$NON-NLS-1$
		element.setAttribute(TemplateEngineHelper.ID, ""); //$NON-NLS-1$
		element.setAttribute(TemplateEngineHelper.VALUE, ""); //$NON-NLS-1$

		DOMSource domSource = new DOMSource(d);
		TransformerFactory transFactory = TransformerFactory.newInstance();
		Result fileResult = new StreamResult(xmlFile);
		try {
			transFactory.newTransformer().transform(domSource, fileResult);
		} catch (Throwable t) {
			TemplateEngineUtil.log(t);
		}
		return xmlFile;
	}

	/**
	 * This method generates XML file for backupshareddefaults and
	 * shareddefaults to support consistent persistency
	 */

	private void generateSharedXML(File xmlFile) {
		Document d;
		try {
			d = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			TemplateEngineUtil.log(e);
			return;
		}
		Node rootElement = d.appendChild(d.createElement("SharedRoot"));  //$NON-NLS-1$
		
		for(Iterator i = sharedDefaultsMap.keySet().iterator(); i.hasNext(); ) {
			String key = (String) i.next();
			Element element = (Element) rootElement.appendChild(d.createElement("SharedProperty"));  //$NON-NLS-1$
			element.setAttribute(TemplateEngineHelper.ID, key);
			element.setAttribute(TemplateEngineHelper.VALUE, (String) sharedDefaultsMap.get(key));
		}

		DOMSource domSource = new DOMSource(d);
		TransformerFactory transFactory = TransformerFactory.newInstance();
		Result fileResult = new StreamResult(xmlFile);
		try {
			transFactory.newTransformer().transform(domSource, fileResult);
		} catch (Throwable t) {
			TemplateEngineUtil.log(t);
		}
	}

	/**
	 * This method swaps the backup file name to XML file containing latest or
	 * persisted data
	 */

	private void swapXML() {
		if (parsedXML.exists())
			parsedXML.delete();
		backUpSharedXML.renameTo(parsedXML);
	}
}

Back to the top